Repository navigation
Add Pascal API for Moonshine v2 #3248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ paraformer_itn | |
| sense_voice | ||
| telespeech_ctc | ||
| moonshine | ||
| moonshine_v2 | ||
| dolphin_ctc | ||
| zipformer_ctc | ||
| wenet_ctc | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| { Copyright (c) 2024-2026 Xiaomi Corporation } | ||
|
|
||
| { | ||
| This file shows how to use a non-streaming Moonshine v2 model | ||
| to decode files. | ||
|
|
||
| You can download the model files from | ||
| https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models | ||
| } | ||
|
|
||
| program moonshine_v2; | ||
|
|
||
| {$mode objfpc} | ||
|
|
||
| uses | ||
| sherpa_onnx, | ||
| DateUtils, | ||
| SysUtils; | ||
|
|
||
| var | ||
| Wave: TSherpaOnnxWave; | ||
| WaveFilename: AnsiString; | ||
|
|
||
| Config: TSherpaOnnxOfflineRecognizerConfig; | ||
| Recognizer: TSherpaOnnxOfflineRecognizer; | ||
| Stream: TSherpaOnnxOfflineStream; | ||
| RecognitionResult: TSherpaOnnxOfflineRecognizerResult; | ||
|
|
||
| Start: TDateTime; | ||
| Stop: TDateTime; | ||
|
|
||
| Elapsed: Single; | ||
| Duration: Single; | ||
| RealTimeFactor: Single; | ||
| begin | ||
| Initialize(Config); | ||
|
|
||
| Config.ModelConfig.Moonshine.Encoder := './sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27/encoder_model.ort'; | ||
| Config.ModelConfig.Moonshine.MergedDecoder := './sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27/decoder_model_merged.ort'; | ||
|
|
||
| Config.ModelConfig.Tokens := './sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27/tokens.txt'; | ||
| Config.ModelConfig.Provider := 'cpu'; | ||
| Config.ModelConfig.NumThreads := 1; | ||
| Config.ModelConfig.Debug := False; | ||
|
|
||
| WaveFilename := './sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27/test_wavs/0.wav'; | ||
|
Comment on lines
+36
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The model directory path is repeated multiple times. To improve maintainability, you can define it as a local variable. This makes it easier to update the path in one place if needed. |
||
|
|
||
| Wave := SherpaOnnxReadWave(WaveFilename); | ||
|
|
||
| Recognizer := TSherpaOnnxOfflineRecognizer.Create(Config); | ||
| Stream := Recognizer.CreateStream(); | ||
| Start := Now; | ||
|
|
||
| Stream.AcceptWaveform(Wave.Samples, Wave.SampleRate); | ||
| Recognizer.Decode(Stream); | ||
|
|
||
| RecognitionResult := Recognizer.GetResult(Stream); | ||
|
|
||
| Stop := Now; | ||
|
|
||
| Elapsed := MilliSecondsBetween(Stop, Start) / 1000; | ||
| Duration := Length(Wave.Samples) / Wave.SampleRate; | ||
| RealTimeFactor := Elapsed / Duration; | ||
|
|
||
| WriteLn(RecognitionResult.ToString); | ||
| WriteLn(Format('NumThreads %d', [Config.ModelConfig.NumThreads])); | ||
| WriteLn(Format('Elapsed %.3f s', [Elapsed])); | ||
| WriteLn(Format('Wave duration %.3f s', [Duration])); | ||
| WriteLn(Format('RTF = %.3f/%.3f = %.3f', [Elapsed, Duration, RealTimeFactor])); | ||
|
|
||
| {Free resources to avoid memory leak. | ||
|
|
||
| Note: You don't need to invoke them for this simple script. | ||
| However, you have to invoke them in your own large/complex project. | ||
| } | ||
| FreeAndNil(Stream); | ||
| FreeAndNil(Recognizer); | ||
| end. | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,42 @@ | ||||||||||||||||||||||||
| #!/usr/bin/env bash | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| set -ex | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd ) | ||||||||||||||||||||||||
| SHERPA_ONNX_DIR=$(cd $SCRIPT_DIR/../.. && pwd) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| echo "SHERPA_ONNX_DIR: $SHERPA_ONNX_DIR" | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if [[ ! -f ../../build/install/lib/libsherpa-onnx-c-api.dylib && ! -f ../../build/install/lib/libsherpa-onnx-c-api.so && ! -f ../../build/install/lib/sherpa-onnx-c-api.dll ]]; then | ||||||||||||||||||||||||
| mkdir -p ../../build | ||||||||||||||||||||||||
| pushd ../../build | ||||||||||||||||||||||||
| cmake \ | ||||||||||||||||||||||||
| -DCMAKE_INSTALL_PREFIX=./install \ | ||||||||||||||||||||||||
| -DSHERPA_ONNX_ENABLE_PYTHON=OFF \ | ||||||||||||||||||||||||
| -DSHERPA_ONNX_ENABLE_TESTS=OFF \ | ||||||||||||||||||||||||
| -DSHERPA_ONNX_ENABLE_CHECK=OFF \ | ||||||||||||||||||||||||
| -DBUILD_SHARED_LIBS=ON \ | ||||||||||||||||||||||||
| -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ | ||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| cmake --build . --target install --config Release | ||||||||||||||||||||||||
| ls -lh lib | ||||||||||||||||||||||||
| popd | ||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if [ ! -f ./sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27/encoder_model.ort ]; then | ||||||||||||||||||||||||
| curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27.tar.bz2 | ||||||||||||||||||||||||
| tar xvf sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27.tar.bz2 | ||||||||||||||||||||||||
| rm sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27.tar.bz2 | ||||||||||||||||||||||||
| fi | ||||||||||||||||||||||||
|
Comment on lines
+27
to
+31
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The model name
Suggested change
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| fpc \ | ||||||||||||||||||||||||
| -dSHERPA_ONNX_USE_SHARED_LIBS \ | ||||||||||||||||||||||||
| -Fu$SHERPA_ONNX_DIR/sherpa-onnx/pascal-api \ | ||||||||||||||||||||||||
| -Fl$SHERPA_ONNX_DIR/build/install/lib \ | ||||||||||||||||||||||||
| ./moonshine_v2.pas | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| export LD_LIBRARY_PATH=$SHERPA_ONNX_DIR/build/install/lib:$LD_LIBRARY_PATH | ||||||||||||||||||||||||
| export DYLD_LIBRARY_PATH=$SHERPA_ONNX_DIR/build/install/lib:$DYLD_LIBRARY_PATH | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| ./moonshine_v2 | ||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
./run-moonshine-v2.shstep is missing therm -rf sherpa-onnx-*andecho "---"lines that every other step in the workflow uses. While the moonshine-v2 directory (sherpa-onnx-moonshine-tiny-en-quantized-2026-02-27) will eventually be cleaned up by therm -rf sherpa-onnx-*that followsrun-moonshine.sh, the inconsistency could leave artifacts during therun-moonshine.shrun. The pattern used throughout the workflow is that each script is immediately followed by its own cleanup and separator. This should be corrected to match the established pattern.