Skip to content
11 min read

A VAD padding measured from the wrong mark: what is left of 200 ms

A risk register named the failure — silence trimming cuts speech — and prescribed 200 ms of padding either side of the VAD's boundaries. The detector cannot emit a start until five frames of speech have gone past, and the index recorded is the start of whatever batch the sound card delivered. What is left in front of the first word is 42 ms in the worst alignment, and 10 ms if the detector is less generous than we assumed.

Vyacheslav Pankratov· Fullstack Developer
Cover art for “A VAD padding measured from the wrong mark: what is left of 200 ms”
On this page

TL;DR: Our risk register named the failure and prescribed the fix as a number: silence trimming cuts speech → use VAD boundaries with vad padding of 200 ms either side. The padding is real and it is subtracted from the wrong reference point — the detector cannot report a start until five consecutive 32 ms frames of speech have already gone past, and the sample index it records is the beginning of whatever batch the sound card happened to deliver. Sweeping every alignment, what is left in front of the first word is 42 ms in the worst case, and 10 ms if the detector is less generous than we assumed. Nothing is ever clipped, so this is a fraction of a remedy rather than no remedy — and no setting a user can reach changes any of it.

A register named the failure and prescribed a number

The row is one line long, and as risk-register rows go it is a good one — it names a real failure mode and it names a specific remedy rather than an intention:

Risk Mitigation
Silence trimming cuts speech Use VAD boundaries with padding (200 ms before/after)

That is the kind of entry that closes a discussion. Somebody implemented it, the constant is in the code with a comment giving its value in milliseconds, and there is a test asserting that the trim range is widened by exactly that much. Everything about it is correct except the thing it is measured from.

The mark is not the onset

Three facts, each individually reasonable, and the interaction is the article:

The detector will not call it speech on the first frame. It consumes 512 samples at a time — 32 ms at 16 kHz — and requires five consecutive speech frames before emitting a start event. That smoothing exists for a good reason: without it a cough or a keyboard click starts a recording. It also means the event cannot arrive until 160 ms of speech has gone past.

The index recorded is not the event's frame either. The capture callback hands the pipeline a batch of samples; the pipeline feeds them to the detector and, if the detector returned a start, records the sample index at which that batch began. Frames are aligned to the stream; batches are aligned to the sound card. The two grids have nothing to do with each other.

Then the padding is subtracted from that. 3,200 samples, the register's 200 ms.

const PADDING_SAMPLES: u64 = 3_200; // 200ms at 16kHz
// …
let trim_start = start.saturating_sub(PADDING_SAMPLES);

So the quantity that actually protects the first word is not 200 ms. It is 200 ms minus however late the mark was, and the mark is late by an amount nobody in the system chose.

What survives, measured

The claim above is arithmetic, so we wrote the arithmetic down and swept it. The harness (rig/vadpad/, with the results.txt every number here comes from) takes one integer — the sample at which speech truly begins — and applies the pipeline's own geometry: 512-sample frames aligned to the stream, five of them before the event, the mark at the delivering batch's start, 3,200 samples subtracted. Each row sweeps exactly one full period of alignments, so every combination of frame-offset and batch-offset is visited once.

It does not run a detector. That matters enough to bracket rather than assume: one arm counts a frame as speech if it contains any speech sample, the other only if the frame lies entirely inside speech. The real detector is Silero VAD v5, reached through the voice_activity_detector crate pinned at 0.2.1 in our lockfile. It returns a probability per frame, and which frame first clears the threshold is its decision, not ours — so the truth is between the two arms, and the optimistic one is an upper bound: the real margin can be worse than everything below and cannot be better.

And the 512-sample frame is not a number anyone here chose. The model accepts exactly one window size per rate — 512 samples at 16 kHz, 256 at 8 kHz — so the 32 ms granularity that the onset requirement multiplies by five is fixed by the model, not by us. What is ours is the multiplier.

