-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Pascal API for reading wave files (#1243)
- Loading branch information
1 parent
968623a
commit 65f1c0f
Showing
8 changed files
with
261 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
name: pascal | ||
|
||
on: | ||
push: | ||
branches: | ||
- master | ||
paths: | ||
- '.github/workflows/pascal.yaml' | ||
- 'CMakeLists.txt' | ||
- 'cmake/**' | ||
- 'pascal-api-examples/**' | ||
- 'sherpa-onnx/csrc/*' | ||
- 'sherpa-onnx/c-api/*' | ||
- 'sherpa-onnx/pascal-api/*' | ||
pull_request: | ||
branches: | ||
- master | ||
paths: | ||
- '.github/workflows/pascal.yaml' | ||
- 'CMakeLists.txt' | ||
- 'cmake/**' | ||
- 'pascal-api-examples/**' | ||
- 'sherpa-onnx/csrc/*' | ||
- 'sherpa-onnx/c-api/*' | ||
- 'sherpa-onnx/pascal-api/*' | ||
|
||
workflow_dispatch: | ||
|
||
concurrency: | ||
group: pascal-${{ github.ref }} | ||
cancel-in-progress: true | ||
|
||
permissions: | ||
contents: read | ||
|
||
jobs: | ||
pascal: | ||
runs-on: ${{ matrix.os }} | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
os: [ubuntu-latest, macos-latest, macos-13] | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
fetch-depth: 0 | ||
|
||
- name: ccache | ||
uses: hendrikmuhs/[email protected] | ||
with: | ||
key: ${{ matrix.os }} | ||
|
||
- name: Install Free pascal compiler (ubuntu) | ||
if: matrix.os == 'ubuntu-latest' | ||
shell: bash | ||
run: | | ||
sudo apt-get update | ||
sudo apt-get install -q -y fpc | ||
- name: Install Free pascal compiler (macos) | ||
if: matrix.os == 'macos-latest' || matrix.os == 'macos-13' | ||
shell: bash | ||
run: | | ||
brew install fpc | ||
# brew install --cask lazarus | ||
- name: FPC info | ||
shell: bash | ||
run: | | ||
which fpc | ||
fpc -i | ||
- name: OS info | ||
shell: bash | ||
run: | | ||
uname -a | ||
- name: Configure CMake | ||
shell: bash | ||
run: | | ||
export CMAKE_CXX_COMPILER_LAUNCHER=ccache | ||
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" | ||
cmake --version | ||
mkdir build | ||
cd build | ||
cmake \ | ||
-D BUILD_SHARED_LIBS=ON \ | ||
-D SHERPA_ONNX_ENABLE_BINARY=OFF \ | ||
-D CMAKE_BUILD_TYPE=Release \ | ||
.. | ||
- name: Build sherpa-onnx | ||
shell: bash | ||
run: | | ||
export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" | ||
cd build | ||
make -j2 sherpa-onnx-c-api | ||
- name: Run Pascal test | ||
shell: bash | ||
run: | | ||
cd ./pascal-api-examples | ||
echo "----read-wav test-----" | ||
pushd read-wav | ||
./run.sh | ||
ls -lh | ||
popd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,3 +114,5 @@ lib*.a | |
sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17 | ||
*.bak | ||
vits-melo-tts-zh_en | ||
*.o | ||
*.ppu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ Copyright (c) 2024 Xiaomi Corporation } | ||
program main; | ||
|
||
{$mode objfpc} | ||
|
||
uses | ||
sherpa_onnx; | ||
|
||
var | ||
Wave: TSherpaOnnxWave; | ||
S: Single; | ||
I: Integer; | ||
begin | ||
Wave := SherpaOnnxReadWave('./lei-jun-test.wav'); | ||
WriteLn('info ', Wave.SampleRate, ' ', Length(Wave.Samples)); | ||
S := 0; | ||
for i := Low(Wave.Samples) to High(Wave.Samples) do | ||
S += Wave.Samples[i]; | ||
|
||
WriteLn('sum is ', S); | ||
end. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#!/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/lib/libsherpa-onnx-c-api.dylib && ! -f ../../build/lib/libsherpa-onnx-c-api.so ]]; then | ||
mkdir -p ../../build | ||
pushd ../../build | ||
cmake \ | ||
-DSHERPA_ONNX_ENABLE_PYTHON=OFF \ | ||
-DSHERPA_ONNX_ENABLE_TESTS=OFF \ | ||
-DSHERPA_ONNX_ENABLE_CHECK=OFF \ | ||
-DBUILD_SHARED_LIBS=ON \ | ||
-DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ | ||
.. | ||
|
||
make -j4 sherpa-onnx-c-api | ||
ls -lh lib | ||
popd | ||
fi | ||
|
||
if [ ! -f ./lei-jun-test.wav ]; then | ||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/lei-jun-test.wav | ||
fi | ||
|
||
fpc \ | ||
-Fu$SHERPA_ONNX_DIR/sherpa-onnx/pascal-api \ | ||
-Fl$SHERPA_ONNX_DIR/build/lib \ | ||
./main.pas | ||
|
||
export LD_LIBRARY_PATH=$SHERPA_ONNX_DIR/build/lib:$LD_LIBRARY_PATH | ||
export DYLD_LIBRARY_PATH=$SHERPA_ONNX_DIR/build/lib:$DYLD_LIBRARY_PATH | ||
|
||
./main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Introduction | ||
|
||
This directory contains APIs for [Object Pascal](https://en.wikipedia.org/wiki/Object_Pascal). | ||
|
||
Please see | ||
https://github.com/k2-fsa/sherpa-onnx/tree/master/pascal-api-examples | ||
for usages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
{ Copyright (c) 2024 Xiaomi Corporation } | ||
|
||
unit sherpa_onnx; | ||
|
||
{$mode objfpc} | ||
|
||
interface | ||
|
||
type | ||
TSherpaOnnxWave = record | ||
Samples: array of Single; { normalized to the range [-1, 1] } | ||
SampleRate: Integer; | ||
end; | ||
|
||
{ It supports reading a single channel wave with 16-bit encoded samples. | ||
Samples are normalized to the range [-1, 1]. | ||
} | ||
function SherpaOnnxReadWave(Filename: string): TSherpaOnnxWave; | ||
|
||
implementation | ||
|
||
uses | ||
ctypes; | ||
|
||
const | ||
{See https://www.freepascal.org/docs-html/prog/progap7.html} | ||
|
||
{$IFDEF WINDOWS} | ||
SherpaOnnxLibName = 'sherpa-onnx-c-api.dll'; | ||
{$ENDIF} | ||
|
||
{$IFDEF DARWIN} | ||
SherpaOnnxLibName = 'sherpa-onnx-c-api'; | ||
{$linklib sherpa-onnx-c-api} | ||
{$ENDIF} | ||
|
||
{$IFDEF LINUX} | ||
SherpaOnnxLibName = 'libsherpa-onnx-c-api.so'; | ||
{$ENDIF} | ||
|
||
type | ||
SherpaOnnxWave = record | ||
Samples: pcfloat; | ||
SampleRate: cint32; | ||
NumSamples: cint32; | ||
end; | ||
|
||
PSherpaOnnxWave = ^SherpaOnnxWave; | ||
|
||
function SherpaOnnxReadWaveWrapper(Filename: PAnsiChar): PSherpaOnnxWave; cdecl; | ||
external SherpaOnnxLibName name 'SherpaOnnxReadWave'; | ||
|
||
procedure SherpaOnnxFreeWaveWrapper(P: PSherpaOnnxWave); cdecl; | ||
external SherpaOnnxLibName name 'SherpaOnnxFreeWave'; | ||
|
||
function SherpaOnnxReadWave(Filename: string): TSherpaOnnxWave; | ||
var | ||
AnsiFilename: AnsiString; | ||
PFilename: PAnsiChar; | ||
PWave: PSherpaOnnxWave; | ||
I: Integer; | ||
begin | ||
AnsiFilename := Filename; | ||
PFilename := PAnsiChar(AnsiFilename); | ||
PWave := SherpaOnnxReadWaveWrapper(PFilename); | ||
|
||
SetLength(Result.Samples, PWave^.NumSamples); | ||
|
||
Result.SampleRate := PWave^.SampleRate; | ||
|
||
for I := Low(Result.Samples) to High(Result.Samples) do | ||
Result.Samples[i] := PWave^.Samples[i]; | ||
|
||
SherpaOnnxFreeWaveWrapper(PWave); | ||
end; | ||
|
||
end. |