SonixAudioUtils

Audio utility functions for manipulating AudioRawData and converting between frame indices and time.

When to Use

ScenarioUse This?Why
Join audio clips end-to-endYesconcatenate(listOf(a, b, c))
Peak-normalize audioYesnormalize(audio)
Mix audio tracks offlineYesmix(listOf(a, b), gains)
Convert frame indices to secondsYesframesToTime(frames, sr, hop)
Find voiced regions from a maskYesframesToSegments(mask, sr, hop)
Mix audio for real-time playbackNoUse SonixMixer
Decode audio filesNoUse SonixDecoder

Quick Start

Kotlin

// Concatenate multiple audio clips
val combined = SonixAudioUtils.concatenate(listOf(audio1, audio2, audio3))

// Convert frame indices to time
val times = SonixAudioUtils.framesToTime(intArrayOf(0, 10, 20), sampleRate = 16000, hopLength = 512)

// Convert a boolean frame mask to time segments
val mask = booleanArrayOf(false, true, true, true, false, false, true, true)
val segments = SonixAudioUtils.framesToSegments(mask, sampleRate = 16000, hopLength = 512)
// segments: [(start1, end1), (start2, end2)]

Swift

// Concatenate multiple audio clips
let combined = SonixAudioUtils.concatenate([audio1, audio2, audio3])

// Convert frame indices to time
let times = SonixAudioUtils.framesToTime(frames: [0, 10, 20], sampleRate: 16000, hopLength: 512)

// Convert a boolean frame mask to time segments
let mask: [Bool] = [false, true, true, true, false, false, true, true]
let segments = SonixAudioUtils.framesToSegments(frameMask: mask, sampleRate: 16000, hopLength: 512)
// segments: [(start: 0.032, end: 0.128), (start: 0.192, end: 0.256)]

See also

For decoding audio files to AudioRawData

For encoding AudioRawData to compressed formats

Functions

Link copied to clipboard

Concatenate multiple AudioRawData objects sequentially.

Link copied to clipboard
fun framesToSegments(frameMask: BooleanArray, sampleRate: Int, hopLength: Int): List<Pair<Float, Float>>

Convert a boolean frame mask to time segments.

Link copied to clipboard
fun framesToTime(frames: IntArray, sampleRate: Int, hopLength: Int): FloatArray

Convert frame indices to time in seconds.

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

Mix multiple mono AudioRawData tracks into a single output.

Link copied to clipboard

Peak-normalize audio so the maximum absolute sample value reaches 1.0.

Link copied to clipboard
fun timeToFrames(times: FloatArray, sampleRate: Int, hopLength: Int): IntArray

Convert time in seconds to frame indices.