Capture batch Optimistic: worst / mean Pessimistic: worst / mean
10 ms 42.00 / 61.97 ms 10.06 / 30.03 ms
32 ms 72.00 / 87.97 ms 40.06 / 56.03 ms
100 ms 44.00 / 107.97 ms 12.06 / 76.03 ms
256 ms 72.00 / 199.97 ms 40.06 / 168.03 ms

Against a prescribed 200. Nothing was clipped in any alignment of any arm — the floor is above zero, always, so this is a fraction of a remedy and not the absence of one. That distinction is worth insisting on because the dramatic version of this article would be wrong.

One arm exists purely to prove the model: mark the true onset instead, and the margin must come out at exactly 200.00 ms. It does — in all three columns, for every batch size. Skip that check and the table above is just as consistent with our arithmetic being broken as with our pipeline being.

  • detector fires on a partly-speech frame
  • only on a frame entirely inside speech
Worst alignment of every onset offset, at six capture batch sizes, against the 200 ms the risk register prescribed. The batch sizes are a sweep, not a measurement — no capture device was interrogated. From rig/vadpad/results.txt.

Why is the worst case not the smallest buffer?

Look down the optimistic worst-case column across every batch size we swept: 42, 44, 72, 72, 44, 72 ms. It does not improve as the buffer grows, and 100 ms buffers do worse than 32 ms ones.

We predicted the opposite, in writing, before running it. The reasoning was sound as far as it went: because the mark is the start of the delivering batch, a bigger batch puts the mark further back in time and keeps more audio — so bigger should be better. That is true of the mean, which climbs from 61.97 ms to 199.97 ms across the sweep, and of the best case. It is simply not true of the worst case, which is decided by how 512 and the batch size divide into each other rather than by size. A prediction that was right about the average and wrong about the tail is the ordinary way to be wrong about a distribution.

That was one of two predictions this run refuted. The other was cruder: we had written down "the floor is about 40 ms" and the answer is a bracket from 10.06 to 42.00, because we cannot say which frame the detector fires on without running the detector. A single number was the wrong shape of answer, which is a worse mistake than being two milliseconds out.

The bug is load-bearing — do not patch half of it

At a 256 ms capture buffer, correcting the batch mark on its own costs 112 ms of margin, because marking the batch start was already earlier than the frame that completed the onset. The two mistakes partially cancel, by an amount the sound card decides. Fix the onset accounting and the mark together, and measure after.

Two mistakes, and your sound card decides how much they cancel

Because the harness can turn each cause off independently, the loss decomposes — and the decomposition is the part worth taking to your own pipeline.

At 10 ms batches, dropping the five-frame onset requirement recovers 128.0 ms of mean margin. Removing the batch granularity recovers 26.0 ms. The smoothing is the dominant cost by five to one, which is the answer you would expect.

At 256 ms batches, "fixing" the batch granularity costs 112 ms. The sign flips. Marking the start of a 256 ms batch was already earlier than the frame that completed the onset, so the mark being crude was buying back most of what the smoothing spent. The two mistakes partially cancel, and the amount they cancel is set by the audio device's buffer size.

Which means a reader who reads this far, patches the batch mark, and ships has made their own system worse if their capture buffer is large. The bug is load-bearing. That is not an argument for leaving it; it is an argument for fixing the two together and measuring after.

Neither constant is a setting, and both of the ones that are do nothing here

The detector's configuration is exposed to the user: whether it is enabled, its probability threshold, and how much silence ends an utterance. All three are in the settings model, serialised, round-tripped and tested.

The two constants that decide the head margin — the padding and the onset-frame count — are compiled in. There is no way to reach them without a build, and nothing in the settings UI hints that they exist. So the one number a user could plausibly want to change after hearing their first word clipped is the one number they cannot, and the knobs they can reach have no effect on it at all.

