Skip to content

Commit

Permalink
Add Pascal API for reading wave files (#1243)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Aug 11, 2024
1 parent 968623a commit 65f1c0f
Show file tree
Hide file tree
Showing 8 changed files with 261 additions and 3 deletions.
112 changes: 112 additions & 0 deletions .github/workflows/pascal.yaml
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
|--------|-------|-----------|-------|---------|---------------|
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |

| 7. Kotlin | 8. Swift | 9. Go | 10. Dart | 11. Rust |
|-----------|----------|-------|----------|----------|
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
| 7. Kotlin | 8. Swift | 9. Go | 10. Dart | 11. Rust | 12. Pascal |
|-----------|----------|-------|----------|----------|------------|
| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |

For Rust support, please see https://github.com/thewh1teagle/sherpa-rs

Expand Down
1 change: 1 addition & 0 deletions pascal-api-examples/read-wav/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main
21 changes: 21 additions & 0 deletions pascal-api-examples/read-wav/main.pas
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.
38 changes: 38 additions & 0 deletions pascal-api-examples/read-wav/run.sh
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
7 changes: 7 additions & 0 deletions sherpa-onnx/pascal-api/README.md
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.
77 changes: 77 additions & 0 deletions sherpa-onnx/pascal-api/sherpa_onnx.pas
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.

0 comments on commit 65f1c0f

Please sign in to comment.