Class Buffer
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
ConstructorsConstructorDescriptionBuffer(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.Constructs a new Buffer with audio data in a generic list format.Constructs a new Buffer as a deep copy of another buffer. -
Method Summary
Modifier and TypeMethodDescriptionvoidappend(byte[] audioData) Appends raw byte array audio data to the buffer.voidappend(short[] audioData) Appends raw short array audio data to the buffer.voidAppends the contents of another buffer to this buffer.<T> voidAppends audio data from a generic list to the buffer.voidClears all audio data from the buffer.ArrayList<byte[]> Returns the internal audio data.intReturns the number of audio channels.intReturns the number of samples in the buffer.intReturns the sample rate.intgetSize()Returns the size of the audio buffer in bytes.booleanisEmpty()Returns whether the buffer is currently empty.makeCopy()Creates and returns a deep copy of this buffer.voidpush(byte[] audioData) Pushes raw byte array audio data into the buffer.voidPushes a list of byte arrays into the buffer.voidsetChannelCount(int channelCount) Sets the number of audio channels.voidsetSampleRate(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[]> takeData()Removes and returns the current audio data from the buffer.byte[]toBytes()Converts the buffer contents into a single byte array.short[]toShorts()Converts the buffer contents into a short array.
-
Constructor Details
-
Buffer
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
Constructs a new Buffer with specified channel count and sample rate.- Parameters:
channelCount- the number of audio channelssampleRate- the sample rate in Hz- Throws:
Exception- if parameters are invalid
-
Buffer
Constructs a new Buffer with the given byte array of audio data.- Parameters:
channelCount- the number of audio channelssampleRate- the sample rate in HzaudioData- the audio data in byte format- Throws:
Exception- if parameters are invalid or data is malformed
-
Buffer
Constructs a new Buffer with the given short array of audio data.The data is copied internally.
- Parameters:
channelCount- the number of audio channelssampleRate- the sample rate in HzaudioData- the audio data in short format- Throws:
Exception- if parameters are invalid
-
Buffer
Constructs a new Buffer with audio data in a generic list format.The type parameter
Tmust be eitherbyte[]orshort[].- Type Parameters:
T- the type of elements in the audio data list; must be byte[] or short[]- Parameters:
channelCount- the number of audio channelssampleRate- the sample rate in HzaudioData- 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
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
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
Appends audio data from a generic list to the buffer.The type parameter
Tmust be eitherbyte[]orshort[]. 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
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
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
Sets the number of audio channels.- Parameters:
channelCount- the new channel count- Throws:
Exception- if the value is invalid
-
setSampleRate
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
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
Removes and returns the current audio data from the buffer.- Returns:
- the extracted audio data
-
takeBytes
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
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:
trueif the buffer has no audio data,falseotherwise
-
clearData
public void clearData()Clears all audio data from the buffer.
-