Class 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());
-
Constructor Summary
ConstructorsConstructorDescriptionAudioMixer(int sampleRate, int numChannels) Creates a new mixer locked to a single sample clock. -
Method Summary
Modifier and TypeMethodDescriptionaddTrack(float[] pcmData, int offset, int len, long offsetMs, float gain) Adds an interleaved PCM track range at the given millisecond offset.addTrack(float[] pcmData, long offsetMs, float gain) Adds an interleaved PCM track at the given millisecond offset.addTrack(AudioBuffer source, long offsetMs, float gain) Adds anAudioBuffertrack at the given millisecond offset.addTrackAtFrame(float[] pcmData, int offset, int len, int offsetFrame, float gain) Adds an interleaved PCM track range at an exact sample-frame offset.addTrackAtFrame(AudioBuffer source, int offsetFrame, float gain) Adds anAudioBuffertrack at an exact sample-frame offset.voidclear()Removes all tracks from this mixer.intGets the number of interleaved channels used by this mixer.intGets the number of sample frames in the mixed output.intGets the number of interleaved samples in the mixed output.intGets the sample rate used by this mixer.intGets the number of tracks currently in the mixer.static booleanIndicates whether the mixer should use SIMD-backed kernels when the current platform supports them.mix()Mixes all tracks into a newAudioBuffer.static voidRestores the default SIMD optimization setting for this platform.static voidsetSimdOptimizationsEnabled(boolean enabled) Enables or disables SIMD-backed mixer optimizations.
-
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
Adds an
AudioBuffertrack 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.0fpreserves level.
Returns
this mixer.
-
addTrackAtFrame
Adds an
AudioBuffertrack 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.0fpreserves level.
Returns
this mixer.
-
addTrack
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.0fpreserves level.
Returns
this mixer.
-
addTrack
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 inpcmDatato 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.0fpreserves 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 inpcmDatato 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.0fpreserves 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
Mixes all tracks into a new
AudioBuffer.The output is clipped to the normalized PCM range
[-1, 1]so it can be passed directly toWAVWriter.Returns
a new
AudioBuffercontaining the mixed PCM timeline.
-