Skip to content

Commit ae35a74

Browse files
hans00claude
andcommitted
refactor(audio-lm): windowed stateless streaming replaces the stateful decode
Hans's original design (da06d97), finished: the streamer decodes [committed - left, avail) instead of [0, avail), holds back the codec's right edge, and cross-fades the joins (the next window decodes the last 20 ms before the commit point again). Codecs with a measured window (src/audio_lm_window.h, internal): Qwen3-TTS-Tokenizer-12Hz and Mimi 1.28 s / 0, DAC 0.32 / 0.32 s, WavTokenizer 1.28 / 0.32 s. Other codecs still decode from the first frame (exact, quadratic). The stateful incremental decode (src/codec_stream.h, vtable entries, the Qwen3 stream graph, the cached ICL reference state) is removed from this PR. Public header: the codec_audio_lm_stream_poll comment in include/codec_audio_lm.h describes the window (comment only). tests/e2e/audio_lm_stream_smoke.py: stream vs batch for Qwen3 (ICL), CSM (Mimi) and OuteTTS (DAC): same length, log-mel distance and seam limits. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f6f51a4 commit ae35a74

15 files changed

Lines changed: 209 additions & 690 deletions

‎common/audio_lm.cpp‎

Lines changed: 18 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include "codec_common.h"
2-
#include "codec_stream.h"
3-
#include "audio_lm_stream.h"
2+
#include "audio_lm_window.h"
43

