aboutsummaryrefslogtreecommitdiff
path: root/tools/svad.c
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-10-08 09:02:54 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-10-08 09:02:54 +0200
commit978aa89541181c7e5b114589fac686a96702d371 (patch)
tree5dd902164af651c8029be43332c2fe6e932e674e /tools/svad.c
parenta5af9c3d351085f4005a949f8c382a3aad4e0b29 (diff)
downloadsvad-master.tar.gz
svad-master.zip
Add command-line toolHEADv0.1.1master
Diffstat (limited to 'tools/svad.c')
-rw-r--r--tools/svad.c261
1 files changed, 261 insertions, 0 deletions
diff --git a/tools/svad.c b/tools/svad.c
new file mode 100644
index 0000000..d9912d7
--- /dev/null
+++ b/tools/svad.c
@@ -0,0 +1,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;
+}