Skip to content

Add JavaScript (WebAssembly) API for ten-vad - #2382

Merged
csukuangfj merged 1 commit into
k2-fsa:masterfrom
csukuangfj:wasm-ten-vad
Jul 12, 2025
Merged

csukuangfj merged 1 commit into
k2-fsa:masterfrom
csukuangfj:wasm-ten-vad

Conversation

@csukuangfj

@csukuangfj csukuangfj commented Jul 12, 2025 •

Copy link
Copy Markdown
Collaborator

WebAssembly for VAD

Huggingface space ModelScope space
silero-vad URL URL
ten-vad URL URL

Screenshots

silero-vad ten-vad
Screenshot 2025-07-12 at 14 00 41 Screenshot 2025-07-12 at 13 57 47

Summary by CodeRabbit

  • New Features

    • Added support for the TEN VAD model alongside Silero VAD, enabling dynamic selection and configuration of VAD models in the WebAssembly demo.
    • Enhanced the VAD demo UI with a modernized layout, improved controls, and an informative footer section.
    • Introduced a new workflow to automate building and publishing WebAssembly SIMD TEN VAD artifacts to external repositories.
  • Bug Fixes

    • Improved asset checks to ensure at least one VAD model is present before building.
  • Documentation

    • Expanded README instructions for downloading and using both Silero and TEN VAD models, including usage examples and download scripts.
    • Updated documentation to clarify model usage and configuration steps.
  • Refactor

    • Modularized VAD initialization and configuration for better maintainability and extensibility.
    • Removed unused and empty functions for cleaner code.
  • Style

    • Refined demo page styling for better user experience and accessibility.

@csukuangfj
csukuangfj requested a review from Copilot July 12, 2025 06:01
@coderabbitai

coderabbitai Bot commented Jul 12, 2025 •

Copy link
Copy Markdown

Walkthrough

This update introduces support for the TEN VAD model alongside Silero VAD in the WebAssembly VAD demo, adds a new GitHub Actions workflow for automated building and publishing of the TEN VAD wasm artifact, enhances the UI and documentation, and updates configuration, build scripts, and model selection logic to accommodate dual VAD models.

Changes

Files/Paths Change Summary
.github/workflows/wasm-simd-hf-space-ten-vad.yaml Added new GitHub Actions workflow for building, packaging, and publishing the wasm SIMD TEN VAD artifact.
wasm/vad/sherpa-onnx-vad.js, wasm/vad/sherpa-onnx-wasm-main-vad.cc Added support for TEN VAD model config, memory management, and printing; updated functions to handle dual VADs.
wasm/vad/app-vad.js, wasm/vad/index.html Enhanced model selection logic, initialization, and UI for dual VAD support; improved layout and status handling.
wasm/vad/assets/README.md, wasm/asr/assets/README.md Updated documentation to include TEN VAD model instructions and clarify usage.
wasm/vad/CMakeLists.txt Modified asset existence checks to allow either Silero or TEN VAD; exported new file existence function.
wasm/vad-asr/app-vad-asr.js Removed unused function.
nodejs-examples/test-vad-with-non-streaming-asr-moonshine.js Added logic and comments for using TEN VAD model; updated window size selection and config.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Browser
    participant JS_Module
    participant WASM_Module

    User->>Browser: Load VAD demo page
    Browser->>JS_Module: Initialize Module (onRuntimeInitialized)
    JS_Module->>JS_Module: initVad()
    JS_Module->>WASM_Module: fileExists("ten-vad.onnx")
    alt ten-vad.onnx exists
        JS_Module->>JS_Module: Set TEN VAD config/model path
    else silero_vad.onnx exists
        JS_Module->>JS_Module: Set Silero VAD config/model path
    else
        JS_Module->>Browser: Show error (no model)
    end
    JS_Module->>WASM_Module: createVad(config)
    JS_Module->>Browser: Ready for user interaction
Loading

Possibly related PRs

  • k2-fsa/sherpa-onnx#2377: Implements support for the "ten-vad" model, modifying overlapping VAD config and integration logic.
  • k2-fsa/sherpa-onnx#2379: Adds C API and test workflow for TEN VAD, overlapping with model config and file existence logic.

