|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | ENUM CONSTANTS | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.tritonus.share.sampled.FloatSampleBuffer
A class for small buffers of samples in linear, 32-bit floating point format.
It is supposed to be a replacement of the byte[] stream architecture of JavaSound, especially for chains of AudioInputStreams. Ideally, all involved AudioInputStreams handle reading into a FloatSampleBuffer.
Specifications:
When a cascade of AudioInputStreams use FloatSampleBuffer for
processing, they may implement the interface FloatSampleInput.
This signals that this stream may provide float buffers
for reading. The data is not converted back to bytes,
but stays in a single buffer that is passed from stream to stream.
For that serves the read(FloatSampleBuffer) method, which is
then used as replacement for the byte-based read functions of
AudioInputStream.
However, backwards compatibility must always be retained, so
even when an AudioInputStream implements FloatSampleInput,
it must work the same way when any of the byte-based read methods
is called.
As an example, consider the following set-up:
initFromByteArray
method of
the float buffer to initialize it with the 8 bit data.
convertToByteArray
method of
the float buffer to fill the byte buffer with the
resulting samples.
To summarize, here are some advantages when using a FloatSampleBuffer for streaming:
Simple benchmarks showed that the processing requirements for the conversion to and from float is about the same as when converting it to shorts or ints without dithering, and significantly higher with dithering. An own implementation of a random number generator may improve this.
"Lazy" deletion of samples and channels:
sampleCount
is reduced. A subsequent
increase of the sample count (which will occur frequently), will check
that and eventually reuse the existing array.
Use the reset
functions to clear the memory and remove
hidden samples and channels.
Note that the lazy mechanism implies that the arrays returned
from getChannel(int)
may have a greater size
than getSampleCount(). Consequently, be sure to never rely on the
length field of the sample arrays.
As an example, consider a chain of converters that all act
on the same instance of FloatSampleBuffer. Some converters
may decrease the sample count (e.g. sample rate converter) and
delete channels (e.g. PCM2PCM converter). So, processing of one
block will decrease both. For the next block, all starts
from the beginning. With the lazy mechanism, all float arrays
are only created once for processing all blocks.
Having lazy disabled would require for each chunk that is processed
Dithering:
By default, this class uses dithering for reduction
of sample width (e.g. original data was 16bit, target
data is 8bit). As dithering may be needed in other cases
(especially when the float samples are processed using DSP
algorithms), or it is preferred to switch it off,
dithering can be explicitely switched on or off with
the method setDitherMode(int).
For a discussion about dithering, see
here and
here.
Field Summary | |
---|---|
static int |
DITHER_MODE_AUTOMATIC
Constant for setDitherMode: dithering will be enabled if sample size is decreased |
static int |
DITHER_MODE_OFF
Constant for setDitherMode: dithering will not be done |
static int |
DITHER_MODE_ON
Constant for setDitherMode: dithering will be done |
Constructor Summary | |
---|---|
FloatSampleBuffer()
Create an instance with initially no channels. |
|
FloatSampleBuffer(byte[] buffer,
int offset,
int byteCount,
AudioFormat format)
Creates a new instance of FloatSampleBuffer and initializes it with audio data given in the interleaved byte array buffer . |
|
FloatSampleBuffer(int channelCount,
int sampleCount,
float sampleRate)
Create an empty FloatSampleBuffer with the specified number of channels, samples, and the specified sample rate. |
Method Summary | |
---|---|
void |
addChannel(boolean silent)
|
void |
changeSampleCount(int newSampleCount,
boolean keepOldSamples)
Resizes this buffer. |
byte[] |
convertToByteArray(AudioFormat format)
Creates a new byte[] buffer, fills it with the audio data, and returns it. |
int |
convertToByteArray(byte[] buffer,
int offset,
AudioFormat format)
Writes this sample buffer's audio data to buffer
as an interleaved byte array. |
void |
copy(int sourceIndex,
int destIndex,
int length)
Copies data inside all channel. |
void |
copy(int channel,
int sourceIndex,
int destIndex,
int length)
Copies data inside a channel. |
void |
copyChannel(int sourceChannel,
int targetChannel)
both source and target channel have to exist. targetChannel will be overwritten |
void |
expandChannel(int targetChannelCount)
Mix up of 1 channel to n channels. |
Object[] |
getAllChannels()
|
int |
getByteArrayBufferSize(AudioFormat format)
|
float[] |
getChannel(int channel)
NOTE: the returned array may be larger than sampleCount. |
int |
getChannelCount()
|
protected float |
getConvertDitherBits(int newFormatType)
|
float |
getDitherBits()
|
int |
getDitherMode()
|
int |
getSampleCount()
|
float |
getSampleRate()
|
protected void |
init(int channelCount,
int sampleCount,
float sampleRate)
|
protected void |
init(int channelCount,
int sampleCount,
float sampleRate,
boolean lazy)
|
void |
initFromByteArray(byte[] buffer,
int offset,
int byteCount,
AudioFormat format)
Resets this buffer with the audio data specified in the arguments. |
void |
initFromByteArray(byte[] buffer,
int offset,
int byteCount,
AudioFormat format,
boolean lazy)
Resets this buffer with the audio data specified in the arguments. |
void |
initFromFloatSampleBuffer(FloatSampleBuffer source)
Resets this sample buffer with the data in source . |
void |
insertChannel(int index,
boolean silent)
Insert a (silent) channel at position index . |
void |
insertChannel(int index,
boolean silent,
boolean lazy)
Inserts a channel at position index . |
void |
makeSilence()
|
void |
makeSilence(int channel)
|
void |
mixDownChannels()
Mix down of n channels to one channel. |
void |
removeChannel(int channel)
performs a lazy remove of the channel |
void |
removeChannel(int channel,
boolean lazy)
Removes a channel. |
void |
reset()
Deletes all channels, frees memory... |
void |
reset(int channels,
int sampleCount,
float sampleRate)
Destroys any existing data and creates new channels. |
void |
setDitherBits(float ditherBits)
Set the number of bits for dithering. |
void |
setDitherMode(int mode)
Sets the mode for dithering. |
void |
setSampleRate(float sampleRate)
Sets the sample rate of this buffer. |
void |
setSamplesFromBytes(byte[] input,
int inByteOffset,
AudioFormat format,
int floatOffset,
int frameCount)
Initializes audio data from the provided byte array. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final int DITHER_MODE_AUTOMATIC
public static final int DITHER_MODE_ON
public static final int DITHER_MODE_OFF
Constructor Detail |
---|
public FloatSampleBuffer()
public FloatSampleBuffer(int channelCount, int sampleCount, float sampleRate)
public FloatSampleBuffer(byte[] buffer, int offset, int byteCount, AudioFormat format)
buffer
.
Method Detail |
---|
protected void init(int channelCount, int sampleCount, float sampleRate)
protected void init(int channelCount, int sampleCount, float sampleRate, boolean lazy)
public void initFromByteArray(byte[] buffer, int offset, int byteCount, AudioFormat format)
byteCount / format.getFrameSize()
.
If LAZY_DEFAULT is true, it will use lazy deletion.
IllegalArgumentException
public void initFromByteArray(byte[] buffer, int offset, int byteCount, AudioFormat format, boolean lazy)
byteCount / format.getFrameSize()
.
lazy
- if true, then existing channels will be tried to be re-used
to minimize garbage collection.
IllegalArgumentException
public void initFromFloatSampleBuffer(FloatSampleBuffer source)
source
.
public void reset()
public void reset(int channels, int sampleCount, float sampleRate)
public int getByteArrayBufferSize(AudioFormat format)
public int convertToByteArray(byte[] buffer, int offset, AudioFormat format)
buffer
as an interleaved byte array.
buffer
must be large enough to hold all data.
buffer
IllegalArgumentException
- when buffer is too small or format
doesn't matchpublic byte[] convertToByteArray(AudioFormat format)
IllegalArgumentException
- when sample rate or channels do not matchconvertToByteArray(byte[], int, AudioFormat)
public void changeSampleCount(int newSampleCount, boolean keepOldSamples)
If keepOldSamples
is true, as much as possible samples are
retained. If the buffer is enlarged, silence is added at the end.
If keepOldSamples
is false, existing samples are discarded
and the buffer contains random samples.
public void makeSilence()
public void makeSilence(int channel)
public void addChannel(boolean silent)
public void insertChannel(int index, boolean silent)
index
.
If LAZY_DEFAULT is true, this is done lazily.
public void insertChannel(int index, boolean silent, boolean lazy)
index
.
If silent
is true, the new channel will be silent.
Otherwise it will contain random data.
If lazy
is true, hidden channels which have at least getSampleCount()
elements will be examined for reusage as inserted channel.
If lazy
is false, still hidden channels are reused,
but it is assured that the inserted channel has exactly getSampleCount() elements,
thus not wasting memory.
public void removeChannel(int channel)
public void removeChannel(int channel, boolean lazy)
public void copyChannel(int sourceChannel, int targetChannel)
public void copy(int sourceIndex, int destIndex, int length)
public void copy(int channel, int sourceIndex, int destIndex, int length)
public void expandChannel(int targetChannelCount)
targetChannelCount
- the number of channels that this sample buffer
will have after expanding. NOT the number of
channels to add !
IllegalArgumentException
- if this buffer does not have one
channel before calling this method.public void mixDownChannels()
public void setSamplesFromBytes(byte[] input, int inByteOffset, AudioFormat format, int floatOffset, int frameCount)
destOffset
.
This FloatSampleBuffer must be big enough to accomodate the samples.
srcBuffer
is read from index srcOffset
to (srcOffset + (lengthInSamples * format.getFrameSize()))
input
- the input buffer in interleaved audio datainByteOffset
- the offset in input
format
- input buffer's audio formatfloatOffset
- the offset where to write the float samplesframeCount
- number of samples to write to this sample buffer
public int getChannelCount()
public int getSampleCount()
public float getSampleRate()
public void setSampleRate(float sampleRate)
public float[] getChannel(int channel)
public Object[] getAllChannels()
public void setDitherBits(float ditherBits)
Note: this value is only used, when dithering is actually performed.
public float getDitherBits()
public void setDitherMode(int mode)
public int getDitherMode()
protected float getConvertDitherBits(int newFormatType)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | ENUM CONSTANTS | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |