Skip to content

Build the C API for WebAssembly and run Silero VAD in a browser - #225

Merged
nyosegawa merged 3 commits into
mainfrom
wasm-build
Nov 20, 2026
Merged

nyosegawa merged 3 commits into
mainfrom
wasm-build

Conversation

@nyosegawa

@nyosegawa nyosegawa commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

What changed

speech.cpp now builds for WebAssembly and runs Silero VAD inside a web page, so that audio stays in the browser.

  • emcmake cmake -B build-wasm builds the C API into libspeech.js and libspeech.wasm, an ES module for a Worker that exports every function of speech.h, on ggml's CPU backend with SIMD128 and WebAssembly's exceptions. The build makes neither the executables nor the checks.
  • wasm/speech.mjs is a thin module that calls the C API from a Worker. It reads a model from its Blob or File through Emscripten's WORKERFS, so the library reads it with fread() as from a disk and the weights are held once, in the module's memory (MEMFS would hold the file and the weights both).
  • wasm/wav.mjs reads WAVE as src/common/wav.cpp does, so a page gives a model the samples speech gives it.
  • wasm/index.html, page.mjs and worker.mjs: a page that finds the regions of speech in a WAVE file, with the load time, RTFx and module memory. ?model=URL&audio=URL runs at once, which is how a phone is checked.
  • SPEECH_WASM_FAMILIES (default silero-vad, or a list, or all) chooses the families of a WebAssembly build. Each family's library is made through speech_family() only where the build takes it, and the table of families in src/api/speech.cpp keeps the line of each family marked SPEECH_FAMILY_<FAMILY>. Native builds take every family, as before.
  • The build without threads refuses threads above 1 at speech_load_params_set_threads() with invalid_argument, where ggml would abort the module when its thread does not start.
  • ggml's repacking code, which runs only on x86, Arm and RISC-V, is left out of the WebAssembly build: 360 KB gzipped instead of 410 KB.
  • The module's stack is 1 MB, as on Windows' main thread, instead of Emscripten's 64 KB: ggml builds a graph by recursing once per node of its longest chain, and RE-USE's graph overflowed 64 KB in a module of every family.
  • checks/smoke/wasm_browser_smoke.mjs runs the page in a headless Chrome and fails unless every region of each WAVE file equals speech vad's on the CPU, number for number, and unless two threads and a model cut in half are refused as C API errors after which the next request still answers.
  • Docs: docs/browser.md (use, limits, numbers, build), the README's Documentation table, docs/install.md's build table, docs/c-api.md's threads, the Silero VAD page. AGENTS.md describes wasm/ and the smoke.
  • ADRs: the platform record now includes the browser archive (renamed to say so), and a new record says why the module runs in a Worker and reads a model through WORKERFS.

Behaviour for existing users: none, but the message for a model of an architecture the build does not run now says "this build of speech.cpp".

The threads build (pthreads, COOP/COEP) and the CI job with the release's packaging follow in their own pull requests.

Numbers

Silero VAD F32, Apple M5, macOS 26.2, headless Chrome 158.0.8127.36, Emscripten 7.0.0, 2026-11-21, five runs:

libspeech.wasm gzipped 360 KB (1.1 MB)
libspeech.js gzipped 18 KB
module start 6 ms
load time 8.7 ms (median)
one minute, first request RTFx 488 (median)
one minute, later requests RTFx about 670
module memory, at most 29.9 MiB

speech vad on the same CPU with one thread: RTFx 675. A module of every family links and is 936 KB gzipped.

