SonixDecoder

Decodes audio files to raw PCM data.

What is Audio Decoding?

Audio files (MP3, M4A, etc.) are compressed - they store audio efficiently but can't be processed directly. Decoding converts compressed audio to raw PCM samples that Calibra APIs can analyze.

When to Use

ScenarioUse This?Why
Load audio file for pitch detectionYesCalibra needs raw PCM
Load audio for melody evaluationYesCore use case
Play audio fileNoUse SonixPlayer (decodes internally)
Record audioNoUse SonixRecorder

Quick Start

Kotlin

// Decode to 16kHz (default) for use with Calibra
val audioData = SonixDecoder.decode("/path/to/audio.mp3")
if (audioData != null) {
println("Sample rate: ${audioData.sampleRate}") // Always 16000
println("Channels: ${audioData.numChannels}")
println("Duration: ${audioData.durationMilliSecs}ms")

// Use with Calibra APIs (expects 16kHz)
val samples = audioData.samples
}

// Decode at native sample rate (no resampling)
val nativeAudio = SonixDecoder.decode("/path/to/audio.mp3", targetSampleRate = null)

Swift

// Decode to 16kHz (default) for use with Calibra
if let audioData = SonixDecoder.decode(path: "/path/to/audio.mp3", targetSampleRate: 16000) {
print("Sample rate: \(audioData.sampleRate)") // Always 16000
print("Channels: \(audioData.numChannels)")
print("Duration: \(audioData.durationMilliSecs)ms")

// Use with Calibra APIs
let samples = audioData.samples
}

// Decode at native sample rate
let nativeAudio = SonixDecoder.decodeNative(path: "/path/to/audio.mp3")

Supported Formats

FormatAndroidiOS
WAVYesYes
MP3YesYes
M4A/AACYesYes
OGGYesNo
FLACYesYes

Platform Notes

iOS

  • Uses AVFoundation for hardware-accelerated decoding

  • Supports any format supported by Core Audio

Android

  • Uses MediaExtractor and MediaCodec for hardware decoding

  • Falls back to software decoding if needed

Common Pitfalls

  1. Null return: Check for null - decoding fails if file doesn't exist or format unsupported

  2. Wrong sample rate for Calibra: Use default (16kHz) or explicitly set targetSampleRate=16000

  3. Memory for large files: Decoding loads entire audio into memory

  4. Relative paths: Use absolute file paths

See also

For the decoded audio data structure

For encoding raw audio back to compressed formats

For explicit resampling control

CalibraPitch

For analyzing decoded audio

Functions

Link copied to clipboard
fun decode(path: String, targetSampleRate: Int? = 16000): AudioRawData?

Decodes an audio file to raw PCM data.

Link copied to clipboard

Decodes an audio file at its native sample rate (no resampling).