public final class SonicAudioProcessor extends java.lang.Object implements AudioProcessor
AudioProcessor
that uses the Sonic library to modify audio speed/pitch/sample rate.AudioProcessor.UnhandledFormatException
Modifier and Type | Field and Description |
---|---|
static float |
MAXIMUM_PITCH
The maximum allowed pitch in
setPitch(float) . |
static float |
MAXIMUM_SPEED
The maximum allowed playback speed in
setSpeed(float) . |
static float |
MINIMUM_PITCH
The minimum allowed pitch in
setPitch(float) . |
static float |
MINIMUM_SPEED
The minimum allowed playback speed in
setSpeed(float) . |
static int |
SAMPLE_RATE_NO_CHANGE
Indicates that the output sample rate should be the same as the input.
|
EMPTY_BUFFER
Constructor and Description |
---|
SonicAudioProcessor()
Creates a new Sonic audio processor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
configure(int sampleRateHz,
int channelCount,
int encoding)
Configures the processor to process input audio with the specified format and returns whether
to
AudioProcessor.flush() it. |
void |
flush()
Clears any state in preparation for receiving a new stream of input buffers.
|
java.nio.ByteBuffer |
getOutput()
Returns a buffer containing processed output data between its position and limit.
|
int |
getOutputChannelCount()
Returns the number of audio channels in the data output by the processor.
|
int |
getOutputEncoding()
Returns the audio encoding used in the data output by the processor.
|
int |
getOutputSampleRateHz()
Returns the sample rate of audio output by the processor, in hertz.
|
boolean |
isActive()
Returns whether the processor is configured and will process input buffers.
|
boolean |
isEnded()
Returns whether this processor will return no more output from
AudioProcessor.getOutput() until it
has been AudioProcessor.flush() ed and more input has been queued. |
void |
queueEndOfStream()
Queues an end of stream signal.
|
void |
queueInput(java.nio.ByteBuffer inputBuffer)
Queues audio data between the position and limit of the input
buffer for processing. |
void |
reset()
Resets the processor to its unconfigured state.
|
long |
scaleDurationForSpeedup(long duration)
Returns the specified duration scaled to take into account the speedup factor of this instance,
in the same units as
duration . |
void |
setOutputSampleRateHz(int sampleRateHz)
Sets the sample rate for output audio, in hertz.
|
float |
setPitch(float pitch)
Sets the playback pitch.
|
float |
setSpeed(float speed)
Sets the playback speed.
|
public static final float MAXIMUM_SPEED
setSpeed(float)
.public static final float MINIMUM_SPEED
setSpeed(float)
.public static final float MAXIMUM_PITCH
setPitch(float)
.public static final float MINIMUM_PITCH
setPitch(float)
.public static final int SAMPLE_RATE_NO_CHANGE
public SonicAudioProcessor()
public float setSpeed(float speed)
isActive()
.speed
- The requested new playback speed.public float setPitch(float pitch)
isActive()
.pitch
- The requested new pitch.public void setOutputSampleRateHz(int sampleRateHz)
SAMPLE_RATE_NO_CHANGE
to output
audio at the same sample rate as the input. After calling this method, call
configure(int, int, int)
to start using the new sample rate.sampleRateHz
- The sample rate for output audio, in hertz.configure(int, int, int)
public long scaleDurationForSpeedup(long duration)
duration
.duration
- The duration to scale taking into account speedup.duration
.public boolean configure(int sampleRateHz, int channelCount, int encoding) throws AudioProcessor.UnhandledFormatException
AudioProcessor
AudioProcessor.flush()
it. After calling this method, if the processor is active, AudioProcessor.getOutputSampleRateHz()
, AudioProcessor.getOutputChannelCount()
and AudioProcessor.getOutputEncoding()
return its output format.configure
in interface AudioProcessor
sampleRateHz
- The sample rate of input audio in Hz.channelCount
- The number of interleaved channels in input audio.encoding
- The encoding of input audio.AudioProcessor.flush()
the processor.AudioProcessor.UnhandledFormatException
- Thrown if the specified format can't be handled as input.public boolean isActive()
AudioProcessor
isActive
in interface AudioProcessor
public int getOutputChannelCount()
AudioProcessor
AudioProcessor.configure(int, int, int)
and is undefined if the instance is
not active.getOutputChannelCount
in interface AudioProcessor
public int getOutputEncoding()
AudioProcessor
AudioProcessor.configure(int, int, int)
and is undefined if the instance is not
active.getOutputEncoding
in interface AudioProcessor
public int getOutputSampleRateHz()
AudioProcessor
AudioProcessor.configure(int, int, int)
and is undefined if the instance is not
active.getOutputSampleRateHz
in interface AudioProcessor
public void queueInput(java.nio.ByteBuffer inputBuffer)
AudioProcessor
buffer
for processing.
buffer
must be a direct byte buffer with native byte order. Its contents are treated as
read-only. Its position will be advanced by the number of bytes consumed (which may be zero).
The caller retains ownership of the provided buffer. Calling this method invalidates any
previous buffer returned by AudioProcessor.getOutput()
.queueInput
in interface AudioProcessor
inputBuffer
- The input buffer to process.public void queueEndOfStream()
AudioProcessor
AudioProcessor.queueInput(ByteBuffer)
may not be called until after the next call to
AudioProcessor.flush()
. Calling AudioProcessor.getOutput()
will return any remaining output data. Multiple
calls may be required to read all of the remaining output data. AudioProcessor.isEnded()
will return
true
once all remaining output data has been read.queueEndOfStream
in interface AudioProcessor
public java.nio.ByteBuffer getOutput()
AudioProcessor
getOutput
in interface AudioProcessor
public boolean isEnded()
AudioProcessor
AudioProcessor.getOutput()
until it
has been AudioProcessor.flush()
ed and more input has been queued.isEnded
in interface AudioProcessor
public void flush()
AudioProcessor
flush
in interface AudioProcessor
public void reset()
AudioProcessor
reset
in interface AudioProcessor