Skip to content

About

Streaming inference framework for speech LMs using discrete DAC tokens — chunk-based processing with crossfade overlap

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

32 Commits

Folders and files

Repository files navigation

StreamSpeechLM 🎙️

Real-time speech processing with discrete codec tokens and LLM inference.

Python PyTorch License

The Problem

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.

What This Does

StreamSpeechLM wraps Llama-based speech language models (operating in the DAC discrete token space) with a streaming engine that:

  1. Buffers incoming audio into overlapping chunks
  2. Encodes each chunk to discrete tokens via DAC
  3. Runs autoregressive generation with the speech LM
  4. Decodes tokens back to audio
  5. Applies raised-cosine crossfade at chunk boundaries to eliminate artifacts

The result: real-time-capable speech enhancement/processing with bounded per-chunk latency.

Show Me

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 function

Getting Started

git clone https://github.com/seycheles/stream-speech-lm
cd stream-speech-lm
pip install -r requirements.txt

Prepare training data

# Tokenize your noisy/clean audio pairs
python scripts/prepare_data.py \
  --noisy-dir ./data/noisy \
  --clean-dir ./data/clean \
  --output ./data/tokens \
  --dac-model 44khz

Train

python scripts/train.py \
  --model meta-llama/Llama-3.2-1B \
  --data ./data/tokens \
  --output ./checkpoints/run1 \
  --epochs 3 --batch-size 4 --grad-accum 8

Inference

python scripts/enhance.py noisy.wav enhanced.wav \
  --model ./checkpoints/run1 \
  --device cuda \
  --chunk-size 4.0

Architecture

Audio 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.


Configuration

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

Real-Time Factor

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.


Roadmap

  • 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

License

Apache 2.0. See LICENSE.

About

Streaming inference framework for speech LMs using discrete DAC tokens — chunk-based processing with crossfade overlap

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages