diff --git a/checks/api/speech-api-detection-stream.c b/checks/api/speech-api-detection-stream.c index 4bb8ef03..3139f64c 100644 --- a/checks/api/speech-api-detection-stream.c +++ b/checks/api/speech-api-detection-stream.c @@ -5,9 +5,11 @@ * min_silence_duration_ms below twice speech_pad_ms, and must give, once ended, the regions that speech_detect() gives * for the same audio, sample for sample. While the audio arrives, a region once given must not change; every start the * detection says is kept must be a region's, and stay kept while it is said; with min_speech_duration_ms 0, every start it - * says someone speaks from must be a region's, kept from when it is first said; and with pieces of one sample, + * says someone speaks from must be a region's, kept from when it is first said; with pieces of one sample, * min_silence_duration_ms of at least twice speech_pad_ms and no max_speech_duration_s, every region given before the end - * must have had its start said before it was given. Two detections on one model pushed in turn must each give + * must have had its start said before it was given; and with pieces of one sample and no max_speech_duration_s, every + * region given before the end, but one that shares the silence before the next, must have had its end said by + * speech_detection_ending() before it was given. Two detections on one model pushed in turn must each give * their own regions. Then it times a minute of audio in pieces of 20 ms, 100 ms and 1 s, and checks what a detection * refuses. */ @@ -107,6 +109,10 @@ typedef struct { int n_said; double said[MAX_SAID]; size_t said_at[MAX_SAID], kept_at[MAX_SAID]; + /** Each end speech_detection_ending() said a region would take, with the samples pushed when it first said it. */ + int n_ending; + double ending[MAX_SAID]; + size_t ending_at[MAX_SAID]; } Streamed; /** Reads what `d` gives after `at` samples have been pushed; 0 when it gives more regions than the check holds. */ @@ -118,6 +124,11 @@ static int record(const speech_detection * d, Streamed * s, size_t at) { s->given_at[i] = at; } s->regions.count = (int) count; + double end = 0; + if (speech_detection_ending(d, &end) && (s->n_ending == 0 || s->ending[s->n_ending - 1] != end) && s->n_ending < MAX_SAID) { + s->ending[s->n_ending] = end; + s->ending_at[s->n_ending++] = at; + } double start = 0; int kept = 0; if (!speech_detection_speaking(d, &start, &kept)) return 1; @@ -167,7 +178,7 @@ static int stream(speech_model * model, const float * x, size_t n, int rate, con ok = speech_detection_region(d, (size_t) i, &a, &b) == SPEECH_OK && a == s->regions.start[i] && b == s->regions.end[i]; if (!ok) fprintf(stderr, "FAIL: region %d changed after it was given\n", i); } - ok = ok && !speech_detection_speaking(d, NULL, NULL); + ok = ok && !speech_detection_speaking(d, NULL, NULL) && !speech_detection_ending(d, NULL); speech_detection_free(d); return ok; } @@ -186,10 +197,12 @@ static void print_regions(const char * what, const Regions * r) { /** * Whether what a detection of `n` samples said agrees with its regions: each start said to be kept is a region's; with - * min_speech_duration_ms 0 each start said is a region's, kept from when it was first said; and where `said_first` asks, - * each region given before the end had its start said before it was given. + * min_speech_duration_ms 0 each start said is a region's, kept from when it was first said; where `said_first` asks, + * each region given before the end had its start said before it was given; and where `ending_first` asks, each region + * given before the end had its end said by speech_detection_ending() before it was given, but one that the region after + * it starts within `sample` seconds of, which shares the silence between them. */ -static int said_well(const Streamed * s, size_t n, int min_speech_zero, int said_first) { +static int said_well(const Streamed * s, size_t n, int min_speech_zero, int said_first, int ending_first, double sample) { for (int j = 0; j < s->n_said; j++) { if (!min_speech_zero && s->kept_at[j] == 0) continue; int found = 0; @@ -211,6 +224,15 @@ static int said_well(const Streamed * s, size_t n, int min_speech_zero, int said return 0; } } + for (int i = 0; ending_first && i < s->regions.count; i++) { + if (s->given_at[i] > n || (i + 1 < s->regions.count && s->regions.start[i + 1] <= s->regions.end[i] + sample)) continue; + int found = 0; + for (int j = 0; !found && j < s->n_ending; j++) found = s->ending[j] == s->regions.end[i] && s->ending_at[j] < s->given_at[i]; + if (!found) { + fprintf(stderr, "FAIL: the region to %.6f s was given before its speech was said to have stopped there\n", s->regions.end[i]); + return 0; + } + } return 1; } @@ -264,7 +286,7 @@ static int check_refusals(speech_model * model, const float * x, size_t n, int r expect(speech_detect(r), SPEECH_OK, NULL, "the request a detection was started from"); speech_request_free(r); speech_detection_free(NULL); - ok &= speech_detection_region_count(NULL) == 0 && speech_detection_speaking(NULL, NULL, NULL) == 0; + ok &= speech_detection_region_count(NULL) == 0 && speech_detection_speaking(NULL, NULL, NULL) == 0 && speech_detection_ending(NULL, NULL) == 0; return ok ? 0 : 1; } @@ -341,7 +363,8 @@ int check_detection_stream(speech_model * model, float * const * audio, const si if (!stream(model, x, n, rates[k], set, kind, &got)) return 1; streams++; const int same = same_regions(&want, &got.regions); - const int said = said_well(&got, n, min_speech == 0, kind == 0 && min_silence >= 2 * pad && set->max_speech_duration_s < 0); + const int said = said_well(&got, n, min_speech == 0, kind == 0 && min_silence >= 2 * pad && set->max_speech_duration_s < 0, + kind == 0 && set->max_speech_duration_s < 0, 1.0 / rate); if (!same || !said) { fprintf(stderr, "FAIL: %s at %d Hz, %s, pieces of %s: %s\n", names[d], rates[k], set->name, PIECES[kind], same ? "what it said disagrees with its regions" : "the regions differ from speech_detect()'s"); diff --git a/docs/adr/a-detection-of-audio-given-a-piece-at-a-time-gives-speech-detects-regions-each-once-it-is-certain.md b/docs/adr/a-detection-of-audio-given-a-piece-at-a-time-gives-speech-detects-regions-each-once-it-is-certain.md index 108c164f..8520d7a7 100644 --- a/docs/adr/a-detection-of-audio-given-a-piece-at-a-time-gives-speech-detects-regions-each-once-it-is-certain.md +++ b/docs/adr/a-detection-of-audio-given-a-piece-at-a-time-gives-speech-detects-regions-each-once-it-is-certain.md @@ -46,6 +46,13 @@ with the number of chunks its graph held (Apple M5, 2026-10-08). after the silence that ends it, so that a caller can say someone speaks long before it can give the utterance, and never for a noise it then drops. `speech_detection_speaking()` gives it beside the start, in one call that reads one region: whether a region has begun, its start, and whether it is kept. +- **The end a region would take is said once its speech has stopped**, so that a caller can start on the region's audio + while the silence that ends it goes on: `speech_detection_ending()` gives, for the region `speech_detection_speaking()` + reads, the end it is given with if no speech starts again, once its probability has fallen below the threshold less + 0.15 without rising above the threshold since and the region would be long enough to be given there, or, for a + region whose end waits, while no region has begun after it. It is a function of its own, added to the API's minor + version, where an output added to `speech_detection_speaking()` would change the call for every caller; no call + between the two changes the region they read. - **The library resamples the pieces** with the filter of the whole audio ([the record of resampling](the-library-resamples-input-audio-with-torchaudios-kaiser-best-filter-and-drama3-references-as-fish-speech-does.md)), which holds back the half of the filter after the last sample until more arrives: 4.2 to 4.3 ms from 24, 32, 44.1 or 48 kHz to diff --git a/docs/adr/live-transcription-cuts-utterances-by-the-detections-regions-and-sends-as-deltas-the-text-two-readings-agree-on.md b/docs/adr/live-transcription-cuts-utterances-by-the-detections-regions-and-sends-as-deltas-the-text-two-readings-agree-on.md index f89be8d1..5caa1651 100644 --- a/docs/adr/live-transcription-cuts-utterances-by-the-detections-regions-and-sends-as-deltas-the-text-two-readings-agree-on.md +++ b/docs/adr/live-transcription-cuts-utterances-by-the-detections-regions-and-sends-as-deltas-the-text-two-readings-agree-on.md @@ -46,6 +46,13 @@ on the CPU with one thread, 0.17 s with the performance cores and 0.52 s on Meta 0.2 s of new audio has come, the next starts at once, so readings never pile up. A committed utterance is recognized before any reading, and a reading under way when its utterance ends is stopped with `speech_request_cancel()`. 0.2 s, because the audio arrives every 0.1 to 0.2 s and the detection works in chunks of 32 ms. +- **While the silence that would end an utterance goes on, one reading reads it to the end it would take, and its + commit takes that reading.** Once the detection says the speech has stopped (`speech_detection_ending()`), a reading + reads the utterance to the end its region takes if the silence lasts, its padding included, as soon as that audio is + there, and no reading reads the silence after it. A commit of a region of the same samples completes with that + reading's text in place of a recognition of its own: the samples, the request and the draft, the reading before, are + those the commit's recognition would have had. Where speech starts again, it is a reading like any other. An + utterance enhanced before its recognition is recognized after its commit, as the reading hears it unenhanced. - **Each recognition takes its place among the host's requests as it is accepted**: a committed utterance's when it is committed, and a reading's when it starts, with the assembly's lock held, so that a commit accepted before the reading runs before it and one accepted after it runs after it. A reading takes no place while it waits for audio, since it @@ -77,6 +84,12 @@ The alternatives were turned down: which the assembly could tell without the detection's word: 0.6 s later, and still wrong where the probability stays between the two thresholds, which keeps a region open without lengthening its speech. - Readings at a fixed interval. It wastes time on a fast machine and falls behind on a slow one. +- The commit's recognition run only once the utterance is committed. The audio of a region is all there once the + silence after its speech has lasted the padding it keeps, 300 ms with the defaults, and the readings meanwhile read + the silence beyond it; the recognition, 0.1 s with Qwen3-ASR 0.6B and 0.25 s with 1.7B on an Apple M5 (below), waited + for the commit, 200 ms later. +- The last reading's text as the completed transcript. Its audio runs into the silence beyond the region's end, so its + text is not the recognition of the region's samples, and could differ from a file's. - Each reading's whole text as it comes, or the difference from the reading before. A delta cannot take back text, and each reading rewrites the end of the one before, which the end of its audio cut. - One delta of the whole text once the utterance is recognized, as manual commits had. No text comes while someone @@ -105,13 +118,15 @@ sessions of five clips per language in each of two rounds, sent over `/v1/realti pieces of 100 ms as they would be said, Qwen3-ASR Q8_0 on Metal and Silero VAD on the CPU; the medians of the utterances the detection did not cut, over Japanese and English, from the end of speech: -| | Silero VAD's padding | the tools' padding | -|---|---|---| -| commit | 0.67 to 0.69 s | 0.58 to 0.60 s | -| completed, Qwen3-ASR 0.6B | 0.79 to 0.80 s | 0.69 to 0.73 s | -| completed, Qwen3-ASR 1.7B | 0.96 to 1.00 s | 0.84 to 0.85 s | +| | Silero VAD's padding | the tools' padding | and the reading before the commit | +|---|---|---|---| +| commit | 0.67 to 0.69 s | 0.58 to 0.60 s | 0.58 to 0.60 s | +| completed, Qwen3-ASR 0.6B | 0.79 to 0.80 s | 0.69 to 0.73 s | 0.58 to 0.59 s | +| completed, Qwen3-ASR 1.7B | 0.96 to 1.00 s | 0.84 to 0.85 s | 0.62 to 0.66 s | -Every completed transcript was the recognition of the same samples sent as a file afterwards. +Every completed transcript was the recognition of the same samples sent as a file afterwards, 58 for each model and +each column. With the reading before the commit, the 0.6B model completes as the utterance is committed, and the 1.7B, +whose recognition takes longer than the 200 ms between the end of an utterance's audio and its commit, soon after. A beginning that two readings agree on and a later reading changes stays in the deltas until the completed event replaces it, and later readings add nothing to them; where the final text goes on from what they gave, one more delta diff --git a/docs/c-api.md b/docs/c-api.md index db46be43..910b8123 100644 --- a/docs/c-api.md +++ b/docs/c-api.md @@ -170,8 +170,8 @@ and link `speech.lib`. Inside this CMake project, link the target `speech` (shar before it runs returns at once. Either returns `SPEECH_CANCELLED`. - **Detections of audio that arrives.** `speech_detection_start()` starts a detection with a request's options and the rate of the audio to come; `speech_detection_push()` gives it samples, `speech_detection_end()` ends the audio, and - `speech_detection_region_count()`, `speech_detection_region()` and `speech_detection_speaking()` read what it has found - so far ([below](#detections)). + `speech_detection_region_count()`, `speech_detection_region()`, `speech_detection_speaking()` and + `speech_detection_ending()` read what it has found so far ([below](#detections)). - **Diarizations of audio that arrives.** `speech_diarization_start()`, `speech_diarization_push()` and `speech_diarization_end()` do the same with a diarization model, and `speech_diarization_segment_count()`, `speech_diarization_segment()`, `speech_diarization_settled()` and `speech_diarization_speaking()` read what it has @@ -239,6 +239,11 @@ A detection finds the regions of audio that arrives a piece at a time, with a de `min_speech_duration_ms` makes no region, so the region under way is kept once its speech has lasted longer than that without falling silent: with the defaults, about 250 ms after it began, where a region is given only after the silence that ends it. +- **`speech_detection_ending()`** (API 4.3) says whether the speech of that region has stopped, and the end it is given + with if no speech starts again: where someone stopped speaking and the region is long enough to be given. A caller can + start on the region's audio while the silence that ends it goes on, as `speech serve` and `speech asr -` recognize an + utterance before it is committed. Speech that starts again within the silence, a region that starts within twice + `speech_pad_ms` and a cut at `max_speech_duration_s` give the region another end. - **Any rate.** The audio is resampled to the model's rate a piece at a time, which holds back the last 4.3 ms of the audio pushed from 24, 44.1 or 48 kHz (8.6 ms from 8 kHz) until more arrives. The model computes a chunk (32 ms for Silero VAD) once its samples have arrived. diff --git a/docs/cli.md b/docs/cli.md index c2e98f0b..8caaf6e0 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -234,7 +234,8 @@ speech asr qwen3-asr-0.6b --vad silero-vad --live --format json > events.jsonl `conversation.item.input_audio_transcription.delta`, text that adds to the end of what came before, and `conversation.item.input_audio_transcription.completed`, whose `transcript` is the whole text and replaces the deltas. - On an Apple M5, speech started comes 0.26 s after the speech begins, the first text 0.5 to 1.5 s after it, and the - final text 0.7 s (Qwen3-ASR 0.6B) to 0.85 s (1.7B) after the speech ends, in the median with the defaults. + final text 0.6 s (Qwen3-ASR 0.6B) to 0.65 s (1.7B) after the speech ends, in the median with the defaults: the + utterance is recognized while the silence that ends it goes on. - stderr reports the microphone and its rate, and at the end the utterances and the time the detection took; `-v` also reports each utterance as it ends. A recognition that fails ends the run with exit 1, after the `failed` event in JSON. - With `--diarize`, the diarization hears the audio as it arrives, in its `low_latency` mode unless `--streaming-mode` diff --git a/docs/server.md b/docs/server.md index 42b381df..53ed3c76 100644 --- a/docs/server.md +++ b/docs/server.md @@ -320,7 +320,10 @@ texts as `chunking_strategy` with the same values. A server without a detection An utterance is at most 10 s long, cut at its longest silence. `speech_started` comes about 0.26 s after the speech begins, once it has lasted long enough to be kept, and the utterance is committed once `silence_duration_ms` of -silence has followed it, within the detection's 32 ms. A shorter `silence_duration_ms` gives the text sooner, and cuts +silence has followed it, within the detection's 32 ms. Its audio is all there once the silence has lasted the padding +it keeps after the speech, and the server recognizes it then, so that the `completed` follows the commit at once where +that recognition takes less than the rest of the silence (200 ms with the defaults); where speech starts again, the +utterance goes on. A shorter `silence_duration_ms` gives the text sooner, and cuts sentences at shorter pauses: below about 200 ms, an utterance ends at nearly every pause, and a stretch of speech shorter than 250 ms between two of them is dropped. The buffer keeps only the utterance under way, or the last moments of silence, so a session can stay open through silence; a diff --git a/include/speech.h b/include/speech.h index 7288a39e..e08d8146 100644 --- a/include/speech.h +++ b/include/speech.h @@ -62,7 +62,7 @@ extern "C" { * version and a minor version of m or more. Version 4.0 is first released with speech.cpp 0.10.0, and a declaration * added since says the version that added it. */ -#define SPEECH_API_VERSION_MINOR 2 +#define SPEECH_API_VERSION_MINOR 3 /** The SPEECH_API_VERSION_MAJOR the library was built with, for a caller that loads it at run time. */ SPEECH_API int speech_api_version_major(void); @@ -1267,6 +1267,17 @@ SPEECH_API speech_status speech_detection_region(const speech_detection * detect */ SPEECH_API int speech_detection_speaking(const speech_detection * detection, double * start, int * kept); +/** + * Whether the speech of the region speech_detection_speaking() reports has stopped (1), with in `*end`, unless NULL, the + * end in seconds it is given with if no speech starts again: for the region under way, once its probability has fallen + * below the threshold less 0.15 (0.01 at least) and not risen above the threshold since, where it is longer than + * min_speech_duration_ms; for a region whose end waits on what follows, while no region has begun after it. The region + * takes another end where a region starts within twice speech_pad_ms of it, max_speech_duration_s cuts it elsewhere or + * the audio ends first. A caller can start on the region's audio while the silence that ends it goes on. 0 while someone + * speaks or no region has begun, and after speech_detection_end(). Added in 4.3. + */ +SPEECH_API int speech_detection_ending(const speech_detection * detection, double * end); + /* * Diarizations of audio that arrives a piece at a time, as from a microphone or a meeting's call, for a diarization model: * the segments of each speaker, each given once nothing that follows can change it. A diarization keeps its speaker cache diff --git a/src/api/detection.cpp b/src/api/detection.cpp index c58c4c98..c4189851 100644 --- a/src/api/detection.cpp +++ b/src/api/detection.cpp @@ -142,4 +142,12 @@ int speech_detection_speaking(const speech_detection * detection, double * start return open ? 1 : 0; } +int speech_detection_ending(const speech_detection * detection, double * end) { + if (!detection) return 0; + const std::optional open = detection->stream->open(); + if (!open || !open->ending) return 0; + if (end) *end = *open->ending; + return 1; +} + } // extern "C" diff --git a/src/engines/engine.h b/src/engines/engine.h index acba96b2..3838e5d8 100644 --- a/src/engines/engine.h +++ b/src/engines/engine.h @@ -206,10 +206,14 @@ class DetectionStream { virtual const std::vector & regions() const = 0; - /** A region begun and not given yet: its start in seconds, and whether it is certain to be given. */ + /** + * A region begun and not given yet: its start in seconds, whether it is certain to be given, and, once its speech has + * stopped, the end it is given with unless speech starts again. + */ struct Begun { double start; bool kept; + std::optional ending; }; /** diff --git a/src/engines/silero-vad-engine.cpp b/src/engines/silero-vad-engine.cpp index ad2e35c2..0f9d9708 100644 --- a/src/engines/silero-vad-engine.cpp +++ b/src/engines/silero-vad-engine.cpp @@ -55,7 +55,9 @@ class SileroVadStream : public DetectionStream { std::optional open() const override { const std::optional begun = rule_.open(); if (!begun) return std::nullopt; - return Begun{(double) begun->start / sample_rate_, begun->kept}; + std::optional ending; + if (begun->ending) ending = (double) *begun->ending / sample_rate_; + return Begun{(double) begun->start / sample_rate_, begun->kept, ending}; } private: diff --git a/src/families/silero-vad/regions.cpp b/src/families/silero-vad/regions.cpp index 58083e28..c8caeb97 100644 --- a/src/families/silero-vad/regions.cpp +++ b/src/families/silero-vad/regions.cpp @@ -111,8 +111,17 @@ void RegionStream::end(int64_t samples) { } std::optional RegionStream::open() const { - if (pending_) return Begun{pending_->start, true}; - if (triggered_) return Begun{padded_start(start_), (double) (earliest_end() - start_) > min_speech_}; + // Both endings are the end padded_end() gives where no region follows within twice the padding. + if (pending_) { + std::optional ending; + if (!triggered_) ending = (int64_t) ((double) pending_->end + pad_); + return Begun{pending_->start, true, ending}; + } + if (triggered_) { + std::optional ending; + if (temp_end_ != 0 && (double) (temp_end_ - start_) > min_speech_) ending = (int64_t) ((double) temp_end_ + pad_); + return Begun{padded_start(start_), (double) (earliest_end() - start_) > min_speech_, ending}; + } return std::nullopt; } diff --git a/src/families/silero-vad/regions.h b/src/families/silero-vad/regions.h index efd981c3..2629b58e 100644 --- a/src/families/silero-vad/regions.h +++ b/src/families/silero-vad/regions.h @@ -32,10 +32,14 @@ struct Region { int64_t start, end; }; -/** A region begun and not given yet: its padded start, and whether it is certain to be given. */ +/** + * A region begun and not given yet: its padded start, whether it is certain to be given, and, once its speech has + * stopped where it would end long enough to be given, the padded end it takes unless speech starts again. + */ struct Begun { int64_t start; bool kept; + std::optional ending; }; /** @@ -79,6 +83,12 @@ class RegionStream { * under way, kept once no end it can still take leaves it no longer than min_speech_duration_ms; none when there is * neither. A region under way not kept is dropped if its speech ends that soon, and no region is then given from its * start. With what settle() or end() was last told of the audio heard. + * + * Its ending is the end it is given with where no region starts within twice speech_pad_ms of its unpadded end, the + * silence after it lasts min_silence_duration_ms, max_speech_duration_s does not cut it elsewhere and the audio does + * not end first: for the region under way, where its probability fell below neg_threshold and has not risen above the + * threshold since, once it is longer than min_speech_duration_ms there; for the region whose end waits, its end + * while no region has begun after it. */ std::optional open() const; diff --git a/tools/cli/asr-stream.cpp b/tools/cli/asr-stream.cpp index ccaf32e3..526629dc 100644 --- a/tools/cli/asr-stream.cpp +++ b/tools/cli/asr-stream.cpp @@ -91,7 +91,9 @@ class ModelRecognizer : public utterances::Recognizer { if (check(cancellation.run(request.get(), [&] { return speech_transcribe(request.get()); })) == SPEECH_CANCELLED) return std::nullopt; std::lock_guard lock(mutex_); if (provisional) readings_++; - else took_[utterance] = seconds_since(t0); + // An utterance's last recognition is the one it completes with: its own, or the reading of the same samples that + // ran while the silence that ended it went on. + took_[utterance] = seconds_since(t0); return transcript_of(speech_request_result(request.get()), false); } diff --git a/tools/common/utterances.cpp b/tools/common/utterances.cpp index 4e38e846..6b5cb095 100644 --- a/tools/common/utterances.cpp +++ b/tools/common/utterances.cpp @@ -80,6 +80,12 @@ std::optional> Detection::speaking() const { return std::make_pair(start, kept != 0); } +std::optional Detection::ending() const { + double end = 0; + if (!speech_detection_ending(detection_.get(), &end)) return std::nullopt; + return end; +} + Assembly::Assembly(Recognizer & recognizer, int rate, std::function emit, Enhancer * enhancer) : recognizer_(recognizer), enhancer_(enhancer), rate_(rate), emit_(std::move(emit)) { worker_ = std::thread([this] { work(); }); @@ -101,9 +107,11 @@ Assembly::~Assembly() { void Assembly::ask(std::optional asked) { std::lock_guard lock(mutex_); asked_ = std::move(asked); - // Readings asked otherwise would not agree as two readings of the same request do. + // Readings asked otherwise would not agree as two readings of the same request do, and would not be the recognition + // a commit asks for. if (reading_) reading_->cancel(); if (current_) current_->last_reading.clear(); + ending_.reset(); changed_.notify_all(); } @@ -200,6 +208,7 @@ void Assembly::clear() { std::lock_guard lock(mutex_); if (reading_) reading_->cancel(); current_.reset(); + ending_.reset(); keep_from(heard_); if (detection_) restart_detection(); } @@ -253,7 +262,10 @@ void Assembly::take_regions(bool ended) { if (speaking->second) next = speaking->first; else unsure = true; } - if (!ended && (unsure || (!next && length - speech.end < cut.silence()))) return; + if (!ended && (unsure || (!next && length - speech.end < cut.silence()))) { + if (current_) current_->ends_at = 0; + return; + } const Region region = cut.pad(speech, from_, next, length); held_.pop_front(); from_ = region.end; @@ -280,6 +292,10 @@ void Assembly::take_regions(bool ended) { if (!current_) current_ = Utterance{next_++, start, false, start, "", "", ""}; keep_from(start); if (speaking->second) announce(*current_); + // The end the region takes where the silence after its speech lasts, as RegionCut pads it, which a reading reads to + // once its audio is there; the sample is the one at() gives the region's end once the audio holds it. + const std::optional ending = current_->announced ? detection_->ending() : std::nullopt; + current_->ends_at = ending ? origin_ + region_samples(Region{*ending + cut.after(), 0}, rate_, SIZE_MAX).first : 0; } void Assembly::announce(Utterance & u) { @@ -309,12 +325,16 @@ void Assembly::commit_samples(uint64_t number, uint64_t first, uint64_t last) { committed.utterance = number; committed.recognized = asked_.has_value(); send(committed); - if (reading_) reading_->cancel(); + // The reading of the same samples, under way or ended, completes the commit; a reading of other samples stops. + std::shared_ptr ending = std::move(ending_); + if (ending && !(ending->number == number && ending->first == first && ending->last == last)) ending.reset(); + if (reading_ && !(ending && !ending->done)) reading_->cancel(); if (asked_) { Final f; f.number = number; f.first = first; f.last = last; + f.ending = std::move(ending); f.diarized = diarized_; if (current_ && current_->number == number) { f.sent = current_->sent; @@ -371,6 +391,7 @@ void Assembly::restart_detection() { regions_ = 0; held_.clear(); from_ = 0; + ending_.reset(); } void Assembly::begin_manual_utterance() { @@ -379,7 +400,11 @@ void Assembly::begin_manual_utterance() { } bool Assembly::reading_due() const { - return asked_ && current_ && finals_.empty() && heard_ >= current_->read_to + (uint64_t) (kReadAgainAfter * rate_); + if (!asked_ || !current_ || !finals_.empty()) return false; + // While the silence after the speech goes on, one reading reads the utterance to where that silence would end it, + // once the audio is there, and none reads the silence after it. + if (current_->ends_at) return heard_ >= current_->ends_at && current_->read_to != current_->ends_at; + return heard_ >= current_->read_to + (uint64_t) (kReadAgainAfter * rate_); } void Assembly::send(const Event & e) { @@ -423,14 +448,17 @@ void Assembly::work() { e.utterance = f.number; try { if (f.refused) std::rethrow_exception(f.refused); - std::optional, int>> audio(std::make_pair(std::move(f.samples), rate_)); - if (f.enhancement) { - audio = enhancer_->enhance(*f.enhancement, std::move(audio->first), rate_, *cancellation); - // The enhancement's place passes as soon as it has ended. - f.enhancement.reset(); + // This thread ran the reading that completes it before it took the commit, which came after. + std::optional t = f.ending ? f.ending->transcript : std::nullopt; + if (!t) { + std::optional, int>> audio(std::make_pair(std::move(f.samples), rate_)); + if (f.enhancement) { + audio = enhancer_->enhance(*f.enhancement, std::move(audio->first), rate_, *cancellation); + // The enhancement's place passes as soon as it has ended. + f.enhancement.reset(); + } + if (audio) t = recognizer_.transcribe(*f.reservation, f.asked, std::move(audio->first), audio->second, *cancellation, f.number, false, f.draft); } - std::optional t; - if (audio) t = recognizer_.transcribe(*f.reservation, f.asked, std::move(audio->first), audio->second, *cancellation, f.number, false, f.draft); if (t) { e.kind = Event::Kind::Completed; e.transcript = std::move(*t); @@ -468,7 +496,8 @@ void Assembly::work() { continue; } const uint64_t number = current_->number; - current_->read_to = heard_; + const uint64_t to = current_->ends_at ? current_->ends_at : heard_; + current_->read_to = to; const Asked asked = *asked_; // A reading takes its place once it is due, with the mutex held, so that a commit accepted before it runs first, // and one accepted after it, which this thread recognizes next, after it. @@ -480,9 +509,13 @@ void Assembly::work() { busy_ = false; continue; } - std::vector samples(audio_.begin() + (long) (current_->start - buffer_), audio_.end()); + std::vector samples(audio_.begin() + (long) (current_->start - buffer_), audio_.begin() + (long) (to - buffer_)); // Each reading reads the utterance from its start again, and the reading before is the likeliest text of this one. const std::string draft = current_->last_reading; + // A reading to where the silence would end the utterance is the recognition its commit would run, unless the + // utterance is enhanced before its recognition. + std::shared_ptr ending; + if (current_->ends_at && !asked.enhance) ending = ending_ = std::make_shared(Ending{number, current_->start, to}); const std::shared_ptr cancellation = reading_ = std::make_shared(); lock.unlock(); std::optional t; @@ -495,6 +528,10 @@ void Assembly::work() { lock.lock(); reading_.reset(); busy_ = false; + if (ending) { + ending->done = true; + if (!cancellation->cancelled()) ending->transcript = t; + } changed_.notify_all(); if (!t || cancellation->cancelled() || !current_ || current_->number != number) continue; current_->agreed = agreed_beginning(current_->last_reading, t->text); diff --git a/tools/common/utterances.h b/tools/common/utterances.h index 2bd94dbd..4c7e05d5 100644 --- a/tools/common/utterances.h +++ b/tools/common/utterances.h @@ -141,6 +141,8 @@ class Detection { * speech_detection_speaking() gives them, or nothing. */ std::optional> speaking() const; + /** Where the speech of the region begun has stopped, unless speech starts again, as speech_detection_ending() gives it. */ + std::optional ending() const; /** Starts again, on the audio that follows, with the same model and options. */ void restart(); @@ -230,9 +232,25 @@ class Assembly { std::string last_reading; std::string agreed; std::string sent; + /** + * The sample after its last where the detection says its speech has stopped and the silence under way would end + * it, or 0 while someone speaks. + */ + uint64_t ends_at = 0; + }; + + /** + * A reading of the utterance under way to where the silence under way would end it, which runs while that silence + * goes on: the commit of a region of the same samples takes its text as its recognition, since a recognition of those + * samples, with the reading before as its draft, is what the commit would run. Its utterance, its first sample and + * the sample after its last, and once it has ended, its text, or nothing where it failed or stopped. + */ + struct Ending { + uint64_t number = 0, first = 0, last = 0; + bool done = false; + std::optional transcript; }; - /** A committed utterance that waits for its recognition, with its place or what refused it one. */ /** A diarization as the utterances committed while it goes on read it. */ struct Diarized { std::shared_ptr diarization; @@ -241,6 +259,7 @@ class Assembly { int first_speaker = 0; }; + /** A committed utterance that waits for its recognition, with its place or what refused it one. */ struct Final { uint64_t number = 0; /** Its first sample and the sample after its last, counted from the start of the audio. */ @@ -259,6 +278,8 @@ class Assembly { std::unique_ptr enhancement; std::unique_ptr reservation; std::exception_ptr refused; + /** The reading of its samples that completes it where it gives a text, its place then passing unused. */ + std::shared_ptr ending; }; void work(); @@ -327,6 +348,8 @@ class Assembly { /** Whether the worker recognizes, an utterance or the beginning of one. */ bool busy_ = false; std::shared_ptr reading_, final_; + /** The reading of the utterance under way to where the silence under way would end it, begun or ended. */ + std::shared_ptr ending_; bool closing_ = false; std::thread worker_; };