1 /*
   2  * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package com.sun.media.sound;
  26 
  27 import java.util.Arrays;
  28 
  29 import javax.sound.sampled.AudioFormat;
  30 
  31 /**
  32  * This class is used to store audio buffer.
  33  *
  34  * @author Karl Helgason
  35  */
  36 public final class SoftAudioBuffer {
  37 
  38     private int size;
  39     private float[] buffer;
  40     private boolean empty = true;
  41     private AudioFormat format;
  42     private AudioFloatConverter converter;
  43     private byte[] converter_buffer;
  44 
  45     public SoftAudioBuffer(int size, AudioFormat format) {
  46         this.size = size;
  47         this.format = format;
  48         converter = AudioFloatConverter.getConverter(format);
  49     }
  50 
  51     public void swap(SoftAudioBuffer swap)
  52     {
  53         int bak_size = size;
  54         float[] bak_buffer = buffer;
  55         boolean bak_empty = empty;
  56         AudioFormat bak_format = format;
  57         AudioFloatConverter bak_converter = converter;
  58         byte[] bak_converter_buffer = converter_buffer;
  59 
  60         size = swap.size;
  61         buffer = swap.buffer;
  62         empty = swap.empty;
  63         format = swap.format;
  64         converter = swap.converter;
  65         converter_buffer = swap.converter_buffer;
  66 
  67         swap.size = bak_size;
  68         swap.buffer = bak_buffer;
  69         swap.empty = bak_empty;
  70         swap.format = bak_format;
  71         swap.converter = bak_converter;
  72         swap.converter_buffer = bak_converter_buffer;
  73     }
  74 
  75     public AudioFormat getFormat() {
  76         return format;
  77     }
  78 
  79     public int getSize() {
  80         return size;
  81     }
  82 
  83     public void clear() {
  84         if (!empty) {
  85             Arrays.fill(buffer, 0);
  86             empty = true;
  87         }
  88     }
  89 
  90     public boolean isSilent() {
  91         return empty;
  92     }
  93 
  94     public float[] array() {
  95         empty = false;
  96         if (buffer == null)
  97             buffer = new float[size];
  98         return buffer;
  99     }
 100 
 101     public void get(byte[] buffer, int channel) {
 102 
 103         int framesize_pc = (format.getFrameSize() / format.getChannels());
 104         int c_len = size * framesize_pc;
 105         if (converter_buffer == null || converter_buffer.length < c_len)
 106             converter_buffer = new byte[c_len];
 107 
 108         if (format.getChannels() == 1) {
 109             converter.toByteArray(array(), size, buffer);
 110         } else {
 111             converter.toByteArray(array(), size, converter_buffer);
 112             if (channel >= format.getChannels())
 113                 return;
 114             int z_stepover = format.getChannels() * framesize_pc;
 115             int k_stepover = framesize_pc;
 116             for (int j = 0; j < framesize_pc; j++) {
 117                 int k = j;
 118                 int z = channel * framesize_pc + j;
 119                 for (int i = 0; i < size; i++) {
 120                     buffer[z] = converter_buffer[k];
 121                     z += z_stepover;
 122                     k += k_stepover;
 123                 }
 124             }
 125         }
 126 
 127     }
 128 }