# Class AudioMixer

java.lang.Object
com.codename1.media.AudioMixer

public class AudioMixer extends Object

Mixes multiple PCM tracks into a single `AudioBuffer`.

`AudioMixer` is a platform-neutral helper for building a sample-accurate PCM timeline. All tracks are locked to the mixer sample rate and channel count. Track offsets are converted to sample frames on that clock, and the mixed output uses interleaved float PCM samples in the same format as `AudioBuffer` and `WAVWriter`.

    AudioMixer mixer = new AudioMixer(44100, 2);
    mixer.addTrack(musicBuffer, 0, 0.5f);
    mixer.addTrack(effectBuffer, 250, 1.0f);
    AudioBuffer mixed = mixer.mix();
    float[] pcm = new float[mixed.getSize()];
    mixed.copyTo(pcm);
    wavWriter.write(pcm, 0, mixed.getSize());

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `AudioMixer(int sampleRate, int numChannels)`
  Creates a new mixer locked to a single sample clock.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `AudioMixer`
  `addTrack(float[] pcmData, int offset, int len, long offsetMs, float gain)`
  Adds an interleaved PCM track range at the given millisecond offset.
  `AudioMixer`
  `addTrack(float[] pcmData, long offsetMs, float gain)`
  Adds an interleaved PCM track at the given millisecond offset.
  `AudioMixer`
  `addTrack(AudioBuffer source, long offsetMs, float gain)`
  Adds an `AudioBuffer` track at the given millisecond offset.
  `AudioMixer`
  `addTrackAtFrame(float[] pcmData, int offset, int len, int offsetFrame, float gain)`
  Adds an interleaved PCM track range at an exact sample-frame offset.
  `AudioMixer`
  `addTrackAtFrame(AudioBuffer source, int offsetFrame, float gain)`
  Adds an `AudioBuffer` track at an exact sample-frame offset.
  `void`
  `clear()`
  Removes all tracks from this mixer.
  `int`
  `getNumChannels()`
  Gets the number of interleaved channels used by this mixer.
  `int`
  `getOutputFrameCount()`
  Gets the number of sample frames in the mixed output.
  `int`
  `getOutputSize()`
  Gets the number of interleaved samples in the mixed output.
  `int`
  `getSampleRate()`
  Gets the sample rate used by this mixer.
  `int`
  `getTrackCount()`
  Gets the number of tracks currently in the mixer.
  `static boolean`
  `isSimdOptimizationsEnabled()`
  Indicates whether the mixer should use SIMD-backed kernels when the current platform supports them.
  `AudioBuffer`
  `mix()`
  Mixes all tracks into a new `AudioBuffer`.
  `static void`
  `resetSimdOptimizationsEnabled()`
  Restores the default SIMD optimization setting for this platform.
  `static void`
  `setSimdOptimizationsEnabled(boolean enabled)`
  Enables or disables SIMD-backed mixer optimizations.

  ### Methods inherited from class Object

  `clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## Constructor Details

  - ### AudioMixer

    public AudioMixer(int sampleRate, int numChannels)

    Creates a new mixer locked to a single sample clock.

    ###### Parameters

    - `sampleRate`: The sample rate in frames per second. E.g. 44100.
    - `numChannels`: The number of interleaved channels. E.g. 1 or 2.

- ## Method Details

  - ### isSimdOptimizationsEnabled

    public static boolean isSimdOptimizationsEnabled()

    Indicates whether the mixer should use SIMD-backed kernels when the current platform supports them.

    ###### Returns

    true when SIMD optimizations are enabled.

  - ### setSimdOptimizationsEnabled

    public static void setSimdOptimizationsEnabled(boolean enabled)

    Enables or disables SIMD-backed mixer optimizations.

    This only affects internal implementation choices; mixed samples and clipping semantics are unchanged.

    ###### Parameters

    - `enabled`: true to use SIMD where supported, false to force scalar loops.

  - ### resetSimdOptimizationsEnabled

    public static void resetSimdOptimizationsEnabled()
    Restores the default SIMD optimization setting for this platform.

  - ### getSampleRate

    public int getSampleRate()

    Gets the sample rate used by this mixer.

    ###### Returns

    the sample rate in frames per second.

  - ### getNumChannels

    public int getNumChannels()

    Gets the number of interleaved channels used by this mixer.

    ###### Returns

    the number of channels.

  - ### getTrackCount

    public int getTrackCount()

    Gets the number of tracks currently in the mixer.

    ###### Returns

    the number of tracks.

  - ### clear

    public void clear()
    Removes all tracks from this mixer.

  - ### addTrack

    public AudioMixer addTrack(AudioBuffer source, long offsetMs, float gain)

    Adds an `AudioBuffer` track at the given millisecond offset.

    The source samples are copied when the track is added so the source buffer can be reused or modified afterwards. The offset is rounded to the nearest sample frame on the mixer clock.

    ###### Parameters

    - `source`: Source PCM buffer. Its sample rate and channel count must match the mixer.
    - `offsetMs`: Offset from the start of the timeline in milliseconds.
    - `gain`: Gain multiplier for this track. `1.0f` preserves level.

    ###### Returns

    this mixer.

  - ### addTrackAtFrame

    public AudioMixer addTrackAtFrame(AudioBuffer source, int offsetFrame, float gain)

    Adds an `AudioBuffer` track at an exact sample-frame offset.

    Use this method when the track position is already known in frames and no millisecond conversion should be applied.

    ###### Parameters

    - `source`: Source PCM buffer. Its sample rate and channel count must match the mixer.
    - `offsetFrame`: Offset from the start of the timeline in sample frames.
    - `gain`: Gain multiplier for this track. `1.0f` preserves level.

    ###### Returns

    this mixer.

  - ### addTrack

    public AudioMixer addTrack(float[] pcmData, long offsetMs, float gain)

    Adds an interleaved PCM track at the given millisecond offset.

    The source samples are copied when the track is added. The offset is rounded to the nearest sample frame on the mixer clock.

    ###### Parameters

    - `pcmData`: Interleaved PCM samples.
    - `offsetMs`: Offset from the start of the timeline in milliseconds.
    - `gain`: Gain multiplier for this track. `1.0f` preserves level.

    ###### Returns

    this mixer.

  - ### addTrack

    public AudioMixer addTrack(float[] pcmData, int offset, int len, long offsetMs, float gain)

    Adds an interleaved PCM track range at the given millisecond offset.

    The source samples are copied when the track is added. The offset is rounded to the nearest sample frame on the mixer clock.

    ###### Parameters

    - `pcmData`: Interleaved PCM samples.
    - `offset`: Offset in `pcmData` to start copying from.
    - `len`: Number of samples to copy.
    - `offsetMs`: Offset from the start of the timeline in milliseconds.
    - `gain`: Gain multiplier for this track. `1.0f` preserves level.

    ###### Returns

    this mixer.

  - ### addTrackAtFrame

    public AudioMixer addTrackAtFrame(float[] pcmData, int offset, int len, int offsetFrame, float gain)

    Adds an interleaved PCM track range at an exact sample-frame offset.

    ###### Parameters

    - `pcmData`: Interleaved PCM samples.
    - `offset`: Offset in `pcmData` to start copying from.
    - `len`: Number of samples to copy.
    - `offsetFrame`: Offset from the start of the timeline in sample frames.
    - `gain`: Gain multiplier for this track. `1.0f` preserves level.

    ###### Returns

    this mixer.

  - ### getOutputFrameCount

    public int getOutputFrameCount()

    Gets the number of sample frames in the mixed output.

    ###### Returns

    the mixed timeline length in sample frames.

  - ### getOutputSize

    public int getOutputSize()

    Gets the number of interleaved samples in the mixed output.

    ###### Returns

    the mixed timeline length in samples.

  - ### mix

    public AudioBuffer mix()

    Mixes all tracks into a new `AudioBuffer`.

    The output is clipped to the normalized PCM range `[-1, 1]` so it can be passed directly to `WAVWriter`.

    ###### Returns

    a new `AudioBuffer` containing the mixed PCM timeline.