Speech-to-Speech and Expressive Voice › Discrete audio representations for language modeling
Language AI HandbookAll chapters Discrete codes give an audio language model a finite vocabulary. A transformer can also consume continuous audio features. The choice here is about prediction targets and the interfaces between stages.
Acoustic tokens from a neural audio codec. A codec can learn to compress speech into discrete codes and decode them back to audio. EnCodec is a streaming encoder–decoder with quantized latents. Frame rates and the number of codebooks depend on the codec and configuration.
Speech features with learned targets. HuBERT uses offline clustering to obtain targets for its masked prediction objective. In a separate use, a system can cluster learned hidden states to build a discrete input representation. The resulting codes are acoustic representations and can retain information beyond the words.
A text backbone plus acoustic tokens. The practical hybrid: transcribe to text for quality and safety, then predict acoustic tokens for the audio output, sometimes conditioned on a text representation and a speaker embedding. This lets the LM see clean text as the "meaning" and acoustic tokens as the "sound," with a learned mapping between them.
If a configured codec emits 50 frames per second, ten seconds produces 500 frame positions. Several quantizer codes can belong to each frame. Model compute depends on whether it predicts those codes in parallel or as separate autoregressive steps.
We’ll quantize a feature from a synthetic chirp plus noise. The example encodes log power by frequency band and decodes those feature values. A neural codec can also learn a decoder for audio.
PythonIn [3]Expand
import numpy as np
rng = np.random.default_rng(7)
SR = 16000
duration_s = 1.0
t = np.arange(int(SR * duration_s)) / SR
# A chirp that sweeps from 300 Hz to 1200 Hz over the sample
f0, f1 = 300.0, 1200.0
phase = 2 * np.pi * (f0 * t + (f1 - f0) / (2 * duration_s) * t**2)
chirp = 0.6 * np.sin(phase)
noise = 0.05 * rng.standard_normal(t.shape)
waveform = chirp + noiseChoose a window length and a hop. At 16 kHz, the 320-sample window spans 20 ms and the 160-sample hop advances 10 ms. That makes 100 frame positions per second in a continuing stream, each holding eight scalar codes in this example.
PythonIn [4]Expand
frame_len = 320 # 20 ms at 16 kHz
hop = 160 # 10 ms
n_frames = 1 + (len(waveform) - frame_len) // hop
# Frame the waveform
frames = np.stack(
[waveform[i * hop : i * hop + frame_len] for i in range(n_frames)]
)
# The 320-sample rFFT gives 161 nonnegative bins spaced 50 Hz apart.
# Split them by index into one 21-bin group and seven 20-bin groups.
n_bands = 8
spectrum = np.fft.rfft(frames * np.hanning(frame_len), axis=1)
bands = np.array_split(np.arange(spectrum.shape[1]), n_bands)
energy = np.stack(
[
np.log(np.mean(np.abs(spectrum[:, bins]) ** 2, axis=1) + 1e-8)
for bins in bands
],
axis=1,
)
# Each band uses a separate 16-level scalar quantizer.
qmin, qmax = energy.min(axis=0), energy.max(axis=0)
scale = (qmax - qmin) / 15
safe_scale = np.where(scale > 0, scale, 1.0)
tokens = np.clip(np.rint((energy - qmin) / safe_scale), 0, 15).astype(int)ConsoleOut [5]Expand
n_frames: 99 codebook size: 16 token stream shape: (99, 8) first 3 frames, all bands: [[15, 1, 6, 6, 6, 12, 6, 8], [15, 1, 12, 7, 11, 7, 4, 4], [15, 0, 8, 10, 6, 9, 7, 12]]
A complete frame starts every 10 ms, giving 99 frames for this signal and framing rule. Each frame carries one level from 0 through 15 per band. The scale and minimum are needed to interpret the tokens. This fixture estimates them from the signal, so it does not demonstrate compression on unseen data.
Decode the integer level back to log power in each band. A nearest-level decoder has at most half a quantization step of error for values within the fitted range. The stored mean power cannot recover the frequencies or phases within a band.
PythonIn [6]Expand
energy_recon = qmin + tokens * scale
frame_times = (np.arange(n_frames) * hop + frame_len / 2) / SR
band = 0VisualizationOut [7]Expand

The decoded figure follows the coarse change in the lowest band's energy. It compares features instead of reconstructing a waveform. An audio codec has a waveform decoder and must be tested separately for intelligibility, sound quality and other deployment targets.
At this point we have the machinery to discuss timing.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!