Pacing synthesised speech into a live call: three constants, three opposite failures
Synthesised speech has to be paced into a live call at the rate a human would have spoken it. Three constants in one file hold three opposite failure modes apart — a prebuffer that deliberately spends 100 ms, a bounded silence tail, and a catch-up ceiling past which the pacer re-anchors its clock rather than bursting. This is what each one bought, and what we did not measure.
On this page
TL;DR: Pacing TTS audio back into a live call is not a buffering problem with one right answer. Three constants in one file hold three opposite failures apart: a receiver whose buffer is cold when the first word arrives, a receiver that unprimes during the gap between two sentences, and a receiver whose latency inflates and never comes back. Push any one constant the way its own failure argues for and you cause one of the other two. Here is what each one is, what the loop before them did instead, and — since this matters more than the constants — what we never measured.
Text-to-speech gives you a buffer of audio. A live call wants frames, at the rate a person would have spoken them. Between those two facts sits a pacer, and it is where every timing decision in the system ends up.
What the loop before it did
The first version was the obvious one: one frame per timer tick.
private tick() {
const frame = this.queue.shift();
if (!frame) return;
const ok = this.send(frame);
// …
this.nextFrameAt += AUDIO_CONNECTOR_FRAME_DURATION_MS;
this.schedule();
}Two things in that shape cause trouble, and neither is obvious until you hear it.
It could only ever run late. schedule() re-anchored the clock on every fire — "if the next
frame is due in the past, make it due now" — and then tick added a frame's duration to it. A
timer that wakes 3 ms late therefore emits at 23 ms, not at 20, and the 3 ms is gone rather than
made up. Lateness is not a rare event on a busy event loop; it is the normal case. So the stream ran
slightly slower than real time, continuously, and a receiver consuming at real time drains what it
has.
It stopped dead on an empty queue. if (!frame) return; with no reschedule, and schedule()
refused to arm a timer while the queue was empty. So an ordinary gap — the next chunk of synthesis
arriving a moment late — did not stretch the audio. It ended the stream, and the next chunk started
a new one.
Three constants, three opposite failures
The replacement is not cleverer. It is three named constants, each buying protection against a failure that one of the others would cause if you leaned on it.
| Constant | Value | The failure it prevents | The failure it would cause if increased |
|---|---|---|---|
| Prebuffer, prepended on every idle→active transition | 5 frames × 20 ms = 100 ms of silence | the first word arrives at a cold receiver and gets stretched or clipped | pure added latency on every utterance, paid by the listener |
| Silence tail, emitted while the queue is empty | 500 ms | the receiver unprimes during a gap between sentences, so the next sentence pays the cold-start cost again | a stream that never goes idle, whose buffer can inflate across a long session |
| Catch-up ceiling, past which the schedule re-anchors | 100 ms | a stalled event loop is followed by a burst of frames, which inflates the receiver's buffer permanently | longer stalls converted into longer bursts, with the same result |
Read the last column, because it is the article. Every one of these has an argument for making it bigger, and each of those arguments is an argument for one of the other two failures. There is no setting of the three that has no downside — only a set of tradeoffs somebody picked.
Why is a prebuffer not just extra latency?
It is exactly extra latency. 100 ms, deliberately, on every utterance, at the front.
That is the honest description and it is why the constant is worth naming rather than tuning away. The alternative to spending it is keeping the stream permanently hot — emitting silence forever so the receiver never unprimes — which trades a bounded 100 ms for an unbounded accumulation. Or you spend nothing, and the receiver stretches or drops the beginning of the first word, which is the part a listener is most likely to need and least likely to forgive.
Prepending silence to prime a receiver is not our idea and not new — it is described in telecommunications patents from the 2000s, and any decent jitter buffer implementation expects it. What is worth writing down is that it is a purchase, with a price in milliseconds, made once per utterance rather than once per session.
- Tail expired and queue empty?return, go idle — no reschedule
- Queue non-empty?stamp the activity clock
- More than 100 ms behind?re-anchor the schedule to now
- Drain every frame now duereal frame, or silence if none
Re-anchoring is not dropping
The catch-up guard is one line, and it is easy to read as a drop policy:
if (this.nextFrameAt < now - MAX_CATCHUP_MS) {
this.nextFrameAt = now;
}It mutates a timestamp. The queue is untouched, so every frame that was queued still ships. What moves is when they are due: instead of the pacer owing 25 frames it must emit immediately, it owes them at cadence starting now. The listener hears a gap where the stall was, then speech at normal speed. Nothing is thrown away.
We had this wrong in our own planning notes and had to correct it — the note said the backlog was discarded, which would have made this the same kind of system as a queue with a drop policy. It is not: this pacer is bounded by nothing and discards nothing, which is a separate decision with its own consequences, and one we have written about elsewhere in this series.
The drain loop underneath is what makes the ceiling meaningful. It emits while the next frame is
due, so small lateness is absorbed by sending two frames in one wake-up rather than being lost. The
ceiling only fires when that catching-up would be large enough to be a burst.
Do not copy these three numbers
100 ms of prebuffer, a 500 ms tail and a 100 ms catch-up ceiling are values a colleague chose while making a reported symptom stop, on one platform — in a change thatdeleted a TODO asking for a measurement under load rather than doing it. Take the shape (you own all three decisions whether or not you have named them), not the values.
What we did not measure, and what was deleted
The commit that introduced these three constants also removed this comment:
// TODO: re-measure under staging load. If Buffer.concat shows up in a
// flame graph, switch to a deque-of-buffers…The measurement was not done. The TODO was replaced, in the same change, by three tuned values and three explanatory comments — which read, a year later, as though they were derived from something.
They were derived from a reported symptom and a fix that made it stop. That is a legitimate way to work and it is not a benchmark, so this article contains no numbers beyond the constants themselves. There is no before-and-after latency figure here because nobody took one, and inventing the shape of one is precisely the failure this whole series is written against.
One more inheritance worth flagging, in the same spirit. Our comments name the receiver's jitter buffer as NetEQ — the WebRTC implementation — and say what it does with the frames we send. We never instrumented that receiver. It is a reasonable read of a platform we integrate with, written by an engineer fixing something real, and it may well be right; it is also an attribution nobody on our side verified, which is worth marking as a read rather than passing on as a fact. If you want the mechanics of the thing being primed, webrtcHacks has the best public explanation, and this article is deliberately not a second one.
What travels and what does not
One connector, one direction, one platform. Everything above is a pacer feeding synthesised speech into one live-call platform through one bidirectional connector. We have not run it against a second, so the three values are ours and not a recommendation.
What does travel is the shape: if you are pushing generated audio into a real-time session, you own all three of these decisions whether or not you have named them. A pacer with no prebuffer has chosen zero. A pacer with no idle timeout has chosen infinity. A pacer with no catch-up ceiling has chosen to burst. The constants exist so that the choices are visible in a file rather than implied by a loop.
FAQ
Why not just let the receiver's jitter buffer handle it? It does handle it — that is its job, and it is good at it. What it cannot do is invent audio it has never been given, or shrink a buffer level it was pushed into by a burst. The pacer's job is to hand it a well-behaved stream so that its adaptation works with small corrections instead of large ones.
Is 100 ms of prebuffer the right number? We do not know, and this article is not the place that would tell you. It is the number that made a reported symptom stop, on one platform, and it has not been swept. If you take one thing from it, take the fact that it is a purchase rather than the size of the purchase.
What happens if the send fails mid-stream? The current version halts the pacer and notifies its owner, which removes it. The version before it dropped every queued frame and kept the pacer alive — the same event, two opposite policies, and worth checking which one your own code has chosen by accident.
How would I know if my pacer is running slow? Compare frames emitted against wall-clock time over a long utterance, not over a short one. A loop that loses a few milliseconds per tick looks perfect in a five-frame test and starves a receiver over a paragraph, which is exactly why the first version survived review.
Three things worth taking away
- A pacer's constants are a policy, and the policy is a triangle. Cold start, mid-sentence gap and long-session inflation pull in different directions; a value that fixes one is an argument for another. Name all three or you have still chosen all three.
- Absolute schedules beat incremental ones. A clock that re-anchors on every tick can only ever fall behind, and it does it invisibly — a few milliseconds per frame is inaudible until it is a drained buffer.
- A tuned constant with a confident comment is not a measurement. Ours replaced a TODO asking for one. Both things can be true at once: the fix was correct, and nobody has the number that would prove it.
If you are pushing generated audio into a live session and the three decisions above are implied by a loop rather than named in a file, that is a cheap thing to have looked at.