sethdford/pocket-voice

Zero-Python real-time voice pipeline for Apple Silicon. Mic → STT → Claude → TTS → Speaker in C and Rust.

C

0

0 commits

updated May 31, 2026

See the code

README

Sonata

License: MIT Platform Language

Real-time voice intelligence for Apple Silicon. Native C + Rust. Full-duplex conversation.

Quick Start

brew install curl opus onnxruntime espeak-ng
make
ANTHROPIC_API_KEY=sk-ant-... ./sonata

Why Sonata?

Most voice pipelines are Python glue connecting cloud APIs. Sonata is different — every audio processing component from speech recognition to text-to-speech runs natively on Apple Silicon, using all four hardware compute units simultaneously (GPU + AMX + ANE + NEON). The result: low-latency full-duplex conversation with barge-in support, entirely on your Mac.

Mic → STT → LLM → TTS → Speaker

Note: The default LLM backend is Claude (cloud API). Use --llm local for fully on-device inference with Llama 3.2.

Key Features

  • 100% native audio pipeline: C + Rust. Single make builds 44 shared libraries, 6 Rust crates, custom Metal kernels, and one binary.
  • Full-duplex barge-in: CoreAudio VoiceProcessingIO with hardware AEC. Speak while the assistant is talking — playback interrupts immediately.
  • Fused 3-signal end-of-utterance: Energy VAD + LSTM endpointer + ASR-inline EOU token, weighted and fused for <240ms turn detection.
  • Speculative prefill: LLM request fires at 70% EOU confidence. Saves 100-300ms when the prediction is correct.
  • Multi-engine STT: Conformer CTC (0.9% WER on LibriSpeech), Kyutai Rust 1B, BNNS/ANE-accelerated. Switch with --stt-engine.
  • Apple Silicon optimized: Every stage runs on the right compute unit — Metal GPU for transformers, AMX for DSP, ANE for power-efficient inference, NEON for SIMD.

TTS Status

Sonata currently ships with two production-ready TTS engines and one in-development custom TTS:

EngineQualitySpeedStatus
Piper VITS (60M, ONNX)100% intelligibility (Whisper-verified)67x realtime, 51ms/sentenceProduction ready
Supertonic-2 (250M, ONNX)70-90% (voice-dependent), 10 voices48x realtimeProduction ready
Sonata TTS (294M, Metal GPU)In training — not yet producing intelligible speechPipeline functionalIn development

The default pipeline uses Piper for TTS. Sonata TTS is a custom from-scratch system designed for Apple Silicon — the inference pipeline is complete and mechanically correct, but the models require training on paired speech data before producing quality output.

Sonata TTS Architecture (In Development)

Text → Semantic LM (241M, Metal GPU) → Semantic Tokens (50 Hz)
     → Flow Matching (36M, Metal/ANE) → Acoustic Latents
     → iSTFT Decoder (5M, vDSP/AMX)  → Waveform (24kHz)
ComponentParamsHardwareNotes
Sonata Codec (encode/decode)16.8MAMX/vDSP5,373x realtime (iSTFT stage)
Sonata LM (text→semantic)241.7MMetal GPU43 tok/s (target: 50 Hz)
Sonata Flow (semantic→acoustic)35.7MMetal/ANEParallel (non-autoregressive)
Total294.2M

