aboutsummaryrefslogtreecommitdiff
path: root/segment.c
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-10-08 06:49:33 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-10-08 07:21:37 +0200
commita5af9c3d351085f4005a949f8c382a3aad4e0b29 (patch)
tree0ad133d931553e68a68588e983976a66b3309083 /segment.c
downloadsvad-a5af9c3d351085f4005a949f8c382a3aad4e0b29.tar.gz
svad-a5af9c3d351085f4005a949f8c382a3aad4e0b29.zip
initv0.1.0
Diffstat (limited to 'segment.c')
-rw-r--r--segment.c258
1 files changed, 258 insertions, 0 deletions
diff --git a/segment.c b/segment.c
new file mode 100644
index 0000000..ae00771
--- /dev/null
+++ b/segment.c
@@ -0,0 +1,258 @@
+/* Turning speech probabilities into speech segments.
+ *
+ * Two ports of the upstream Python behaviour, both operating purely on the
+ * probability sequence:
+ *
+ * svad_segmenter_* mirrors VADIterator, emitting events as chunks
+ * arrive, with one window of look-behind;
+ * svad_speech_timestamps mirrors get_speech_timestamps, which sees the whole
+ * sequence and additionally drops short segments,
+ * splits over-long ones and pads the results.
+ *
+ * Both are integer arithmetic over the same thresholds, so given identical
+ * probabilities they reproduce the reference output exactly. temp_end == 0
+ * doubles as "unset" in the reference and is kept that way here. */
+
+#include "svad.h"
+
+#include <math.h>
+
+void svad_segmenter_init(svad_segmenter *s, int sample_rate) {
+ s->threshold = 0.5f;
+ s->neg_threshold = 0.5f - 0.15f;
+ s->min_silence_samples = (int64_t)sample_rate * 100 / 1000;
+ s->speech_pad_samples = (int64_t)sample_rate * 30 / 1000;
+ s->window_samples = sample_rate == 8000 ? 256 : 512;
+ svad_segmenter_reset(s);
+}
+
+void svad_segmenter_reset(svad_segmenter *s) {
+ s->triggered = 0;
+ s->current_sample = 0;
+ s->temp_end = 0;
+}
+
+svad_event svad_segmenter_update(svad_segmenter *s, float prob) {
+ svad_event ev = {SVAD_EVENT_NONE, 0};
+
+ s->current_sample += s->window_samples;
+
+ if (prob >= s->threshold && s->temp_end)
+ s->temp_end = 0;
+
+ if (prob >= s->threshold && !s->triggered) {
+ s->triggered = 1;
+ int64_t start = s->current_sample - s->speech_pad_samples - s->window_samples;
+ ev.kind = SVAD_EVENT_SPEECH_START;
+ ev.sample = start > 0 ? start : 0;
+ return ev;
+ }
+
+ if (prob < s->neg_threshold && s->triggered) {
+ if (!s->temp_end)
+ s->temp_end = s->current_sample;
+ if (s->current_sample - s->temp_end >= s->min_silence_samples) {
+ ev.kind = SVAD_EVENT_SPEECH_END;
+ ev.sample = s->temp_end + s->speech_pad_samples - s->window_samples;
+ s->temp_end = 0;
+ s->triggered = 0;
+ }
+ }
+ return ev;
+}
+
+/* ---- offline --------------------------------------------------------- */
+
+void svad_segment_params_default(svad_segment_params *p) {
+ p->threshold = 0.5f;
+ p->neg_threshold = 0.0f; /* derived */
+ p->min_speech_ms = 250;
+ p->max_speech_s = HUGE_VAL;
+ p->min_silence_ms = 100;
+ p->speech_pad_ms = 30;
+ p->lead_pad_ms = 0;
+ p->min_silence_at_max_speech_ms = 98;
+ p->use_max_poss_sil_at_max_speech = 1;
+}
+
+/* Scratch for the segment scan. `speeches` is written straight into the
+ * caller's array when it fits, and counted beyond that. */
+typedef struct {
+ svad_segment *out;
+ size_t cap;
+ size_t n;
+} seglist;
+
+static void seg_push(seglist *l, int64_t start, int64_t end) {
+ if (l->out && l->n < l->cap) {
+ l->out[l->n].start = start;
+ l->out[l->n].end = end;
+ }
+ ++l->n;
+}
+
+size_t svad_speech_timestamps(const float *probs,
+ size_t n_probs,
+ int64_t audio_length_samples,
+ int sample_rate,
+ const svad_segment_params *params,
+ svad_segment *out,
+ size_t out_cap) {
+ svad_segment_params def;
+ if (!params) {
+ svad_segment_params_default(&def);
+ params = &def;
+ }
+
+ const int64_t window = sample_rate == 8000 ? 256 : 512;
+ const float threshold = params->threshold;
+ float neg = params->neg_threshold;
+ if (!(neg > 0.0f)) {
+ neg = threshold - 0.15f;
+ if (neg < 0.01f)
+ neg = 0.01f;
+ }
+ const int64_t min_speech = (int64_t)sample_rate * params->min_speech_ms / 1000;
+ const int64_t pad = (int64_t)sample_rate * params->speech_pad_ms / 1000;
+ const int64_t min_silence = (int64_t)sample_rate * params->min_silence_ms / 1000;
+ const int64_t lead = (int64_t)sample_rate * params->lead_pad_ms / 1000;
+ const int64_t min_sil_max = (int64_t)sample_rate * params->min_silence_at_max_speech_ms / 1000;
+ const double max_speech =
+ params->max_speech_s * (double)sample_rate - (double)window - 2.0 * (double)pad;
+
+ seglist list = {out, out_cap, 0};
+ int triggered = 0, have_speech = 0;
+ int64_t cur_start = 0;
+ int64_t temp_end = 0, prev_end = 0, next_start = 0;
+ /* The reference keeps every candidate silence and later takes the first
+ * longest one; a running maximum with a strict comparison is the same
+ * selection without the list. */
+ int64_t best_end = 0, best_dur = 0;
+ int have_best = 0;
+
+ for (size_t i = 0; i < n_probs; ++i) {
+ const float p = probs[i];
+ const int64_t cur = window * (int64_t)i;
+
+ if (p >= threshold && temp_end) {
+ const int64_t sil = cur - temp_end;
+ if (sil > min_sil_max && (!have_best || sil > best_dur)) {
+ best_end = temp_end;
+ best_dur = sil;
+ have_best = 1;
+ }
+ temp_end = 0;
+ if (next_start < prev_end)
+ next_start = cur;
+ }
+
+ if (p >= threshold && !triggered) {
+ triggered = 1;
+ have_speech = 1;
+ cur_start = cur;
+ continue;
+ }
+
+ if (triggered && (double)(cur - cur_start) > max_speech) {
+ if (params->use_max_poss_sil_at_max_speech && have_best) {
+ prev_end = best_end;
+ seg_push(&list, cur_start, prev_end);
+ have_speech = 0;
+ next_start = prev_end + best_dur;
+ if (next_start < prev_end + cur) {
+ cur_start = next_start;
+ have_speech = 1;
+ } else {
+ triggered = 0;
+ }
+ prev_end = next_start = temp_end = 0;
+ have_best = 0;
+ best_dur = 0;
+ } else if (prev_end) {
+ seg_push(&list, cur_start, prev_end);
+ have_speech = 0;
+ if (next_start < prev_end) {
+ triggered = 0;
+ } else {
+ cur_start = next_start;
+ have_speech = 1;
+ }
+ prev_end = next_start = temp_end = 0;
+ have_best = 0;
+ best_dur = 0;
+ } else {
+ seg_push(&list, cur_start, cur);
+ have_speech = 0;
+ prev_end = next_start = temp_end = 0;
+ triggered = 0;
+ have_best = 0;
+ best_dur = 0;
+ continue;
+ }
+ }
+
+ if (p < neg && triggered) {
+ if (!temp_end)
+ temp_end = cur;
+ const int64_t sil_now = cur - temp_end;
+ if (!params->use_max_poss_sil_at_max_speech && sil_now > min_sil_max)
+ prev_end = temp_end;
+ if (sil_now < min_silence)
+ continue;
+ if (temp_end - cur_start > min_speech)
+ seg_push(&list, cur_start, temp_end);
+ have_speech = 0;
+ prev_end = next_start = temp_end = 0;
+ triggered = 0;
+ have_best = 0;
+ best_dur = 0;
+ }
+ }
+
+ if (have_speech && audio_length_samples - cur_start > min_speech)
+ seg_push(&list, cur_start, audio_length_samples);
+
+ /* Padding runs over the complete list, so it is applied only when every
+ * segment was written. A short buffer yields the count and nothing else. */
+ if (!out || list.n > out_cap)
+ return list.n;
+
+ /* Each step reads its neighbour's still-unmodified start, so one forward
+ * sweep reproduces the reference's in-place updates. Gaps are never
+ * negative, so truncating division matches the reference's floor. */
+ for (size_t i = 0; i < list.n; ++i) {
+ if (i == 0)
+ out[0].start = out[0].start > pad ? out[0].start - pad : 0;
+ if (i + 1 < list.n) {
+ const int64_t gap = out[i + 1].start - out[i].end;
+ int64_t shift = pad;
+ if (gap < 2 * pad) {
+ shift = gap / 2;
+ out[i].end += shift;
+ } else {
+ const int64_t e = out[i].end + pad;
+ out[i].end = e < audio_length_samples ? e : audio_length_samples;
+ }
+ const int64_t s = out[i + 1].start - shift;
+ out[i + 1].start = s > 0 ? s : 0;
+ } else {
+ const int64_t e = out[i].end + pad;
+ out[i].end = e < audio_length_samples ? e : audio_length_samples;
+ }
+ }
+
+ /* Reach further back for the opening of each segment. This runs after the
+ * padding pass so it cannot disturb how neighbours share a gap, and each
+ * start stops at the previous segment's end, so segments never overlap. */
+ if (lead > 0) {
+ for (size_t i = 0; i < list.n; ++i) {
+ int64_t s = out[i].start - lead;
+ if (s < 0)
+ s = 0;
+ if (i > 0 && s < out[i - 1].end)
+ s = out[i - 1].end;
+ out[i].start = s;
+ }
+ }
+ return list.n;
+}