Skip to content

Refactor Go API examples to use sherpa_onnx.ReadWave() - #3415

Merged
csukuangfj merged 2 commits into
k2-fsa:masterfrom
csukuangfj:go
Mar 26, 2026
Merged

csukuangfj merged 2 commits into
k2-fsa:masterfrom
csukuangfj:go

Conversation

@csukuangfj

@csukuangfj csukuangfj commented Mar 26, 2026 •

Copy link
Copy Markdown
Collaborator

Summary by CodeRabbit

  • Refactor
    • Unified audio loading across example apps to use a shared waveform loader, simplifying code and improving consistency.
  • Bug Fixes
    • Added robust error handling for audio loading and updated duration reporting so displayed audio lengths are computed from the loaded waveform.

@dosubot dosubot Bot added the size:XL This PR changes 500-999 lines, ignoring generated files. label Mar 26, 2026
@coderabbitai

coderabbitai Bot commented Mar 26, 2026 •

Copy link
Copy Markdown

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 10251e31-8272-4ef0-99bf-e541d05be33e

📥 Commits

Reviewing files that changed from the base of the PR and between 954479e and 9e2c1c9.

📒 Files selected for processing (3)
  • go-api-examples/non-streaming-decode-files/main.go
  • go-api-examples/streaming-decode-files/main.go
  • go-api-examples/streaming-hlg-decoding/main.go
✅ Files skipped from review due to trivial changes (2)
  • go-api-examples/streaming-hlg-decoding/main.go
  • go-api-examples/non-streaming-decode-files/main.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • go-api-examples/streaming-decode-files/main.go

📝 Walkthrough

Walkthrough

Eight Go example programs replace local WAV-reading helpers with a single call to sherpa.ReadWave(), add a nil-check that fatals on failure, and pass wave.SampleRate and wave.Samples into recognizer/stream ingestion sites.

Changes

Cohort / File(s) Summary
Non-streaming examples
go-api-examples/non-streaming-canary-decode-files/main.go, go-api-examples/non-streaming-decode-files/main.go, go-api-examples/non-streaming-funasr-nano-decode-files/main.go, go-api-examples/non-streaming-medasr-ctc-decode-files/main.go, go-api-examples/non-streaming-moonshine-v2-decode-files/main.go, go-api-examples/non-streaming-omnilingual-asr-ctc-decode-files/main.go
Removed local WAV parsing helpers and related imports. Replaced with sherpa.ReadWave(waveFilename), added nil-check with log.Fatalf, and updated calls to use wave.SampleRate and wave.Samples.
Streaming examples
go-api-examples/streaming-decode-files/main.go, go-api-examples/streaming-hlg-decoding/main.go
Removed local WAV-loading utilities and imports. Switched to sherpa.ReadWave(), added nil-check fatal handling, updated padding calculations and stream.AcceptWaveform calls to use wave.SampleRate and wave.Samples, and adjusted wave-duration logging.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Poem

🐇
I nibbled bytes in fields of sound,
Helpers hopping close to ground.
Sherpa called — I left the maze,
One path now shines through audio haze.
Hooray, less hops, more ears to please!

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change across all modified files: refactoring Go API examples to use the sherpa_onnx.ReadWave() function instead of local WAV-loading utilities.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request refactors the Go API examples by replacing the custom WAV file reading implementations with the dedicated sherpa_onnx.ReadWave() function. This change streamlines the audio input process, removes redundant code, and leverages the library's built-in capabilities for handling audio waveforms, resulting in cleaner and more consistent example code.

Highlights

  • API Usage Refinement: The Go API examples now exclusively use the sherpa_onnx.ReadWave() function for loading WAV audio files, standardizing the approach to audio input.
  • Code Simplification: Custom WAV parsing logic, including imports for bytes, encoding/binary, os, and github.com/youpy/go-wav, along with the readWave and samplesInt16ToFloat helper functions, has been removed from all affected examples.
  • Consistency Across Examples: All Go API examples for non-streaming and streaming decoding have been updated to reflect this change, ensuring a uniform and more maintainable codebase.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request refactors several Go API examples by replacing custom wave file reading logic with a new sherpa.ReadWave function from the sherpa-onnx-go library. This change simplifies the code by removing redundant imports and functions, and introduces error handling for wave file reading. Feedback suggests improving readability by storing flag.Arg(0) in a variable and ensuring consistent camelCase naming for variables like wav_filename to align with Go conventions.

Comment on lines +77 to +82
log.Println("Reading", flag.Arg(0))

