Class Buffer

java.lang.Object
com.vivoka.vsdk.audio.Buffer

public class Buffer extends Object
The Buffer class provides an abstraction for handling audio data in different formats. It supports various ways to construct and manipulate internal audio buffers, including operations like pushing, appending, copying, and extracting audio data.

This class supports byte and short array formats and provides thread-safe access to the underlying data structure when used appropriately.

  • Constructor Summary

    Constructors
    Constructor
    Description
    Buffer(int channelCount, int sampleRate)
    Constructs a new Buffer with specified channel count and sample rate.
    Buffer(int channelCount, int sampleRate, byte[] audioData)
    Constructs a new Buffer with the given byte array of audio data.
    Buffer(int channelCount, int sampleRate, short[] audioData)
    Constructs a new Buffer with the given short array of audio data.
    Buffer(int channelCount, int sampleRate, ArrayList<T> audioData)
    Constructs a new Buffer with audio data in a generic list format.
    Buffer(Buffer other)
    Constructs a new Buffer as a deep copy of another buffer.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    append(byte[] audioData)
    Appends raw byte array audio data to the buffer.
    void
    append(short[] audioData)
    Appends raw short array audio data to the buffer.
    void
    append(Buffer buffer)
    Appends the contents of another buffer to this buffer.
    <T> void
    append(ArrayList<T> audioData)
    Appends audio data from a generic list to the buffer.
    void
    Clears all audio data from the buffer.
    ArrayList<byte[]>
    Returns the internal audio data.
    int
    Returns the number of audio channels.
    int
    Returns the number of samples in the buffer.
    int
    Returns the sample rate.
    int
    Returns the size of the audio buffer in bytes.
    boolean
    Returns whether the buffer is currently empty.
    Creates and returns a deep copy of this buffer.
    void
    push(byte[] audioData)
    Pushes raw byte array audio data into the buffer.
    void
    push(ArrayList<byte[]> audioData)
    Pushes a list of byte arrays into the buffer.
    void
    setChannelCount(int channelCount)
    Sets the number of audio channels.
    void
    setSampleRate(int sampleRate)
    Sets the sample rate.
    ArrayList<byte[]>
    splitBytes(int chunkSize)
    Splits the buffer's audio data into chunks of the specified size.
    byte[]
    takeBytes(int size)
    Extracts a specified number of bytes from the buffer.
    ArrayList<byte[]>
    Removes and returns the current audio data from the buffer.
    byte[]
    Converts the buffer contents into a single byte array.
    short[]
    Converts the buffer contents into a short array.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Buffer

      public Buffer(Buffer other) throws Exception
      Constructs a new Buffer as a deep copy of another buffer.
      Parameters:
      other - the buffer to copy from
      Throws:
      Exception - if an error occurs during copying
    • Buffer

      public Buffer(int channelCount, int sampleRate) throws Exception
      Constructs a new Buffer with specified channel count and sample rate.
      Parameters:
      channelCount - the number of audio channels
      sampleRate - the sample rate in Hz
      Throws:
      Exception - if parameters are invalid
    • Buffer

      public Buffer(int channelCount, int sampleRate, byte[] audioData) throws Exception
      Constructs a new Buffer with the given byte array of audio data.
      Parameters:
      channelCount - the number of audio channels
      sampleRate - the sample rate in Hz
      audioData - the audio data in byte format
      Throws:
      Exception - if parameters are invalid or data is malformed
    • Buffer

      public Buffer(int channelCount, int sampleRate, short[] audioData) throws Exception
      Constructs a new Buffer with the given short array of audio data.

      The data is copied internally.

      Parameters:
      channelCount - the number of audio channels
      sampleRate - the sample rate in Hz
      audioData - the audio data in short format
      Throws:
      Exception - if parameters are invalid
    • Buffer

      public Buffer(int channelCount, int sampleRate, ArrayList<T> audioData) throws Exception
      Constructs a new Buffer with audio data in a generic list format.

      The type parameter T must be either byte[] or short[].

      Type Parameters:
      T - the type of elements in the audio data list; must be byte[] or short[]
      Parameters:
      channelCount - the number of audio channels
      sampleRate - the sample rate in Hz
      audioData - the audio data as a list of byte[] or short[]
      Throws:
      Exception - if parameters are invalid or data type is unsupported
  • Method Details

    • push

      public void push(byte[] audioData)
      Pushes raw byte array audio data into the buffer.

      This method does not copy the data. The buffer stores a reference to the array. Changes to the array after pushing may affect the buffer.

      Parameters:
      audioData - the audio data to push
    • push

      public void push(ArrayList<byte[]> audioData)
      Pushes a list of byte arrays into the buffer.

      This method does not copy the data. The buffer stores references to the arrays. Changes to the arrays after pushing may affect the buffer.

      Parameters:
      audioData - the list of byte arrays to push
    • append

      public void append(byte[] audioData)
      Appends raw byte array audio data to the buffer.

      This method makes a copy of the input data.

      Parameters:
      audioData - the audio data to append
    • append

      public void append(short[] audioData) throws Exception
      Appends raw short array audio data to the buffer.

      This method makes a copy of the input data.

      Parameters:
      audioData - the audio data to append
      Throws:
      Exception - if the append operation fails
    • append

      public <T> void append(ArrayList<T> audioData) throws Exception
      Appends audio data from a generic list to the buffer.

      The type parameter T must be either byte[] or short[]. This method makes a copy of each element in the list.

      Type Parameters:
      T - the type of elements in the list (byte[] or short[])
      Parameters:
      audioData - the audio data to append
      Throws:
      Exception - if the data type is unsupported or append fails
    • append

      public void append(Buffer buffer) throws Exception
      Appends the contents of another buffer to this buffer.

      This method makes a copy of the other buffer's data.

      Parameters:
      buffer - the buffer to append
      Throws:
      Exception - if the append operation fails
    • makeCopy

      public Buffer makeCopy() throws Exception
      Creates and returns a deep copy of this buffer.
      Returns:
      a new buffer containing a copy of the original data
      Throws:
      Exception - if the copy operation fails
    • setChannelCount

      public void setChannelCount(int channelCount) throws Exception
      Sets the number of audio channels.
      Parameters:
      channelCount - the new channel count
      Throws:
      Exception - if the value is invalid
    • setSampleRate

      public void setSampleRate(int sampleRate) throws Exception
      Sets the sample rate.
      Parameters:
      sampleRate - the new sample rate in Hz
      Throws:
      Exception - if the value is invalid
    • getChannelCount

      public int getChannelCount()
      Returns the number of audio channels.
      Returns:
      the channel count
    • getSampleRate

      public int getSampleRate()
      Returns the sample rate.
      Returns:
      the sample rate in Hz
    • getSize

      public int getSize()
      Returns the size of the audio buffer in bytes.
      Returns:
      the buffer size
    • getSampleCount

      public int getSampleCount()
      Returns the number of samples in the buffer.
      Returns:
      the buffer size in bytes
    • getAudioData

      public ArrayList<byte[]> getAudioData()
      Returns the internal audio data.

      This method returns a reference to the internal list. Ensure thread-safety when accessing it.

      Returns:
      the list of byte arrays representing audio data
    • takeData

      public ArrayList<byte[]> takeData()
      Removes and returns the current audio data from the buffer.
      Returns:
      the extracted audio data
    • takeBytes

      public byte[] takeBytes(int size) throws Exception
      Extracts a specified number of bytes from the buffer.
      Parameters:
      size - the number of bytes to take
      Returns:
      a byte array containing the extracted audio data
      Throws:
      Exception - if there is insufficient data
    • toBytes

      public byte[] toBytes()
      Converts the buffer contents into a single byte array.
      Returns:
      a byte array representing the buffer data
    • toShorts

      public short[] toShorts()
      Converts the buffer contents into a short array.
      Returns:
      a short array representing the buffer data
    • splitBytes

      public ArrayList<byte[]> splitBytes(int chunkSize) throws Exception
      Splits the buffer's audio data into chunks of the specified size.
      Parameters:
      chunkSize - the size of each chunk in bytes
      Returns:
      a list of byte arrays representing chunks
      Throws:
      Exception - if chunk size is invalid or splitting fails
    • isEmpty

      public boolean isEmpty()
      Returns whether the buffer is currently empty.
      Returns:
      true if the buffer has no audio data, false otherwise
    • clearData

      public void clearData()
      Clears all audio data from the buffer.