Skip to content

Add Go API for ten-vad - #2384

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

csukuangfj merged 4 commits into
k2-fsa:masterfrom
csukuangfj:go-api-ten-vad

Conversation

@csukuangfj

@csukuangfj csukuangfj commented Jul 12, 2025 •

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • New Features

    • Added support for an additional voice activity detection (VAD) model, allowing users to choose between two different models for improved flexibility.
    • The application now automatically downloads the required VAD model files if they are not present, streamlining setup.
  • Improvements

    • Enhanced audio capture performance and reliability by switching to a new audio input library with callback-driven processing.
    • Improved audio processing accuracy and efficiency with updated buffering and sample conversion methods.

@csukuangfj
csukuangfj requested a review from Copilot July 12, 2025 07:35
@coderabbitai

coderabbitai Bot commented Jul 12, 2025 •

Copy link
Copy Markdown

Caution

Review failed

The pull request is closed.

"""

Walkthrough

The changes update the voice activity detection (VAD) example to support both Silero and Ten-VAD models, switching audio input from portaudio to malgo with callback-based capture. The VAD configuration and model selection are now dynamic, and the code introduces helper functions and struct extensions for Ten-VAD support. The build script now ensures Ten-VAD model availability.

Changes

File(s) Change Summary
go-api-examples/vad/main.go Replaces portaudio with malgo for audio capture; adds support for Ten-VAD model; introduces helper functions; dynamic model selection and configuration; uses circular buffer for audio; updates control flow.
go-api-examples/vad/run.sh Adds logic to check and download ten-vad.onnx if missing before building and running the example.
scripts/go/sherpa_onnx.go Adds TenVadModelConfig struct; extends VadModelConfig; updates NewVoiceActivityDetector to initialize Ten-VAD fields.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant MainGo as main.go
    participant Malgo as malgo
    participant VAD as VoiceActivityDetector

    User->>MainGo: Start program
    MainGo->>Malgo: Initialize context & device
    Malgo->>MainGo: Callback with audio data
    MainGo->>MainGo: Convert & buffer audio samples
    MainGo->>VAD: Feed window of samples
    VAD-->>MainGo: Speech segment detected?
    MainGo->>MainGo: Save speech as WAV if detected
    User->>MainGo: Interrupt to exit
Loading

Possibly related PRs

Poem

In fields of code where rabbits hop,
New VADs appear, the changes don’t stop!
Malgo now listens, portaudio’s gone,
Ten-VAD and Silero both carry on.
With buffers and models, we capture the sound—
Hopping through bytes, where speech is found! 🐇🎤
"""


📜 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 a571d45 and 34853d2.

📒 Files selected for processing (2)
  • go-api-examples/vad/main.go (2 hunks)
  • go-api-examples/vad/run.sh (1 hunks)
✨ 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

This PR introduces support for the TenVad model in the Go bindings and updates the VAD example to download and use ten-vad.onnx.

  • Added a new TenVadModelConfig struct and integrated it into VadModelConfig and the C binding in NewVoiceActivityDetector
  • Enhanced the example shell script to download ten-vad.onnx when missing
  • Updated the Go example (main.go) to choose between Silero VAD and TenVad, switch to malgo for audio I/O, and add helper utilities

Reviewed Changes

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

File Description
scripts/go/sherpa_onnx.go Added TenVadModelConfig and populated C struct fields for TenVad in constructor
go-api-examples/vad/run.sh Added logic to download ten-vad.onnx
go-api-examples/vad/main.go Implemented file‐based model selection, switched to malgo, and added helpers
Comments suppressed due to low confidence (3)

go-api-examples/vad/main.go:47

  • [nitpick] Local variable window_size uses snake_case; consider renaming to camelCase (e.g., windowSize) to follow Go naming conventions.
	window_size := config.SileroVad.WindowSize

scripts/go/sherpa_onnx.go:1145

  • [nitpick] The new TenVadModelConfig API is introduced but lacks unit tests. Consider adding tests that instantiate TenVadModelConfig and call NewVoiceActivityDetector to verify TenVad behavior.
type TenVadModelConfig struct {

go-api-examples/vad/main.go:146

  • [nitpick] Consider adding a GoDoc comment for samplesInt16ToFloat to explain its purpose and input/output conversion details.
func samplesInt16ToFloat(inSamples []byte) []float32 {

Comment thread go-api-examples/vad/main.go

@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)
go-api-examples/vad/main.go (3)

47-50: Use camelCase for variable naming.

Go convention prefers camelCase over snake_case for variable names.

-	window_size := config.SileroVad.WindowSize
+	windowSize := config.SileroVad.WindowSize
 	if config.TenVad.Model != "" {
-		window_size = config.TenVad.WindowSize
+		windowSize = config.TenVad.WindowSize
 	}

84-87: Update variable reference to use camelCase.

Following the previous suggestion to rename window_size to windowSize.

-		for buffer.Size() >= window_size {
+		for buffer.Size() >= windowSize {
 			head := buffer.Head()
-			s := buffer.Get(head, window_size)
-			buffer.Pop(window_size)
+			s := buffer.Get(head, windowSize)
+			buffer.Pop(windowSize)

159-166: Consider using a shared utility package to avoid code duplication.

This FileExists function appears to be identical to the one in scripts/go/_internal/vad/main.go. Consider moving it to a shared utility package to follow DRY principles.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 2778498 and a571d45.

📒 Files selected for processing (3)
  • go-api-examples/vad/main.go (2 hunks)
  • go-api-examples/vad/run.sh (1 hunks)
  • scripts/go/sherpa_onnx.go (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
go-api-examples/vad/main.go (2)
scripts/go/_internal/vad/main.go (1)
  • FileExists (159-166)
scripts/go/sherpa_onnx.go (5)
  • NewVoiceActivityDetector (1221-1256)
  • DeleteVoiceActivityDetector (1258-1261)
  • NewCircularBuffer (1172-1176)
  • DeleteCircularBuffer (1167-1170)
  • GeneratedAudio (939-944)
🔇 Additional comments (4)
scripts/go/sherpa_onnx.go (2)

1145-1156: LGTM! Well-structured extension for Ten-VAD support.

The new TenVadModelConfig struct and its integration into VadModelConfig follow the existing pattern established by SileroVadModelConfig, maintaining consistency in the API design.


1233-1240: Proper memory management for Ten-VAD configuration.

The initialization correctly allocates and defers cleanup of C strings, following the same safe pattern used for Silero VAD configuration.

go-api-examples/vad/main.go (2)

21-40: Good implementation of dynamic VAD model selection.

The code properly checks for the presence of model files and configures the appropriate VAD parameters. The window size differs between models (512 for Silero, 256 for Ten-VAD), which is correctly handled.


146-157: Correct implementation of int16 to float32 conversion.

The function properly handles little-endian byte order and normalizes samples to the [-1, 1] range using the standard division by 32768.

Comment thread go-api-examples/vad/run.sh
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