# DefaultAudioMixer

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

*** ** * ** ***


```
@UnstableApi
public final class DefaultAudioMixer implements AudioMixer
```

<br />

*** ** * ** ***

An `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer` that incrementally mixes source audio into a fixed size mixing buffer.

By default, the output signal is guaranteed to be in the range corresponding to its encoding. This range is \[`https://developer.android.com/reference/java/lang/Short.html#MIN_VALUE`, `https://developer.android.com/reference/java/lang/Short.html#MAX_VALUE`\] for `https://developer.android.com/reference/androidx/media3/common/C#ENCODING_PCM_16BIT()`, and \[-1.0, 1.0\] for `https://developer.android.com/reference/androidx/media3/common/C#ENCODING_PCM_FLOAT()`. Before adding a value to the output buffer, it is first converted to the output encoding (in the corresponding range). It is then added to the output buffer value, and the result is clipped by moving it to the closest value in this range.

## Summary

| ### Nested types |
|---|
| `public final class https://developer.android.com/reference/androidx/media3/transformer/DefaultAudioMixer.Factory implements https://developer.android.com/reference/androidx/media3/transformer/AudioMixer.Factory` An `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer.Factory` implementation for `https://developer.android.com/reference/androidx/media3/transformer/DefaultAudioMixer` instances. |

| ### Public methods |
|---|---|
| `int` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultAudioMixer#addSource(androidx.media3.common.audio.AudioProcessor.AudioFormat,long)(https://developer.android.com/reference/androidx/media3/common/audio/AudioProcessor.AudioFormat sourceFormat, long startTimeUs)` Adds an audio source to mix starting at the given time. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultAudioMixer#configure(androidx.media3.common.audio.AudioProcessor.AudioFormat,int,long)( https://developer.android.com/reference/androidx/media3/common/audio/AudioProcessor.AudioFormat outputAudioFormat, int bufferSizeMs, long startTimeUs )` Configures the mixer. |
| `https://developer.android.com/reference/java/nio/ByteBuffer.html` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultAudioMixer#getOutput()()` Returns a buffer containing output audio data between its position and limit. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultAudioMixer#hasSource(int)(int sourceId)` Returns whether there is an `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#addSource(androidx.media3.common.audio.AudioProcessor.AudioFormat,long)` with the given `sourceId`. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultAudioMixer#isEnded()()` Returns whether the mixer can accept more `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#queueInput(int,java.nio.ByteBuffer)` or produce more `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#getOutput()`, based on the `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#setEndTimeUs(long)`. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultAudioMixer#queueInput(int,java.nio.ByteBuffer)(int sourceId, https://developer.android.com/reference/java/nio/ByteBuffer.html sourceBuffer)` Queues audio data between the position and limit of the `sourceBuffer`. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultAudioMixer#removeSource(int)(int sourceId)` Removes an audio source. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultAudioMixer#reset()()` Resets the mixer to its unconfigured state, releasing any resources. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultAudioMixer#setEndTimeUs(long)(long endTimeUs)` Sets the end time of the output audio. |
| `void` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultAudioMixer#setSourceVolume(int,float)(int sourceId, float volume)` Sets the volume applied to future samples queued from the given source. |
| `boolean` | `https://developer.android.com/reference/androidx/media3/transformer/DefaultAudioMixer#supportsSourceAudioFormat(androidx.media3.common.audio.AudioProcessor.AudioFormat)(https://developer.android.com/reference/androidx/media3/common/audio/AudioProcessor.AudioFormat sourceFormat)` Indicates whether the mixer supports mixing sources with the given audio format. |

## Public methods

### addSource

```
public int addSource(AudioProcessor.AudioFormat sourceFormat, long startTimeUs)
```

Adds an audio source to mix starting at the given time.

If the mixer has already `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#getOutput()` samples past the `
startTimeUs`, audio from this source will be discarded up to the last output end timestamp.

If the source start time is earlier than the configured mixer start time then audio from this source will be discarded up to the mixer start time.

