JamMate
Local real-time AI jam band for electric and bass guitar on Apple Silicon, pairing a lock-free 48 kHz PortAudio callback and C++20 DSP kernels with hybrid waveguide and Magenta RT2 (MLX) accompaniment.

Overview
JamMate is a local, offline desktop application for macOS on Apple Silicon (arm64) that listens to live electric guitar or bass input over a USB audio interface and generates real-time drum and complementary bass or rhythm-guitar accompaniment without preconfigured BPM, key, or backing tracks.
Problem
Interactive musical accompaniment combines two conflicting systems constraints:
- At
48 kHzwith a128-frame buffer, the CoreAudio/PortAudio callback has a hard2.67 msdeadline. Any Python heap allocation, mutex contention, or GIL stall inside the audio thread causes audible dropouts. - Neural audio codec models such as Google Magenta RealTime 2 (
MagentaRT2, running at25 Hz/40 msframes) lack explicit beat-phase or BPM conditioning tensors, while time-domain pitch trackers (YIN) misclassify root-fifth guitar power chords (f0 + 1.5 * f0) at0.5 * f0—an octave below the true root.
Approach and architecture
JamMate isolates real-time audio transport from off-thread DSP analysis and generative synthesis using lock-free single-producer/single-consumer (SPSC) ring buffers:
- Zero-allocation callback and C++20 DSP kernels:
AudioEngine._callbackoperates exclusively in place on preallocatedfloat32buffers, verified in CI by an AST call-graph audit (audit_realtime_safety.py). Inner DSP loops (_jammate_native.cpp, compiled withclang++ -std=c++20 -O3and invoked viactypes) release the Python GIL during YIN cumulative mean normalized difference, biquad filtering, 8-voice drum interpolation, and soft-clipping. - Power-chord disambiguation and bounded-PLL beat clock: An off-thread
AnalysisWorkerprocesses10 mshops to extract onsets, tempo, key, and chord roots, inspecting harmonic peak ratios (f,2f,3f,5f) to correct0.5 * f0power-chord subharmonics.BeatClocktracks player groove with bounded<= 8 msper-onset phase nudges. - Hybrid waveguide + MLX accompaniment:
HybridAccompanimentEnginecombines a beat-locked Karplus-Strong plucked-string waveguide and 96-tap cabinet FIR withMagentaRT2streaming on Apple Silicon GPU (mlx), using MusicCoCa audio-space style projection, masked pianoroll classifier-free guidance, latency-compensated onset pulsing, and a4.0 msraised-cosine packet-loss concealment crossfader.
Engineering decisions and trade-offs
- Hybrid synthesis over pure neural generation: Relying solely on an un-clocked
25 Hzcodec model yielded weaker beat lock (r = 0.313in local evaluation) and occasional high-frequency drum bleed. Layering a deterministic physical-modeling attack with MLX neural texture improved beat lock (r = 0.591) while providing an instant zero-download fallback when model weights are absent. - C++20
ctypesboundary vs. full C++ rewrite: Keeping session orchestration,BandDirectorarrangement logic, and MLX inference in Python while moving sample-level loops to a compact C++20 shared library preserved rapid iteration without violating the2.67 msaudio budget.
Evaluation and benchmarks
Measured on Apple M1 Pro (arm64, Python 3.12) via scripts/benchmark_system.py alongside 624 hardware-free deterministic unit and integration tests:
- PortAudio callback (
128frames @48 kHz, 7-channel stem tap active):67.3–73.2 µsmean /183.8 µsp99 (2.7%of the2,666.7 µsbudget). - Streaming DSP analysis hop (
10 msbudget):303.6 µsmean (31xfaster than real time). - Accompaniment synthesis (
40 msframe budget):0.44 ms(0.011xRTF) for the deterministic waveguide engine and23.14 ms(0.58xRTF) for the hybrid MLX backend with0underruns.
Limitations
JamMate is a desktop prototype restricted to macOS on Apple Silicon (M1–M4) when using MLX GPU acceleration, requires ~1.15 GB of local Magenta RT2 weights for neural modes, and tracks monophonic lines plus standard root-fifth/triad guitar voicings rather than dense extended jazz voicings.