|
| 1 | +# Draft: `audiospeed` throws away its phase at every source buffer |
| 2 | + |
| 3 | +**Note to the poster — strip everything above the `---`.** |
| 4 | + |
| 5 | +The larger of the two `audiospeed` drafts, and the one with an audible symptom. |
| 6 | +Independent of [speedchanger-rate-rounding.md](speedchanger-rate-rounding.md) — |
| 7 | +different lines of the same file — but the repro reads better with that one |
| 8 | +applied, because otherwise the rates in it are a hair off as well. Suggested |
| 9 | +title: |
| 10 | + |
| 11 | +> `audiospeed_fetch_source_buffer` resets the phase, so a `SpeedChanger` drifts |
| 12 | +> and only renders correctly when the rate divides the buffer length |
| 13 | +
|
| 14 | +Verified present on `10.3.0` 2026-09-17 on a build of that tag with |
| 15 | +`CIRCUITPY_AUDIOSPEED` on. The fix was verified by applying it to a port of the |
| 16 | +same code: every measurement below goes to its ideal value, and the node's |
| 17 | +output stops depending on the source's block size at all. |
| 18 | + |
| 19 | +--- |
| 20 | + |
| 21 | +### `audiospeed_fetch_source_buffer` resets the phase, so a `SpeedChanger` drifts and only renders correctly when the rate divides the buffer length |
| 22 | + |
| 23 | +`shared-module/audiospeed/__init__.c`: |
| 24 | + |
| 25 | +```c |
| 26 | +bool audiospeed_fetch_source_buffer(audiospeed_base_t *self) { |
| 27 | + ... |
| 28 | + self->src_sample_count = len / bytes_per_frame; |
| 29 | + self->source_done = (result == GET_BUFFER_DONE); |
| 30 | + // Reset phase to index within this new buffer |
| 31 | + audiospeed_reset_phase(&self->speed); |
| 32 | + return true; |
| 33 | +} |
| 34 | +``` |
| 35 | +
|
| 36 | +and `audiospeed_get_buffer` assumes that reset a few lines later: |
| 37 | +
|
| 38 | +```c |
| 39 | + if (src_index >= self->src_sample_count) { |
| 40 | + ... |
| 41 | + if (!audiospeed_fetch_source_buffer(self)) { |
| 42 | + break; |
| 43 | + } |
| 44 | + src_index = 0; // phase was reset by fetch |
| 45 | + } |
| 46 | +``` |
| 47 | + |
| 48 | +The accumulator is in units of **source frames since the stream began**, not |
| 49 | +since this buffer began, so the remainder it is holding when a buffer runs out |
| 50 | +belongs to the next one. Zeroing it discards that remainder. |
| 51 | + |
| 52 | +A buffer boundary is only ever the right place to resume from frame 0 when the |
| 53 | +rate divides the buffer length exactly. At `rate` 1.8433 with a 256-frame |
| 54 | +upstream, 0.2 of a frame is dropped every 139 output frames; at `rate` 6 with |
| 55 | +the same upstream, four whole frames are dropped every 43. |
| 56 | + |
| 57 | +#### What it sounds like |
| 58 | + |
| 59 | +A `SpeedChanger` pair used as a sample-and-hold — decimate at N, restore at 1/N, |
| 60 | +which is how a lo-fi rate reducer is built out of this module — is the clearest |
| 61 | +case, because a hold has an exactly known answer. |
| 62 | + |
| 63 | +Measured with a 256-frame source, everything else at defaults: |
| 64 | + |
| 65 | +| | 10.3.0 | with the fix | |
| 66 | +|---|---:|---:| |
| 67 | +| the same hold over 64-, 100-, 256- and 1000-frame source buffers: frames differing from the 64-frame render, of 8192, N = 1.8433 | 8023, 8077, 8076 | 0, 0, 0 | |
| 68 | +| frames differing from `source[(((n·up)>>16)·down)>>16]`, of 16384, at N = 1.8433 / 6.0 / 2.5 | 15657 / 16066 / 16090 | 0 / 0 / 0 | |
| 69 | +| a 1 kHz image under a 7 kHz tone held at 8 kHz, where a zero-order hold puts it at −0.22 dB | −39.86 dB | −0.22 dB | |
| 70 | +| worst miss against `20·log₁₀\|sinc(f·T)\|` from 500 Hz to 5 kHz, N = 1.8433 | 1.41 dB | 0.02 dB | |
| 71 | +| a full-scale ramp of one code per frame: lag after 65536 frames at 48 / 44.1 kHz | 73 / 430 codes | 0 / 1 | |
| 72 | + |
| 73 | +The first row is the property worth keeping: **what a `SpeedChanger` renders |
| 74 | +should not depend on how the node above it happens to chunk its output**, and |
| 75 | +today it depends on it almost entirely. The rest follow from it — the staircase |
| 76 | +restarts 187 times a second at 48 kHz, so the held waveform is not the hold that |
| 77 | +was asked for, its images are spread instead of placed, and the stream drifts |
| 78 | +against everything playing beside it. |
| 79 | + |
| 80 | +A rate that divides the buffer length is exempt, which is why this is easy to |
| 81 | +miss: at N = 4 over 256-frame buffers, and at the 2.0 / 1.0 / 0.5 a `Resampler` |
| 82 | +is usually bound to, the accumulator lands on the boundary every time and the |
| 83 | +output is correct. |
| 84 | + |
| 85 | +#### Fix |
| 86 | + |
| 87 | +Subtract what the previous buffer held instead of zeroing, and let the caller |
| 88 | +ask the accumulator where it is rather than assuming frame 0: |
| 89 | + |
| 90 | +```diff |
| 91 | + bool audiospeed_fetch_source_buffer(audiospeed_base_t *self) { |
| 92 | + ... |
| 93 | +- self->src_sample_count = len / bytes_per_frame; |
| 94 | +- self->source_done = (result == GET_BUFFER_DONE); |
| 95 | +- // Reset phase to index within this new buffer |
| 96 | +- audiospeed_reset_phase(&self->speed); |
| 97 | ++ // The frames just consumed are the ones the previous buffer held; the |
| 98 | ++ // remainder belongs to this one. |
| 99 | ++ audiospeed_consume_frames(&self->speed, self->src_sample_count); |
| 100 | ++ self->src_sample_count = len / bytes_per_frame; |
| 101 | ++ self->source_done = (result == GET_BUFFER_DONE); |
| 102 | + return true; |
| 103 | + } |
| 104 | +``` |
| 105 | + |
| 106 | +with, in `__init__.h`: |
| 107 | + |
| 108 | +```c |
| 109 | +static inline void audiospeed_consume_frames(audiospeed_speed_t *self, uint32_t frames) { |
| 110 | + uint32_t consumed = frames << SPEED_SHIFT; |
| 111 | + self->phase = self->phase >= consumed ? self->phase - consumed : 0; |
| 112 | +} |
| 113 | +``` |
| 114 | +
|
| 115 | +and, in both arms of `audiospeed_get_buffer`, pulling until the index lands |
| 116 | +inside a buffer instead of assuming one pull is enough — at a rate above 1.0 the |
| 117 | +carry can be several frames, and the next buffer is free to be shorter than |
| 118 | +that: |
| 119 | +
|
| 120 | +```diff |
| 121 | +- uint32_t src_index = audiospeed_get_index(&self->speed); |
| 122 | +- if (src_index >= self->src_sample_count) { |
| 123 | ++ while (audiospeed_get_index(&self->speed) >= self->src_sample_count) { |
| 124 | + if (self->source_done) { |
| 125 | + self->source_exhausted = true; |
| 126 | + break; |
| 127 | + } |
| 128 | + if (!audiospeed_fetch_source_buffer(self)) { |
| 129 | + break; |
| 130 | + } |
| 131 | +- src_index = 0; // phase was reset by fetch |
| 132 | + } |
| 133 | ++ if (audiospeed_get_index(&self->speed) >= self->src_sample_count) { |
| 134 | ++ break; |
| 135 | ++ } |
| 136 | ++ uint32_t src_index = audiospeed_get_index(&self->speed); |
| 137 | +``` |
| 138 | + |
| 139 | +One guard goes with it. `audiospeed_fetch_source_buffer` tests `len == 0`; with |
| 140 | +the carry it needs `len < bytes_per_frame`, because a buffer holding no whole |
| 141 | +frame can never advance the index. Today that case is an unconditional |
| 142 | +`src_index = 0` against a zero-frame buffer, which reads off the end of it, so |
| 143 | +the guard is worth having either way. |
| 144 | + |
| 145 | +`audiospeed_reset_buffer` should keep zeroing the phase: it resets the source as |
| 146 | +well, so the stream genuinely restarts there. |
| 147 | + |
| 148 | +#### Repro |
| 149 | + |
| 150 | +```python |
| 151 | +# What a SpeedChanger renders should not depend on the block size of the node |
| 152 | +# above it. Two identical holds, two different upstream buffer sizes. |
| 153 | +import array, math |
| 154 | +import audiocore, audiofilters, audiospeed |
| 155 | + |
| 156 | +RATE, FRAMES, DOWN = 48000, 4096, 1.8433 |
| 157 | + |
| 158 | +values = array.array("h", bytes(FRAMES * 4)) |
| 159 | +for frame in range(FRAMES): |
| 160 | + value = int(round(16384 * math.sin(2 * math.pi * 100 * frame / RATE))) |
| 161 | + values[frame * 2] = values[frame * 2 + 1] = value |
| 162 | + |
| 163 | + |
| 164 | +def hold(block): |
| 165 | + wire = audiofilters.Filter(sample_rate=RATE, channel_count=2, |
| 166 | + buffer_size=block * 4) |
| 167 | + wire.play(audiocore.RawSample(values, sample_rate=RATE, channel_count=2)) |
| 168 | + node = audiospeed.SpeedChanger( |
| 169 | + audiospeed.SpeedChanger(wire, DOWN), 1.0 / DOWN) |
| 170 | + out = array.array("h") |
| 171 | + while len(out) < FRAMES * 2: |
| 172 | + out.extend(array.array("h", bytes(audiocore.get_buffer(node)[1]))) |
| 173 | + return [out[index * 2] for index in range(FRAMES // 2)] |
| 174 | + |
| 175 | + |
| 176 | +small, large = hold(64), hold(256) |
| 177 | +wrong = sum(1 for a, b in zip(small, large) if a != b) |
| 178 | +print("%d of %d frames differ between a 64- and a 256-frame source" |
| 179 | + % (wrong, len(small))) |
| 180 | +``` |
| 181 | + |
| 182 | +``` |
| 183 | +1947 of 2048 frames differ between a 64- and a 256-frame source |
| 184 | +``` |
| 185 | + |
| 186 | +With the fix applied it prints `0 of 2048`. |
| 187 | + |
| 188 | +`audiocore.get_buffer` is not upstream; on a board the same two graphs played |
| 189 | +through an `AudioOut` differ audibly, since one of them is not the hold that was |
| 190 | +asked for. |
0 commit comments