Both of them are ours in a stronger sense than "we typed them", and that is worth being precise about. The detector library returns a probability per frame and nothing else: it has no minimum-speech-duration parameter, so the five-frame confirmation is a smoothing layer we wrote rather than a setting we left at its default. It does ship a padding facility — its chunk-labelling helpers take a padding_chunks argument that widens each speech segment at both ends — and we do not use it; we hand-rolled a padding that subtracts samples from a mark taken elsewhere. The library had a version of this that pads relative to its own segment boundaries. We built a second one, in different units, against a different reference point.

The tail is the same constant doing nothing at all

Read off the code rather than run, and marked as such because the harness does not model it.

The end mark comes from the opposite mechanism: the detector waits for 640 ms of silence before emitting an end, and the index recorded is the end of the batch that carried it. So the tail is already 640-odd milliseconds past the last word before the padding is considered — and then the same 200 ms is added and clamped to the length of the recording, which the recorder stops writing on that very event. The addition is clamped away. It is clamped on the other exit too: stop the recording by hand and no end mark is set at all, so the range runs to the file's length and the padding again has nothing to reach into.

One constant, two boundaries, and it is a fifth of its nominal value at one end and nothing at all at the other. Neither is what the register's author had in mind when they wrote a single number for both.

What we would change

Record the mark you mean, not the mark you have. The detector already knows how many frames it waited; the pipeline already knows the batch's start. Subtracting onset_frames × frame_size from the recorded index costs one line and recovers the largest term.

Then make the padding a setting, or delete it. A constant that is silently a fifth of itself is worse than an honest one, because the register's row reads as closed.

And keep the smoothing. Every number above is an argument about accounting for the onset delay, not for removing it. Five frames of confirmation is why a keyboard click does not start a recording, and the version of this article that ends "so we set onset frames to 1" would have traded a clipped consonant for a corpus of coughs.

FAQ

Is 42 ms of lead-in actually a problem? We do not know, and nothing here measures it. Whether a plosive survives a 42 ms run-up is a question about speech and about the recogniser, and answering it needs a different rig and a listening test. What we can say is that the system promises 200 and delivers between 10 and 42, which is a gap worth knowing about regardless of where the threshold turns out to be.

Why not just increase the padding constant? It works, and it is the fix we would reach for last. Padding is subtracted blindly, so raising it also drags silence into every recording that had a clean onset, and it leaves the register's number meaning something different from what it says. Fixing the reference point makes the constant honest; raising the constant makes it lucky.

Does this happen with every VAD? The specific numbers are ours. The shape is not: every practical voice detector smooths its onset, and every capture path delivers in batches. If your padding is subtracted from an index recorded when the event fired, you have some version of this, and the size of it is your frame count times your frame size.

How would I check my own pipeline? Write down the three numbers — frame size, frames required before the event, and what index you record — and subtract. It is arithmetic, and it took us longer to write the prediction down than to run it. If the answer surprises you, that surprise is the finding.

Three things worth taking away

  1. A remedy written as a number needs its reference point written down beside it. "200 ms of padding" is not a specification; "200 ms before the first speech sample" is. Ours said the first and implemented something that could only ever approximate it.
  2. A detector's confirmation delay is a debt somebody pays. Smoothing an onset is correct and it moves the event later by a knowable amount. If nothing subtracts that amount back, it is paid out of the margin that was supposed to protect the first word.
  3. Check which of your constants a user can reach. Here the exposed settings are the ones with no effect on the failure, and the two that decide it need a rebuild. That inversion is easy to create and invisible until somebody reports a clipped word.

If you have a capture pipeline whose padding is subtracted from whatever index happened to be handy, that is a cheap thing to have looked at.

Was this helpful?

// Build it

Running this in production?

Audio has no idle — silence is something you generate, a 20 ms hole is audible, and anything that talks must choose what it will never say. Tell us where the sound goes wrong; we reply within a day with a concrete next step.