Built-in macOS speech synthesis sounds noticeably robotic the moment you listen to it for more than thirty seconds, and cloud voice APIs turn every quick terminal status update into an external network round trip with latency and billing attached. On Apple Silicon Macs with unified memory, open-weights neural voices like Qwen3-TTS running on mlx-audio are fast enough to feel interactive—if the desktop host around them behaves like a real Mac app instead of a fragile Python tutorial.
In Vitruvian Speaker, we added Qwen3-TTS alongside Apple’s system voices and Gemini TTS, including zero-shot voice cloning from local audio samples. Shipping it reliably inside one native Swift menu-bar app forced us to solve four concrete engineering problems: zero open TCP ports, deterministic offline-safe installation, adaptive audio buffering under heavy CPU load, and single-instance runtime supervision.
Figure 1: Running local neural text-to-speech inside a Mac app works best when the Python MLX runtime acts like an invisible, single-instance appliance behind native Swift audio playback.
1. Zero Open TCP Ports Over a Private Unix Socket
Most local AI runners tell you to launch uvicorn on 127.0.0.1:8000. On a developer machine, localhost ports collide quickly, and any unrelated browser tab or local process on the Mac can hit an unauthenticated loopback port.
Instead, Vitruvian Speaker runs its speech server exclusively over a Unix domain socket inside a user-only 0700 directory (<data>/ai-voice-runtime/sock/server.sock), with a small Swift client (AIVoiceTransport) speaking raw HTTP/1.1 directly over AF_UNIX.
There is one subtle macOS trap here: the kernel sockaddr_un.sun_path buffer on macOS is 104 bytes long (103 usable characters plus the null terminator). Under ~/Library/Application Support/.../ai-voice-runtime/sock/server.sock, real user paths easily exceed 104 bytes and fail bind() or connect() silently or with ENAMETOOLONG. Our launcher avoids this by resolving through a short private symlink inside a 0700 directory right when binding and connecting—keeping permissions private to your user while never binding a TCP socket at all.
Figure 2:
AIVoiceTransport speaks HTTP/1.1 over a Unix socket inside a 0700 directory with zero open TCP ports, isolating mlx-audio and Qwen3-TTS inference behind SHA-256-verified dependencies and AVAudioEngine playback.
Inside that private runtime folder, every coordinate is explicit and inspectable on disk:
- Locked Python wheels (
requirements.lock+installed.sha256): Bootstrapped with a SHA-256-verifieduvbinary and--only-binary :all:, so no compiler toolchain or arbitrarysetup.pyexecutes on a user’s machine during setup. - Commit-pinned weights:
Qwen/Qwen3-TTS-12Hz-1.7B-Base(around3.5 GB, roughly8 GBtotal with dependencies) is fetched at a pinned Hugging Face revision rather than floating onmain. - Single-purpose endpoints:
/v1/audio/speechstreams raw24 kHz16-bit mono PCM chunks straight intoEnginePCMPlayer(AVAudioEngine), avoiding intermediate WAV temp files or re-encoding overhead.
2. Why Real Compiles Broke Naive Streaming Audio
At rest, Qwen3-TTS on Apple Silicon streams PCM faster than real time. The bug appeared during realistic desktop usage—specifically while Xcode, Bazel, or Rust builds saturated CPU and GPU memory bandwidth.
We measured an M4 Max under heavy compilation where Qwen3-TTS inference slowed to 0.54× real-time: generating 9.8s of speech took 18.3s and hit 29 buffer underrun gaps. On a long 120s summary, playback stuttered through 73 gaps totaling 89s of silence as every tiny incoming chunk played immediately and starved milliseconds later.
The fix in AIVoicePacer (AIVoiceSpeaker.swift) treats local TTS like adaptive video streaming instead of a naive pipe:
Figure 3: Under
0.54× real-time synthesis during heavy builds, playing chunks immediately caused 29–73 stutter gaps; AIVoicePacer starts cleanly after a 7,200-byte (0.15s) cushion on fast runs and estimates needed headroom when inference slows down.
Here is how AIVoicePacer keeps playback natural without dragging down interactive speed:
- Fast start when uncontended: Playback waits only for
7,200bytes (0.15sat24 kHz16-bit mono), keeping initial latency crisp when your machine is idle. - Adaptive catch-up under load: As chunks arrive, the pacer measures live synthesis rate against playback speed. When synthesis falls below real time, it holds the audio start (capped at
30smax first-word wait) so enough audio accumulates upfront to finish cleanly—allowing at most one cushioned pause on very long texts. - Never switch voices mid-sentence: If a user interrupts playback, presses stop, or triggers a second announcement while the first is playing,
AIVoiceSpeakercancels cleanly with.interruptedinstead of falling back halfway through an announcement into Apple’s system synthesizer.
3. Coordinating One 3.5 GB Model Across Concurrent Tools
A desktop Mac typically has more than one consumer hitting the speaker at the same time: the menu-bar UI, a CLI tool (speaker-broadcast), and multiple background AI agent hooks. Spawning two Python processes would immediately consume ~7 GB of unified memory and thrash GPU command queues.
Instead, every caller goes through the single-instance supervisor (AIVoiceRuntime):
Figure 4: File-locked supervisor state coordinates model switches across processes, favors whichever process is already loaded using
sinceExact timestamps, enforces 1m to 30m exponential backoff on failures, and shuts down after 20 minutes of idle time.
Three details make the supervisor feel invisible in daily use:
- Honest clock exclusion for idle sleep: The
20-minute idle timer resets based onlast-usedactivity excluding initial wheel installation and Hugging Face weight download time, so a slow internet download does not kill the server before its first utterance. - Fractional-second
sinceExactclaim tie-breaker: Switching between a stock speaker (aiden,ryan,serena) and a custom cloned voice written underai-voices/writeswanted-modelwith high-precision timestamps, resolving races between concurrent requests in favor of keeping an already-warm model alive when ties happen. - Controlled crash recovery: Transient server failures automatically retry up to 3 times using
1m→30mexponential backoff stored inretry-after.json, while intentional user stops (supervisor.lock+supervisor.pid) stay stopped and never respawn in the background.
Key Takeaways for Shipping Local AI on macOS
- Hide Python behind Unix domain sockets and a native supervisor: Users should never manage virtualenvs, open localhost ports, or kill orphan
python3processes manually. - Benchmark inference during dirty workloads, not empty desktops: A voice model that screams at
2.0×real-time on an idle M4 Max can drop to0.5×during a parallel Bazel or Xcode compile; adaptive buffering turns 70 stutters into smooth audio. - Pin every byte: SHA-256-locked binary wheels plus revision-pinned weights eliminate “worked on my machine last week” breakage on Apple Silicon Macs.
You can browse the full Swift + Python implementation in apps/desktop/vitruvian-speaker or inspect the local speech guide.