Repository navigation
Build the C API for WebAssembly and run Silero VAD in a browser - #225
Merged
Merged
Conversation
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>
This was referenced Nov 20, 2026
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>
Owner
Author
|
Checked outside CI, on an Apple M5 (macOS 26.2) with Emscripten 7.0.0 and this branch at f3a6280:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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-wasmbuilds the C API intolibspeech.jsandlibspeech.wasm, an ES module for a Worker that exports every function ofspeech.h, on ggml's CPU backend with SIMD128 and WebAssembly's exceptions. The build makes neither the executables nor the checks.wasm/speech.mjsis 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 withfread()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.mjsreads WAVE assrc/common/wav.cppdoes, so a page gives a model the samplesspeechgives it.wasm/index.html,page.mjsandworker.mjs: a page that finds the regions of speech in a WAVE file, with the load time, RTFx and module memory.?model=URL&audio=URLruns at once, which is how a phone is checked.SPEECH_WASM_FAMILIES(defaultsilero-vad, or a list, orall) chooses the families of a WebAssembly build. Each family's library is made throughspeech_family()only where the build takes it, and the table of families insrc/api/speech.cppkeeps the line of each family markedSPEECH_FAMILY_<FAMILY>. Native builds take every family, as before.threadsabove 1 atspeech_load_params_set_threads()withinvalid_argument, where ggml would abort the module when its thread does not start.checks/smoke/wasm_browser_smoke.mjsruns the page in a headless Chrome and fails unless every region of each WAVE file equalsspeech 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/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 describeswasm/and the smoke.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.wasmgzippedlibspeech.jsgzippedspeech vadon 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.mjswith Silero VAD F32 on seven WAVE files (the six inputs ofreference/silero-vad's dumps as 16 kHz float, and a 48 kHz 16-bit recording): every region equal tospeech vad's. Withspeech vadgiven 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-checkwith Qwen3-TTS 0.6B Q8_0;detectwith Silero VAD;transcribewith reazonspeech-nemo-v2 F32.Not run:
speech-api-check transcribewith 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