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
2121namespace {
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).
3129class alm_streamer {
3230public:
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
5949private:
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 }
0 commit comments