Fangjun Kuang
Committed by GitHub

Add Pascal API for reading wave files (#1243)

  1 +name: pascal
  2 +
  3 +on:
  4 + push:
  5 + branches:
  6 + - master
  7 + paths:
  8 + - '.github/workflows/pascal.yaml'
  9 + - 'CMakeLists.txt'
  10 + - 'cmake/**'
  11 + - 'pascal-api-examples/**'
  12 + - 'sherpa-onnx/csrc/*'
  13 + - 'sherpa-onnx/c-api/*'
  14 + - 'sherpa-onnx/pascal-api/*'
  15 + pull_request:
  16 + branches:
  17 + - master
  18 + paths:
  19 + - '.github/workflows/pascal.yaml'
  20 + - 'CMakeLists.txt'
  21 + - 'cmake/**'
  22 + - 'pascal-api-examples/**'
  23 + - 'sherpa-onnx/csrc/*'
  24 + - 'sherpa-onnx/c-api/*'
  25 + - 'sherpa-onnx/pascal-api/*'
  26 +
  27 + workflow_dispatch:
  28 +
  29 +concurrency:
  30 + group: pascal-${{ github.ref }}
  31 + cancel-in-progress: true
  32 +
  33 +permissions:
  34 + contents: read
  35 +
  36 +jobs:
  37 + pascal:
  38 + runs-on: ${{ matrix.os }}
  39 + strategy:
  40 + fail-fast: false
  41 + matrix:
  42 + os: [ubuntu-latest, macos-latest, macos-13]
  43 +
  44 + steps:
  45 + - uses: actions/checkout@v4
  46 + with:
  47 + fetch-depth: 0
  48 +
  49 + - name: ccache
  50 + uses: hendrikmuhs/ccache-action@v1.2
  51 + with:
  52 + key: ${{ matrix.os }}
  53 +
  54 + - name: Install Free pascal compiler (ubuntu)
  55 + if: matrix.os == 'ubuntu-latest'
  56 + shell: bash
  57 + run: |
  58 + sudo apt-get update
  59 + sudo apt-get install -q -y fpc
  60 +
  61 + - name: Install Free pascal compiler (macos)
  62 + if: matrix.os == 'macos-latest' || matrix.os == 'macos-13'
  63 + shell: bash
  64 + run: |
  65 + brew install fpc
  66 + # brew install --cask lazarus
  67 +
  68 + - name: FPC info
  69 + shell: bash
  70 + run: |
  71 + which fpc
  72 + fpc -i
  73 +
  74 + - name: OS info
  75 + shell: bash
  76 + run: |
  77 + uname -a
  78 +
  79 + - name: Configure CMake
  80 + shell: bash
  81 + run: |
  82 + export CMAKE_CXX_COMPILER_LAUNCHER=ccache
  83 + export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
  84 + cmake --version
  85 +
  86 + mkdir build
  87 + cd build
  88 +
  89 + cmake \
  90 + -D BUILD_SHARED_LIBS=ON \
  91 + -D SHERPA_ONNX_ENABLE_BINARY=OFF \
  92 + -D CMAKE_BUILD_TYPE=Release \
  93 + ..
  94 +
  95 + - name: Build sherpa-onnx
  96 + shell: bash
  97 + run: |
  98 + export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
  99 +
  100 + cd build
  101 + make -j2 sherpa-onnx-c-api
  102 +
  103 + - name: Run Pascal test
  104 + shell: bash
  105 + run: |
  106 + cd ./pascal-api-examples
  107 +
  108 + echo "----read-wav test-----"
  109 + pushd read-wav
  110 + ./run.sh
  111 + ls -lh
  112 + popd
@@ -114,3 +114,5 @@ lib*.a @@ -114,3 +114,5 @@ lib*.a
114 sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17 114 sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17
115 *.bak 115 *.bak
116 vits-melo-tts-zh_en 116 vits-melo-tts-zh_en
  117 +*.o
  118 +*.ppu
@@ -29,9 +29,9 @@ @@ -29,9 +29,9 @@
29 |--------|-------|-----------|-------|---------|---------------| 29 |--------|-------|-----------|-------|---------|---------------|
30 | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | 30 | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
31 31
32 -| 7. Kotlin | 8. Swift | 9. Go | 10. Dart | 11. Rust |  
33 -|-----------|----------|-------|----------|----------|  
34 -| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | 32 +| 7. Kotlin | 8. Swift | 9. Go | 10. Dart | 11. Rust | 12. Pascal |
  33 +|-----------|----------|-------|----------|----------|------------|
  34 +| ✔️ | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
35 35
36 For Rust support, please see https://github.com/thewh1teagle/sherpa-rs 36 For Rust support, please see https://github.com/thewh1teagle/sherpa-rs
37 37
  1 +{ Copyright (c) 2024 Xiaomi Corporation }
  2 +program main;
  3 +
  4 +{$mode objfpc}
  5 +
  6 +uses
  7 + sherpa_onnx;
  8 +
  9 +var
  10 + Wave: TSherpaOnnxWave;
  11 + S: Single;
  12 + I: Integer;
  13 +begin
  14 + Wave := SherpaOnnxReadWave('./lei-jun-test.wav');
  15 + WriteLn('info ', Wave.SampleRate, ' ', Length(Wave.Samples));
  16 + S := 0;
  17 + for i := Low(Wave.Samples) to High(Wave.Samples) do
  18 + S += Wave.Samples[i];
  19 +
  20 + WriteLn('sum is ', S);
  21 +end.
  1 +#!/usr/bin/env bash
  2 +
  3 +set -ex
  4 +
  5 +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
  6 +SHERPA_ONNX_DIR=$(cd $SCRIPT_DIR/../.. && pwd)
  7 +
  8 +echo "SHERPA_ONNX_DIR: $SHERPA_ONNX_DIR"
  9 +
  10 +if [[ ! -f ../../build/lib/libsherpa-onnx-c-api.dylib && ! -f ../../build/lib/libsherpa-onnx-c-api.so ]]; then
  11 + mkdir -p ../../build
  12 + pushd ../../build
  13 + cmake \
  14 + -DSHERPA_ONNX_ENABLE_PYTHON=OFF \
  15 + -DSHERPA_ONNX_ENABLE_TESTS=OFF \
  16 + -DSHERPA_ONNX_ENABLE_CHECK=OFF \
  17 + -DBUILD_SHARED_LIBS=ON \
  18 + -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
  19 + ..
  20 +
  21 + make -j4 sherpa-onnx-c-api
  22 + ls -lh lib
  23 + popd
  24 +fi
  25 +
  26 +if [ ! -f ./lei-jun-test.wav ]; then
  27 + curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/lei-jun-test.wav
  28 +fi
  29 +
  30 +fpc \
  31 + -Fu$SHERPA_ONNX_DIR/sherpa-onnx/pascal-api \
  32 + -Fl$SHERPA_ONNX_DIR/build/lib \
  33 + ./main.pas
  34 +
  35 +export LD_LIBRARY_PATH=$SHERPA_ONNX_DIR/build/lib:$LD_LIBRARY_PATH
  36 +export DYLD_LIBRARY_PATH=$SHERPA_ONNX_DIR/build/lib:$DYLD_LIBRARY_PATH
  37 +
  38 +./main
  1 +# Introduction
  2 +
  3 +This directory contains APIs for [Object Pascal](https://en.wikipedia.org/wiki/Object_Pascal).
  4 +
  5 +Please see
  6 +https://github.com/k2-fsa/sherpa-onnx/tree/master/pascal-api-examples
  7 +for usages.
  1 +{ Copyright (c) 2024 Xiaomi Corporation }
  2 +
  3 +unit sherpa_onnx;
  4 +
  5 +{$mode objfpc}
  6 +
  7 +interface
  8 +
  9 +type
  10 + TSherpaOnnxWave = record
  11 + Samples: array of Single; { normalized to the range [-1, 1] }
  12 + SampleRate: Integer;
  13 + end;
  14 +
  15 +{ It supports reading a single channel wave with 16-bit encoded samples.
  16 + Samples are normalized to the range [-1, 1].
  17 +}
  18 +function SherpaOnnxReadWave(Filename: string): TSherpaOnnxWave;
  19 +
  20 +implementation
  21 +
  22 +uses
  23 + ctypes;
  24 +
  25 +const
  26 + {See https://www.freepascal.org/docs-html/prog/progap7.html}
  27 +
  28 + {$IFDEF WINDOWS}
  29 + SherpaOnnxLibName = 'sherpa-onnx-c-api.dll';
  30 + {$ENDIF}
  31 +
  32 + {$IFDEF DARWIN}
  33 + SherpaOnnxLibName = 'sherpa-onnx-c-api';
  34 + {$linklib sherpa-onnx-c-api}
  35 + {$ENDIF}
  36 +
  37 + {$IFDEF LINUX}
  38 + SherpaOnnxLibName = 'libsherpa-onnx-c-api.so';
  39 + {$ENDIF}
  40 +
  41 +type
  42 + SherpaOnnxWave = record
  43 + Samples: pcfloat;
  44 + SampleRate: cint32;
  45 + NumSamples: cint32;
  46 + end;
  47 +
  48 + PSherpaOnnxWave = ^SherpaOnnxWave;
  49 +
  50 +function SherpaOnnxReadWaveWrapper(Filename: PAnsiChar): PSherpaOnnxWave; cdecl;
  51 + external SherpaOnnxLibName name 'SherpaOnnxReadWave';
  52 +
  53 +procedure SherpaOnnxFreeWaveWrapper(P: PSherpaOnnxWave); cdecl;
  54 + external SherpaOnnxLibName name 'SherpaOnnxFreeWave';
  55 +
  56 +function SherpaOnnxReadWave(Filename: string): TSherpaOnnxWave;
  57 +var
  58 + AnsiFilename: AnsiString;
  59 + PFilename: PAnsiChar;
  60 + PWave: PSherpaOnnxWave;
  61 + I: Integer;
  62 +begin
  63 + AnsiFilename := Filename;
  64 + PFilename := PAnsiChar(AnsiFilename);
  65 + PWave := SherpaOnnxReadWaveWrapper(PFilename);
  66 +
  67 + SetLength(Result.Samples, PWave^.NumSamples);
  68 +
  69 + Result.SampleRate := PWave^.SampleRate;
  70 +
  71 + for I := Low(Result.Samples) to High(Result.Samples) do
  72 + Result.Samples[i] := PWave^.Samples[i];
  73 +
  74 + SherpaOnnxFreeWaveWrapper(PWave);
  75 +end;
  76 +
  77 +end.