# SonicAudioProcessor

# SonicAudioProcessor

Artifact:[androidx.media3:media3-common](https://developer.android.com/jetpack/androidx/releases/media3)  
[View Source](https://cs.android.com/search?q=file:androidx/media3/common/audio/SonicAudioProcessor.java+class:androidx.media3.common.audio.SonicAudioProcessor)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/media3/common/audio/SonicAudioProcessor "View this page in Java")

<br />

```
@UnstableApi
class SonicAudioProcessor : AudioProcessor
```

<br />

*** ** * ** ***

An[AudioProcessor](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor)that uses the Sonic library to modify audio speed/pitch/sample rate.

## Summary

|                                    ### Constants                                    |
|-------------------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `const `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [SAMPLE_RATE_NO_CHANGE](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#SAMPLE_RATE_NO_CHANGE())` = -1` Indicates that the output sample rate should be the same as the input. |

|                                                                                ### Public constructors                                                                                |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [SonicAudioProcessor](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#SonicAudioProcessor())`()` Creates a new Sonic audio processor. |

|                                                          ### Public functions                                                           |
|-----------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [AudioProcessor.AudioFormat](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor.AudioFormat)`!` | [configure](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#configure(androidx.media3.common.audio.AudioProcessor.AudioFormat))`(inputAudioFormat: `[AudioProcessor.AudioFormat](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor.AudioFormat)`!)` Configures the processor to process input audio with the specified format.                                                                                                                                                                                                                                 |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                           | [flush](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#flush(androidx.media3.common.audio.AudioProcessor.StreamMetadata))`(streamMetadata: `[AudioProcessor.StreamMetadata](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor.StreamMetadata)`!)` Prepares the audio processor to receive a new stream of input in the last[configured format](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#configure(androidx.media3.common.audio.AudioProcessor.AudioFormat))and clears any buffered data and pending output. |
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                           | [getDurationAfterProcessorApplied](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#getDurationAfterProcessorApplied(long))`(durationUs: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Returns the expected duration of the output stream when the processor is applied given a input`durationUs`.                                                                                                                                                                                                                                                                   |
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                           | [getMediaDuration](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#getMediaDuration(long))`(playoutDuration: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Returns the media duration corresponding to the specified playout duration, taking speed adjustment into account.                                                                                                                                                                                                                                                                                        |
| [ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer.html)`!`                                                       | [getOutput](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#getOutput())`()` Returns a buffer containing processed output data between its position and limit.                                                                                                                                                                                                                                                                                                                                                                                                                                          |
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                           | [getPlayoutDuration](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#getPlayoutDuration(long))`(mediaDuration: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Returns the playout duration corresponding to the specified media duration, taking speed adjustment into account.                                                                                                                                                                                                                                                                                      |
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)                                                           | [getProcessedInputBytes](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#getProcessedInputBytes())`()` Returns the number of bytes processed since last flush or reset.                                                                                                                                                                                                                                                                                                                                                                                                                                 |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                                                     | [isActive](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#isActive())`()` Returns whether the processor is configured and will process input buffers.                                                                                                                                                                                                                                                                                                                                                                                                                                                  |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)                                                     | [isEnded](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#isEnded())`()` Returns whether this processor will return no more output from[getOutput](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#getOutput())until[flush](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#flush())has been called and more input has been queued.                                                                                                                                                                               |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                           | [queueEndOfStream](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#queueEndOfStream())`()` Queues an end of stream signal.                                                                                                                                                                                                                                                                                                                                                                                                                                                                              |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                           | [queueInput](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#queueInput(java.nio.ByteBuffer))`(inputBuffer: `[ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer.html)`!)` Queues audio data between the position and limit of the`inputBuffer`for processing.                                                                                                                                                                                                                                                                                                                     |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                           | [reset](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#reset())`()` Resets the processor to its unconfigured state, releasing any resources.                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                           | [setOutputSampleRateHz](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#setOutputSampleRateHz(int))`(sampleRateHz: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Sets the sample rate for output audio, in Hertz.                                                                                                                                                                                                                                                                                                                                                     |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                           | [setPitch](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#setPitch(float))`(pitch: @`[FloatRange](https://developer.android.com/reference/kotlin/androidx/annotation/FloatRange)`(from = 0.0, fromInclusive = false) `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`)` Sets the target playback pitch.                                                                                                                                                                                                                                                               |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)                                                           | [setSpeed](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#setSpeed(float))`(speed: @`[FloatRange](https://developer.android.com/reference/kotlin/androidx/annotation/FloatRange)`(from = 0.0, fromInclusive = false) `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html)`)` Sets the target playback speed.                                                                                                                                                                                                                                                               |

|                                                                                                                                                                                                                                                                                                                                                                                      ### Inherited Constants                                                                                                                                                                                                                                                                                                                                                                                      |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| From[androidx.media3.common.audio.AudioProcessor](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor) |-------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `const `[ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer.html)`!` | [EMPTY_BUFFER](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#EMPTY_BUFFER()) An empty, direct[ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer.html). | |

|                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ### Inherited functions                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| From[androidx.media3.common.audio.AudioProcessor](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor) |-------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html) | ~~[flush](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#flush())~~`()` **This function is deprecated.** Use[flush](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#flush(androidx.media3.common.audio.AudioProcessor.StreamMetadata))instead. <br /> | |

## Constants

### SAMPLE_RATE_NO_CHANGE

```
const val SAMPLE_RATE_NO_CHANGE = -1: Int
```

Indicates that the output sample rate should be the same as the input.  

## Public constructors

### SonicAudioProcessor

```
SonicAudioProcessor()
```

Creates a new Sonic audio processor.  

## Public functions

### configure

```
fun configure(inputAudioFormat: AudioProcessor.AudioFormat!): AudioProcessor.AudioFormat!
```

Configures the processor to process input audio with the specified format. After calling this method, call[isActive](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#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](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#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](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#queueEndOfStream())when no more input will be supplied in the old input format.  

|                                                                         Parameters                                                                          |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `inputAudioFormat: `[AudioProcessor.AudioFormat](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor.AudioFormat)`!` | The format of audio that will be queued after the next call to[flush](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#flush()). |

|                                                                 Returns                                                                 |
|-----------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [AudioProcessor.AudioFormat](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor.AudioFormat)`!` | The configured output audio format if this instance is[active](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#isActive()). |

|                                                                                                Throws                                                                                                 |
|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------|
| [androidx.media3.common.audio.AudioProcessor.UnhandledAudioFormatException](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor.UnhandledAudioFormatException) | Thrown if the specified format can't be handled as input. |

### flush

```
fun flush(streamMetadata: AudioProcessor.StreamMetadata!): Unit
```

Prepares the audio processor to receive a new stream of input in the last[configured format](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#configure(androidx.media3.common.audio.AudioProcessor.AudioFormat))and clears any buffered data and pending output.

This method is a no-op if the processor is not[active](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#isActive()).  

### getDurationAfterProcessorApplied

```
fun getDurationAfterProcessorApplied(durationUs: Long): Long
```

Returns the expected duration of the output stream when the processor is applied given a input`durationUs`.

This method assumes`durationUs`of input is applied from the beginning of the stream (i.e. after a`flush(StreamMetadata.DEFAULT)`call).  

### getMediaDuration

```
fun getMediaDuration(playoutDuration: Long): Long
```

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                                            |
|--------------------------------------------------------------------------------------------------|--------------------------------|
| `playoutDuration: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | The playout duration to scale. |

|                                    Returns                                    |
|-------------------------------------------------------------------------------|-------------------------------------------------------------------|
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | The corresponding media duration, in the same units as`duration`. |

### getOutput

```
fun getOutput(): ByteBuffer!
```

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](https://developer.android.com/reference/java/nio/ByteBuffer.html)`!` | A buffer containing processed output data between its position and limit. |

### getPlayoutDuration

```
fun getPlayoutDuration(mediaDuration: Long): Long
```

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                                           |
|------------------------------------------------------------------------------------------------|------------------------------|
| `mediaDuration: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | The media duration to scale. |

|                                    Returns                                    |
|-------------------------------------------------------------------------------|--------------------------------------------------------------------------|
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | The corresponding playout duration, in the same units as`mediaDuration`. |

### getProcessedInputBytes

```
fun getProcessedInputBytes(): Long
```

Returns the number of bytes processed since last flush or reset.  

### isActive

```
fun isActive(): Boolean
```

Returns whether the processor is configured and will process input buffers.  

### isEnded

```
fun isEnded(): Boolean
```

Returns whether this processor will return no more output from[getOutput](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#getOutput())until[flush](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#flush())has been called and more input has been queued.  

### queueEndOfStream

```
fun queueEndOfStream(): Unit
```

Queues an end of stream signal. After this method has been called,[queueInput](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#queueInput(java.nio.ByteBuffer))may not be called until after the next call to[flush](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#flush()). Calling[getOutput](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#getOutput())will return any remaining output data. Multiple calls may be required to read all of the remaining output data.[isEnded](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#isEnded())will return`true`once all remaining output data has been read.  

### queueInput

```
fun queueInput(inputBuffer: ByteBuffer!): Unit
```

Queues audio data between the position and limit of the`inputBuffer`for processing. After calling this method, processed output may be available via[getOutput](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#getOutput()). Calling`queueInput(ByteBuffer)`again invalidates any pending output.  

|                                            Parameters                                            |
|--------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `inputBuffer: `[ByteBuffer](https://developer.android.com/reference/java/nio/ByteBuffer.html)`!` | 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. |

### reset

```
fun reset(): Unit
```

Resets the processor to its unconfigured state, releasing any resources.  

### setOutputSampleRateHz

```
fun setOutputSampleRateHz(sampleRateHz: Int): Unit
```

Sets the sample rate for output audio, in Hertz. Pass[SAMPLE_RATE_NO_CHANGE](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#SAMPLE_RATE_NO_CHANGE())to output audio at the same sample rate as the input. After calling this method, call[configure](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#configure(androidx.media3.common.audio.AudioProcessor.AudioFormat))to configure the processor with the new sample rate.  

|                                         Parameters                                          |
|---------------------------------------------------------------------------------------------|---------------------------------------------|
| `sampleRateHz: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The sample rate for output audio, in Hertz. |

|                                                                                    See also                                                                                     |
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [configure](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#configure(androidx.media3.common.audio.AudioProcessor.AudioFormat)) |   |

### setPitch

```
fun setPitch(pitch: @FloatRange(from = 0.0, fromInclusive = false) Float): Unit
```

Sets the target playback pitch. This method may only be called after draining data through the processor. The value returned by[isActive](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#isActive())may change, and the processor must be[flushed](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#flush())before queueing more data.  

|                                                                                                         Parameters                                                                                                         |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-------------------|
| `pitch: @`[FloatRange](https://developer.android.com/reference/kotlin/androidx/annotation/FloatRange)`(from = 0.0, fromInclusive = false) `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | The target pitch. |

### setSpeed

```
fun setSpeed(speed: @FloatRange(from = 0.0, fromInclusive = false) Float): Unit
```

Sets the target playback speed. This method may only be called after draining data through the processor. The value returned by[isActive](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/SonicAudioProcessor#isActive())may change, and the processor must be[flushed](https://developer.android.com/reference/kotlin/androidx/media3/common/audio/AudioProcessor#flush())before queueing more data.  

|                                                                                                         Parameters                                                                                                         |
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|--------------------------------------------------------|
| `speed: @`[FloatRange](https://developer.android.com/reference/kotlin/androidx/annotation/FloatRange)`(from = 0.0, fromInclusive = false) `[Float](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-float/index.html) | The target factor by which playback should be sped up. |