Real-time speech processing with discrete codec tokens and LLM inference.
Most speech LM inference is batch-only. You hand the model an audio file, wait for it to process the whole thing, and get output back. Fine for offline pipelines, but useless for anything real-time.
The obvious fix — just process short chunks — runs into a nasty artifact: audible clicks and discontinuities at every chunk boundary, because the model has no context about what came before.
StreamSpeechLM wraps Llama-based speech language models (operating in the DAC discrete token space) with a streaming engine that:
- Buffers incoming audio into overlapping chunks
- Encodes each chunk to discrete tokens via DAC
- Runs autoregressive generation with the speech LM
- Decodes tokens back to audio
- Applies raised-cosine crossfade at chunk boundaries to eliminate artifacts
The result: real-time-capable speech enhancement/processing with bounded per-chunk latency.
from stream_speech_lm import StreamingPipeline
pipeline = StreamingPipeline(
model_id="./checkpoints/se-llama-250m",
device="cuda",
chunk_size_sec=4.0,
overlap_sec=0.05,
)
# Process a file
pipeline.process_file("noisy_speech.wav", "enhanced.wav")
# → Processed 12.3s audio (RTF=0.72x)
# Or stream from any generator
import numpy as np
def mic_stream():
# simulate mic input
for _ in range(10):
yield np.random.randn(44100 * 2).astype(np.float32)
for chunk in pipeline.stream(mic_stream()):
play(chunk) # your audio output functiongit clone https://github.com/seycheles/stream-speech-lm
cd stream-speech-lm
pip install -r requirements.txt# Tokenize your noisy/clean audio pairs
python scripts/prepare_data.py \
--noisy-dir ./data/noisy \
--clean-dir ./data/clean \
--output ./data/tokens \
--dac-model 44khzpython scripts/train.py \
--model meta-llama/Llama-3.2-1B \
--data ./data/tokens \
--output ./checkpoints/run1 \
--epochs 3 --batch-size 4 --grad-accum 8python scripts/enhance.py noisy.wav enhanced.wav \
--model ./checkpoints/run1 \
--device cuda \
--chunk-size 4.0Audio Input
│
▼
┌─────────────┐ chunk_size=4s, overlap=50ms
│ AudioBuffer │ ──────────────────────────────────────┐
└─────────────┘ │
┌──────▼──────┐
│ DACTokenizer│
│ (encode) │
└──────┬──────┘
│ discrete codes
┌──────▼──────┐
│ LlamaSpeech │
│ LM │
└──────┬──────┘
│ generated codes
┌──────▼──────┐
│ DACTokenizer│
│ (decode) │
└──────┬──────┘
│
┌──────▼──────┐
│ Crossfade │
│ overlap-add │
└──────┬──────┘
│
Audio Output
Constrained decoding: The ConstrainedCodebookLogitsProcessor enforces that at generation step i, only tokens from codebook (i % n_codebooks) are valid. This prevents the model from producing malformed multi-codebook sequences that would crash the DAC decoder.
| Parameter | Default | Description |
|---|---|---|
chunk_size_sec |
4.0 |
Duration of each processed chunk |
overlap_sec |
0.05 |
Crossfade overlap (50ms) |
max_new_tokens |
512 |
Max tokens per chunk generation |
constrained_decoding |
True |
Enforce codebook token constraints |
dac_model_tag |
"44khz" |
Which DAC model to use |
Measured on a single RTX 3090 with the 250M Llama model:
- Chunk duration: 4.0s
- Processing time: ~2.8s
- RTF: ~0.70x (faster than real-time)
The 1B model runs at ~1.1x RTF on the same hardware (slightly above real-time).
Reducing chunk_size_sec to 2.0s increases RTF but reduces context for the LM.
- DAC tokenizer with multi-codebook flatten/unflatten
- AudioBuffer with overlap and crossfade
- LlamaSpeechLM with constrained decoding
- StreamingPipeline end-to-end
- Training script (HF Trainer)
- Multi-GPU distributed training (DDP)
- Quantization (4-bit, 8-bit via bitsandbytes)
- Live microphone demo (Gradio) — working on this next
- Streaming over WebSocket for server deployment
Apache 2.0. See LICENSE.