public class AudioMixer
- Object
- 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
Inherited methods
Constructor details
AudioMixer
public AudioMixer(int sampleRate, int numChannels)Parameters
sampleRateint- The sample rate in frames per second. E.g. 44100.
numChannelsint- The number of interleaved channels. E.g. 1 or 2.
Method details
isSimdOptimizationsEnabled
public static boolean isSimdOptimizationsEnabled()Returns
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
enabledboolean- true to use SIMD where supported, false to force scalar loops.
resetSimdOptimizationsEnabled
public static void resetSimdOptimizationsEnabled()getSampleRate
public int getSampleRate()Returns
getNumChannels
public int getNumChannels()Returns
getTrackCount
public int getTrackCount()Returns
clear
public void clear()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
sourceAudioBuffer- Source PCM buffer. Its sample rate and channel count must match the mixer.
offsetMslong- Offset from the start of the timeline in milliseconds.
gainfloat- Gain multiplier for this track.
1.0fpreserves level.
Returns
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
sourceAudioBuffer- Source PCM buffer. Its sample rate and channel count must match the mixer.
offsetFrameint- Offset from the start of the timeline in sample frames.
gainfloat- Gain multiplier for this track.
1.0fpreserves level.
Returns
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
pcmDatafloat[]- Interleaved PCM samples.
offsetMslong- Offset from the start of the timeline in milliseconds.
gainfloat- Gain multiplier for this track.
1.0fpreserves level.
Returns
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
pcmDatafloat[]- Interleaved PCM samples.
offsetint- Offset in
pcmDatato start copying from. lenint- Number of samples to copy.
offsetMslong- Offset from the start of the timeline in milliseconds.
gainfloat- Gain multiplier for this track.
1.0fpreserves level.
Returns
addTrackAtFrame
public AudioMixer addTrackAtFrame(float[] pcmData, int offset, int len, int offsetFrame, float gain)Parameters
pcmDatafloat[]- Interleaved PCM samples.
offsetint- Offset in
pcmDatato start copying from. lenint- Number of samples to copy.
offsetFrameint- Offset from the start of the timeline in sample frames.
gainfloat- Gain multiplier for this track.
1.0fpreserves level.
Returns
getOutputFrameCount
public int getOutputFrameCount()Returns
getOutputSize
public int getOutputSize()Returns
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
AudioBuffer containing the mixed PCM timeline.