public class AudioMixer

  1. Object
  2. AudioMixer

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());

Constructors

public AudioMixer(int sampleRate, int numChannels)Creates a new mixer locked to a single sample clock.

Methods

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

Inherited methods

Constructor details

AudioMixer

public AudioMixer(int sampleRate, int numChannels)
Creates a new mixer locked to a single sample clock.

Parameters

sampleRate int
The sample rate in frames per second. E.g. 44100.
numChannels int
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 boolean
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 AudioBuffer
Source PCM buffer. Its sample rate and channel count must match the mixer.
offsetMs long
Offset from the start of the timeline in milliseconds.
gain float
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 AudioBuffer
Source PCM buffer. Its sample rate and channel count must match the mixer.
offsetFrame int
Offset from the start of the timeline in sample frames.
gain float
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 float[]
Interleaved PCM samples.
offsetMs long
Offset from the start of the timeline in milliseconds.
gain float
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 float[]
Interleaved PCM samples.
offset int
Offset in pcmData to start copying from.
len int
Number of samples to copy.
offsetMs long
Offset from the start of the timeline in milliseconds.
gain float
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 float[]
Interleaved PCM samples.
offset int
Offset in pcmData to start copying from.
len int
Number of samples to copy.
offsetFrame int
Offset from the start of the timeline in sample frames.
gain float
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.