Design innovations:

  • Single LM pass per frame — no sequential codebook prediction (unlike Mimi's DepFormer)
  • FSQ quantization — 4096-entry codebook with zero codebook collapse
  • Conditional Flow Matching — continuous acoustic latents, zero quantization error
  • iSTFT decoder — 100x faster than ConvTranspose, AMX-native

Performance (Measured)

MetricValueEngineSource
STT WER (LibriSpeech test-clean)0.9%Conformer CTC 0.6Bbench_output/TTS_BENCHMARK_REPORT.md
STT Real-Time Factor0.075x (13x realtime)Conformer CTC 0.6Bbench_output/TTS_BENCHMARK_REPORT.md
TTS Intelligibility100% (Whisper round-trip)Piper VITSbench_output/TTS_BENCHMARK_REPORT.md
TTS Real-Time Factor0.015x (67x realtime)Piper VITSbench_output/TTS_BENCHMARK_REPORT.md
TTS→STT Round-Trip WER1.62%Piper → Conformerbench_output/BENCHMARK_REPORT.md
Full Round-Trip Latency~320msPiper + Conformerbench_output/TTS_BENCHMARK_REPORT.md
EOU Turn Detection<240msFused 3-signalREADME (design target)

Note: Latency numbers are for audio processing stages. End-to-end latency including LLM response time depends on backend (Claude API ~300ms TTFT, on-device Llama ~100ms TTFT).

Architecture

┌───────────────────────────────────────────────────────────────────────────┐
│                              sonata                                       │
│                                                                           │
│  ┌──────────────┐   ┌────────────┐   ┌────────────┐   ┌───────────────┐ │
│  │ CoreAudio     │   │ STT Engine │   │ LLM Engine │   │ TTS Engine    │ │
│  │ VoiceProc IO  │──▶│ Rust/C/ANE│──▶│Claude/Gemini──▶│ Piper/Sonata │ │
│  │ (48kHz, AEC)  │   │ fp32/16/8 │   │  /Local    │   │              │ │
│  └──────┬────────┘   └─────┬──────┘   └────────────┘   └──────┬────────┘ │
│         │                  │                                    │         │
│         │           ┌──────▼─────────────────────────┐         │         │
│         │           │  Speech Detector (unified)      │         │         │
│         │           │  Native VAD + LSTM + ASR Token   │         │         │
│         │           │  → Speculative Prefill (70%)     │         │         │
│         │           └────────────────────────────────┘         │         │
│         │                                                       │         │
│         │           Audio Post-Processing (AMX/vDSP)            │         │
│         │    ┌──────────────────────────────────────────┐       │         │
│         │    │ vDSP Prosody (pitch/volume/EQ/limiter)   │       │         │
│         │    │ Noise Gate (spectral, vDSP FFT)          │◀──────┘         │
│         │    │ LUFS Loudness Normalization (BS.1770)     │                 │
│         │    │ AudioConverter HW Resampler (24→48kHz)   │                 │
│         │    │ Breath Synthesis (Voss-McCartney pink)    │                 │
│         │    │ Spatial Audio HRTF (optional 3D)          │                 │
│         ◀────│ SPMC Ring → Speaker + Opus Encoder        │                 │
│              └──────────────────────────────────────────┘                 │
│                                                                           │
│  ┌───────────────────────────────────────────────────────────────────┐   │
│  │  Conversation Intelligence                                         │   │
│  │  Backchannel · Emotion · Diarizer · Memory · Prosody Prediction   │   │
│  └───────────────────────────────────────────────────────────────────┘   │
│                                                                           │
│  ┌───────────────────────────────────────────────────────────────────┐   │
│  │  Network APIs: HTTP REST · WebSocket · Web Remote                  │   │
│  └───────────────────────────────────────────────────────────────────┘   │
└───────────────────────────────────────────────────────────────────────────┘

Pipeline State Machine

Listening → Recording → Processing → Streaming → Speaking → Listening
                 │           │            ↑
                 │           │  (speculative prefill at 70% EOU)
                 │           │
                 └───────────┘  Barge-in (any state) ──→ Listening
StateDescription
ListeningEnergy VAD monitors mic for speech onset
RecordingCaptures audio, feeds STT frame-by-frame, runs fused EOU detection
ProcessingSends transcript to LLM (or skips if speculative prefill already in-flight)
StreamingReceives LLM tokens via SSE, feeds sentence buffer → TTS incrementally
SpeakingDrains remaining TTS audio to speaker
Barge-inUser speaks during playback → immediate interrupt, back to Listening

Native Libraries (44 C shared libraries + 6 Rust cdylibs + 1 metallib)

Core Audio Engine

LibraryPurposeHardware
pocket_voice.cCoreAudio VoiceProcessingIO, lock-free SPSC rings, energy VAD, barge-inCoreAudio RT thread
neon_audio.hARM NEON SIMD: float32↔int16 PCM, vectorized copy, crossfadeNEON (8 floats/cycle)

Speech Recognition

LibraryPurposeHardware
pocket_stt (Rust)Kyutai STT 1B inferencecandle + Metal GPU
conformer_stt.cPure C FastConformer CTC engine, .cstt format (fp32/fp16/int8), EOU, cache-aware streamingAMX + NEON
sonata_stt.cCTC streaming ASR with RoPE conformer, beam search, inline EOU detectionAMX (cblas_sgemm)
sonata_refiner.cSemantic token → text encoder-decoder transformer (GQA, RoPE, RMSNorm)AMX (cblas_sgemm)
bnns_conformer.cBNNS Graph accelerated Conformer encoder for ANE dispatch (macOS 15+)Apple Neural Engine
mel_spectrogram.cStreaming 80-bin log-mel extraction using vDSP FFTAMX (vDSP_fft_zrip)
ctc_beam_decoder.cppCTC prefix beam search with optional KenLM n-gram rescoringCPU
tdt_decoder.cToken Duration Transducer decoder (LSTM prediction + joint network)AMX (Accelerate)
spm_tokenizer.cSentencePiece unigram tokenizer (pure C, Viterbi decode)CPU

Speech Synthesis

LibraryPurposeHardware
sonata_lm (Rust)241M semantic language model (text → semantic tokens at 50 Hz)candle + Metal GPU
sonata_flow (Rust)35.7M conditional flow matching (semantic → acoustic latents)candle + Metal GPU
sonata_storm (Rust)Parallel TTS — non-autoregressive batch synthesiscandle + Metal GPU
sonata_istft.ciSTFT decoder: magnitude+phase → waveform via vDSP (~100x faster than ConvTranspose)AMX (vDSP)
bnns_convnext_decoder.cANE-accelerated ConvNeXt decoder (frees GPU for flow network)Apple Neural Engine
phonemizer.cespeak-ng IPA phonemizer for TTS text preprocessinglibespeak-ng

LLM Backends

LibraryPurposeHardware
Claude SSE (libcurl)Anthropic Claude API with streaming SSENetwork
Gemini SSE (libcurl)Google Gemini API with streaming SSENetwork
pocket_llm (Rust)On-device Llama 3.2 (1B-3B) with top-p sampling and KV cachecandle + Metal GPU

Voice Activity & End-of-Utterance Detection

LibraryPurposeHardware
native_vad.cPure C VAD: STFT → Conv → LSTM (weights from Silero VAD ONNX)AMX (Accelerate)
speech_detector.cUnified VAD+EOU wrapper: manages resampling, chunking, and signal fusionCPU
mimi_endpointer.cLSTM-based endpointer on mel-energy features from capture audioAMX (cblas_sgemv)
fused_eou.c3-signal weighted fusion: energy + LSTM + ASR token, EMA smoothing, speculative prefill logicCPU

Audio Post-Processing

LibraryPurposeHardware
vdsp_prosody.cPhase vocoder pitch shift, WSOLA time stretch, biquad EQ, soft-knee limiterAMX (vDSP, vForce)
audio_converter.cApple AudioConverter HW sample rate conversion (24↔48kHz)Apple AudioConverter
spatial_audio.cHRTF binaural 3D audio positioning (azimuth/elevation)vDSP convolution
breath_synthesis.cVoss-McCartney pink noise with Butterworth bandpass, ADSR envelopesAccelerate + NEON
lufs.cITU-R BS.1770 loudness meter and normalization with K-weightingvDSP biquad
noise_gate.cSpectral noise gate for STT preprocessing (adaptive noise floor, per-bin gating)AMX (vDSP FFT)
opus_codec.cReal-time Opus encoding/decodinglibopus

Prosody & Expression

LibraryPurposeHardware
prosody_predict.cText-based prosody prediction (syllable duration, emotion, conversational adaptation)CPU
prosody_log.cJSONL prosody logging for visualization dashboardCPU
emphasis_predict.cLinguistics-based emphasis prediction (contrast, intensifiers, negation, enumeration)CPU

Text Processing

LibraryPurpose
text_normalize.cNumber, date, currency, phone number expansion for STT/TTS
ssml_parser.cSSML parsing: <prosody>, <break>, <say-as>, <emphasis>
sentence_buffer.cStreaming LLM token accumulation, sentence boundary detection, predictive length EMA

Conversation Intelligence

LibraryPurposeHardware
audio_emotion.cReal-time emotion detection from mel-energy features (valence, arousal)AMX (vDSP)
speaker_encoder.cONNX-based speaker embedding extraction (ECAPA-TDNN, WavLM)ONNX Runtime
speaker_diarizer.cSpeaker diarization via cosine similarity on running centroid embeddingsAccelerate
conversation_memory.cConversation context persistence (JSONL history, token-aware truncation)CPU
backchannel.cActive listening backchannel generation ("mhm", "yeah") from acoustic cuesAccelerate
voice_onboard.cReal-time voice onboarding: speaker embedding + prosody profile extractionAccelerate

Network & API

LibraryPurposeHardware
http_api.cREST API server with TTS endpoints (PCM, WAV, mu-law, A-law encoding)CPU
websocket.cRFC 6455 WebSocket implementation (text, binary, ping/pong, close)CPU
web_remote.cWebSocket audio server: phone browser mic → pipeline → audio back to phoneCPU

Quality & Profiling

LibraryPurposeHardware
voice_quality.cPESQ-lite, STOI-lite, Log-Spectral Distance, MOS predictionAccelerate (vDSP FFT)
latency_profiler.cPer-stage nanosecond latency with P50/P95/P99 statsmach_absolute_time
apple_perf.cApple Silicon perf: RT thread scheduling, huge pages, IOSurface zero-copy, NEON softmax/GELUIOSurface + Foundation
metal_loader.cRuntime .metallib loader for custom GPU kernelsMetal (Objective-C)

Infrastructure

LibraryPurpose
vm_ring.cVM-mirrored ring buffer via mach_vm_remap for zero-copy wraparound
spmc_ring.hSingle-producer multi-consumer lock-free ring (speaker + Opus encoder)
kv_cache.hCache-oblivious interleaved KV cache [H][T][2][D] — halves L2 misses
triple_buffer.hLock-free triple buffer for GPU→CPU→CoreAudio
arena.hBump-pointer arena allocator, checkpoint/restore, zero per-turn malloc overhead
lstm_ops.hLSTM cell operations for native VAD and endpointer

Optimizations

Hardware Utilization

Sonata runs on all four Apple Silicon compute units simultaneously:

UnitWhat RunsWhy
Metal GPUSonata LM + Flow + Storm inference, custom kernels (.metallib)Massively parallel transformer ops
AMX CoprocessorProsody, FFT, LUFS, LSTM endpointer, Conformer STT (Accelerate)Matrix-vector products, vDSP
Neural EngineBNNS Graph Conformer encoder + ConvNeXt decoder (macOS 15+)Power-efficient inference
ARM NEONPCM conversion, INT8 dequantize, crossfade, ring copies, softmax/GELU8-wide SIMD on integer/float

Key Performance Techniques

  • Lock-free everywhere: SPSC and SPMC ring buffers with 128-byte cache-line aligned atomics. Zero locks in the audio hot path.
  • Zero allocation in hot loops: Arena allocator provides per-turn memory. CoreAudio callback does zero malloc/free.
  • INT8 quantization: Per-channel symmetric quantization with NEON-vectorized dequantize → cblas_sgemm. ~4x memory reduction.
  • Fused operations: vDSP pitch→EQ→volume→limit chain. Interleaved KV cache eliminates separate K/V fetches.
  • VM-mirrored ring buffers: mach_vm_remap creates a virtual memory mirror — reads/writes never need to handle wraparound.
  • Hardware resampling: Apple AudioConverter for sample rate conversion is higher quality and faster than FIR for non-integer ratios.
  • Speculative prefill: LLM API request starts at 70% EOU confidence, saving 100-300ms when the prediction is correct.
  • Conformer cache-aware streaming: Per-layer K/V projection and convolution state caching for true frame-by-frame inference without recomputation.
  • Custom Metal kernels: Flash Attention v2, fused SiLU+gate, layer norm — loaded at runtime from compiled .metallib.
  • Concurrent compute unit dispatch: GPU runs Sonata LM while ANE runs ConvNeXt decoder while AMX runs iSTFT — all three stages overlap.
  • Apple perf primitives: RT thread scheduling, huge page model loading, IOSurface zero-copy GPU↔CPU sharing.

Quality Assurance Framework

Sonata includes a comprehensive native C quality benchmark suite for proving STT and TTS quality.

Metrics Implemented

CategoryMetricGolden Signal
IntelligibilityWER (Word Error Rate)< 5% (human-level)
IntelligibilityCER (Character Error Rate)< 2%
IntelligibilitySTOI (Short-Time Objective Intelligibility)> 0.9
NaturalnessMCD (Mel-Cepstral Distortion)< 4.0 dB
NaturalnessF0 RMSE + Correlation< 15 Hz, r > 0.85
Voice QualitySegmental SNR> 25 dB
Voice QualitySpeaker Similarity> 0.90
LatencyRTF (Real-Time Factor)< 0.2x
LatencyFirst Chunk Latency< 200ms

Round-Trip Testing

The ultimate quality proof: text → TTS → audio → STT → transcript → WER(original, transcript). If the round-trip WER is low, both TTS and STT are working correctly.

make bench-quality    # Run benchmark self-tests
make test-quality     # Run all quality metric tests
make test-roundtrip   # Round-trip with mock TTS/STT callbacks

Test Suite

make test    # Run all 30 test suites
SuiteWhat It Covers
bench-qualitySelf-tests for all quality metric implementations
test-qualityWER, CER, MCD, STOI, SNR, F0, speaker similarity, latency harness, grading
test-eouMimi endpointer LSTM, fused EOU, speculative prefill, Conformer EOU flags/API
test-roundtripRound-trip framework with mock TTS/STT callbacks, NULL handling
test-pipelineText normalization, sentence buffer, SSML parser
test-new-modulesBreath synthesis, LUFS, arena, VM ring, triple buffer, SPMC ring, KV cache
test-new-enginesPhonemizer and speaker encoder API tests
test-bugfixesSPMC mirror copy, KV cache overflow, arena accounting, LUFS non-48kHz
test-conformerMel spectrogram extraction, Conformer STT API
test-llm-prosodyLLM prosody integration with SSML and text normalization
test-optimizationsINT8 round-trip, BNNS API, FP16 NEON, latency profiler, voice quality metrics
test-beam-searchCTC beam decoder with and without KenLM
test-sonataSonata iSTFT, SentencePiece tokenizer, ConvNeXt decoder, LM + Flow FFI
test-sonata-v3Sonata Flow v3 API tests
test-real-modelsPhonemizer and speaker encoder with real model files
test-prosody-predictText-based prosody prediction (syllables, emotion, adaptation)
test-prosody-logJSONL prosody logging and replay
test-emphasisLinguistics-based emphasis prediction rules
test-prosody-integrationEnd-to-end prosody pipeline (emphasis + SSML + prediction)
test-voice-onboardVoice onboarding: embedding extraction + prosody profiling
test-conversation-memoryConversation context persistence and token-aware truncation
test-diarizerSpeaker diarization with cosine similarity tracking
test-vdsp-prosodyvDSP prosody processing: pitch shift, time stretch, EQ, limiter
test-http-apiREST API server endpoints and WebSocket integration
test-sonata-stormSonata Storm parallel TTS FFI
test-audio-emotionAudio emotion detection (valence, arousal)
test-sonata-flow-ffiSonata Flow Rust FFI boundary tests
test-sonata-lm-ffiSonata LM Rust FFI boundary tests
test-pipeline-threadingPipeline threading, lock-free ring buffer concurrency
test-phase2-regressionsPhase 2 regression tests across breath, mel, sentence, conformer modules

Additional targets (not in make test):

make test-sonata-quality     # End-to-end TTS→STT quality with Sonata
make test-sonata-stt         # Sonata STT + refiner API tests
make test-native-vad         # Pure C VAD unit tests
make test-speech-detector    # Unified speech detector tests
make test-apple-perf         # Apple Silicon perf primitives
make test-quality-improvements  # Noise gate + LUFS + voice quality
make bench-vad               # VAD throughput benchmark
make bench-sonata            # Sonata TTS throughput benchmark
make bench-live              # Live model benchmark with real audio
make bench-industry          # Industry-standard quality benchmarks

Additional Features

  • INT8 quantization: Per-channel symmetric INT8 weights with NEON-vectorized dequantization. ~4x smaller models with <1% accuracy loss.
  • Apple Neural Engine: BNNS Graph-accelerated Conformer encoder and ConvNeXt decoder dispatch to the ANE on macOS 15+ for power-efficient inference.
  • Custom Metal kernels: Flash Attention v2, fused SiLU+gate, layer norm, and fp16 GEMM compiled as .metallib and loaded at runtime.
  • Per-turn latency profiling: --profiler flag enables nanosecond-precision breakdown of every pipeline stage (STT, LLM TTFT, TTS TTFS, E2E) with P50/P95/P99 statistics.
  • Voice quality metrics: PESQ-lite, STOI-lite, Log-Spectral Distance, and MOS prediction for automated TTS quality evaluation.
  • Speaker diarization: ONNX-based speaker embedding extraction with cosine similarity tracking for multi-speaker conversations.
  • Active listening: Backchannel generation ("mhm", "yeah") from acoustic cues at ~50ms latency — no LLM round-trip required.
  • Web remote: Phone browser captures mic via Web Audio API → streams PCM over WebSocket → pipeline processes and streams audio back.
  • REST API: HTTP server with WebSocket streaming for integration with external applications.
  • Multi-LLM backend: Supports Claude (SSE), Gemini, and on-device Llama 3.2 (1B-3B). Switch with --llm claude|gemini|local.
  • Multi-STT backend: Kyutai Rust (1B), pure C Conformer CTC (fp32/fp16/int8), Sonata CTC (RoPE conformer), and BNNS/ANE-accelerated Conformer. Switch with --stt-engine rust|conformer|bnns.

Requirements

  • macOS 14+ on Apple Silicon (M1/M2/M3/M4)
  • Xcode Command Line Tools (xcode-select --install)
  • Rust (curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh)
  • Homebrew dependencies: brew install curl opus onnxruntime espeak-ng
  • Anthropic API key (ANTHROPIC_API_KEY) for Claude backend (or use --llm local for on-device)

Build

make          # Build everything: 44 C shared libraries + 6 Rust cdylibs + 1 metallib + binary
make libs     # Build just the C shared libraries + metallib
make clean    # Remove all build artifacts

Build output:

  1. build/*.dylib — 44 C shared libraries
  2. build/tensor_ops.metallib — Custom Metal kernels (Flash Attention v2, fused SiLU+gate, layer norm)
  3. src/stt/target/release/libpocket_stt.dylib — Rust STT cdylib (Kyutai 1B, candle + Metal)
  4. src/llm/target/release/libpocket_llm.dylib — Rust local LLM cdylib (Llama 3.2, candle + Metal)
  5. src/local_llm/target/release/liblocal_llm.dylib — Rust local LLM alternative cdylib (candle + Metal)
  6. src/sonata_lm/target/release/libsonata_lm.dylib — Rust Sonata LM cdylib (241M semantic model)
  7. src/sonata_flow/target/release/libsonata_flow.dylib — Rust Sonata Flow cdylib (35.7M flow matching)
  8. src/sonata_storm/target/release/libsonata_storm.dylib — Rust Sonata Storm cdylib (parallel TTS)
  9. sonata — Pipeline binary linking everything

Run

ANTHROPIC_API_KEY=sk-ant-... ./sonata

Speak naturally. The pipeline detects speech onset, transcribes in real-time, sends to the LLM, and streams TTS audio back — all with full-duplex barge-in support.

Options

sonata [OPTIONS]

Voice & Model:
  --voice PATH       Voice .wav or .safetensors path
  --stt-repo REPO    STT HuggingFace repo (default: kyutai/stt-1b-en_fr-candle)
  --tts-repo REPO    TTS HuggingFace repo (default: kyutai/tts-1.6b-en_fr)
  --n-q N            Audio codebooks for TTS (default: 24)

STT Engine:
  --stt-engine E     STT engine: rust (default), conformer, or bnns
  --cstt-model PATH  Conformer STT .cstt model file path
  --bnns-model PATH  BNNS .mlmodelc path (ANE accelerated)

TTS Engine:
  --tts-engine E     TTS engine: rust (default), c (Kyutai C), or pocket (100M)

LLM Backend:
  --llm ENGINE       LLM backend: claude (default), gemini, or local
  --llm-model M      LLM model name (auto-detected per engine)
  --system PROMPT    System prompt

VAD:
  --no-vad           Disable semantic VAD (use energy VAD only)
  --vad-threshold F  Semantic VAD threshold (default: 0.7)

Audio Post-Processing:
  --pitch F          Pitch multiplier (1.0 = normal, 1.2 = higher)
  --volume F         Volume in dB (0.0 = normal, 6.0 = louder)
  --no-hw-resample   Disable AudioConverter (use FIR fallback)
  --spatial AZ       Enable 3D spatial audio at azimuth AZ degrees

Advanced:
  --metallib PATH    Custom .metallib path for GPU kernels
  --profiler         Enable per-turn latency profiling
  --prosody          Enable SSML-aware system prompt

Examples

# Default — conversational voice assistant
ANTHROPIC_API_KEY=sk-... ./sonata

# Custom voice with pitched-up output
./sonata --voice /path/to/voice.wav --pitch 1.15

# Spatial audio — voice positioned 30° to the right
./sonata --spatial 30

# On-device LLM (no API key needed)
./sonata --llm local --llm-model meta-llama/Llama-3.2-1B-Instruct

# Conformer STT with INT8 model
./sonata --stt-engine conformer --cstt-model models/parakeet_0.6b_int8.cstt

# BNNS/ANE accelerated STT (macOS 15+)
./sonata --stt-engine bnns --cstt-model models/parakeet_0.6b.cstt \
  --bnns-model models/conformer_ctc_0.6b.mlmodelc

# Latency profiling
./sonata --profiler

# Custom Metal kernels
./sonata --metallib build/tensor_ops.metallib

# Custom Claude model with system prompt
./sonata --llm-model claude-sonnet-4-20250514 \
  --system "You are a helpful coding assistant."

Project Structure

sonata/
├── Makefile              # Build system (C libs + Rust + binary + tests)
├── README.md             # This file
├── CONTRIBUTING.md       # Contributing guide
├── AGENTS.md             # AI agent guidance
├── LICENSE               # MIT
├── src/                  # 45 C source files + 6 Rust crates + Metal kernels
├── include/              # C header files
├── tests/                # 30+ test suites
├── scripts/              # Benchmarking, conversion, and export scripts
├── web/                  # Web dashboard and remote UI
├── docs/                 # API reference, architecture, troubleshooting
├── examples/             # curl, WebSocket, and integration examples
├── models/               # Downloaded/compiled models (gitignored)
└── build/                # Compiled output (gitignored)

Contributing

See CONTRIBUTING.md for development setup, code style, and pull request process.

Companion Project

pocket-tts — Python/MLX text-to-speech library with custom Metal kernels, Apple AMX/Accelerate DSP, speculative decoding, and OpenAI-compatible API. Sonata uses Rust inference crates (candle + Metal) instead of MLX, but shares the same philosophy of deep Apple Silicon optimization.

License

MIT — Copyright (c) 2026 Seth Ford

sethdford/pocket-voice

Zero-Python real-time voice pipeline for Apple Silicon. Mic → STT → Claude → TTS → Speaker in C and Rust.

C

0

0 commits

updated May 31, 2026

See the code

README

Sonata

License: MIT Platform Language

Real-time voice intelligence for Apple Silicon. Native C + Rust. Full-duplex conversation.

Quick Start

brew install curl opus onnxruntime espeak-ng
make
ANTHROPIC_API_KEY=sk-ant-... ./sonata

Why Sonata?

Most voice pipelines are Python glue connecting cloud APIs. Sonata is different — every audio processing component from speech recognition to text-to-speech runs natively on Apple Silicon, using all four hardware compute units simultaneously (GPU + AMX + ANE + NEON). The result: low-latency full-duplex conversation with barge-in support, entirely on your Mac.

Mic → STT → LLM → TTS → Speaker

Note: The default LLM backend is Claude (cloud API). Use --llm local for fully on-device inference with Llama 3.2.

Key Features

  • 100% native audio pipeline: C + Rust. Single make builds 44 shared libraries, 6 Rust crates, custom Metal kernels, and one binary.
  • Full-duplex barge-in: CoreAudio VoiceProcessingIO with hardware AEC. Speak while the assistant is talking — playback interrupts immediately.
  • Fused 3-signal end-of-utterance: Energy VAD + LSTM endpointer + ASR-inline EOU token, weighted and fused for <240ms turn detection.
  • Speculative prefill: LLM request fires at 70% EOU confidence. Saves 100-300ms when the prediction is correct.
  • Multi-engine STT: Conformer CTC (0.9% WER on LibriSpeech), Kyutai Rust 1B, BNNS/ANE-accelerated. Switch with --stt-engine.
  • Apple Silicon optimized: Every stage runs on the right compute unit — Metal GPU for transformers, AMX for DSP, ANE for power-efficient inference, NEON for SIMD.

TTS Status

Sonata currently ships with two production-ready TTS engines and one in-development custom TTS:

EngineQualitySpeedStatus
Piper VITS (60M, ONNX)100% intelligibility (Whisper-verified)67x realtime, 51ms/sentenceProduction ready
Supertonic-2 (250M, ONNX)70-90% (voice-dependent), 10 voices48x realtimeProduction ready
Sonata TTS (294M, Metal GPU)In training — not yet producing intelligible speechPipeline functionalIn development

The default pipeline uses Piper for TTS. Sonata TTS is a custom from-scratch system designed for Apple Silicon — the inference pipeline is complete and mechanically correct, but the models require training on paired speech data before producing quality output.

Sonata TTS Architecture (In Development)

Text → Semantic LM (241M, Metal GPU) → Semantic Tokens (50 Hz)
     → Flow Matching (36M, Metal/ANE) → Acoustic Latents
     → iSTFT Decoder (5M, vDSP/AMX)  → Waveform (24kHz)
ComponentParamsHardwareNotes
Sonata Codec (encode/decode)16.8MAMX/vDSP5,373x realtime (iSTFT stage)
Sonata LM (text→semantic)241.7MMetal GPU43 tok/s (target: 50 Hz)
Sonata Flow (semantic→acoustic)35.7MMetal/ANEParallel (non-autoregressive)
Total294.2M

Design innovations:

  • Single LM pass per frame — no sequential codebook prediction (unlike Mimi's DepFormer)
  • FSQ quantization — 4096-entry codebook with zero codebook collapse
  • Conditional Flow Matching — continuous acoustic latents, zero quantization error
  • iSTFT decoder — 100x faster than ConvTranspose, AMX-native

Performance (Measured)

MetricValueEngineSource
STT WER (LibriSpeech test-clean)0.9%Conformer CTC 0.6Bbench_output/TTS_BENCHMARK_REPORT.md
STT Real-Time Factor0.075x (13x realtime)Conformer CTC 0.6Bbench_output/TTS_BENCHMARK_REPORT.md
TTS Intelligibility100% (Whisper round-trip)Piper VITSbench_output/TTS_BENCHMARK_REPORT.md
TTS Real-Time Factor0.015x (67x realtime)Piper VITSbench_output/TTS_BENCHMARK_REPORT.md
TTS→STT Round-Trip WER1.62%Piper → Conformerbench_output/BENCHMARK_REPORT.md
Full Round-Trip Latency~320msPiper + Conformerbench_output/TTS_BENCHMARK_REPORT.md
EOU Turn Detection<240msFused 3-signalREADME (design target)

Note: Latency numbers are for audio processing stages. End-to-end latency including LLM response time depends on backend (Claude API ~300ms TTFT, on-device Llama ~100ms TTFT).

Architecture

┌───────────────────────────────────────────────────────────────────────────┐
│                              sonata                                       │
│                                                                           │
│  ┌──────────────┐   ┌────────────┐   ┌────────────┐   ┌───────────────┐ │
│  │ CoreAudio     │   │ STT Engine │   │ LLM Engine │   │ TTS Engine    │ │
│  │ VoiceProc IO  │──▶│ Rust/C/ANE│──▶│Claude/Gemini──▶│ Piper/Sonata │ │
│  │ (48kHz, AEC)  │   │ fp32/16/8 │   │  /Local    │   │              │ │
│  └──────┬────────┘   └─────┬──────┘   └────────────┘   └──────┬────────┘ │
│         │                  │                                    │         │
│         │           ┌──────▼─────────────────────────┐         │         │
│         │           │  Speech Detector (unified)      │         │         │
│         │           │  Native VAD + LSTM + ASR Token   │         │         │
│         │           │  → Speculative Prefill (70%)     │         │         │
│         │           └────────────────────────────────┘         │         │
│         │                                                       │         │
│         │           Audio Post-Processing (AMX/vDSP)            │         │
│         │    ┌──────────────────────────────────────────┐       │         │
│         │    │ vDSP Prosody (pitch/volume/EQ/limiter)   │       │         │
│         │    │ Noise Gate (spectral, vDSP FFT)          │◀──────┘         │
│         │    │ LUFS Loudness Normalization (BS.1770)     │                 │
│         │    │ AudioConverter HW Resampler (24→48kHz)   │                 │
│         │    │ Breath Synthesis (Voss-McCartney pink)    │                 │
│         │    │ Spatial Audio HRTF (optional 3D)          │                 │
│         ◀────│ SPMC Ring → Speaker + Opus Encoder        │                 │
│              └──────────────────────────────────────────┘                 │
│                                                                           │
│  ┌───────────────────────────────────────────────────────────────────┐   │
│  │  Conversation Intelligence                                         │   │
│  │  Backchannel · Emotion · Diarizer · Memory · Prosody Prediction   │   │
│  └───────────────────────────────────────────────────────────────────┘   │
│                                                                           │
│  ┌───────────────────────────────────────────────────────────────────┐   │
│  │  Network APIs: HTTP REST · WebSocket · Web Remote                  │   │
│  └───────────────────────────────────────────────────────────────────┘   │
└───────────────────────────────────────────────────────────────────────────┘

Pipeline State Machine

Listening → Recording → Processing → Streaming → Speaking → Listening
                 │           │            ↑
                 │           │  (speculative prefill at 70% EOU)
                 │           │
                 └───────────┘  Barge-in (any state) ──→ Listening
StateDescription
ListeningEnergy VAD monitors mic for speech onset
RecordingCaptures audio, feeds STT frame-by-frame, runs fused EOU detection
ProcessingSends transcript to LLM (or skips if speculative prefill already in-flight)
StreamingReceives LLM tokens via SSE, feeds sentence buffer → TTS incrementally
SpeakingDrains remaining TTS audio to speaker
Barge-inUser speaks during playback → immediate interrupt, back to Listening

Native Libraries (44 C shared libraries + 6 Rust cdylibs + 1 metallib)

Core Audio Engine

LibraryPurposeHardware
pocket_voice.cCoreAudio VoiceProcessingIO, lock-free SPSC rings, energy VAD, barge-inCoreAudio RT thread
neon_audio.hARM NEON SIMD: float32↔int16 PCM, vectorized copy, crossfadeNEON (8 floats/cycle)

Speech Recognition

LibraryPurposeHardware
pocket_stt (Rust)Kyutai STT 1B inferencecandle + Metal GPU
conformer_stt.cPure C FastConformer CTC engine, .cstt format (fp32/fp16/int8), EOU, cache-aware streamingAMX + NEON
sonata_stt.cCTC streaming ASR with RoPE conformer, beam search, inline EOU detectionAMX (cblas_sgemm)
sonata_refiner.cSemantic token → text encoder-decoder transformer (GQA, RoPE, RMSNorm)AMX (cblas_sgemm)
bnns_conformer.cBNNS Graph accelerated Conformer encoder for ANE dispatch (macOS 15+)Apple Neural Engine
mel_spectrogram.cStreaming 80-bin log-mel extraction using vDSP FFTAMX (vDSP_fft_zrip)
ctc_beam_decoder.cppCTC prefix beam search with optional KenLM n-gram rescoringCPU
tdt_decoder.cToken Duration Transducer decoder (LSTM prediction + joint network)AMX (Accelerate)
spm_tokenizer.cSentencePiece unigram tokenizer (pure C, Viterbi decode)CPU

Speech Synthesis

LibraryPurposeHardware
sonata_lm (Rust)241M semantic language model (text → semantic tokens at 50 Hz)candle + Metal GPU
sonata_flow (Rust)35.7M conditional flow matching (semantic → acoustic latents)candle + Metal GPU
sonata_storm (Rust)Parallel TTS — non-autoregressive batch synthesiscandle + Metal GPU
sonata_istft.ciSTFT decoder: magnitude+phase → waveform via vDSP (~100x faster than ConvTranspose)AMX (vDSP)
bnns_convnext_decoder.cANE-accelerated ConvNeXt decoder (frees GPU for flow network)Apple Neural Engine
phonemizer.cespeak-ng IPA phonemizer for TTS text preprocessinglibespeak-ng

LLM Backends

LibraryPurposeHardware
Claude SSE (libcurl)Anthropic Claude API with streaming SSENetwork
Gemini SSE (libcurl)Google Gemini API with streaming SSENetwork
pocket_llm (Rust)On-device Llama 3.2 (1B-3B) with top-p sampling and KV cachecandle + Metal GPU

Voice Activity & End-of-Utterance Detection

LibraryPurposeHardware
native_vad.cPure C VAD: STFT → Conv → LSTM (weights from Silero VAD ONNX)AMX (Accelerate)
speech_detector.cUnified VAD+EOU wrapper: manages resampling, chunking, and signal fusionCPU
mimi_endpointer.cLSTM-based endpointer on mel-energy features from capture audioAMX (cblas_sgemv)
fused_eou.c3-signal weighted fusion: energy + LSTM + ASR token, EMA smoothing, speculative prefill logicCPU

Audio Post-Processing

LibraryPurposeHardware
vdsp_prosody.cPhase vocoder pitch shift, WSOLA time stretch, biquad EQ, soft-knee limiterAMX (vDSP, vForce)
audio_converter.cApple AudioConverter HW sample rate conversion (24↔48kHz)Apple AudioConverter
spatial_audio.cHRTF binaural 3D audio positioning (azimuth/elevation)vDSP convolution
breath_synthesis.cVoss-McCartney pink noise with Butterworth bandpass, ADSR envelopesAccelerate + NEON
lufs.cITU-R BS.1770 loudness meter and normalization with K-weightingvDSP biquad
noise_gate.cSpectral noise gate for STT preprocessing (adaptive noise floor, per-bin gating)AMX (vDSP FFT)
opus_codec.cReal-time Opus encoding/decodinglibopus

Prosody & Expression

LibraryPurposeHardware
prosody_predict.cText-based prosody prediction (syllable duration, emotion, conversational adaptation)CPU
prosody_log.cJSONL prosody logging for visualization dashboardCPU
emphasis_predict.cLinguistics-based emphasis prediction (contrast, intensifiers, negation, enumeration)CPU

Text Processing

LibraryPurpose
text_normalize.cNumber, date, currency, phone number expansion for STT/TTS
ssml_parser.cSSML parsing: <prosody>, <break>, <say-as>, <emphasis>
sentence_buffer.cStreaming LLM token accumulation, sentence boundary detection, predictive length EMA

Conversation Intelligence

LibraryPurposeHardware
audio_emotion.cReal-time emotion detection from mel-energy features (valence, arousal)AMX (vDSP)
speaker_encoder.cONNX-based speaker embedding extraction (ECAPA-TDNN, WavLM)ONNX Runtime
speaker_diarizer.cSpeaker diarization via cosine similarity on running centroid embeddingsAccelerate
conversation_memory.cConversation context persistence (JSONL history, token-aware truncation)CPU
backchannel.cActive listening backchannel generation ("mhm", "yeah") from acoustic cuesAccelerate
voice_onboard.cReal-time voice onboarding: speaker embedding + prosody profile extractionAccelerate

Network & API

LibraryPurposeHardware
http_api.cREST API server with TTS endpoints (PCM, WAV, mu-law, A-law encoding)CPU
websocket.cRFC 6455 WebSocket implementation (text, binary, ping/pong, close)CPU
web_remote.cWebSocket audio server: phone browser mic → pipeline → audio back to phoneCPU

Quality & Profiling

LibraryPurposeHardware
voice_quality.cPESQ-lite, STOI-lite, Log-Spectral Distance, MOS predictionAccelerate (vDSP FFT)
latency_profiler.cPer-stage nanosecond latency with P50/P95/P99 statsmach_absolute_time
apple_perf.cApple Silicon perf: RT thread scheduling, huge pages, IOSurface zero-copy, NEON softmax/GELUIOSurface + Foundation
metal_loader.cRuntime .metallib loader for custom GPU kernelsMetal (Objective-C)

Infrastructure

LibraryPurpose
vm_ring.cVM-mirrored ring buffer via mach_vm_remap for zero-copy wraparound
spmc_ring.hSingle-producer multi-consumer lock-free ring (speaker + Opus encoder)
kv_cache.hCache-oblivious interleaved KV cache [H][T][2][D] — halves L2 misses
triple_buffer.hLock-free triple buffer for GPU→CPU→CoreAudio
arena.hBump-pointer arena allocator, checkpoint/restore, zero per-turn malloc overhead
lstm_ops.hLSTM cell operations for native VAD and endpointer

Optimizations

Hardware Utilization

Sonata runs on all four Apple Silicon compute units simultaneously:

UnitWhat RunsWhy
Metal GPUSonata LM + Flow + Storm inference, custom kernels (.metallib)Massively parallel transformer ops
AMX CoprocessorProsody, FFT, LUFS, LSTM endpointer, Conformer STT (Accelerate)Matrix-vector products, vDSP
Neural EngineBNNS Graph Conformer encoder + ConvNeXt decoder (macOS 15+)Power-efficient inference
ARM NEONPCM conversion, INT8 dequantize, crossfade, ring copies, softmax/GELU8-wide SIMD on integer/float

Key Performance Techniques

  • Lock-free everywhere: SPSC and SPMC ring buffers with 128-byte cache-line aligned atomics. Zero locks in the audio hot path.
  • Zero allocation in hot loops: Arena allocator provides per-turn memory. CoreAudio callback does zero malloc/free.
  • INT8 quantization: Per-channel symmetric quantization with NEON-vectorized dequantize → cblas_sgemm. ~4x memory reduction.
  • Fused operations: vDSP pitch→EQ→volume→limit chain. Interleaved KV cache eliminates separate K/V fetches.
  • VM-mirrored ring buffers: mach_vm_remap creates a virtual memory mirror — reads/writes never need to handle wraparound.
  • Hardware resampling: Apple AudioConverter for sample rate conversion is higher quality and faster than FIR for non-integer ratios.
  • Speculative prefill: LLM API request starts at 70% EOU confidence, saving 100-300ms when the prediction is correct.
  • Conformer cache-aware streaming: Per-layer K/V projection and convolution state caching for true frame-by-frame inference without recomputation.
  • Custom Metal kernels: Flash Attention v2, fused SiLU+gate, layer norm — loaded at runtime from compiled .metallib.
  • Concurrent compute unit dispatch: GPU runs Sonata LM while ANE runs ConvNeXt decoder while AMX runs iSTFT — all three stages overlap.
  • Apple perf primitives: RT thread scheduling, huge page model loading, IOSurface zero-copy GPU↔CPU sharing.

Quality Assurance Framework

Sonata includes a comprehensive native C quality benchmark suite for proving STT and TTS quality.

Metrics Implemented

CategoryMetricGolden Signal
IntelligibilityWER (Word Error Rate)< 5% (human-level)
IntelligibilityCER (Character Error Rate)< 2%
IntelligibilitySTOI (Short-Time Objective Intelligibility)> 0.9
NaturalnessMCD (Mel-Cepstral Distortion)< 4.0 dB
NaturalnessF0 RMSE + Correlation< 15 Hz, r > 0.85
Voice QualitySegmental SNR> 25 dB
Voice QualitySpeaker Similarity> 0.90
LatencyRTF (Real-Time Factor)< 0.2x
LatencyFirst Chunk Latency< 200ms

Round-Trip Testing

The ultimate quality proof: text → TTS → audio → STT → transcript → WER(original, transcript). If the round-trip WER is low, both TTS and STT are working correctly.

make bench-quality    # Run benchmark self-tests
make test-quality     # Run all quality metric tests
make test-roundtrip   # Round-trip with mock TTS/STT callbacks

Test Suite

make test    # Run all 30 test suites
SuiteWhat It Covers
bench-qualitySelf-tests for all quality metric implementations
test-qualityWER, CER, MCD, STOI, SNR, F0, speaker similarity, latency harness, grading
test-eouMimi endpointer LSTM, fused EOU, speculative prefill, Conformer EOU flags/API
test-roundtripRound-trip framework with mock TTS/STT callbacks, NULL handling
test-pipelineText normalization, sentence buffer, SSML parser
test-new-modulesBreath synthesis, LUFS, arena, VM ring, triple buffer, SPMC ring, KV cache
test-new-enginesPhonemizer and speaker encoder API tests
test-bugfixesSPMC mirror copy, KV cache overflow, arena accounting, LUFS non-48kHz
test-conformerMel spectrogram extraction, Conformer STT API
test-llm-prosodyLLM prosody integration with SSML and text normalization
test-optimizationsINT8 round-trip, BNNS API, FP16 NEON, latency profiler, voice quality metrics
test-beam-searchCTC beam decoder with and without KenLM
test-sonataSonata iSTFT, SentencePiece tokenizer, ConvNeXt decoder, LM + Flow FFI
test-sonata-v3Sonata Flow v3 API tests
test-real-modelsPhonemizer and speaker encoder with real model files
test-prosody-predictText-based prosody prediction (syllables, emotion, adaptation)
test-prosody-logJSONL prosody logging and replay
test-emphasisLinguistics-based emphasis prediction rules
test-prosody-integrationEnd-to-end prosody pipeline (emphasis + SSML + prediction)
test-voice-onboardVoice onboarding: embedding extraction + prosody profiling
test-conversation-memoryConversation context persistence and token-aware truncation
test-diarizerSpeaker diarization with cosine similarity tracking
test-vdsp-prosodyvDSP prosody processing: pitch shift, time stretch, EQ, limiter
test-http-apiREST API server endpoints and WebSocket integration
test-sonata-stormSonata Storm parallel TTS FFI
test-audio-emotionAudio emotion detection (valence, arousal)
test-sonata-flow-ffiSonata Flow Rust FFI boundary tests
test-sonata-lm-ffiSonata LM Rust FFI boundary tests
test-pipeline-threadingPipeline threading, lock-free ring buffer concurrency
test-phase2-regressionsPhase 2 regression tests across breath, mel, sentence, conformer modules

Additional targets (not in make test):

make test-sonata-quality     # End-to-end TTS→STT quality with Sonata
make test-sonata-stt         # Sonata STT + refiner API tests
make test-native-vad         # Pure C VAD unit tests
make test-speech-detector    # Unified speech detector tests
make test-apple-perf         # Apple Silicon perf primitives
make test-quality-improvements  # Noise gate + LUFS + voice quality
make bench-vad               # VAD throughput benchmark
make bench-sonata            # Sonata TTS throughput benchmark
make bench-live              # Live model benchmark with real audio
make bench-industry          # Industry-standard quality benchmarks

Additional Features

  • INT8 quantization: Per-channel symmetric INT8 weights with NEON-vectorized dequantization. ~4x smaller models with <1% accuracy loss.
  • Apple Neural Engine: BNNS Graph-accelerated Conformer encoder and ConvNeXt decoder dispatch to the ANE on macOS 15+ for power-efficient inference.
  • Custom Metal kernels: Flash Attention v2, fused SiLU+gate, layer norm, and fp16 GEMM compiled as .metallib and loaded at runtime.
  • Per-turn latency profiling: --profiler flag enables nanosecond-precision breakdown of every pipeline stage (STT, LLM TTFT, TTS TTFS, E2E) with P50/P95/P99 statistics.
  • Voice quality metrics: PESQ-lite, STOI-lite, Log-Spectral Distance, and MOS prediction for automated TTS quality evaluation.
  • Speaker diarization: ONNX-based speaker embedding extraction with cosine similarity tracking for multi-speaker conversations.
  • Active listening: Backchannel generation ("mhm", "yeah") from acoustic cues at ~50ms latency — no LLM round-trip required.
  • Web remote: Phone browser captures mic via Web Audio API → streams PCM over WebSocket → pipeline processes and streams audio back.
  • REST API: HTTP server with WebSocket streaming for integration with external applications.
  • Multi-LLM backend: Supports Claude (SSE), Gemini, and on-device Llama 3.2 (1B-3B). Switch with --llm claude|gemini|local.
  • Multi-STT backend: Kyutai Rust (1B), pure C Conformer CTC (fp32/fp16/int8), Sonata CTC (RoPE conformer), and BNNS/ANE-accelerated Conformer. Switch with --stt-engine rust|conformer|bnns.

Requirements

  • macOS 14+ on Apple Silicon (M1/M2/M3/M4)
  • Xcode Command Line Tools (xcode-select --install)
  • Rust (curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh)
  • Homebrew dependencies: brew install curl opus onnxruntime espeak-ng
  • Anthropic API key (ANTHROPIC_API_KEY) for Claude backend (or use --llm local for on-device)

Build

make          # Build everything: 44 C shared libraries + 6 Rust cdylibs + 1 metallib + binary
make libs     # Build just the C shared libraries + metallib
make clean    # Remove all build artifacts

Build output:

  1. build/*.dylib — 44 C shared libraries
  2. build/tensor_ops.metallib — Custom Metal kernels (Flash Attention v2, fused SiLU+gate, layer norm)
  3. src/stt/target/release/libpocket_stt.dylib — Rust STT cdylib (Kyutai 1B, candle + Metal)
  4. src/llm/target/release/libpocket_llm.dylib — Rust local LLM cdylib (Llama 3.2, candle + Metal)
  5. src/local_llm/target/release/liblocal_llm.dylib — Rust local LLM alternative cdylib (candle + Metal)
  6. src/sonata_lm/target/release/libsonata_lm.dylib — Rust Sonata LM cdylib (241M semantic model)
  7. src/sonata_flow/target/release/libsonata_flow.dylib — Rust Sonata Flow cdylib (35.7M flow matching)
  8. src/sonata_storm/target/release/libsonata_storm.dylib — Rust Sonata Storm cdylib (parallel TTS)
  9. sonata — Pipeline binary linking everything

Run

ANTHROPIC_API_KEY=sk-ant-... ./sonata

Speak naturally. The pipeline detects speech onset, transcribes in real-time, sends to the LLM, and streams TTS audio back — all with full-duplex barge-in support.

Options

sonata [OPTIONS]

Voice & Model:
  --voice PATH       Voice .wav or .safetensors path
  --stt-repo REPO    STT HuggingFace repo (default: kyutai/stt-1b-en_fr-candle)
  --tts-repo REPO    TTS HuggingFace repo (default: kyutai/tts-1.6b-en_fr)
  --n-q N            Audio codebooks for TTS (default: 24)

STT Engine:
  --stt-engine E     STT engine: rust (default), conformer, or bnns
  --cstt-model PATH  Conformer STT .cstt model file path
  --bnns-model PATH  BNNS .mlmodelc path (ANE accelerated)

TTS Engine:
  --tts-engine E     TTS engine: rust (default), c (Kyutai C), or pocket (100M)

LLM Backend:
  --llm ENGINE       LLM backend: claude (default), gemini, or local
  --llm-model M      LLM model name (auto-detected per engine)
  --system PROMPT    System prompt

VAD:
  --no-vad           Disable semantic VAD (use energy VAD only)
  --vad-threshold F  Semantic VAD threshold (default: 0.7)

Audio Post-Processing:
  --pitch F          Pitch multiplier (1.0 = normal, 1.2 = higher)
  --volume F         Volume in dB (0.0 = normal, 6.0 = louder)
  --no-hw-resample   Disable AudioConverter (use FIR fallback)
  --spatial AZ       Enable 3D spatial audio at azimuth AZ degrees

Advanced:
  --metallib PATH    Custom .metallib path for GPU kernels
  --profiler         Enable per-turn latency profiling
  --prosody          Enable SSML-aware system prompt

Examples

# Default — conversational voice assistant
ANTHROPIC_API_KEY=sk-... ./sonata

# Custom voice with pitched-up output
./sonata --voice /path/to/voice.wav --pitch 1.15

# Spatial audio — voice positioned 30° to the right
./sonata --spatial 30

# On-device LLM (no API key needed)
./sonata --llm local --llm-model meta-llama/Llama-3.2-1B-Instruct

# Conformer STT with INT8 model
./sonata --stt-engine conformer --cstt-model models/parakeet_0.6b_int8.cstt

# BNNS/ANE accelerated STT (macOS 15+)
./sonata --stt-engine bnns --cstt-model models/parakeet_0.6b.cstt \
  --bnns-model models/conformer_ctc_0.6b.mlmodelc

# Latency profiling
./sonata --profiler

# Custom Metal kernels
./sonata --metallib build/tensor_ops.metallib

# Custom Claude model with system prompt
./sonata --llm-model claude-sonnet-4-20250514 \
  --system "You are a helpful coding assistant."

Project Structure

sonata/
├── Makefile              # Build system (C libs + Rust + binary + tests)
├── README.md             # This file
├── CONTRIBUTING.md       # Contributing guide
├── AGENTS.md             # AI agent guidance
├── LICENSE               # MIT
├── src/                  # 45 C source files + 6 Rust crates + Metal kernels
├── include/              # C header files
├── tests/                # 30+ test suites
├── scripts/              # Benchmarking, conversion, and export scripts
├── web/                  # Web dashboard and remote UI
├── docs/                 # API reference, architecture, troubleshooting
├── examples/             # curl, WebSocket, and integration examples
├── models/               # Downloaded/compiled models (gitignored)
└── build/                # Compiled output (gitignored)

Contributing

See CONTRIBUTING.md for development setup, code style, and pull request process.

Companion Project

pocket-tts — Python/MLX text-to-speech library with custom Metal kernels, Apple AMX/Accelerate DSP, speculative decoding, and OpenAI-compatible API. Sonata uses Rust inference crates (candle + Metal) instead of MLX, but shares the same philosophy of deep Apple Silicon optimization.

License

MIT — Copyright (c) 2026 Seth Ford

Languages

C

54.3%

Python

26.9%

Rust

13.4%

Shell

3.1%