Sonix Encoder
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
| Scenario | Use This? | Why |
|---|---|---|
| Save recording to file | Yes | Core use case |
| Export synthesized audio | Yes | After synthesis |
| Convert audio formats | Yes | Decode + encode |
| Record audio directly | No | Use 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
| Format | Quality | Compatibility | Use Case |
|---|---|---|---|
| M4A/AAC | Best | iOS/Android native | Default choice |
| MP3 | Good | Universal | Maximum compatibility |
Bitrate Guide
| Bitrate | Quality | File Size |
|---|---|---|
| 64 kbps | Low (voice) | ~0.5 MB/min |
| 128 kbps | Standard | ~1 MB/min |
| 192 kbps | High | ~1.5 MB/min |
| 256 kbps | Very 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
False return: Encoding failed - check file permissions and path validity
Large memory usage: Encoding processes entire audio in memory
Wrong sample format: Float samples must be in -1.0, 1.0 range
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
Encode AudioRawData to a compressed audio file.
Encode PCM bytes to a compressed audio file.
Encode float samples to a compressed audio file.
Check if a format is available on this platform.