aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--Makefile12
-rw-r--r--README.md33
-rw-r--r--tools/svad.c261
-rw-r--r--tools/wav.c138
-rw-r--r--tools/wav.h23
6 files changed, 461 insertions, 7 deletions
diff --git a/.gitignore b/.gitignore
index 89c6270..daa95cc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,5 @@
build
libsvad.a
+/svad
/weights_16k.h
/weights_8k.h
diff --git a/Makefile b/Makefile
index 0cfef55..73081b8 100644
--- a/Makefile
+++ b/Makefile
@@ -19,6 +19,7 @@ $(error MODE must be release, debug, or profile)
endif
CFLAGS ?= $(OPT_FLAGS) $(WARNING_FLAGS) -std=gnu11 $(ARCH_FLAGS)
ARFLAGS := rcs
+LDLIBS += -lm
SOURCES := kernels.c rfft.c segment.c svad.c weights.c
OBJECTS := $(patsubst %.c,$(OBJDIR)/%.o,$(SOURCES))
@@ -26,7 +27,7 @@ HEADERS := $(wildcard *.h)
.PHONY: all release debug profile clean FORCE
-all: $(TARGET)
+all: $(TARGET) svad
release:
$(MAKE) MODE=release all
@@ -41,6 +42,10 @@ $(TARGET): $(OBJECTS)
$(RM) $@
$(AR) $(ARFLAGS) $@ $^
+svad: tools/svad.c tools/wav.c tools/wav.h $(TARGET) $(BUILD_CONFIG)
+ $(CC) $(CPPFLAGS) $(CFLAGS) tools/svad.c tools/wav.c $(TARGET) \
+ $(LDFLAGS) $(LDLIBS) -o $@
+
$(OBJECTS): $(BUILD_CONFIG)
$(OBJECTS): $(OBJDIR)/%.o: %.c $(HEADERS) | build
@@ -50,7 +55,8 @@ $(OBJECTS): $(OBJDIR)/%.o: %.c $(HEADERS) | build
$(BUILD_CONFIG): FORCE | build
@printf '%s\n' 'MODE=$(MODE)' 'CC=$(CC)' \
'CPPFLAGS=$(CPPFLAGS)' 'CFLAGS=$(CFLAGS)' \
- 'ARCH_FLAGS=$(ARCH_FLAGS)' > $@.new
+ 'ARCH_FLAGS=$(ARCH_FLAGS)' 'LDFLAGS=$(LDFLAGS)' \
+ 'LDLIBS=$(LDLIBS)' > $@.new
@cmp -s $@.new $@ || cp $@.new $@
@rm -f $@.new
@@ -60,4 +66,4 @@ build:
mkdir -p $@
clean:
- rm -rf build $(TARGET)
+ rm -rf build $(TARGET) svad
diff --git a/README.md b/README.md
index 88f135e..0199fd5 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# svad
-Voice activity detection library using [Silero VAD](https://github.com/snakers4/silero-vad) v6.2 weights. Supports mono 16 kHz and 8 kHz audio, speech probabilities, and speech segmentation.
+Voice activity detection using [Silero VAD](https://github.com/snakers4/silero-vad) v6.2 weights. Supports mono 16 kHz and 8 kHz audio, speech probabilities, and speech segmentation.
## Build
@@ -28,17 +28,42 @@ cp build/generated/src/weights_16k.h build/generated/src/weights_8k.h .
The script also creates `build/generated/svad_weights.bin`. You only need that file if you use `svad_create_from_file` to load weights at run time.
-### Build the library
+### Build the library and command
```sh
-make # builds libsvad.a
+make # builds libsvad.a and svad
make debug # optional debug build
make clean # removes build outputs
```
Link your application with `libsvad.a` and `-lm`.
-## Usage
+## Command-line usage
+
+`svad` reads a 16-bit PCM WAV file at 8 kHz or 16 kHz. Multichannel audio is downmixed to mono. It prints speech start/end times in seconds and sample positions.
+
+```sh
+./svad audio.wav
+./svad --json --quiet audio.wav > segments.json
+./svad --stream audio.wav
+```
+
+Usage:
+
+```text
+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
+```
+
+* `--stream` prints speech start/end events while processing the file.
+* `--json` formats offline segments as JSON. `--quiet` suppresses statistics.
+* `--probs FILE` saves one native float32 probability per 32 ms chunk.
+
+Use `--help` to display the available options.
+
+## Library usage
Save this example as `example.c`. It reads raw mono signed 16-bit PCM in native byte order from standard input at 16 kHz and prints one speech probability per 32 ms chunk. Input must already be decoded and resampled; WAV headers are not accepted.
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;
+}
diff --git a/tools/wav.c b/tools/wav.c
new file mode 100644
index 0000000..b2a5330
--- /dev/null
+++ b/tools/wav.c
@@ -0,0 +1,138 @@
+#include "wav.h"
+
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static int rd_u32(FILE *f, uint32_t *v) {
+ unsigned char b[4];
+ if (fread(b, 1, 4, f) != 4)
+ return -1;
+ *v = (uint32_t)b[0] | (uint32_t)b[1] << 8 | (uint32_t)b[2] << 16 | (uint32_t)b[3] << 24;
+ return 0;
+}
+
+static int rd_u16(FILE *f, uint16_t *v) {
+ unsigned char b[2];
+ if (fread(b, 1, 2, f) != 2)
+ return -1;
+ *v = (uint16_t)((uint32_t)b[0] | (uint32_t)b[1] << 8);
+ return 0;
+}
+
+#define FAIL(msg) \
+ do { \
+ if (err) \
+ *err = (msg); \
+ goto fail; \
+ } while (0)
+
+int wav_read_mono_s16(const char *path, wav_t *out, const char **err) {
+ memset(out, 0, sizeof *out);
+ FILE *f = fopen(path, "rb");
+ if (!f) {
+ if (err)
+ *err = "cannot open file";
+ return -1;
+ }
+
+ char id[4];
+ uint32_t riff_size = 0;
+ if (fread(id, 1, 4, f) != 4 || memcmp(id, "RIFF", 4) != 0)
+ FAIL("not a RIFF file");
+ if (rd_u32(f, &riff_size) != 0)
+ FAIL("truncated header");
+ if (fread(id, 1, 4, f) != 4 || memcmp(id, "WAVE", 4) != 0)
+ FAIL("not a WAVE file");
+
+ uint16_t fmt = 0, channels = 0, bits = 0;
+ uint32_t rate = 0;
+ int have_fmt = 0;
+
+ for (;;) {
+ uint32_t size;
+ if (fread(id, 1, 4, f) != 4 || rd_u32(f, &size) != 0)
+ break; /* end of file, or a truncated trailing chunk */
+
+ if (memcmp(id, "fmt ", 4) == 0) {
+ uint16_t block_align;
+ uint32_t byte_rate;
+ if (size < 16)
+ FAIL("short fmt chunk");
+ if (rd_u16(f, &fmt) || rd_u16(f, &channels) || rd_u32(f, &rate) ||
+ rd_u32(f, &byte_rate) || rd_u16(f, &block_align) || rd_u16(f, &bits))
+ FAIL("truncated fmt chunk");
+ if (fseek(f, (long)size - 16, SEEK_CUR) != 0)
+ FAIL("bad fmt chunk size");
+ have_fmt = 1;
+ continue;
+ }
+
+ if (memcmp(id, "data", 4) == 0) {
+ if (!have_fmt)
+ FAIL("data chunk before fmt chunk");
+ /* Format 1 is PCM; 0xFFFE is extensible, whose subformat this
+ * reader does not inspect, so it is accepted only at 16 bits. */
+ if ((fmt != 1 && fmt != 0xFFFE) || bits != 16)
+ FAIL("only 16-bit PCM is supported");
+ if (channels == 0)
+ FAIL("zero channels");
+
+ const size_t frames = size / (2u * channels);
+ if (frames == 0)
+ FAIL("empty data chunk");
+ int16_t *mono = malloc(frames * sizeof(int16_t));
+ if (!mono)
+ FAIL("out of memory");
+
+ const size_t step = channels;
+ int16_t buf[1024];
+ size_t got = 0;
+ const size_t batch_frames = sizeof buf / sizeof buf[0] / step;
+ if (batch_frames == 0) {
+ free(mono);
+ FAIL("too many channels");
+ }
+ while (got < frames) {
+ const size_t take = (frames - got) < batch_frames ? frames - got : batch_frames;
+ const size_t want = take * step;
+ const size_t n = fread(buf, sizeof(int16_t), want, f);
+ if (n < step)
+ break;
+ for (size_t i = 0; i + step <= n; i += step) {
+ int64_t sum = 0;
+ for (size_t c = 0; c < step; ++c)
+ sum += buf[i + c];
+ mono[got++] = (int16_t)lrint((double)sum / channels);
+ }
+ if (n < want)
+ break;
+ }
+ if (got == 0) {
+ free(mono);
+ FAIL("no samples read");
+ }
+ out->samples = mono;
+ out->count = got;
+ out->sample_rate = (int)rate;
+ out->channels = (int)channels;
+ fclose(f);
+ return 0;
+ }
+
+ /* Skip unknown chunks; RIFF chunks are word aligned. */
+ if (fseek(f, (long)size + (size & 1u), SEEK_CUR) != 0)
+ break;
+ }
+ FAIL("no data chunk");
+
+fail:
+ fclose(f);
+ return -1;
+}
+
+void wav_free(wav_t *w) {
+ free(w->samples);
+ memset(w, 0, sizeof *w);
+}
diff --git a/tools/wav.h b/tools/wav.h
new file mode 100644
index 0000000..3fde703
--- /dev/null
+++ b/tools/wav.h
@@ -0,0 +1,23 @@
+/* Minimal RIFF/WAVE reader for the command line tool: uncompressed 16-bit PCM
+ * only. Multichannel input is downmixed by averaging every channel. The
+ * library itself never touches files. */
+
+#ifndef VIX_WAV_H
+#define VIX_WAV_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+typedef struct {
+ int16_t *samples; /* mono, averaged across source channels */
+ size_t count;
+ int sample_rate;
+ int channels;
+} wav_t;
+
+/* Returns 0 on success. On failure returns non-zero and, when err is
+ * non-NULL, writes a short reason there. */
+int wav_read_mono_s16(const char *path, wav_t *out, const char **err);
+void wav_free(wav_t *w);
+
+#endif