mix

fun mix(audioList: List<AudioRawData>, gains: FloatArray? = null, targetSampleRate: Int? = null): AudioRawData

Mix multiple mono AudioRawData tracks into a single output.

All tracks are resampled to targetSampleRate if needed. Shorter tracks are zero-padded to match the longest. Output is soft-clipped to prevent distortion.

For real-time playback mixing, use SonixMixer instead.

Usage

Kotlin

// Mix two tracks with equal gain
val mixed = SonixAudioUtils.mix(listOf(vocal, backing))

// Mix with custom gains (attenuate backing track)
val mixed = SonixAudioUtils.mix(
audioList = listOf(vocal, backing),
gains = floatArrayOf(1.0f, 0.5f)
)

Swift

// Mix two tracks
let mixed = SonixAudioUtils.mix(audioList: [vocal, backing])

// Mix with custom gains
let mixed = SonixAudioUtils.mix(
audioList: [vocal, backing],
gains: [1.0, 0.5]
)

Return

A new mono AudioRawData containing the mixed audio.

Parameters

audioList

List of mono AudioRawData tracks to mix (must not be empty).

gains

Optional per-track gain factors. If provided, must have same size as audioList.

targetSampleRate

Output sample rate in Hz. Pass null (default) to use the sample rate of the first track.

Throws

if audioList is empty, gains size doesn't match, or any track is not mono.