Poem

In the warren of code, a new friend hops in—
TEN VAD joins Silero, let dual voices begin!
With workflows that build and docs that explain,
The UI’s now brighter, less loading, more gain.
Two models, one demo, a bunny’s delight—
Hop on and detect, from morning to night!
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Add support for the ten-vad model alongside silero-vad in the WebAssembly VAD API, update the UI and documentation, and extend examples and CI workflows to handle the new model.

  • Extend C++ bindings and printing logic to include ten-vad configuration.
  • Implement JavaScript init/free routines and runtime detection for ten-vad.
  • Update UI layout, README assets, example scripts, and CI workflow to support ten-vad.

Reviewed Changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
wasm/vad/sherpa-onnx-wasm-main-vad.cc Updated static_assert and MyPrint to include ten_vad config.
wasm/vad/sherpa-onnx-vad.js Added initSherpaOnnxTenVadModelConfig and free logic for ten-vad.
wasm/vad/index.html Enhanced layout, loading states, and added footer with ten-vad details.
wasm/vad/assets/README.md Added download instructions for ten-vad.onnx.
wasm/vad/app-vad.js Detects presence of ten-vad.onnx and selects appropriate config.
wasm/vad/CMakeLists.txt Updated asset existence check to require at least one VAD model.
wasm/vad-asr/app-vad-asr.js Removed unused placeholder function.
wasm/asr/assets/README.md Corrected sed command to target sherpa-onnx-asr.js.
nodejs-examples/test-vad-with-non-streaming-asr-moonshine.js Updated example to show how to switch to ten-vad and adjusted windowSize logic.
.github/workflows/wasm-simd-hf-space-ten-vad.yaml Added GitHub Actions workflow for building and publishing the ten-vad variant.
Comments suppressed due to low confidence (2)

wasm/vad/sherpa-onnx-vad.js:67

  • The variable offset is assigned without a let/const declaration, which pollutes the global scope and may lead to unexpected behavior. Declare it locally (e.g., let offset = 0;).
  offset = 0;

wasm/vad/sherpa-onnx-vad.js:146

  • The buffer variable is not defined in this scope, causing a ReferenceError at runtime. Ensure you either declare buffer or return the correct property (e.g., sileroVad.buffer or tenVad.buffer).
    buffer: buffer, ptr: ptr, len: len, sileroVad: sileroVad, tenVad: tenVad

Comment thread nodejs-examples/test-vad-with-non-streaming-asr-moonshine.js

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (2)
wasm/vad/assets/README.md (1)

6-6: Fix markdown formatting issues for better documentation quality.

Address the static analysis findings to improve markdown compliance:

  1. Bare URLs: Wrap URLs in angle brackets or use proper markdown link format
  2. Code blocks: Add language specifiers to fenced code blocks

Apply these formatting improvements:

-https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/silero_vad.onnx
+<https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/silero_vad.onnx>

-https://github.com/k2-fsa/sherpa-onnx/blob/master/.github/workflows/wasm-simd-hf-space-silero-vad.yaml
+<https://github.com/k2-fsa/sherpa-onnx/blob/master/.github/workflows/wasm-simd-hf-space-silero-vad.yaml>

-```
+```bash

-https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/ten-vad.onnx
+<https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/ten-vad.onnx>

-https://github.com/k2-fsa/sherpa-onnx/blob/master/.github/workflows/wasm-simd-hf-space-ten-vad.yaml
+<https://github.com/k2-fsa/sherpa-onnx/blob/master/.github/workflows/wasm-simd-hf-space-ten-vad.yaml>

-```
+```bash

Also applies to: 10-10, 12-15, 20-20, 24-24, 26-32

wasm/vad/sherpa-onnx-vad.js (1)

55-89: Consider refactoring to reduce code duplication.

The initSherpaOnnxTenVadModelConfig function is nearly identical to initSherpaOnnxSileroVadModelConfig except for the default window size. Consider extracting a common function to reduce duplication.

You could refactor to a common function:

+function initSherpaOnnxVadModelConfigBase(config, Module, defaultWindowSize) {
+  const modelLen = Module.lengthBytesUTF8(config.model || '') + 1;
+  const n = modelLen;
+  const buffer = Module._malloc(n);
+  const len = 6 * 4;
+  const ptr = Module._malloc(len);
+  Module.stringToUTF8(config.model || '', buffer, modelLen);
+  
+  let offset = 0;
+  Module.setValue(ptr, buffer, 'i8*');
+  offset += 4;
+  Module.setValue(ptr + offset, config.threshold || 0.5, 'float');
+  offset += 4;
+  Module.setValue(ptr + offset, config.minSilenceDuration || 0.5, 'float');
+  offset += 4;
+  Module.setValue(ptr + offset, config.minSpeechDuration || 0.25, 'float');
+  offset += 4;
+  Module.setValue(ptr + offset, config.windowSize || defaultWindowSize, 'i32');
+  offset += 4;
+  Module.setValue(ptr + offset, config.maxSpeechDuration || 20, 'float');
+  offset += 4;
+  
+  return { buffer: buffer, ptr: ptr, len: len };
+}

 function initSherpaOnnxSileroVadModelConfig(config, Module) {
-  // ... existing implementation
+  return initSherpaOnnxVadModelConfigBase(config, Module, 512);
 }

 function initSherpaOnnxTenVadModelConfig(config, Module) {
-  // ... existing implementation  
+  return initSherpaOnnxVadModelConfigBase(config, Module, 256);
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between f2df116 and ffcc833.

📒 Files selected for processing (10)
  • .github/workflows/wasm-simd-hf-space-ten-vad.yaml (1 hunks)
  • nodejs-examples/test-vad-with-non-streaming-asr-moonshine.js (3 hunks)
  • wasm/asr/assets/README.md (1 hunks)
  • wasm/vad-asr/app-vad-asr.js (0 hunks)
  • wasm/vad/CMakeLists.txt (2 hunks)
  • wasm/vad/app-vad.js (1 hunks)
  • wasm/vad/assets/README.md (1 hunks)
  • wasm/vad/index.html (1 hunks)
  • wasm/vad/sherpa-onnx-vad.js (5 hunks)
  • wasm/vad/sherpa-onnx-wasm-main-vad.cc (2 hunks)
💤 Files with no reviewable changes (1)
  • wasm/vad-asr/app-vad-asr.js
🧰 Additional context used
🧬 Code Graph Analysis (1)
nodejs-examples/test-vad-with-non-streaming-asr-moonshine.js (2)
nodejs-addon-examples/test_vad_asr_non_streaming_moonshine_microphone.js (2)
  • windowSize (77-77)
  • vad (57-57)
nodejs-addon-examples/test_vad_with_non_streaming_asr_moonshine.js (2)
  • windowSize (70-70)
  • vad (55-55)
🪛 markdownlint-cli2 (0.17.2)
wasm/vad/assets/README.md

6-6: Bare URL used

(MD034, no-bare-urls)


10-10: Bare URL used

(MD034, no-bare-urls)


12-12: Fenced code blocks should have a language specified

(MD040, fenced-code-language)


20-20: Bare URL used

(MD034, no-bare-urls)


24-24: Bare URL used

(MD034, no-bare-urls)


26-26: Fenced code blocks should have a language specified

(MD040, fenced-code-language)

🔇 Additional comments (21)
wasm/asr/assets/README.md (1)

62-62: LGTM: File name correction in sed command.

The update correctly targets ./sherpa-onnx-asr.js instead of ./sherpa-onnx.js, ensuring the sed command operates on the intended ASR-specific JavaScript file.

wasm/vad/CMakeLists.txt (2)

5-7: LGTM: Enhanced asset check for dual VAD model support.

The updated condition correctly allows either silero_vad.onnx or ten-vad.onnx to be present, enabling flexible VAD model selection while maintaining the requirement that at least one model must be available.


33-33: LGTM: Export SherpaOnnxFileExists for runtime model detection.

Adding SherpaOnnxFileExists to the exported functions enables JavaScript code to dynamically detect which VAD model files are available at runtime, supporting the flexible model selection mechanism.

wasm/vad/assets/README.md (1)

3-33: Excellent documentation structure for dual VAD model support.

The updated README provides clear, comprehensive instructions for both silero-vad and ten-vad models, including download URLs, placement instructions, and usage examples. The parallel structure makes it easy for users to understand how to configure either model.

nodejs-examples/test-vad-with-non-streaming-asr-moonshine.js (3)

29-37: Excellent documentation for VAD model switching.

The comments clearly explain how to configure and switch between the two VAD models, providing users with straightforward instructions for using ten-vad as an alternative to silero-vad.


47-55: Well-structured tenVad configuration.

The tenVad configuration object follows the same structure as sileroVad with appropriate default values. The commented-out model path and empty string default provide a clear pattern for users to enable ten-vad when needed.


82-85: Correct window size selection logic.

The conditional logic properly prioritizes tenVad window size when its model is configured (non-empty path), while maintaining backward compatibility by defaulting to sileroVad. This ensures the appropriate window size is used based on the active VAD model.

wasm/vad/sherpa-onnx-wasm-main-vad.cc (3)

17-17: LGTM: Correct static assertion for SherpaOnnxTenVadModelConfig.

The static assertion ensures the ten-vad configuration structure maintains the expected 24-byte size (6 * 4), providing compile-time verification of memory layout compatibility.


19-22: LGTM: Updated size assertion for extended VAD config.

The updated assertion correctly accounts for the addition of the ten_vad member to the overall VAD model configuration, ensuring the total structure size remains consistent with expectations.


25-25: LGTM: Consistent ten-vad configuration printing.

The ten_vad pointer initialization and printing logic perfectly mirror the existing silero_vad implementation, maintaining code consistency and providing users with comprehensive configuration visibility for both VAD models.

Also applies to: 38-45

wasm/vad/index.html (3)

14-16: LGTM! Good CSS pattern for loading states.

The .loading class provides a clean way to hide content during initialization, which integrates well with the dynamic status management in the JavaScript code.


20-20: Excellent modern styling improvements.

The flexbox layout with centered content and modern typography significantly improves the user experience compared to the basic styling.


47-74: Comprehensive footer enhances user understanding.

The footer section provides valuable information about the demo's capabilities, privacy considerations, and contact options. This helps users understand the local processing benefits and how to get support.

wasm/vad/app-vad.js (3)

46-50: LGTM! Good Emscripten integration pattern.

The Module.locateFile function follows Emscripten's recommended pattern for customizing file path resolution.


52-73: Well-designed status management system.

The Module.setStatus function elegantly handles both status display and loading state management by toggling CSS classes. The integration with the HTML loading states is well thought out.


87-97: Proper memory management in native function call.

The fileExists() function correctly handles UTF-8 string conversion and memory allocation/deallocation when calling the native _SherpaOnnxFileExists function.

wasm/vad/sherpa-onnx-vad.js (2)

10-12: LGTM! Consistent cleanup pattern.

The recursive cleanup of tenVad configuration follows the same pattern as sileroVad, ensuring proper memory management.


119-119: Memory layout calculation and data copying look correct.

The memory layout properly accounts for both silero and ten VAD configurations, and the data copying using Module._CopyHeap is implemented correctly.

Also applies to: 142-143

.github/workflows/wasm-simd-hf-space-ten-vad.yaml (3)

3-15: Well-configured workflow triggers and concurrency.

The workflow properly handles multiple trigger types (push, tags, manual dispatch) and includes concurrency control to prevent conflicting runs.


83-83: Good security practice with repository owner check.

The conditional release creation properly restricts access to authorized repository owners, preventing unauthorized releases.


94-98: Excellent retry mechanism for external services.

The use of nick-fields/retry action with reasonable timeout and retry settings provides robustness against temporary network or service issues when publishing to external repositories.

Also applies to: 131-135

Comment thread wasm/vad/app-vad.js
Comment thread .github/workflows/wasm-simd-hf-space-ten-vad.yaml
Comment thread .github/workflows/wasm-simd-hf-space-ten-vad.yaml
@csukuangfj
csukuangfj merged commit fb1c35a into k2-fsa:master Jul 12, 2025
@csukuangfj
csukuangfj deleted the wasm-ten-vad branch July 12, 2025 06:10
This was referenced Jul 12, 2025
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.

2 participants