SonicAudioProcessor
@UnstableApi
public final class SonicAudioProcessor implements AudioProcessor
An AudioProcessor
that uses the Sonic library to modify audio speed/pitch/sample rate.
Summary
Constants |
|
---|---|
static final int |
Indicates that the output sample rate should be the same as the input. |
Public constructors |
---|
Creates a new Sonic audio processor. |
Public methods |
|
---|---|
AudioProcessor.AudioFormat |
configure(AudioProcessor.AudioFormat inputAudioFormat) Configures the processor to process input audio with the specified format. |
void |
flush() Clears any buffered data and pending output. |
long |
getDurationAfterProcessorApplied(long durationUs) Returns the expected duration of the output stream when the processor is applied given a input |
long |
getMediaDuration(long playoutDuration) Returns the media duration corresponding to the specified playout duration, taking speed adjustment into account. |
ByteBuffer |
Returns a buffer containing processed output data between its position and limit. |
long |
getPlayoutDuration(long mediaDuration) Returns the playout duration corresponding to the specified media duration, taking speed adjustment into account. |
long |
Returns the number of bytes processed since last flush or reset. |
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 |
void |
Queues an end of stream signal. |
void |
queueInput(ByteBuffer inputBuffer) Queues audio data between the position and limit of the |
void |
reset() Resets the processor to its unconfigured state, releasing any resources. |
void |
setOutputSampleRateHz(int sampleRateHz) Sets the sample rate for output audio, in Hertz. |
void |
setPitch(@FloatRange(from = 0.0, fromInclusive = false) float pitch) Sets the target playback pitch. |
void |
setSpeed(@FloatRange(from = 0.0, fromInclusive = false) float speed) Sets the target playback speed. |
Inherited Constants |
||
---|---|---|
|
Constants
SAMPLE_RATE_NO_CHANGE
public static final int SAMPLE_RATE_NO_CHANGE = -1
Indicates that the output sample rate should be the same as the input.
Public constructors
Public methods
configure
public AudioProcessor.AudioFormat configure(AudioProcessor.AudioFormat inputAudioFormat)
Configures the processor to process input audio with the specified format. After calling this method, call isActive
to determine whether the audio processor is active. Returns the configured output audio format if this instance is active.
After calling this method, it is necessary to flush
the processor to apply the new configuration. Before applying the new configuration, it is safe to queue input and get output in the old input/output formats. Call queueEndOfStream
when no more input will be supplied in the old input format.
Parameters | |
---|---|
AudioProcessor.AudioFormat inputAudioFormat |
The format of audio that will be queued after the next call to |
Returns | |
---|---|
AudioProcessor.AudioFormat |
The configured output audio format if this instance is |
Throws | |
---|---|
androidx.media3.common.audio.AudioProcessor.UnhandledAudioFormatException |
Thrown if the specified format can't be handled as input. |
flush
public void flush()
Clears any buffered data and pending output. If the audio processor is active, also prepares the audio processor to receive a new stream of input in the last configured (pending) format.
getDurationAfterProcessorApplied
public long getDurationAfterProcessorApplied(long durationUs)
Returns the expected duration of the output stream when the processor is applied given a input durationUs
.
getMediaDuration
public long getMediaDuration(long playoutDuration)
Returns the media duration corresponding to the specified playout duration, taking speed adjustment into account.
The scaling performed by this method will use the actual playback speed achieved by the audio processor, on average, since it was last flushed. This may differ very slightly from the target playback speed.
Parameters | |
---|---|
long playoutDuration |
The playout duration to scale. |
Returns | |
---|---|
long |
The corresponding media duration, in the same units as |
getOutput
public ByteBuffer getOutput()
Returns a buffer containing processed output data between its position and limit. The buffer will always be a direct byte buffer with native byte order. Calling this method invalidates any previously returned buffer. The buffer will be empty if no output is available.
Returns | |
---|---|
ByteBuffer |
A buffer containing processed output data between its position and limit. |
getPlayoutDuration
public long getPlayoutDuration(long mediaDuration)
Returns the playout duration corresponding to the specified media duration, taking speed adjustment into account.
The scaling performed by this method will use the actual playback speed achieved by the audio processor, on average, since it was last flushed. This may differ very slightly from the target playback speed.
Parameters | |
---|---|
long mediaDuration |
The media duration to scale. |
Returns | |
---|---|
long |
The corresponding playout duration, in the same units as |
getProcessedInputBytes
public long getProcessedInputBytes()
Returns the number of bytes processed since last flush or reset.
isActive
public boolean isActive()
Returns whether the processor is configured and will process input buffers.
isEnded
public boolean isEnded()
Returns whether this processor will return no more output from getOutput
until flush
has been called and more input has been queued.
queueEndOfStream
public void queueEndOfStream()
Queues an end of stream signal. After this method has been called, queueInput
may not be called until after the next call to flush
. Calling getOutput
will return any remaining output data. Multiple calls may be required to read all of the remaining output data. isEnded
will return true
once all remaining output data has been read.
queueInput
public void queueInput(ByteBuffer inputBuffer)
Queues audio data between the position and limit of the inputBuffer
for processing. After calling this method, processed output may be available via getOutput
. Calling queueInput(ByteBuffer)
again invalidates any pending output.
Parameters | |
---|---|
ByteBuffer inputBuffer |
The input buffer to process. It 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. |
setOutputSampleRateHz
public void setOutputSampleRateHz(int sampleRateHz)
Sets the sample rate for output audio, in Hertz. Pass SAMPLE_RATE_NO_CHANGE
to output audio at the same sample rate as the input. After calling this method, call configure
to configure the processor with the new sample rate.
Parameters | |
---|---|
int sampleRateHz |
The sample rate for output audio, in Hertz. |
See also | |
---|---|
configure |
setPitch
public void setPitch(@FloatRange(from = 0.0, fromInclusive = false) float pitch)
Sets the target playback pitch. This method may only be called after draining data through the processor. The value returned by isActive
may change, and the processor must be flushed
before queueing more data.
Parameters | |
---|---|
@FloatRange(from = 0.0, fromInclusive = false) float pitch |
The target pitch. |
setSpeed
public void setSpeed(@FloatRange(from = 0.0, fromInclusive = false) float speed)
Sets the target playback speed. This method may only be called after draining data through the processor. The value returned by isActive
may change, and the processor must be flushed
before queueing more data.
Parameters | |
---|---|
@FloatRange(from = 0.0, fromInclusive = false) float speed |
The target factor by which playback should be sped up. |