Checked

  • The review's three findings (allocations not checked, overlapping runs of the page, integers past 64 bits) are fixed with smoke checks that fail before the fixes.

  • RE-USE F32 in a module of every family: a stack overflow in ggml_visit_parents_graph() with 64 KB, enhanced with 128 KB and more.

  • wasm_browser_smoke.mjs with Silero VAD F32 on seven WAVE files (the six inputs of reference/silero-vad's dumps as 16 kHz float, and a 48 kHz 16-bit recording): every region equal to speech vad's. With speech vad given another threshold the smoke fails, naming the first region that differs.

  • The page in Safari on the iOS 26.5 simulator (iPhone 17) gives the same regions for the minute of audio.

  • checks/run.py silero-vad --devices cpu,gpu.

  • speech-api-check with Qwen3-TTS 0.6B Q8_0; detect with Silero VAD; transcribe with reazonspeech-nemo-v2 F32.

  • Not run: speech-api-check transcribe with Qwen3-ASR, which needs an F32 or F16 file that is not on this machine; Windows and Linux compile in CI.

🤖 Generated with Claude Code

nyosegawa and others added 2 commits November 21, 2026 01:01
The library now builds with Emscripten (emcmake cmake) into libspeech.js and
libspeech.wasm, an ES module for a Worker that exports every function of
speech.h, on ggml's CPU backend with SIMD128 and WebAssembly's exceptions.
wasm/speech.mjs is the thin module that calls the C API from a Worker and reads
a model from its Blob through WORKERFS, so that the weights are held once, in
the module's memory; wasm/wav.mjs reads WAVE as src/common/wav.cpp does, and
index.html, page.mjs and worker.mjs are a page that detects speech in a file.

SPEECH_WASM_FAMILIES chooses the families of a WebAssembly build, Silero VAD
by default: each family's library is made through speech_family() only where
the build takes it, and the table of families keeps the line of each one
marked SPEECH_FAMILY_<FAMILY>. A native build takes every family as before.
The build without threads refuses more than one thread at
speech_load_params_set_threads(), where ggml would abort the module when its
thread does not start. ggml's repacking code, never run in WebAssembly, is
left out: 360 KB gzipped instead of 410 KB.

checks/smoke/wasm_browser_smoke.mjs runs the page in a headless Chrome and
fails unless the regions of each WAVE file are those speech vad finds, number
for number, and unless two threads and a model cut in half are refused as
errors of the C API, after which the next request still answers. The
platform record now has the browser archive, and a new record says why the
module runs in a Worker and reads a model through WORKERFS. docs/browser.md
says how to use it and its limits and numbers.

Checked: wasm_browser_smoke.mjs with Silero VAD F32 on seven WAVE files
(the six inputs of reference/silero-vad's dumps, 16 kHz float, and a 48 kHz
16-bit recording): every region equal to speech vad's on the CPU; the smoke
fails when speech vad is given another threshold. The page in Safari on the
iOS 26.5 simulator gives the same regions. checks/run.py silero-vad on the
CPU and Metal; speech-api-check with Qwen3-TTS 0.6B Q8_0, detect with Silero
VAD and transcribe with reazonspeech-nemo-v2 F32; a WebAssembly build of
every family links (936 KB gzipped). Apple M5, macOS 26.2, Chrome 158,
Emscripten 7.0.0.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… integers past 64 bits

From the review: speech.mjs wrote into the address malloc() gave without
checking it, and malloc() gives 0 when the module's memory cannot grow, so the
audio went over the module's own data; it now refuses with out_of_memory,
naming the bytes asked for, before any write. Two runs of the page shared the
Worker's one model, so a run that loaded another while one detected freed or
replaced it under the first; runs now wait for the ones before them. A BigInt
reached the library modulo 2^64 and a Number past 2^53 as another integer, so
-2^64 set an option to 0; an integer option now takes a Number that holds the
integer exactly or a BigInt of 64 bits, and refuses the rest naming the option.

Checked: wasm_browser_smoke.mjs now runs a whole model with one cut in half
loaded beside it, and min_speech_duration_ms of -2^64 and 2^60; both fail
before the fixes and pass after, with every region of the seven WAVE files
equal to speech vad's. The allocation's refusal needs a module whose memory
cannot grow, which the smoke of the build with threads, of 1 GB, will reach.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Emscripten gives a module's stack 64 KB, and ggml builds a graph by
recursing in ggml_visit_parents_graph() once for each node of its longest
chain. A module of every family overflowed it building RE-USE's graph on two
seconds of audio, which aborted the module with "memory access out of
bounds"; it ran in 128 KB. The stack is now 1 MB, the stack of Windows' main
thread, the least the library runs on in a native build. Silero VAD's
graphs fit in 64 KB, and its module now holds 29.9 MiB at most instead of
28.6 MiB.

Checked: RE-USE F32 in a module of every family, built with
-sSTACK_OVERFLOW_CHECK=2, stops on a stack overflow in
ggml_visit_parents_graph() with 64 KB and enhances the two seconds with
128 KB; wasm_browser_smoke.mjs on the seven WAVE files with this module.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@nyosegawa

Copy link
Copy Markdown
Owner Author

Checked outside CI, on an Apple M5 (macOS 26.2) with Emscripten 7.0.0 and this branch at f3a6280:

  • node checks/smoke/wasm_browser_smoke.mjs build/speech build-wasm silero-vad-309K-v6.2-F32.gguf <7 WAVE files> in headless Chrome 158: every region equal to speech vad's, two threads refused, a model cut in half refused while a run of the whole one keeps its model, integers past 64 bits refused, ok.
  • The page in Safari on the iOS 26.5 simulator (iPhone 17), opened with ?model=…&audio=long.wav: the 14 regions of speech vad for the minute of audio.
  • A module of every family (-DSPEECH_WASM_FAMILIES=all) built with -sSTACK_OVERFLOW_CHECK=2: RE-USE F32 on two seconds overflows the stack in ggml_visit_parents_graph() at 64 KB and runs at 128 KB, 256 KB, 512 KB and 1 MB.

@nyosegawa
nyosegawa merged commit 517067a into main Nov 20, 2026
7 checks passed
@nyosegawa
nyosegawa deleted the wasm-build branch November 20, 2026 16:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant