Skip to content

Add Pascal API for ten-vad - #2388

Merged
csukuangfj merged 2 commits into
k2-fsa:masterfrom
csukuangfj:pascal-api-ten-vad
Jul 12, 2025
Merged

csukuangfj merged 2 commits into
k2-fsa:masterfrom
csukuangfj:pascal-api-ten-vad

Conversation

@csukuangfj

@csukuangfj csukuangfj commented Jul 12, 2025 •

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • New Features
    • Added support for the ten-vad model in the Pascal VAD API, enabling silence removal from audio with this model.
    • Introduced a new example program and script demonstrating silence removal using ten-vad.
  • Documentation
    • Updated comments to clarify the use of silero-vad in existing examples.
  • Chores
    • Updated workflow to run the new ten-vad VAD test earlier in the sequence and included timing for additional scripts.
    • Modified ignore patterns to exclude generated ten-vad-related files.

@csukuangfj
csukuangfj requested a review from Copilot July 12, 2025 10:27
@coderabbitai

coderabbitai Bot commented Jul 12, 2025 •

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

Walkthrough

Support for the ten-vad voice activity detection (VAD) model was added to the Pascal API and example suite. This includes new configuration records, a new Pascal example program for silence removal using ten-vad, an accompanying shell script for setup and execution, workflow adjustments to test the new example, and relevant updates to documentation and ignore files.

Changes

File(s) Change Summary
.github/workflows/pascal.yaml Moved and enhanced the Pascal VAD test step; added execution and timing of the new ten-vad example.
pascal-api-examples/vad/.gitignore Added remove_silence_ten_vad to ignored files.
pascal-api-examples/vad/remove_silence.pas Updated header comment to clarify use of silero-vad.
pascal-api-examples/vad/remove_silence_ten_vad.pas, run-remove-silence-ten-vad.sh Added new Pascal example and shell script for silence removal using ten-vad model.
sherpa-onnx/pascal-api/sherpa_onnx.pas Introduced TSherpaOnnxTenVadModelConfig and extended VAD config/constructor for ten-vad support.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ShellScript as run-remove-silence-ten-vad.sh
    participant PascalApp as remove_silence_ten_vad.pas
    participant SherpaAPI as SherpaOnnx Pascal API
    participant TenVadModel

    User->>ShellScript: Execute script
    ShellScript->>ShellScript: Build dependencies, download model/audio
    ShellScript->>PascalApp: Compile and run remove_silence_ten_vad
    PascalApp->>SherpaAPI: Configure VAD with ten-vad model
    loop For each audio window
        PascalApp->>SherpaAPI: Feed audio window
        SherpaAPI->>TenVadModel: Perform VAD
        TenVadModel-->>SherpaAPI: Speech segments
        SherpaAPI-->>PascalApp: Speech segments
    end
    PascalApp->>SherpaAPI: Flush VAD
    SherpaAPI-->>PascalApp: Remaining segments
    PascalApp->>PascalApp: Concatenate speech, write output WAV
Loading

Possibly related PRs

  • k2-fsa/sherpa-onnx#2385: Adds C# API support and example usage for the ten-vad model, paralleling Pascal API extensions.
  • k2-fsa/sherpa-onnx#2384: Adds ten-vad support to the Go API with analogous configuration and example updates, paralleling the Pascal extension.
  • k2-fsa/sherpa-onnx#2387: Adds Swift API support and example usage for the ten-vad model, related by implementing ten-vad support in different language APIs.

Poem

In Pascal fields where silence lay,
A ten-vad rabbit hops today.
With scripts and configs all anew,
It sniffs out speech and hops right through!
Now silences vanish, swift and clean—
The code runs bright, the tests all green.
🐇✨


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ca2a1f3 and befd142.

📒 Files selected for processing (1)
  • sherpa-onnx/pascal-api/sherpa_onnx.pas (5 hunks)

🪧 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

This PR adds support for the new Ten-VAD voice activity detection model in the Pascal API, updates example scripts, and extends CI to run the ten-vad example.

  • Introduce TSherpaOnnxTenVadModelConfig and integrate it into the existing VAD model records and C API.
  • Implement ToString and Initialize for TenVad, and update the parent ToString formatting to include the new field.
  • Add example scripts and Pascal sample for ten-vad, update .gitignore, and extend the GitHub Actions workflow to run the new example.

Reviewed Changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
sherpa-onnx/pascal-api/sherpa_onnx.pas Add TenVad config record, update C binding, ToString, and constructor mapping.
pascal-api-examples/vad/run-remove-silence-ten-vad.sh New shell script to download assets, build, and run the ten-vad Pascal example.
pascal-api-examples/vad/remove_silence_ten_vad.pas New Pascal example showing how to remove silence with ten-vad.
pascal-api-examples/vad/remove_silence.pas Clarify comment to specify silero-vad in the existing example.
pascal-api-examples/vad/.gitignore Ignore generated artifacts for the ten-vad example.
.github/workflows/pascal.yaml Add a CI step to run the Pascal ten-vad example alongside existing VAD tests.
Comments suppressed due to low confidence (1)

pascal-api-examples/vad/remove_silence_ten_vad.pas:27

  • [nitpick] The variable AllSpeechSegment holds multiple segments but uses a singular name. Consider renaming it to AllSpeechSegments for clarity.
  AllSpeechSegment: array of TSherpaOnnxSpeechSegment;

Comment thread sherpa-onnx/pascal-api/sherpa_onnx.pas Outdated
'Provider := %s, ' +
'Debug := %s' +
'Debug := %s, ' +
'SileroVad := %s' +

Copilot AI Jul 12, 2025

Copy link

Choose a reason for hiding this comment

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

The last field label in the TSherpaOnnxVadModelConfig.ToString format string is duplicated as 'SileroVad'. It should read 'TenVad := %s' to match the added field.

Suggested change
'SileroVad := %s' +
'TenVad := %s' +

Copilot uses AI. Check for mistakes.
end;

class operator TSherpaOnnxTenVadModelConfig.Initialize({$IFDEF FPC}var{$ELSE}out{$ENDIF} Dest: TSherpaOnnxTenVadModelConfig);
begin

Copilot AI Jul 12, 2025

Copy link

Choose a reason for hiding this comment

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

The Initialize operator sets default numeric values but does not initialize the Model string. Consider adding Dest.Model := ''; for clarity.

Suggested change
begin
begin
Dest.Model := '';

Copilot uses AI. Check for mistakes.

@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: 1

🧹 Nitpick comments (3)
pascal-api-examples/vad/remove_silence_ten_vad.pas (1)

61-78: Consider refactoring to reduce code duplication.

The VAD processing logic is identical to the silero-vad example. Consider creating a shared utility function or a more generic example that accepts VAD type as a parameter to reduce code duplication.

For example, you could extract the processing logic into a shared procedure:

procedure ProcessVadSegments(var Vad: TSherpaOnnxVoiceActivityDetector; 
                           Wave: TSherpaOnnxWave; 
                           WindowSize: Integer; 
                           SampleRate: Integer;
                           OutputFilename: AnsiString);

Also applies to: 82-93, 95-109

pascal-api-examples/vad/run-remove-silence-ten-vad.sh (2)

3-4: Harden bash options

set -ex prints commands and aborts on non-zero exit but misses two common foot-guns:

set -euo pipefail

• -u aborts on unset variables (avoids typos)
• -o pipefail surfaces failures hidden inside pipelines.

Adding them costs nothing and makes the script more robust.


40-43: Prepend only if the dir isn’t already present

Repeated runs append duplicate entries to LD_LIBRARY_PATH / DYLD_LIBRARY_PATH. Use a lightweight check to avoid path bloat:

add_path() {
  case ":$1:" in
    *":$2:"*) ;; # already present
    *) export "$1"="$2:${!1}" ;;
  esac
}
add_path LD_LIBRARY_PATH  "$SHERPA_ONNX_DIR/build/install/lib"
add_path DYLD_LIBRARY_PATH "$SHERPA_ONNX_DIR/build/install/lib"
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 0b8ce73 and ca2a1f3.

📒 Files selected for processing (6)
  • .github/workflows/pascal.yaml (1 hunks)
  • pascal-api-examples/vad/.gitignore (1 hunks)
  • pascal-api-examples/vad/remove_silence.pas (1 hunks)
  • pascal-api-examples/vad/remove_silence_ten_vad.pas (1 hunks)
  • pascal-api-examples/vad/run-remove-silence-ten-vad.sh (1 hunks)
  • sherpa-onnx/pascal-api/sherpa_onnx.pas (5 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: pascal (ubuntu-latest)
  • GitHub Check: pascal (ubuntu-22.04-arm)
  • GitHub Check: pascal (windows-latest)
  • GitHub Check: windows-latest
  • GitHub Check: ubuntu-22.04
🔇 Additional comments (14)
sherpa-onnx/pascal-api/sherpa_onnx.pas (6)

429-438: LGTM! Well-structured ten-vad configuration.

The new TSherpaOnnxTenVadModelConfig record follows the same pattern as the existing TSherpaOnnxSileroVadModelConfig, ensuring API consistency.


845-852: Consistent native C structure implementation.

The native C-compatible SherpaOnnxTenVadModelConfig record properly mirrors the Pascal record structure for interoperability.


1933-1946: Excellent ToString implementation.

The ToString method for TSherpaOnnxTenVadModelConfig provides comprehensive string representation of all configuration parameters.


1957-1964: Good default configuration with appropriate window size.

The Initialize operator sets sensible defaults, with a window size of 256 which appears to be the appropriate default for ten-vad models (different from silero-vad's 512).


2128-2133: Proper configuration copying for ten-vad.

The constructor correctly copies all TenVad configuration parameters to the native C structure, ensuring proper initialization.


1973-1977: Fix duplicate field name in ToString method.

The ToString method incorrectly shows 'SileroVad := %s' twice instead of including the TenVad field.

-    'Debug := %s, ' +
-    'SileroVad := %s' +
+    'Debug := %s, ' +
+    'TenVad := %s' +

Likely an incorrect or invalid review comment.

pascal-api-examples/vad/remove_silence.pas (1)

4-4: Good clarification for VAD model specificity.

The comment update clearly identifies this example as using silero-vad, which helps distinguish it from the new ten-vad example.

pascal-api-examples/vad/.gitignore (1)

4-4: Appropriate gitignore entry for new example.

The addition of remove_silence_ten_vad follows the existing pattern and ensures compiled artifacts are properly excluded from version control.

.github/workflows/pascal.yaml (1)

139-158: Excellent CI integration for ten-vad testing.

The workflow properly integrates the new ten-vad example into the test suite, maintaining existing tests while adding the new functionality. The timing measurements and clear separation with echo statements improve test observability.

pascal-api-examples/vad/remove_silence_ten_vad.pas (4)

1-5: Well-documented example for ten-vad usage.

The header comment clearly explains the purpose and distinguishes this example from the silero-vad version.


43-50: Appropriate ten-vad configuration parameters.

The configuration uses appropriate defaults for ten-vad: window size of 256 and threshold of 0.25, which differ from silero-vad settings.


46-46: Correct usage of TenVad configuration.

The example properly uses Config.TenVad.Model instead of Config.SileroVad.Model, demonstrating the new API correctly.


111-111: Good output filename differentiation.

Using a distinct output filename (lei-jun-test-no-silence-ten-vad.wav) prevents conflicts with the silero-vad example output.

pascal-api-examples/vad/run-remove-silence-ten-vad.sh (1)

34-38: Quote FPC flags and pass absolute paths

Unquoted -Fu/-Fl values break when the project lives under a directory containing spaces. Also, relying on ./remove_silence_ten_vad.pas has the same pwd issue mentioned earlier.

-fpc \
-  -dSHERPA_ONNX_USE_SHARED_LIBS \
-  -Fu$SHERPA_ONNX_DIR/sherpa-onnx/pascal-api \
-  -Fl$SHERPA_ONNX_DIR/build/install/lib \
-  ./remove_silence_ten_vad.pas
+fpc \
+  -dSHERPA_ONNX_USE_SHARED_LIBS \
+  -Fu"$SHERPA_ONNX_DIR/sherpa-onnx/pascal-api" \
+  -Fl"$SHERPA_ONNX_DIR/build/install/lib" \
+  "$SCRIPT_DIR/remove_silence_ten_vad.pas"

Ensure CI invokes the script after these path changes; the compile step should still succeed.

Comment on lines +5 to +7
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
SHERPA_ONNX_DIR=$(cd $SCRIPT_DIR/../.. && pwd)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Use $SHERPA_ONNX_DIR instead of hard-coded relative paths & always quote variables

Several commands assume the caller’s pwd matches the script location (../../build/..., ./ten-vad.onnx, etc.).
If the script is invoked from any other directory (e.g. by CI or via an absolute path), those relatives resolve incorrectly and the build/downloads will silently fail.

- if [[ ! -f ../../build/install/lib/libsherpa-onnx-c-api.dylib ...
+if [[ ! -f "$SHERPA_ONNX_DIR/build/install/lib/libsherpa-onnx-c-api.dylib" \
+      && ! -f "$SHERPA_ONNX_DIR/build/install/lib/libsherpa-onnx-c-api.so" \
+      && ! -f "$SHERPA_ONNX_DIR/build/install/lib/sherpa-onnx-c-api.dll" ]]; then
-  mkdir -p ../../build
-  pushd ../../build
+  mkdir -p "$SHERPA_ONNX_DIR/build"
+  pushd "$SHERPA_ONNX_DIR/build"
...
-if [[ ! -f ./ten-vad.onnx ]]; then
-  curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/ten-vad.onnx
+MODEL_PATH="$SCRIPT_DIR/ten-vad.onnx"
+if [[ ! -f "$MODEL_PATH" ]]; then
+  curl -fLso "$MODEL_PATH" \
+       https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/ten-vad.onnx

Quoting also guards against spaces in $SCRIPT_DIR and $SHERPA_ONNX_DIR.
Fail-fast curl -f prevents silent 404/5xx downloads.

Also applies to: 10-12, 26-33

🤖 Prompt for AI Agents
In pascal-api-examples/vad/run-remove-silence-ten-vad.sh around lines 5 to 7,
replace all hard-coded relative paths with the variable $SHERPA_ONNX_DIR to
ensure paths resolve correctly regardless of the current working directory.
Additionally, quote all variable expansions like "$SCRIPT_DIR" and
"$SHERPA_ONNX_DIR" to handle spaces safely. Also, update curl commands to use
the -f flag to fail fast on HTTP errors. Apply these changes similarly to lines
10-12 and 26-33.

@csukuangfj
csukuangfj merged commit fc2fc3d into k2-fsa:master Jul 12, 2025
@csukuangfj
csukuangfj deleted the pascal-api-ten-vad branch July 12, 2025 11:49
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