aboutsummaryrefslogtreecommitdiff
path: root/segment.c
blob: ae00771c8035779db2b2db7fc2ac794e911fafb7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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;
}