All projects

speech/ml

Voice agent from scratch

A real-time voice agent where I built every layer myself in PyTorch: features, voice activity detection, speech recognition, text-to-speech, denoising and speaker ID.

Open source learning repo. Runs on a Mac (MPS) or CPU. Diarization is not written yet.

Voice agent from scratch: overview card
Word error rate after swapping the BiGRU+CTC baseline for a Conformer, on the same 4.1 h of training audio
1.076 → 0.607Word error rate after swapping the BiGRU+CTC baseline for a Conformer, on the same 4.1 h of training audio
Faster inference from the same architecture change
2.7×Faster inference from the same architecture change
Relative cost in word error of streaming with 640 ms chunks
3%Relative cost in word error of streaming with 640 ms chunks
Speaker verification on 8 speakers held out of training (95% CI 6.13–7.44)
6.80% EERSpeaker verification on 8 speakers held out of training (95% CI 6.13–7.44)

What it is

Most voice agents are a chain of API calls. That works, but you never learn why a recognizer hallucinates over silence or why synthesised speech sounds metallic. I wanted to understand every stage, so I built each one from first principles and kept a runnable script at every step.

The repo is a curriculum of ten modules that connect into one loop: mic, denoise, VAD, endpointing, ASR, a reply, TTS, speaker. Every stage is a swappable engine, so I can run my own models against what production systems use on the same audio. Whisper base.en still wins by a wide margin, trained on about 165,000× more data, and that gap is the point the project makes.

How it works

  1. 1

    Features and denoising

    Audio becomes a log-mel spectrogram that I compute by hand. A spectral denoiser built from MCRA noise tracking and log-MMSE/OM-LSA cleans the signal for the VAD only, measured at +5.3 dB SNR with 16 ms latency.

  2. 2

    Voice activity detection

    A tiny 1-D convolutional frame classifier, about 55,000 parameters, decides speech or silence per frame. Hysteresis smoothing stops it flickering.

  3. 3

    Endpointing

    A state machine with no model decides when a turn has ended. If the utterance sounds finished it commits after 300 ms; if it trails off on "and", "so" or "um" it waits 950 ms, and a 320 ms redemption window stops a breath ending the turn.

  4. 4

    Recognition

    The modern recognizer is a Conformer with BPE subwords, relative-position attention and a hybrid CTC/attention head on one shared encoder. Chunked attention masks and a causal convolution let one set of weights stream at different latencies.

  5. 5

    Reply and speech

    The reply slot echoes by default, or calls OpenAI or Claude with per-session history. A Tacotron-style attention model turns text into mels, and Griffin-Lim or my Vocos-style ISTFT vocoder turns mels into audio, one sentence at a time so barge-in wastes at most one sentence.

  6. 6

    Server and swaps

    A WebSocket server streams mic audio from the browser and reports p50/p95 latency per stage. Each stage can swap to a production engine: Silero VAD v5, faster-whisper, Kokoro-82M, or the GTCRN and DTLN denoisers.

Decisions and trade-offs

A failure the loss couldn't see

My Conformer learned that emitting blank on every frame was a safe answer with a finite loss: 100% of frames decoded to blank and greedy decoding returned an empty string. I nearly spent three days shrinking the subword vocabulary from 1024 to 256. An hour reading reference recipes showed ESPnet uses 5,000 subwords on the same corpus, so the vocabulary was never the problem.

The VAD was a data problem

My first VAD reported P(speech) = 1.0 on room noise with nobody talking. Same architecture, same training loop, corrected data, and the same input scores 0.04. A hard-case evaluation set written before training then chose the checkpoint: 0.806 at step 2750, against 0.699 at the final step.

Two ideas that measured worse

Restricting TTS attention to a window at decode time took half an hour to write and doubled the error, because the model was trained on a full softmax. Running the decoder on Apple's MPS backend was 13× slower than CPU, since tiny sequential ops are dominated by dispatch overhead.

Reporting the result that didn't resolve

Both trained speaker encoders beat the untrained 11.10% floor, but AAM-softmax against plain softmax did not separate: the confidence intervals overlap and the medians are identical. With only 32 training speakers the experiment can't tell, and the README says so rather than picking the flattering number.