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.

JamMate desktop session showing live E2 power-chord detection at 120 BPM, band mixer controls, and 48 kHz real-time callback diagnostics
JamMate desktop session tracking live electric guitar input, beat phase, and 48 kHz / 128-frame callback telemetry.

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 kHz with a 128-frame buffer, the CoreAudio/PortAudio callback has a hard 2.67 ms deadline. 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 at 25 Hz / 40 ms frames) lack explicit beat-phase or BPM conditioning tensors, while time-domain pitch trackers (YIN) misclassify root-fifth guitar power chords (f0 + 1.5 * f0) at 0.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._callback operates exclusively in place on preallocated float32 buffers, verified in CI by an AST call-graph audit (audit_realtime_safety.py). Inner DSP loops (_jammate_native.cpp, compiled with clang++ -std=c++20 -O3 and invoked via ctypes) 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 AnalysisWorker processes 10 ms hops to extract onsets, tempo, key, and chord roots, inspecting harmonic peak ratios (f, 2f, 3f, 5f) to correct 0.5 * f0 power-chord subharmonics. BeatClock tracks player groove with bounded <= 8 ms per-onset phase nudges.
  • Hybrid waveguide + MLX accompaniment: HybridAccompanimentEngine combines a beat-locked Karplus-Strong plucked-string waveguide and 96-tap cabinet FIR with MagentaRT2 streaming on Apple Silicon GPU (mlx), using MusicCoCa audio-space style projection, masked pianoroll classifier-free guidance, latency-compensated onset pulsing, and a 4.0 ms raised-cosine packet-loss concealment crossfader.

Engineering decisions and trade-offs

  • Hybrid synthesis over pure neural generation: Relying solely on an un-clocked 25 Hz codec model yielded weaker beat lock (r = 0.313 in 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 ctypes boundary vs. full C++ rewrite: Keeping session orchestration, BandDirector arrangement logic, and MLX inference in Python while moving sample-level loops to a compact C++20 shared library preserved rapid iteration without violating the 2.67 ms audio 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 (128 frames @ 48 kHz, 7-channel stem tap active): 67.3–73.2 µs mean / 183.8 µs p99 (2.7% of the 2,666.7 µs budget).
  • Streaming DSP analysis hop (10 ms budget): 303.6 µs mean (31x faster than real time).
  • Accompaniment synthesis (40 ms frame budget): 0.44 ms (0.011x RTF) for the deterministic waveguide engine and 23.14 ms (0.58x RTF) for the hybrid MLX backend with 0 underruns.

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.