54
#include <algorithm>
65
#include <cstring>
@@ -48,10 +47,6 @@ struct audio_lm_context {
4847
// the caller (owned here). NULL = model default.
4948
const codec_speaker * decode_speaker = nullptr;
5049

51-
// Incremental-decode state after the last stream prefix (an ICL reference), reused while the prefix repeats.
52-
codec_decode_stream * prefix_stream = nullptr;
53-
std::vector<int32_t> prefix_codes;
54-
5550
// Cached capabilities (read once at init).
5651
uint32_t modality_mask = 0;
5752
int32_t n_cb = 0;
@@ -427,7 +422,6 @@ static bool audio_lm_attach(audio_lm_context * ctx, std::string * err) {
427422

428423
void audio_lm_free(audio_lm_context * ctx) {
429424
if (ctx == nullptr) return;
430-
codec_decode_stream_free(ctx->prefix_stream);
431425
if (ctx->state) codec_lm_state_free(ctx->state);
432426
if (ctx->lm) codec_lm_free(ctx->lm);
433427
if (ctx->codec_ctx) codec_free(ctx->codec_ctx);
@@ -1779,6 +1773,23 @@ static int32_t audio_lm_max_delay(const audio_lm_context * ctx) {
17791773
return max_delay;
17801774
}
17811775

1776+
// Measured per codec (docs/codec_common_api.md, "Audio"): left context and right hold-back, in seconds, at which a
1777+
// stateless window decode matches one whole decode by ASR read-back, with no audible seams.
1778+
bool audio_lm_stream_window(const audio_lm_context * ctx, double * left_s, double * right_s) {
1779+
if (ctx == nullptr || ctx->model == nullptr) return false;
1780+
switch (codec_model_arch(ctx->model)) {
1781+
case CODEC_ARCH_QWEN3_TTS_TOKENIZER: // strictly causal: nothing to hold back
1782+
case CODEC_ARCH_MIMI:
1783+
*left_s = 1.28; *right_s = 0.0; return true;
1784+
case CODEC_ARCH_DAC: // convolutional: a short context on both sides
1785+
*left_s = 0.32; *right_s = 0.32; return true;
1786+
case CODEC_ARCH_WAVTOKENIZER_LARGE:
1787+
*left_s = 1.28; *right_s = 0.32; return true;
1788+
default:
1789+
return false;
1790+
}
1791+
}
1792+
17821793
int32_t audio_lm_codec_hop(const audio_lm_context * ctx) {
17831794
return ctx != nullptr && ctx->model != nullptr ? codec_model_hop_size(ctx->model) : 0;
17841795
}
@@ -2007,119 +2018,6 @@ bool audio_lm_decode_audio_range(audio_lm_context * ctx, int32_t frame_begin, in
20072018
return audio_lm_decode_codec_codes(ctx, codes.data(), (int32_t) (codes.size() / (size_t) n_q), n_q, out);
20082019
}
20092020

2010-
// ─── Incremental decode (internal: src/audio_lm_stream.h) ───────────
2011-
2012-
static bool audio_lm_stream_push_codec(audio_lm_context * ctx, codec_decode_stream * st, const int32_t * codes,
2013-
int32_t n_frames, int32_t n_q, audio_lm_audio_output * out) {
2014-
codec_token_buffer tokens = {};
2015-
tokens.data = const_cast<int32_t *>(codes);
2016-
tokens.n_frames = n_frames;
2017-
tokens.n_q = n_q;
2018-
tokens.n_tokens = n_frames * n_q;
2019-
tokens.codebook_size = codec_model_codebook_size(ctx->model);
2020-
tokens.sample_rate = codec_model_sample_rate(ctx->model);
2021-
tokens.hop_size = codec_model_hop_size(ctx->model);
2022-
codec_pcm_buffer pcm = {};
2023-
if (codec_decode_stream_push(st, &tokens, &pcm) != CODEC_STATUS_SUCCESS) {
2024-
const char * raw = codec_get_last_error(ctx->codec_ctx);
2025-
ctx->last_error = std::string("incremental decode failed (") + (raw ? raw : "?") + ")";
2026-
return false;
2027-
}
2028-
if (out != nullptr) audio_lm_take_pcm(&pcm, out); else codec_pcm_buffer_free(&pcm);
2029-
return true;
2030-
}
2031-
2032-
static bool audio_lm_stream_capable(const audio_lm_context * ctx) {
2033-
return ctx != nullptr && ctx->codec_ctx != nullptr && codec_model_has_decode_stream(ctx->model) &&
2034-
(ctx->modality_mask & OUTPUT_AUDIO) && ctx->audio_cb_offset == 0 && ctx->delay_pattern.empty() &&
2035-
ctx->cb0_speech_offset == 0 && !ctx->is_continuous;
2036-
}
2037-
2038-
// A new stream, from the cached state after `prefix` (codec codes) when there is one.
2039-
static codec_decode_stream * audio_lm_stream_after(audio_lm_context * ctx, const int32_t * prefix, int32_t n_prefix,
2040-
int32_t width) {
2041-
if (!audio_lm_stream_capable(ctx) || width <= 0) return nullptr;
2042-
if (prefix == nullptr || n_prefix <= 0) return codec_decode_stream_new(ctx->codec_ctx, width);
2043-
std::vector<int32_t> key(prefix, prefix + (size_t) n_prefix * width);
2044-
if (ctx->prefix_stream == nullptr || key != ctx->prefix_codes) {
2045-
codec_decode_stream_free(ctx->prefix_stream);
2046-
ctx->prefix_stream = nullptr;
2047-
ctx->prefix_codes.clear();
2048-
codec_decode_stream * st = codec_decode_stream_new(ctx->codec_ctx, width);
2049-
if (st == nullptr || !audio_lm_stream_push_codec(ctx, st, key.data(), n_prefix, width, nullptr)) {
2050-
codec_decode_stream_free(st);
2051-
return nullptr;
2052-
}
2053-
ctx->prefix_stream = st;
2054-
ctx->prefix_codes = std::move(key);
2055-
}
2056-
return codec_decode_stream_clone(ctx->prefix_stream);
2057-
}
2058-
2059-
// The current voice's in-context reference (Qwen3-TTS ICL: a talker with a text projection, a voice with ref_text
2060-
// and ref_codes), as audio_lm_family_codebook decodes it ahead of the generation.
2061-
static bool audio_lm_icl_prefix(const audio_lm_context * ctx, std::vector<int32_t> * ref) {
2062-
ref->clear();
2063-
const codec_speaker * spk = audio_lm_decode_speaker(ctx);
2064-
if (spk == nullptr || !audio_lm_talker_has_projection(ctx)) return false;
2065-
codec_speaker_slot_type ty;
2066-
int64_t ne[4];
2067-
const void * d = nullptr;
2068-
const char * text = codec_speaker_ref_text(spk);
2069-
if (text == nullptr || !*text || !codec_speaker_get_slot(spk, "ref_codes", &ty, ne, &d) ||
2070-
ty != CODEC_SPEAKER_SLOT_I32 || ne[0] != ctx->n_cb) {
2071-
return false;
2072-
}
2073-
ref->assign(static_cast<const int32_t *>(d), static_cast<const int32_t *>(d) + ne[0] * ne[1]);
2074-
return true;
2075-
}
2076-
2077-
struct audio_lm_stream {
2078-
codec_decode_stream * st = nullptr;
2079-
int32_t next = 0; // first accumulated frame not decoded yet
2080-
};
2081-
2082-
audio_lm_stream * audio_lm_stream_begin(audio_lm_context * ctx) {
2083-
if (!audio_lm_stream_capable(ctx)) return nullptr;
2084-
std::vector<int32_t> ref;
2085-
audio_lm_icl_prefix(ctx, &ref);
2086-
const int32_t width = ctx->n_cb;
2087-
codec_decode_stream * st = audio_lm_stream_after(ctx, ref.empty() ? nullptr : ref.data(), (int32_t) ref.size() / width, width);
2088-
if (st == nullptr) return nullptr;
2089-
auto * s = new (std::nothrow) audio_lm_stream();
2090-
if (s == nullptr) { codec_decode_stream_free(st); return nullptr; }
2091-
s->st = st;
2092-
return s;
2093-
}
2094-
2095-
bool audio_lm_stream_next(audio_lm_context * ctx, audio_lm_stream * s, int32_t frame_end, audio_lm_audio_output * out) {
2096-
if (ctx == nullptr || s == nullptr || out == nullptr || frame_end <= s->next || ctx->codes_n_frames <= 0) return false;
2097-
std::vector<int32_t> cc;
2098-
int32_t n_q = 0;
2099-
if (!audio_lm_codec_codes(ctx, ctx->codes.data(), ctx->codes_n_frames, ctx->n_cb, s->next, frame_end, &cc, &n_q) ||
2100-
!audio_lm_stream_push_codec(ctx, s->st, cc.data(), (int32_t) (cc.size() / (size_t) n_q), n_q, out)) {
2101-
return false;
2102-
}
2103-
s->next = frame_end;
2104-
return true;
2105-
}
2106-
2107-
void audio_lm_stream_end(audio_lm_stream * s) {
2108-
if (s == nullptr) return;
2109-
codec_decode_stream_free(s->st);
2110-
delete s;
2111-
}
2112-
2113-
bool audio_lm_decode_after_prefix(audio_lm_context * ctx, const int32_t * prefix, int32_t n_prefix, const int32_t * codes,
2114-
int32_t n_frames, int32_t width, audio_lm_audio_output * out) {
2115-
if (codes == nullptr || n_frames <= 0 || out == nullptr) return false;
2116-
codec_decode_stream * st = audio_lm_stream_after(ctx, prefix, n_prefix, width);
2117-
if (st == nullptr) return false;
2118-
const bool ok = audio_lm_stream_push_codec(ctx, st, codes, n_frames, width, out);
2119-
codec_decode_stream_free(st);
2120-
return ok;
2121-
}
2122-
21232021
// ─── Decoder speaker ────────────────────────────────────────────────
21242022

21252023
bool audio_lm_codec_can_speaker_from_audio(const audio_lm_context * ctx) {

‎common/audio_lm_c.cpp‎

Lines changed: 38 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include "audio_lm_family.h"
55
#include "codec_audio_lm.h"
66
#include "codec_common.h"
7-
#include "audio_lm_stream.h"
7+
#include "audio_lm_window.h"
88

99
#include <algorithm>
1010
#include <cmath>
@@ -20,36 +20,26 @@ using namespace codec_common;
2020

2121
namespace {
2222

23-
// Frame-level streaming over the current generation.
24-
// * Strictly causal codecs (src/audio_lm_stream.h): an incremental decoder
25-
// carries the decoder's state, so each poll decodes only the new frames and
26-
// the pieces join exactly (no holdback, no cross-fade). An in-context
27-
// reference voice is decoded first (once per voice, cached).
28-
// * Otherwise: re-decodes [0, avail) once enough new frames exist (causal
29-
// decoders need the whole left context), holds back the right edge the
30-
// decoder may still revise, and cross-fades the joins.
23+
// Frame-level streaming over the current generation. Each poll decodes a window statelessly:
24+
// * codecs with a measured window (src/audio_lm_window.h): [committed - left, avail), so the cost per poll is
25+
// bounded (linear overall);
26+
// * others: [0, avail) (exact for any decoder, quadratic).
27+
// It holds back the codec's right edge (frames the decoder may still revise) and the last cross-fade span before
28+
// the commit point; the next window decodes that span again and the two are cross-faded (20 ms).
3129
class alm_streamer {
3230
public:
3331
explicit alm_streamer(audio_lm_context * alm) : alm_(alm) {}
34-
~alm_streamer() { audio_lm_stream_end(inc_); }
3532

3633
// New final audio since the last call (possibly none).
3734
bool poll(int32_t chunk_ms, std::vector<float> * piece, int32_t * sr, int32_t * nch) {
3835
chunk_ms_ = chunk_ms;
3936
const int32_t avail = audio_lm_decodable_frames(alm_);
40-
if (exact()) {
41-
// whole chunks once the rate is known (few graph shapes); the first piece as soon as a frame exists
42-
const int32_t step = chunk_ > 0 ? chunk_ : 1;
43-
const int32_t end = committed_ + (avail - committed_) / step * step;
44-
return (end <= committed_ || exact_piece(end, piece)) && out(piece, sr, nch);
45-
}
4637
if (spf_ == 0 ? avail < k_probe_frames : avail < committed_ + chunk_ + right_) return out(piece, sr, nch);
4738
return window(avail, false, piece) && out(piece, sr, nch);
4839
}
4940

5041
bool finish(std::vector<float> * piece, int32_t * sr, int32_t * nch) {
5142
const int32_t avail = audio_lm_decodable_frames(alm_);
52-
if (exact()) return (avail <= committed_ || exact_piece(avail, piece)) && out(piece, sr, nch);
5343
if (avail > committed_ && !window(avail, true, piece)) return false;
5444
return out(piece, sr, nch);
5545
}
@@ -58,7 +48,7 @@ class alm_streamer {
5848

5949
private:
6050
static constexpr int32_t k_probe_frames = 8;
61-
static constexpr double k_right_s = 0.32;
51+
static constexpr double k_right_s = 0.32; // codecs without a measured window
6252
static constexpr double k_xfade_s = 0.02;
6353

6454
bool out(std::vector<float> *, int32_t * sr, int32_t * nch) const {
@@ -68,76 +58,62 @@ class alm_streamer {
6858
}
6959

7060
bool window(int32_t avail, bool final, std::vector<float> * piece) {
61+
if (spf_ == 0) {
62+
spf_ = audio_lm_codec_hop(alm_);
63+
if (spf_ <= 0) return fail("streaming: the codec reports no hop size");
64+
double left_s = 0.0, right_s = k_right_s;
65+
bounded_ = audio_lm_stream_window(alm_, &left_s, &right_s);
66+
left_s_ = left_s;
67+
right_s_ = right_s;
68+
}
69+
// first frame of the window: enough left context, and the held-back span must be in it
70+
const int32_t w0 = !bounded_ ? 0 : std::max(0, committed_ - left_);
7171
audio_lm_audio_output w;
72-
if (!audio_lm_decode_audio_range(alm_, 0, avail, &w)) {
72+
if (!audio_lm_decode_audio_range(alm_, w0, avail, &w)) {
7373
return fail(std::string("streaming decode failed: ") + audio_lm_last_error(alm_));
7474
}
7575
const int32_t nch = std::max(1, w.n_channels);
7676
const int64_t len = (int64_t) (w.pcm.size() / (size_t) nch);
77-
if (spf_ == 0) {
78-
spf_ = audio_lm_codec_hop(alm_);
79-
if (spf_ <= 0) return fail("streaming: the codec reports no hop size");
77+
if (sr_ == 0) {
8078
sr_ = w.sample_rate;
8179
nch_ = nch;
8280
const double frames_per_s = (double) sr_ / spf_;
8381
chunk_ = std::max(1, (int32_t) std::lround(chunk_ms_ * frames_per_s / 1000.0));
84-
right_ = std::max(1, (int32_t) std::lround(k_right_s * frames_per_s));
85-
xfade_ = std::min((int32_t) std::lround(k_xfade_s * sr_), right_ * spf_);
82+
right_ = (int32_t) std::lround(right_s_ * frames_per_s);
83+
xfade_ = (int32_t) std::lround(k_xfade_s * sr_);
84+
left_ = std::max((int32_t) std::lround(left_s_ * frames_per_s), (xfade_ + spf_ - 1) / spf_ + 1);
8685
}
87-
if (nch != nch_ || std::llabs(len - (int64_t) avail * spf_) > spf_) {
88-
return fail("streaming: a window of " + std::to_string(avail) + " frames decoded to " +
89-
std::to_string(len) + " samples, not about " + std::to_string((int64_t) avail * spf_));
86+
if (nch != nch_ || std::llabs(len - (int64_t) (avail - w0) * spf_) > spf_) {
87+
return fail("streaming: a window of " + std::to_string(avail - w0) + " frames decoded to " +
88+
std::to_string(len) + " samples, not about " + std::to_string((int64_t) (avail - w0) * spf_));
9089
}
9190
const int32_t commit = final ? avail : std::max(committed_, avail - right_);
92-
const size_t base = (size_t) committed_ * spf_;
93-
if ((int64_t) base > len) return fail("streaming: decoded window too short");
94-
const size_t n_new = final ? (size_t) len - base : (size_t) (commit - committed_) * spf_;
95-
piece->assign(w.pcm.begin() + (ptrdiff_t) (base * nch_), w.pcm.begin() + (ptrdiff_t) ((base + n_new) * nch_));
96-
const size_t nx = std::min(pending_.size() / nch_, n_new);
97-
for (size_t i = 0; i < nx; ++i) {
98-
const float g = (float) (i + 1) / (float) (nx + 1);
91+
const size_t held = pending_.size() / (size_t) nch_; // samples before `committed_` not emitted yet
92+
const int64_t b0 = (int64_t) (committed_ - w0) * spf_ - (int64_t) held; // in this window
93+
const int64_t e0 = final ? len : std::min<int64_t>(len, (int64_t) (commit - w0) * spf_);
94+
const int64_t keep = final ? 0 : std::min<int64_t>(xfade_, std::max<int64_t>(0, e0 - b0 - (int64_t) held));
95+
if (b0 < 0 || e0 < b0) return fail("streaming: decoded window too short");
96+
piece->assign(w.pcm.begin() + (ptrdiff_t) (b0 * nch_), w.pcm.begin() + (ptrdiff_t) ((e0 - keep) * nch_));
97+
for (size_t i = 0; i < held && i < piece->size() / (size_t) nch_; ++i) { // previous window fades out
98+
const float g = (float) (i + 1) / (float) (held + 1);
9999
for (int32_t c = 0; c < nch_; ++c) {
100100
float & v = (*piece)[i * nch_ + c];
101101
v = g * v + (1.0f - g) * pending_[i * nch_ + c];
102102
}
103103
}
104-
const size_t tail = base + n_new;
105-
const size_t np = final ? 0 : std::min((size_t) xfade_, (size_t) len - tail);
106-
pending_.assign(w.pcm.begin() + (ptrdiff_t) (tail * nch_), w.pcm.begin() + (ptrdiff_t) ((tail + np) * nch_));
104+
pending_.assign(w.pcm.begin() + (ptrdiff_t) ((e0 - keep) * nch_), w.pcm.begin() + (ptrdiff_t) (e0 * nch_));
107105
committed_ = commit;
108106
return true;
109107
}
110108

111109
bool fail(const std::string & e) { error_ = e; return false; }
112110

113-
// The path is chosen once per generation, at its first poll.
114-
bool exact() {
115-
if (!opened_) { opened_ = true; inc_ = audio_lm_stream_begin(alm_); }
116-
return inc_ != nullptr;
117-
}
118-
119-
bool exact_piece(int32_t end, std::vector<float> * piece) {
120-
audio_lm_audio_output w;
121-
if (!audio_lm_stream_next(alm_, inc_, end, &w)) {
122-
return fail(std::string("streaming decode failed: ") + audio_lm_last_error(alm_));
123-
}
124-
sr_ = w.sample_rate;
125-
nch_ = std::max(1, w.n_channels);
126-
if (chunk_ == 0) {
127-
const int32_t hop = audio_lm_codec_hop(alm_);
128-
if (hop > 0 && sr_ > 0) chunk_ = std::max(1, (int32_t) std::lround(chunk_ms_ * (double) sr_ / hop / 1000.0));
129-
}
130-
*piece = std::move(w.pcm);
131-
committed_ = end;
132-
return true;
133-
}
134-
135111
audio_lm_context * alm_;
136-
audio_lm_stream * inc_ = nullptr;
137-
bool opened_ = false;
138112
int32_t chunk_ms_ = 0;
139113
int32_t spf_ = 0, sr_ = 0, nch_ = 1;
140-
int32_t chunk_ = 0, right_ = 0, xfade_ = 0;
114+
int32_t chunk_ = 0, right_ = 0, left_ = 0, xfade_ = 0;
115+
bool bounded_ = false;
116+
double left_s_ = 0.0, right_s_ = 0.0;
141117
int32_t committed_ = 0;
142118
std::vector<float> pending_;
143119
std::string error_;
@@ -172,7 +148,6 @@ struct codec_audio_lm {
172148
alm_generation exported; // codec_audio_lm_get_generation
173149

174150
~codec_audio_lm() {
175-
streamer.reset(); // its incremental decoder lives on alm's codec context
176151
family.reset();
177152
if (alm && owns_alm) audio_lm_free(alm);
178153
}

‎common/audio_lm_family_codebook.cpp‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// from the backbone's own vocabulary under a grammar, the rest from heads).
66

77
#include "audio_lm_family.h"
8-
#include "audio_lm_stream.h"
98

109
#include <algorithm>
1110
#include <cmath>
@@ -299,9 +298,6 @@ class codebook_family : public alm_family {
299298
}
300299
if (ref.empty()) return alm_family::decode_generation(g, out);
301300
const int32_t n_ref = (int32_t) ref.size() / g.width;
302-
// A strictly causal codec decodes the generation from the decoder state after the reference (cached per
303-
// voice): the same samples, without decoding the reference again.
304-
if (audio_lm_decode_after_prefix(alm_, ref.data(), n_ref, g.codes.data(), g.n_frames, g.width, out)) return true;
305301
alm_generation all = g;
306302
all.codes.insert(all.codes.begin(), ref.begin(), ref.end());
307303
all.n_frames += n_ref;

0 commit comments

Comments
 (0)