Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions checks/api/speech-api-detection-stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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. */
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions docs/c-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
5 changes: 4 additions & 1 deletion docs/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 12 additions & 1 deletion include/speech.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/api/detection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<DetectionStream::Begun> open = detection->stream->open();
if (!open || !open->ending) return 0;
if (end) *end = *open->ending;
return 1;
}

} // extern "C"
6 changes: 5 additions & 1 deletion src/engines/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,14 @@ class DetectionStream {

virtual const std::vector<TimedText> & 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<double> ending;
};

/**
Expand Down
4 changes: 3 additions & 1 deletion src/engines/silero-vad-engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ class SileroVadStream : public DetectionStream {
std::optional<Begun> open() const override {
const std::optional<silero_vad::Begun> begun = rule_.open();
if (!begun) return std::nullopt;
return Begun{(double) begun->start / sample_rate_, begun->kept};
std::optional<double> ending;
if (begun->ending) ending = (double) *begun->ending / sample_rate_;
return Begun{(double) begun->start / sample_rate_, begun->kept, ending};
}

private:
Expand Down
Loading
Loading