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
259
260
261
|
/* Command line front end: reads a wav, prints speech segments.
*
* Default output is the offline segmentation, which is what the upstream
* get_speech_timestamps produces. --stream instead prints events at the moment
* the streaming segmenter decides them, which is what a live pipeline sees. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/resource.h>
#include <time.h>
#include "svad.h"
#include "wav.h"
static double now_sec(void) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
}
/* ru_maxrss is bytes on Darwin and kilobytes elsewhere. */
static double max_rss_mib(void) {
struct rusage ru;
getrusage(RUSAGE_SELF, &ru);
#if defined(__APPLE__)
return (double)ru.ru_maxrss / (1024.0 * 1024.0);
#else
return (double)ru.ru_maxrss / 1024.0;
#endif
}
static void usage(FILE *f) {
fputs("usage: svad [--stream] [--probs FILE] [--json] [--quiet] "
"[--threshold X] [--neg-threshold X] [--min-speech-ms N] "
"[--max-speech-s X] [--min-silence-ms N] [--speech-pad-ms N] "
"[--weights FILE] [--scalar] [--dense-stft] [--no-ftz] INPUT.wav\n",
f);
}
static int arg_int(int argc, char **argv, int *i, const char *name, long *out) {
if (++*i >= argc) {
fprintf(stderr, "svad: %s needs a value\n", name);
return -1;
}
char *end;
*out = strtol(argv[*i], &end, 10);
if (*end) {
fprintf(stderr, "svad: %s: bad integer '%s'\n", name, argv[*i]);
return -1;
}
return 0;
}
static int arg_dbl(int argc, char **argv, int *i, const char *name, double *out) {
if (++*i >= argc) {
fprintf(stderr, "svad: %s needs a value\n", name);
return -1;
}
char *end;
*out = strtod(argv[*i], &end);
if (*end) {
fprintf(stderr, "svad: %s: bad number '%s'\n", name, argv[*i]);
return -1;
}
return 0;
}
int main(int argc, char **argv) {
const char *wav_path = NULL, *probs_path = NULL, *weights_path = NULL;
int stream = 0, json = 0, quiet = 0;
unsigned flags = 0;
svad_segment_params sp;
svad_segment_params_default(&sp);
long lv;
double dv;
for (int i = 1; i < argc; ++i) {
const char *a = argv[i];
if (strcmp(a, "-h") == 0 || strcmp(a, "--help") == 0) {
usage(stdout);
return 0;
} else if (strcmp(a, "--stream") == 0) {
stream = 1;
} else if (strcmp(a, "--json") == 0) {
json = 1;
} else if (strcmp(a, "--quiet") == 0) {
quiet = 1;
} else if (strcmp(a, "--scalar") == 0) {
flags |= SVAD_SCALAR_KERNELS;
} else if (strcmp(a, "--dense-stft") == 0) {
flags |= SVAD_DENSE_STFT;
} else if (strcmp(a, "--no-ftz") == 0) {
flags |= SVAD_NO_FTZ;
} else if (strcmp(a, "--probs") == 0 && i + 1 < argc) {
probs_path = argv[++i];
} else if (strcmp(a, "--weights") == 0 && i + 1 < argc) {
weights_path = argv[++i];
} else if (strcmp(a, "--threshold") == 0) {
if (arg_dbl(argc, argv, &i, a, &dv))
return 2;
sp.threshold = (float)dv;
} else if (strcmp(a, "--neg-threshold") == 0) {
if (arg_dbl(argc, argv, &i, a, &dv))
return 2;
sp.neg_threshold = (float)dv;
} else if (strcmp(a, "--max-speech-s") == 0) {
if (arg_dbl(argc, argv, &i, a, &dv))
return 2;
sp.max_speech_s = dv;
} else if (strcmp(a, "--min-speech-ms") == 0) {
if (arg_int(argc, argv, &i, a, &lv))
return 2;
sp.min_speech_ms = (int)lv;
} else if (strcmp(a, "--min-silence-ms") == 0) {
if (arg_int(argc, argv, &i, a, &lv))
return 2;
sp.min_silence_ms = (int)lv;
} else if (strcmp(a, "--speech-pad-ms") == 0) {
if (arg_int(argc, argv, &i, a, &lv))
return 2;
sp.speech_pad_ms = (int)lv;
} else if (a[0] == '-' && a[1]) {
fprintf(stderr, "svad: unknown option '%s'\n", a);
usage(stderr);
return 2;
} else if (!wav_path) {
wav_path = a;
} else {
fprintf(stderr, "svad: unexpected argument '%s'\n", a);
return 2;
}
}
if (!wav_path) {
usage(stderr);
return 2;
}
wav_t wav;
const char *err = NULL;
if (wav_read_mono_s16(wav_path, &wav, &err) != 0) {
fprintf(stderr, "svad: %s: %s\n", wav_path, err ? err : "read failed");
return 1;
}
if (wav.sample_rate != 8000 && wav.sample_rate != 16000) {
fprintf(stderr, "svad: %s: %d Hz; only 8000 and 16000 are supported\n", wav_path,
wav.sample_rate);
wav_free(&wav);
return 1;
}
const double t_init0 = now_sec();
svad_t *v = svad_create_ex(weights_path, wav.sample_rate, flags);
const double init_ms = (now_sec() - t_init0) * 1e3;
if (!v) {
fprintf(stderr, "svad: initialization failed%s\n", weights_path ? " (bad weight file?)" : "");
wav_free(&wav);
return 1;
}
/* A trailing partial chunk is zero-padded and processed, matching the
* reference implementation's chunking of the same file. */
const int chunk = svad_chunk_samples(v);
const size_t n_chunks = (wav.count + (size_t)chunk - 1) / (size_t)chunk;
float *probs = malloc((n_chunks ? n_chunks : 1) * sizeof(float));
if (!probs) {
fprintf(stderr, "svad: out of memory\n");
svad_destroy(v);
wav_free(&wav);
return 1;
}
svad_segmenter seg;
svad_segmenter_init(&seg, wav.sample_rate);
seg.threshold = sp.threshold;
if (sp.neg_threshold > 0.0f)
seg.neg_threshold = sp.neg_threshold;
else
seg.neg_threshold = sp.threshold - 0.15f;
seg.min_silence_samples = (int64_t)wav.sample_rate * sp.min_silence_ms / 1000;
seg.speech_pad_samples = (int64_t)wav.sample_rate * sp.speech_pad_ms / 1000;
int16_t tail[512] = {0};
const double t0 = now_sec();
for (size_t i = 0; i < n_chunks; ++i) {
const size_t off = i * (size_t)chunk;
const int16_t *src = wav.samples + off;
if (off + (size_t)chunk > wav.count) {
memset(tail, 0, sizeof tail);
memcpy(tail, src, (wav.count - off) * sizeof(int16_t));
src = tail;
}
probs[i] = svad_process_s16(v, src);
if (stream) {
const svad_event ev = svad_segmenter_update(&seg, probs[i]);
if (ev.kind == SVAD_EVENT_SPEECH_START)
printf("start %.3f\n", (double)ev.sample / wav.sample_rate);
else if (ev.kind == SVAD_EVENT_SPEECH_END)
printf("end %.3f\n", (double)ev.sample / wav.sample_rate);
}
}
const double elapsed = now_sec() - t0;
if (probs_path) {
FILE *pf = fopen(probs_path, "wb");
if (!pf || fwrite(probs, sizeof(float), n_chunks, pf) != n_chunks) {
fprintf(stderr, "svad: cannot write %s\n", probs_path);
if (pf)
fclose(pf);
free(probs);
svad_destroy(v);
wav_free(&wav);
return 1;
}
fclose(pf);
}
if (!stream) {
const size_t n = svad_speech_timestamps(probs, n_chunks, (int64_t)wav.count, wav.sample_rate,
&sp, NULL, 0);
svad_segment *segs = n ? malloc(n * sizeof *segs) : NULL;
if (n && !segs) {
fprintf(stderr, "svad: out of memory\n");
free(probs);
svad_destroy(v);
wav_free(&wav);
return 1;
}
svad_speech_timestamps(probs, n_chunks, (int64_t)wav.count, wav.sample_rate, &sp, segs, n);
if (json)
printf("[");
for (size_t i = 0; i < n; ++i) {
const double s = (double)segs[i].start / wav.sample_rate;
const double e = (double)segs[i].end / wav.sample_rate;
if (json)
printf("%s{\"start\":%.3f,\"end\":%.3f,\"start_sample\":%lld,\"end_sample\":%lld}",
i ? "," : "", s, e, (long long)segs[i].start, (long long)segs[i].end);
else
printf("%8.3f %8.3f %10lld %10lld\n", s, e, (long long)segs[i].start,
(long long)segs[i].end);
}
if (json)
printf("]\n");
free(segs);
}
if (!quiet) {
const double audio_s = (double)wav.count / wav.sample_rate;
fprintf(stderr,
"%s: %.2f s audio, %zu chunks, %s backend, init %.3f ms, "
"%.1f us/chunk, %.0fx realtime, max rss %.1f MiB\n",
wav_path, audio_s, n_chunks, svad_backend_name(v), init_ms,
n_chunks ? elapsed / (double)n_chunks * 1e6 : 0.0,
elapsed > 0 ? audio_s / elapsed : 0.0, max_rss_mib());
}
free(probs);
svad_destroy(v);
wav_free(&wav);
return 0;
}
|