All audio sources start with a volume of 1.0 on all channels.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/audio/AudioProcessor.AudioFormat sourceFormat` | Audio format of source buffers. |
| `long startTimeUs` | Source start time in microseconds. |

| Returns |
|---|---|
| `int` | Non-negative integer identifying the source (`sourceId`). |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/audio/AudioProcessor.UnhandledAudioFormatException` | If the source format is not supported. |

### configure

```
public void configure(
    AudioProcessor.AudioFormat outputAudioFormat,
    int bufferSizeMs,
    long startTimeUs
)
```

Configures the mixer.

The mixer must be configured before use and can only be reconfigured after a call to `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#reset()`.

The mixing buffer size is set by `bufferSizeMs` and indicates how much audio can be queued before `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#getOutput()` is called.

| Parameters |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/audio/AudioProcessor.AudioFormat outputAudioFormat` | The audio format of buffers returned from `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#getOutput()`. |
| `int bufferSizeMs` | The optional mixing buffer size in milliseconds, or `https://developer.android.com/reference/androidx/media3/common/C#LENGTH_UNSET()`. |
| `long startTimeUs` | The start time of the mixer output in microseconds. |

| Throws |
|---|---|
| `https://developer.android.com/reference/androidx/media3/common/audio/AudioProcessor.UnhandledAudioFormatException` | If the output audio format is not supported. |

### getOutput

```
public ByteBuffer getOutput()
```

Returns a buffer containing output audio data between its position and limit.

The buffer will be no larger than the configured buffer size and will include no more than the frames that have been queued from all sources, up to the `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#setEndTimeUs(long)`. Silence will be generated for any periods with no sources.

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

### hasSource

```
public boolean hasSource(int sourceId)
```

Returns whether there is an `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#addSource(androidx.media3.common.audio.AudioProcessor.AudioFormat,long)` with the given `sourceId`.

### isEnded

```
public boolean isEnded()
```

Returns whether the mixer can accept more `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#queueInput(int,java.nio.ByteBuffer)` or produce more `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#getOutput()`, based on the `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#setEndTimeUs(long)`.

**Note:** If no end time is set this will always return `false`.

### queueInput

```
public void queueInput(int sourceId, ByteBuffer sourceBuffer)
```

Queues audio data between the position and limit of the `sourceBuffer`.

After calling this method output may be available via `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#getOutput()` if all sources have queued data.

| Parameters |
|---|---|
| `int sourceId` | Source identifier from `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#addSource(androidx.media3.common.audio.AudioProcessor.AudioFormat,long)`. |
| `https://developer.android.com/reference/java/nio/ByteBuffer.html sourceBuffer` | The source buffer to mix. 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. |

### removeSource

```
public void removeSource(int sourceId)
```

Removes an audio source.

No more audio can be queued from this source. All audio queued before removal will be output.

| Parameters |
|---|---|
| `int sourceId` | Source identifier from `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#addSource(androidx.media3.common.audio.AudioProcessor.AudioFormat,long)`. |

### reset

```
public void reset()
```

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

### setEndTimeUs

```
public void setEndTimeUs(long endTimeUs)
```

Sets the end time of the output audio.

The mixer will not accept input nor produce output past this point.

| Parameters |
|---|---|
| `long endTimeUs` | The end time in microseconds. |

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/IllegalArgumentException.html` | If `endTimeUs` is before the configured start time. |

### setSourceVolume

```
public void setSourceVolume(int sourceId, float volume)
```

Sets the volume applied to future samples queued from the given source.

| Parameters |
|---|---|
| `int sourceId` | Source identifier from `https://developer.android.com/reference/androidx/media3/transformer/AudioMixer#addSource(androidx.media3.common.audio.AudioProcessor.AudioFormat,long)`. |
| `float volume` | Non-negative scalar applied to all source channels. |

### supportsSourceAudioFormat

```
public boolean supportsSourceAudioFormat(AudioProcessor.AudioFormat sourceFormat)
```

Indicates whether the mixer supports mixing sources with the given audio format.