Documentation
¶
Overview ¶
Package spoken answers one question the rest of the runtime keeps asking and has never been able to answer: of the words the agent decided to say, which ones did the user actually hear?
A cascade decides a whole sentence, hands it to a synthesiser, and paces the audio out at the rate it plays. When somebody interrupts, the audio stops wherever it had got to. Everything downstream then had two choices, and both are wrong. Believing the agent said the whole sentence makes it carry on past words nobody heard - ask it to read a long number sequence, interrupt it, and it resumes at a number that was never spoken. Believing it said none of the sentence makes it start again from the beginning, so the same words are said twice and the person who interrupted is answered with a repetition.
The honest answer is a boundary inside the sentence, and a boundary inside a sentence needs the sentence laid out against its own audio. That is what a Timeline is: the utterance's own words, each with the interval of its utterance-relative audio. Given how much audio was paced out before playback stopped, the split is then arithmetic rather than guesswork.
Two things produce a Timeline. Measured times come from listening to the synthesised audio with a recogniser that reports word times, reconciled back onto the words the synthesiser was asked to say - the recogniser's job here is timing, never wording, because the wording is already known exactly. Estimated times come from a proportional model over the same words. Which one a Timeline holds is recorded rather than inferred, because a caller deciding whether to trust a boundary needs to know whether anything listened to the audio.
Index ¶
- Constants
- func ExpectedSpan(text string, audioMS uint64) uint64
- func PriorDuration(words []string) uint64
- func Words(text string) []string
- type Aligner
- type Audio
- type Mark
- type Timeline
- type Tracker
- func (tracker *Tracker) Audio(id string, chunk []byte, sampleRateHz uint32)
- func (tracker *Tracker) Begin(id, text string)
- func (tracker *Tracker) Current() (Mark, bool)
- func (tracker *Tracker) End(ctx context.Context, id string, playedMS uint64) Mark
- func (tracker *Tracker) Mark(id string) (Mark, bool)
- func (tracker *Tracker) MarkAt(id string, playedMS uint64) (Mark, bool)
- func (tracker *Tracker) Played(id string, playedMS uint64)
- func (tracker *Tracker) Synthesised(id string)
- func (tracker *Tracker) Timeline(id string) (Timeline, bool)
- type TrackerConfig
- type Word
Constants ¶
const PriorMSPerWeight = 33
PriorMSPerWeight is how long one unit of weigh() is assumed to take before the audio has proved otherwise.
Calibrated against the synthesisers this ships with: an English word averages about eleven units and about three hundred and fifty milliseconds, which is roughly a hundred and fifty words a minute. It is deliberately a little slow. The two ways of being wrong here are not symmetrical - a layout that runs fast reports words as heard that nobody heard, and the agent then carries on past them, which is the exact failure this package exists to remove, while a layout that runs slow has the agent repeat a word somebody already heard, which is what a person does anyway when they are interrupted.
Variables ¶
This section is empty.
Functions ¶
func ExpectedSpan ¶
ExpectedSpan is how long an utterance whose synthesis is still running is believed to run in total.
It is the prior, or the audio that already exists plus one prior word, whichever is longer. The second term carries the invariant that matters: audio nobody has produced cannot have been played, so while more is coming the last word of the utterance must sit past everything that exists.
func PriorDuration ¶
PriorDuration is how long these words are expected to take before anything has measured them.
Types ¶
type Aligner ¶
Aligner reports where the words are in a piece of audio.
It is a recogniser with word times, and the interface says nothing about which one: a local Whisper, a hosted transcription endpoint, or a forced aligner all answer the same question. What it must not be asked to do is decide what was said - Reconcile owns that, because the text is already known and the recogniser's version of it is strictly worse.
type Mark ¶
type Mark struct {
// Spoken is the text whose audio finished before playback stopped. It is
// what the user heard, and it is the only thing that may be presented to a
// later model as something the agent said.
Spoken string `json:"spoken,omitempty"`
// Cut is the single word playback stopped inside, empty when the stop fell
// on a word boundary. It is not in Spoken, and it opens Pending: resuming
// from a half-said word is what a person does.
Cut string `json:"cut,omitempty"`
// Pending is everything the user never heard whole, the cut word included.
Pending string `json:"pending,omitempty"`
// Measured says the boundary came from listening to the audio rather than
// from a proportional model of it.
Measured bool `json:"measured,omitempty"`
// PlayedMS is the playback position this mark was taken at, kept so that a
// record of a cut carries the duration it was derived from.
PlayedMS uint64 `json:"played_ms,omitempty"`
}
Mark is what an utterance had actually said by a moment in its playback.
The three fields are deliberately not a two-way split. A cut almost never lands on a word boundary, and the word it lands inside was neither heard nor unheard: the listener got part of it. Folding that word into Spoken makes the agent skip it when it resumes, and folding it silently into Pending loses the one fact that explains why the resumption repeats a word. It is named.
func Distribute ¶
Distribute splits one utterance's mark back across the separate pieces of text that were joined to make it.
One utterance may carry several assistant items: a turn committed as two sentences is two items in the trajectory and one thing to say out loud. The cut is a fact about the audio, so it is discovered once, for the utterance, and then has to be recorded against each item it covers - otherwise an item that was fully spoken and an item that was never reached are both marked with whatever happened to the utterance as a whole.
Matching is by word count rather than by text search, because the same words recur inside one turn constantly - a count-out-loud turn is nothing but repeated shapes - and a search would attach the boundary to the first occurrence rather than to the right one.
type Timeline ¶
type Timeline struct {
Text string `json:"text"`
Words []Word `json:"words,omitempty"`
AudioMS uint64 `json:"audio_ms,omitempty"`
// Measured says a recogniser listened to this audio. False means the times
// are a proportional model of the same words, which is a good deal better
// than nothing and is not the same claim.
Measured bool `json:"measured,omitempty"`
}
Timeline is one utterance's text laid out against its own audio.
Words carries the utterance's own words in order, with the punctuation and casing the synthesiser was given, because what a Timeline is for is reconstructing sayable text on either side of a cut. AudioMS is the duration the layout covers: the utterance's real length once synthesis has ended, and before that the length it is expected to reach.
func Estimate ¶
Estimate lays text out over a duration proportionally.
It is the fallback, and it is a real one rather than a placeholder. A synthesiser speaks at a fairly even rate within one utterance, so distributing its duration across the words by length puts each boundary within about a word of where it belongs - which is the resolution the decisions downstream actually need. What it cannot do is notice that the voice paused, hesitated, or read a number as three words, and that is why Measured stays false.
audioMS is how much audio exists so far, which while synthesis is running is not how much there will be. That distinction is the whole of this function's difficulty. A synthesiser is paced to realtime by the planner that consumes it, so "the audio produced so far" tracks "the audio played so far" almost exactly - and laying the text out over it would report every utterance as nearly finished at every moment of its life. So the layout is made over whichever is longer, the prior or the audio plus one prior word, and the second term is what keeps the last word from ever landing inside audio that has not been produced yet.
Complete is what replaces all of this with the truth once there is one.
func Reconcile ¶
Reconcile puts a recogniser's word times onto the words the synthesiser was actually given.
The recogniser is being used for timing, not for wording. What was said is already known exactly - it is the text the synthesiser was handed - and the recogniser's transcript of it is a worse copy: it re-spells numbers, drops punctuation, splits and joins words, and occasionally mishears one outright. Taking its words as the utterance's words would mean the agent resumed from a misspelling of its own sentence.
So the two are aligned rather than substituted. The alignment runs over characters instead of words, and over a canonical form of them, because the disagreements that matter are exactly the ones that break a word-to-word correspondence: "23" against "twenty three" is one word against two, "sea bass" against "seabass" is two against one, and a hyphen is a word boundary in one and not in the other. Canonicalised into a stream of letters, all three become the same characters in the same order, and an ordinary sequence alignment lands each reference word on the audio that carried it.
Reference words no recogniser word matched - a swallowed article, a word mangled past recognition - are interpolated between their neighbours, which is what the estimated layout would have done for the whole utterance anyway. The result is measured because its anchors came from the audio. spanMS is how long the whole utterance is believed to run, which while synthesis is still going is longer than the audio that was listened to. Words the recogniser never reached are laid out past that audio rather than squeezed inside it: audio that does not exist yet cannot have been played, and a layout that puts the tail of a sentence inside the first second of it reports the whole sentence as spoken one second in.
func (Timeline) At ¶
At splits the utterance at a playback position.
A word counts as spoken only when its audio finished, because a word whose last syllable was cut off is not a word the listener received. The one word straddling the boundary is reported as the cut rather than assigned to either side.
func (Timeline) Complete ¶
Complete rebuilds a layout once the utterance's real duration is known.
Until synthesis ends, the duration is a guess with headroom in it, and a layout still carrying that headroom reports a completed utterance as unfinished - the agent would resume mid-sentence after saying the whole thing. This is the moment the guess is replaced, and it matters whether or not anything ever listened to the audio.
A measured layout is rescaled rather than thrown away: its anchors came from the audio and are the best information there is about where the words are. Only the tail that was extrapolated past the audio is moved, and it is moved to end exactly where the audio does.
type Tracker ¶
type Tracker struct {
// contains filtered or unexported fields
}
Tracker follows every utterance from its first synthesised sample to the moment playback stops, and answers what had actually been said by then.
One per session. Speech is serial by construction - a second voice over the first is not a feature - so at most one utterance is ever in flight, and the bookkeeping stays a map only because callers ask about utterances that have already ended.
func NewTracker ¶
func NewTracker(config TrackerConfig) *Tracker
NewTracker creates a tracker. A nil aligner is allowed and documented.
func (*Tracker) End ¶
End closes an utterance at the position playback actually reached and returns the boundary that will be recorded for it.
It waits, briefly, for a listen already in flight. The wait is deliberate and it is bounded: this is the one moment where the answer is about to be written into the trajectory and read by every model afterwards, and a cut recorded from the proportional layout when a measured one arrives fifty milliseconds later is a worse record for the rest of the session. The bound is the point - a recogniser that has stopped answering must not stop the voice.
func (*Tracker) MarkAt ¶
MarkAt re-splits a finished utterance at a different playback position.
The client is the authority on what was actually heard: the server knows what it sent, and only the client knows where playback stopped. When it says so afterwards, the boundary moves, and the layout that was worked out for the utterance is exactly what makes moving it possible.
func (*Tracker) Synthesised ¶
Synthesised says the provider produced its last sample.
It is the moment the estimated layout stops guessing at a total duration and starts using the real one, so it matters even where no aligner is configured.
type TrackerConfig ¶
type TrackerConfig struct {
// Aligner is optional. Without one every boundary is proportional, which
// is the whole feature working at lower resolution rather than the feature
// switched off - a deployment with no spare recogniser still stops
// resuming from words nobody heard.
Aligner Aligner
// Interval is how much new audio is worth another listen while an
// utterance is still being synthesised. Zero selects a second, which is
// short enough that a cut lands inside measured audio and long enough that
// an ordinary sentence is listened to once or twice rather than ten times.
Interval time.Duration
// Timeout bounds one listen, and separately bounds how long the end of an
// utterance waits for one already running.
Timeout time.Duration
// Retain is how many finished utterances stay readable afterwards. The
// trajectory keeps the durable record; this is a short tail for the
// callers that ask again a moment later.
Retain int
// Report receives alignment failures. A failed listen is not a session
// failure - the proportional layout still answers - but it is not nothing
// either, and swallowing it is how a deployment discovers months later
// that nothing has ever been measured.
Report func(error)
}
TrackerConfig configures the per-session tracker.
type Word ¶
type Word struct {
Text string `json:"text"`
StartMS uint64 `json:"start_ms"`
EndMS uint64 `json:"end_ms"`
}
Word is one word of an utterance and where its audio sits inside it.
Times are relative to the first sample of that utterance, not to the session clock. An utterance is the unit that gets cancelled, so it is the unit whose internal offsets stay meaningful when everything around them moves.