# Class AudioBuffer

java.lang.Object
com.codename1.media.AudioBuffer

public class AudioBuffer extends java.lang.Object
This class can be used to capture raw PCM data from the device's microphone. AudioBuffers should be obtained via the `boolean, int)` method.

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static interface`
  `AudioBuffer.AudioBufferCallback`
  A callback that can be registered to receive notifications when the contents of the AudioBuffer is changed.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `AudioBuffer(int maxSize)`
  Creates a new AudioBuffer with the given maximum size.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `void`
  `addCallback(AudioBuffer.AudioBufferCallback l)`
  Adds a callback to be notified when the contents of this buffer are changed.
  `void`
  `copyFrom(int sampleRate, int numChannels, float[] source)`
  Copies data from the source array into this buffer.
  `void`
  `copyFrom(int sampleRate, int numChannels, float[] source, int offset, int len)`
  Copies data from the source array (in the given range) into the buffer.
  `void`
  `copyFrom(AudioBuffer source)`
  Copies data into the buffer from the given source buffer.
  `void`
  `copyTo(float[] dest)`
  Copies data from this buffer to the given float array.
  `void`
  `copyTo(float[] dest, int offset)`
  Copies data from this buffer to the given float array.
  `void`
  `copyTo(AudioBuffer dest)`
  Copies data to another audio buffer.
  `void`
  `downSample(int targetSampleRate)`
  Downsamples the buffer to the given rate.
  `int`
  `getMaxSize()`
  Gets the maximum size of the buffer.
  `int`
  `getNumChannels()`

  ###### Returns

  `int`
  `getSampleRate()`

  ###### Returns

  `int`
  `getSize()`
  The current size of the buffer.
  `void`
  `removeCallback(AudioBuffer.AudioBufferCallback l)`
  Removes a callback from the audio buffer.

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### AudioBuffer

    public AudioBuffer(int maxSize)

    Creates a new AudioBuffer with the given maximum size.

    ###### Parameters

    - `maxSize`: The maximum size of the buffer.

- ## Method Details

  - ### copyFrom

    public void copyFrom(AudioBuffer source)

    Copies data into the buffer from the given source buffer. This will trigger the callbacks' `AudioBufferCallback#frameReceived(com.codename1.media.AudioBuffer)` method.

    ###### Parameters

    - `source`: The source buffer to copy from.

  - ### copyFrom

    public void copyFrom(int sampleRate, int numChannels, float[] source)

    Copies data from the source array into this buffer. This will trigger the callbacks' `AudioBufferCallback#frameReceived(com.codename1.media.AudioBuffer)` method.

    ###### Parameters

    - `source`

  - ### copyFrom

    public void copyFrom(int sampleRate, int numChannels, float[] source, int offset, int len)

    Copies data from the source array (in the given range) into the buffer. This will trigger the callbacks' `AudioBufferCallback#frameReceived(com.codename1.media.AudioBuffer)` method.

    ###### Parameters

    - `source`: The source array to copy data from.

    - `offset`: The offset in the source array to begin copying from.

    - `len`: The length of the range to copy.

  - ### copyTo

    public void copyTo(AudioBuffer dest)

    Copies data to another audio buffer. This will trigger callbacks in the destination.

    ###### Parameters

    - `dest`: The destination audio buffer.

  - ### copyTo

    public void copyTo(float[] dest)

    Copies data from this buffer to the given float array.

    ###### Parameters

    - `dest`: The destination float array to copy to.

  - ### copyTo

    public void copyTo(float[] dest, int offset)

    Copies data from this buffer to the given float array.

    ###### Parameters

    - `dest`: The destination float array.

    - `offset`: The offset in the destination array to start copying to.

  - ### getSize

    public int getSize()
    The current size of the buffer. This value will be changed each time data is copied into the buffer to reflect the current size of the data.

  - ### getMaxSize

    public int getMaxSize()
    Gets the maximum size of the buffer. Trying to copy more than this amount of data into the buffer will result in an IndexOutOfBoundsException.

  - ### addCallback

    public void addCallback(AudioBuffer.AudioBufferCallback l)

    Adds a callback to be notified when the contents of this buffer are changed.

    ###### Parameters

    - `l`: The AudioBufferCallback

  - ### removeCallback

    public void removeCallback(AudioBuffer.AudioBufferCallback l)

    Removes a callback from the audio buffer.

    ###### Parameters

    - `l`: The callback to remove.

  - ### getSampleRate

    public int getSampleRate()

    ###### Returns

    the sampleRate

  - ### getNumChannels

    public int getNumChannels()

    ###### Returns

    the numChannels

  - ### downSample

    public void downSample(int targetSampleRate)

    Downsamples the buffer to the given rate. This will change the result of `#getSize()` and `#getSampleRate()`.

    Note: This should only be called inside the AudioBuffer callback since it is modifying the contents of the buffer.

    ###### Parameters

    - `targetSampleRate`: The new target rate.