SonixEncoder

Encodes audio to compressed formats (M4A/AAC, MP3).

What is Audio Encoding?

Raw PCM audio uses a lot of space (~10MB per minute at 44kHz stereo). Encoding compresses audio to efficient formats like M4A or MP3 for storage and sharing.

Use it when you need to:

  • Save recorded audio to a file

  • Export synthesized audio

  • Convert between audio formats

When to Use

ScenarioUse This?Why
Save recording to fileYesCore use case
Export synthesized audioYesAfter synthesis
Convert audio formatsYesDecode + encode
Record audio directlyNoUse SonixRecorder (encodes internally)

Quick Start

Kotlin

// Encode from AudioRawData (e.g., after decoding or synthesis)
val rawData = SonixDecoder.decode("input.wav")
if (rawData != null) {
val success = SonixEncoder.encode(
data = rawData,
outputPath = "/path/to/output.mp3",
format = "mp3"
)
}

// Encode from float samples
val success = SonixEncoder.encode(
samples = floatArray,
sampleRate = 44100,
channels = 1,
outputPath = "/path/to/output.m4a"
)

// Check format availability
if (SonixEncoder.isFormatAvailable("mp3")) {
// MP3 encoding is supported
}

Swift

// Encode from AudioRawData
if let rawData = SonixDecoder.decode(path: "input.wav", targetSampleRate: nil) {
let success = SonixEncoder.encode(
data: rawData,
outputPath: "/path/to/output.mp3",
format: "mp3",
bitrateKbps: 128
)
}

// Encode from float samples
let success = SonixEncoder.encode(
samples: floatArray,
sampleRate: 44100,
channels: 1,
outputPath: "/path/to/output.m4a",
format: "m4a",
bitrateKbps: 128
)

Supported Formats

FormatQualityCompatibilityUse Case
M4A/AACBestiOS/Android nativeDefault choice
MP3GoodUniversalMaximum compatibility

Bitrate Guide

BitrateQualityFile Size
64 kbpsLow (voice)~0.5 MB/min
128 kbpsStandard~1 MB/min
192 kbpsHigh~1.5 MB/min
256 kbpsVery high~2 MB/min

Platform Notes

iOS

  • M4A/AAC uses AVFoundation (hardware accelerated)

  • MP3 uses LAME library

Android

  • M4A/AAC uses MediaCodec (hardware accelerated)

  • MP3 uses LAME library

Common Pitfalls

  1. False return: Encoding failed - check file permissions and path validity

  2. Large memory usage: Encoding processes entire audio in memory

  3. Wrong sample format: Float samples must be in -1.0, 1.0 range

  4. Blocking call: Runs synchronously; consider calling from background thread

See also

For decoding audio files to raw PCM

For recording with automatic encoding

For the raw audio data structure

Functions

Link copied to clipboard
fun encode(data: AudioRawData, outputPath: String, format: String = "m4a", bitrateKbps: Int = 128): Boolean

Encode AudioRawData to a compressed audio file.

fun encode(pcmData: ByteArray, sampleRate: Int, channels: Int, outputPath: String, format: String = "m4a", bitrateKbps: Int = 128): Boolean

Encode PCM bytes to a compressed audio file.

fun encode(samples: FloatArray, sampleRate: Int, channels: Int, outputPath: String, format: String = "m4a", bitrateKbps: Int = 128): Boolean

Encode float samples to a compressed audio file.

Link copied to clipboard

Check if a format is available on this platform.