samples, sampleRate := readWave(flag.Arg(0))
wave := sherpa.ReadWave(flag.Arg(0))
if wave == nil {
log.Fatalf("Failed to read %v", flag.Arg(0))
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

To improve readability and avoid calling flag.Arg(0) multiple times, it's a good practice to store its result in a variable.

Suggested change
log.Println("Reading", flag.Arg(0))
samples, sampleRate := readWave(flag.Arg(0))
wave := sherpa.ReadWave(flag.Arg(0))
if wave == nil {
log.Fatalf("Failed to read %v", flag.Arg(0))
}
waveFilename := flag.Arg(0)
log.Println("Reading", waveFilename)
wave := sherpa.ReadWave(waveFilename)
if wave == nil {
log.Fatalf("Failed to read %v", waveFilename)
}

Comment on lines +46 to +51
log.Println("Reading", flag.Arg(0))

samples, sampleRate := readWave(flag.Arg(0))
wave := sherpa.ReadWave(flag.Arg(0))
if wave == nil {
log.Fatalf("Failed to read %v", flag.Arg(0))
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

To improve readability and avoid calling flag.Arg(0) multiple times, it's a good practice to store its result in a variable.

Suggested change
log.Println("Reading", flag.Arg(0))
samples, sampleRate := readWave(flag.Arg(0))
wave := sherpa.ReadWave(flag.Arg(0))
if wave == nil {
log.Fatalf("Failed to read %v", flag.Arg(0))
}
waveFilename := flag.Arg(0)
log.Println("Reading", waveFilename)
wave := sherpa.ReadWave(waveFilename)
if wave == nil {
log.Fatalf("Failed to read %v", waveFilename)
}

Comment on lines +26 to +31
wav_filename := "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/test_wavs/8k.wav"

samples, sampleRate := readWave(wav_filename)
wave := sherpa.ReadWave(wav_filename)
if wave == nil {
log.Fatalf("Failed to read %v", wav_filename)
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

For consistency with Go conventions and other examples in this repository, it's better to use camelCase for variable names. Please rename wav_filename to waveFilename.

Suggested change
wav_filename := "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/test_wavs/8k.wav"
samples, sampleRate := readWave(wav_filename)
wave := sherpa.ReadWave(wav_filename)
if wave == nil {
log.Fatalf("Failed to read %v", wav_filename)
}
waveFilename := "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/test_wavs/8k.wav"
wave := sherpa.ReadWave(waveFilename)
if wave == nil {
log.Fatalf("Failed to read %v", waveFilename)
}

@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.

🧹 Nitpick comments (1)
go-api-examples/streaming-hlg-decoding/main.go (1)

26-26: Minor: Pre-existing naming inconsistency.

The variable wav_filename uses snake_case while other example files use camelCase (waveFilename). This is a pre-existing inconsistency, not introduced by this PR, but could be addressed for consistency if desired.

♻️ Optional: Align variable naming with other examples
-	wav_filename := "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/test_wavs/8k.wav"
+	waveFilename := "./sherpa-onnx-streaming-zipformer-ctc-small-2024-03-18/test_wavs/8k.wav"

-	wave := sherpa.ReadWave(wav_filename)
+	wave := sherpa.ReadWave(waveFilename)
 	if wave == nil {
-		log.Fatalf("Failed to read %v", wav_filename)
+		log.Fatalf("Failed to read %v", waveFilename)
 	}
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@go-api-examples/streaming-hlg-decoding/main.go` at line 26, Rename the
snake_case variable wav_filename to camelCase waveFilename across the file to
match other examples; update all references (declaration and uses) where
wav_filename appears (e.g., in main or any helper functions) so the identifier
is consistently waveFilename and build/imports remain unchanged.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@go-api-examples/streaming-hlg-decoding/main.go`:
- Line 26: Rename the snake_case variable wav_filename to camelCase waveFilename
across the file to match other examples; update all references (declaration and
uses) where wav_filename appears (e.g., in main or any helper functions) so the
identifier is consistently waveFilename and build/imports remain unchanged.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6d789010-87c9-43fa-8d4e-88c8c2db80b6

📥 Commits

Reviewing files that changed from the base of the PR and between 2da3796 and 954479e.

📒 Files selected for processing (8)
  • go-api-examples/non-streaming-canary-decode-files/main.go
  • go-api-examples/non-streaming-decode-files/main.go
  • go-api-examples/non-streaming-funasr-nano-decode-files/main.go
  • go-api-examples/non-streaming-medasr-ctc-decode-files/main.go
  • go-api-examples/non-streaming-moonshine-v2-decode-files/main.go
  • go-api-examples/non-streaming-omnilingual-asr-ctc-decode-files/main.go
  • go-api-examples/streaming-decode-files/main.go
  • go-api-examples/streaming-hlg-decoding/main.go

@csukuangfj
csukuangfj merged commit ca22668 into k2-fsa:master Mar 26, 2026
1 check passed
@csukuangfj
csukuangfj deleted the go branch March 26, 2026 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:XL This PR changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant