diff --git a/.github/scripts/node-addon/README-optional.md b/.github/scripts/node-addon/README-optional.md new file mode 100644 index 00000000..152a8984 --- /dev/null +++ b/.github/scripts/node-addon/README-optional.md @@ -0,0 +1,3 @@ +# Introduction + +Please see [sherpa-onnx-node](https://www.npmjs.com/package/sherpa-onnx-node) diff --git a/.github/scripts/node-addon/README.md b/.github/scripts/node-addon/README.md new file mode 100644 index 00000000..cd776968 --- /dev/null +++ b/.github/scripts/node-addon/README.md @@ -0,0 +1,11 @@ +# Introduction + +Please see +https://github.com/k2-fsa/sherpa-onnx/blob/master/nodejs-addon-examples/README.md +for usages. + + +||Method|Support multiple threads|Minimum required node version| +|---|---|---|---| +|this package| https://github.com/nodejs/node-addon-api | Yes | v10| +|https://www.npmjs.com/package/sherpa-onnx| WebAssembly | No | v18| diff --git a/.github/scripts/node-addon/index.js b/.github/scripts/node-addon/index.js new file mode 100644 index 00000000..20141ac6 --- /dev/null +++ b/.github/scripts/node-addon/index.js @@ -0,0 +1 @@ +module.exports = require('./sherpa-onnx.node'); diff --git a/.github/scripts/node-addon/notes.md b/.github/scripts/node-addon/notes.md new file mode 100644 index 00000000..07344bdd --- /dev/null +++ b/.github/scripts/node-addon/notes.md @@ -0,0 +1,8 @@ +# Introduction + +See also + + - https://github.com/WonderInventions/node-webrtc/blob/develop/package.json + - https://stackoverflow.com/questions/15176082/npm-package-json-os-specific-dependency + - https://github.com/WonderInventions/node-webrtc/blob/develop/lib/binding.js + - cross-compiling https://github.com/nodejs/node-gyp/issues/829#issuecomment-665527032 diff --git a/.github/scripts/node-addon/package-optional.json b/.github/scripts/node-addon/package-optional.json new file mode 100644 index 00000000..6c0edf8c --- /dev/null +++ b/.github/scripts/node-addon/package-optional.json @@ -0,0 +1,53 @@ +{ + "name": "sherpa-onnx-PLATFORM-ARCH", + "version": "SHERPA_ONNX_VERSION", + "description": "Speech-to-text and text-to-speech using Next-gen Kaldi without internet connection", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/k2-fsa/sherpa-onnx.git" + }, + "keywords": [ + "speech to text", + "text to speech", + "transcription", + "real-time speech recognition", + "without internet connection", + "embedded systems", + "open source", + "zipformer", + "asr", + "tts", + "stt", + "c++", + "onnxruntime", + "onnx", + "ai", + "next-gen kaldi", + "offline", + "privacy", + "open source", + "vad", + "speaker id", + "language id", + "node-addon-api", + "streaming speech recognition", + "speech", + "recognition" + ], + "author": "The next-gen Kaldi team", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/k2-fsa/sherpa-onnx/issues" + }, + "homepage": "https://github.com/k2-fsa/sherpa-onnx#readme", + "os": [ + "PLATFORM" + ], + "cpu": [ + "ARCH" + ] +} diff --git a/.github/scripts/node-addon/package.json b/.github/scripts/node-addon/package.json new file mode 100644 index 00000000..f26f4dd8 --- /dev/null +++ b/.github/scripts/node-addon/package.json @@ -0,0 +1,52 @@ +{ + "name": "sherpa-onnx-node", + "version": "SHERPA_ONNX_VERSION", + "description": "Speech-to-text and text-to-speech using Next-gen Kaldi without internet connection", + "main": "sherpa-onnx.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/k2-fsa/sherpa-onnx.git" + }, + "keywords": [ + "speech to text", + "text to speech", + "transcription", + "real-time speech recognition", + "without internet connection", + "embedded systems", + "open source", + "zipformer", + "asr", + "tts", + "stt", + "c++", + "onnxruntime", + "onnx", + "ai", + "next-gen kaldi", + "offline", + "privacy", + "open source", + "vad", + "speaker id", + "language id", + "node-addon-api", + "streaming speech recognition", + "speech", + "recognition" + ], + "author": "The next-gen Kaldi team", + "license": "Apache-2.0", + "bugs": { + "url": "https://github.com/k2-fsa/sherpa-onnx/issues" + }, + "homepage": "https://github.com/k2-fsa/sherpa-onnx#readme", + "optionalDependencies": { + "sherpa-onnx-darwin-arm64": "^SHERPA_ONNX_VERSION", + "sherpa-onnx-darwin-x64": "^SHERPA_ONNX_VERSION", + "sherpa-onnx-linux-x64": "^SHERPA_ONNX_VERSION" + } +} diff --git a/.github/scripts/node-addon/run.sh b/.github/scripts/node-addon/run.sh new file mode 100755 index 00000000..219cdfe4 --- /dev/null +++ b/.github/scripts/node-addon/run.sh @@ -0,0 +1,37 @@ +#!/usr/bin/env bash + +sherpa_onnx_dir=$PWD +echo "sherpa_onnx_dir: $sherpa_onnx_dir" + +src_dir=$sherpa_onnx_dir/.github/scripts/node-addon + +platform=$(node -p "require('os').platform()") +arch=$(node -p "require('os').arch()") + +SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) +echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" + +if [ -z $owner ]; then + owner=k2-fsa +fi + +sed -i.bak s/SHERPA_ONNX_VERSION/$SHERPA_ONNX_VERSION/g $src_dir/package-optional.json +sed -i.bak s/k2-fsa/$owner/g $src_dir/package-optional.json +sed -i.bak s/PLATFORM/$platform/g $src_dir/package-optional.json +sed -i.bak s/ARCH/$arch/g $src_dir/package-optional.json + +git diff $src_dir/package-optional.json + +dst=$sherpa_onnx_dir/sherpa-onnx-node +mkdir -p $dst + +cp $src_dir/package-optional.json $dst/package.json +cp $src_dir/index.js $dst/ +cp $src_dir/README-optional.md $dst/README.md + +cp -fv build/install/lib/lib* $dst/ || true +cp -fv build/install/lib/*dll $dst/ || true + +cp scripts/node-addon-api/build/Release/sherpa-onnx.node $dst/ + +ls $dst diff --git a/.github/scripts/test-nodejs-addon-npm.sh b/.github/scripts/test-nodejs-addon-npm.sh new file mode 100755 index 00000000..ab6fd572 --- /dev/null +++ b/.github/scripts/test-nodejs-addon-npm.sh @@ -0,0 +1,23 @@ +#!/usr/bin/env bash + +set -ex + +d=nodejs-addon-examples +echo "dir: $d" +cd $d +npm install --verbose +git status +ls -lh +ls -lh node_modules + +export DYLD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-darwin-x64:$DYLD_LIBRARY_PATH +export DYLD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-darwin-arm64:$DYLD_LIBRARY_PATH +export LD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-linux-x64:$LD_LIBRARY_PATH + +curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 +tar xvf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 +rm sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 + +node test_asr_streaming_transducer.js + +rm -rf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20 diff --git a/.github/workflows/npm-addon-linux-x64.yaml b/.github/workflows/npm-addon-linux-x64.yaml new file mode 100644 index 00000000..3578cab8 --- /dev/null +++ b/.github/workflows/npm-addon-linux-x64.yaml @@ -0,0 +1,122 @@ +name: npm-addon-linux-x64 + +on: + workflow_dispatch: + +concurrency: + group: npm-addon-linux-x64-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + id-token: write + +jobs: + npm-addon-linux-x64: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest] + python-version: ["3.8"] + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - uses: actions/setup-node@v4 + with: + registry-url: 'https://registry.npmjs.org' + + - name: Display node version + shell: bash + run: | + node --version + + - name: Build sherpa-onnx + uses: addnab/docker-run-action@v3 + with: + image: quay.io/pypa/manylinux2014_x86_64 + options: | + --volume ${{ github.workspace }}/:/shared/ + shell: bash + run: | + uname -a + gcc --version + cmake --version + cd /shared + + mkdir build + cd build + cmake \ + -DCMAKE_INSTALL_PREFIX=./install \ + -DBUILD_SHARED_LIBS=ON \ + -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \ + -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ + -DSHERPA_ONNX_ENABLE_BINARY=OFF \ + .. + make -j install + + - name: Build sherpa-onnx node-addon + shell: bash + run: | + export PKG_CONFIG_PATH=$PWD/build/install:$PKG_CONFIG_PATH + sudo mkdir /shared + sudo ln -s $PWD/build /shared/ + + ls -lh /shared/build + + pkg-config --cflags sherpa-onnx + pkg-config --libs sherpa-onnx + + cd scripts/node-addon-api/ + + npm i + + ./node_modules/.bin/node-gyp configure build --verbose + + - name: Prepare for publish + shell: bash + run: | + export PKG_CONFIG_PATH=$PWD/build/install:$PKG_CONFIG_PATH + + owner=${{ github.repository_owner }} + export owner + + + echo "---" + ls -lh build/install/lib/ + sudo chown -R runner ./build + echo "---" + ls -lh build/install/lib/ + echo "---" + + # find build/install/lib/ -maxdepth 1 -type l + # find build/install/lib/ -maxdepth 1 -type l -delete + # + # echo "---" + # ls -lh build/install/lib/ + + .github/scripts/node-addon/run.sh + + - name: Display files to be published + shell: bash + run: | + ls -lh ./sherpa-onnx-node + + - name: Publish + shell: bash + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + run: | + cd ./sherpa-onnx-node + npm install + npm ci + # see https://docs.npmjs.com/generating-provenance-statements + npm publish --provenance --access public diff --git a/.github/workflows/npm-addon-macos.yaml b/.github/workflows/npm-addon-macos.yaml new file mode 100644 index 00000000..96e7dd3e --- /dev/null +++ b/.github/workflows/npm-addon-macos.yaml @@ -0,0 +1,114 @@ +name: npm-addon-macos + +on: + workflow_dispatch: + +concurrency: + group: npm-addon-macos-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + id-token: write + +jobs: + npm-addon-macos: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [macos-11, macos-14] + python-version: ["3.8"] + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - uses: actions/setup-node@v4 + with: + registry-url: 'https://registry.npmjs.org' + + - name: Display node version + shell: bash + run: | + node --version + + - name: ccache + uses: hendrikmuhs/ccache-action@v1.2 + with: + key: ${{ matrix.os }}-release-shared + + - name: Build sherpa-onnx + shell: bash + run: | + export CMAKE_CXX_COMPILER_LAUNCHER=ccache + export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" + + mkdir build + cd build + cmake \ + -DCMAKE_INSTALL_PREFIX=./install \ + -DBUILD_SHARED_LIBS=ON \ + -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \ + -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ + -DSHERPA_ONNX_ENABLE_BINARY=OFF \ + .. + make -j install + + + + - name: Build sherpa-onnx node-addon + shell: bash + run: | + export PKG_CONFIG_PATH=$PWD/build/install:$PKG_CONFIG_PATH + + pkg-config --cflags sherpa-onnx + pkg-config --libs sherpa-onnx + + cd scripts/node-addon-api/ + + npm i + + ./node_modules/.bin/node-gyp configure build --verbose + + - name: Prepare for publish + shell: bash + run: | + export PKG_CONFIG_PATH=$PWD/build/install:$PKG_CONFIG_PATH + + owner=${{ github.repository_owner }} + export owner + + + ls -lh build/install/lib/ + echo "---" + + # find build/install/lib/ -maxdepth 1 -type l + # find build/install/lib/ -maxdepth 1 -type l -delete + + # echo "---" + # ls -lh build/install/lib/ + + .github/scripts/node-addon/run.sh + + - name: Display files to be published + shell: bash + run: | + ls -lh ./sherpa-onnx-node + + - name: Publish + shell: bash + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + run: | + cd ./sherpa-onnx-node + npm install + npm ci + # see https://docs.npmjs.com/generating-provenance-statements + npm publish --provenance --access public diff --git a/.github/workflows/npm-addon.yaml b/.github/workflows/npm-addon.yaml new file mode 100644 index 00000000..45067180 --- /dev/null +++ b/.github/workflows/npm-addon.yaml @@ -0,0 +1,80 @@ +name: npm-addon + +on: + workflow_dispatch: + +concurrency: + group: npm-addon-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + id-token: write + +jobs: + npm-addon: + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest] + python-version: ["3.8"] + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - name: Setup Python ${{ matrix.python-version }} + uses: actions/setup-python@v5 + with: + python-version: ${{ matrix.python-version }} + + - uses: actions/setup-node@v4 + with: + registry-url: 'https://registry.npmjs.org' + + - name: Display node version + shell: bash + run: | + node --version + + - name: ccache + uses: hendrikmuhs/ccache-action@v1.2 + with: + key: ${{ matrix.os }}-release-shared + + - name: Prepare for publish + shell: bash + run: | + owner=${{ github.repository_owner }} + export owner + + SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2) + echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION" + + src_dir=.github/scripts/node-addon + sed -i.bak s/SHERPA_ONNX_VERSION/$SHERPA_ONNX_VERSION/g $src_dir/package.json + sed -i.bak s/k2-fsa/$owner/g $src_dir/package.json + + dst=sherpa-onnx-node + mkdir $dst + cp $src_dir/package.json $dst/ + cp $src_dir/README.md $dst/ + cp scripts/node-addon-api/lib/*.js $dst/ + + - name: Display files to be published + shell: bash + run: | + ls -lh ./sherpa-onnx-node + + - name: Publish + shell: bash + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + run: | + cd ./sherpa-onnx-node + npm install + npm ci + # see https://docs.npmjs.com/generating-provenance-statements + npm publish --provenance --access public diff --git a/.github/workflows/test-nodejs-addon-api.yaml b/.github/workflows/test-nodejs-addon-api.yaml index 4ceba0f9..49d3b717 100644 --- a/.github/workflows/test-nodejs-addon-api.yaml +++ b/.github/workflows/test-nodejs-addon-api.yaml @@ -5,7 +5,7 @@ on: branches: - master paths: - - '.github/workflows/test-node-addon-api.yaml' + - '.github/workflows/test-nodejs-addon-api.yaml' - 'CMakeLists.txt' - 'cmake/**' - 'sherpa-onnx/csrc/*' @@ -16,7 +16,7 @@ on: branches: - master paths: - - '.github/workflows/test-node-addon-api.yaml' + - '.github/workflows/test-nodejs-addon-api.yaml' - 'CMakeLists.txt' - 'cmake/**' - 'sherpa-onnx/csrc/*' @@ -34,12 +34,13 @@ permissions: jobs: test-node-addon-api: - name: ${{ matrix.os }} + name: ${{ matrix.os }} ${{ matrix.node-version }} runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: - os: [macos-latest, macos-14] + os: [macos-11, macos-14, ubuntu-20.04, ubuntu-22.04] + node-version: ["16", "17", "18", "19", "21", "22"] python-version: ["3.8"] steps: @@ -55,6 +56,7 @@ jobs: - uses: actions/setup-node@v4 with: registry-url: 'https://registry.npmjs.org' + node-version: ${{ matrix.node-version }} - name: Display node version shell: bash @@ -69,9 +71,18 @@ jobs: - name: Build sherpa-onnx shell: bash run: | + export CMAKE_CXX_COMPILER_LAUNCHER=ccache + export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" + mkdir build cd build - cmake -DCMAKE_INSTALL_PREFIX=/tmp/sherpa-onnx -DBUILD_SHARED_LIBS=ON .. + cmake \ + -DCMAKE_INSTALL_PREFIX=/tmp/sherpa-onnx \ + -DBUILD_SHARED_LIBS=ON \ + -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \ + -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ + -DSHERPA_ONNX_ENABLE_BINARY=OFF \ + .. make -j make install @@ -87,8 +98,6 @@ jobs: pkg-config --cflags sherpa-onnx pkg-config --libs sherpa-onnx - a=$(pkg-config --cflags sherpa-onnx);a=${a:2};echo $a - npm i ./node_modules/.bin/node-gyp configure build --verbose diff --git a/.github/workflows/test-nodejs-addon-npm.yaml b/.github/workflows/test-nodejs-addon-npm.yaml new file mode 100644 index 00000000..8f54c5af --- /dev/null +++ b/.github/workflows/test-nodejs-addon-npm.yaml @@ -0,0 +1,66 @@ +name: test-node-addon-npm + +on: + push: + branches: + - master + paths: + - '.github/workflows/test-nodejs-addon-npm.yaml' + - 'CMakeLists.txt' + - 'cmake/**' + - 'sherpa-onnx/csrc/*' + - 'sherpa-onnx/c-api/*' + - 'scripts/node-addon-api/**' + - 'scripts/node-addon-api/*.js' + - 'nodejs-addon-examples/package.json' + + pull_request: + branches: + - master + paths: + - '.github/workflows/test-nodejs-addon-npm.yaml' + - 'CMakeLists.txt' + - 'cmake/**' + - 'sherpa-onnx/csrc/*' + - 'sherpa-onnx/c-api/*' + - 'scripts/node-addon-api/*.js' + - 'nodejs-addon-examples/package.json' + + workflow_dispatch: + +concurrency: + group: test-node-addon-npm-${{ github.ref }} + cancel-in-progress: true + +permissions: + contents: read + +jobs: + test-node-addon-npm: + name: ${{ matrix.os }} node v${{ matrix.node-version }} + runs-on: ${{ matrix.os }} + strategy: + fail-fast: false + matrix: + os: [macos-11, macos-14, ubuntu-20.04, ubuntu-22.04] + node-version: ["16", "17", "18", "19", "21", "22"] + + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + + - uses: actions/setup-node@v4 + with: + registry-url: 'https://registry.npmjs.org' + node-version: ${{ matrix.node-version }} + + - name: Display node version + shell: bash + run: | + node --version + + - name: Run tests + shell: bash + run: | + .github/scripts/test-nodejs-addon-npm.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index 99336706..f0fdb858 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -266,6 +266,7 @@ endif() add_subdirectory(sherpa-onnx) if(SHERPA_ONNX_ENABLE_C_API AND SHERPA_ONNX_ENABLE_BINARY) + set(SHERPA_ONNX_PKG_WITH_CARGS "-lcargs") add_subdirectory(c-api-examples) endif() diff --git a/cmake/sherpa-onnx-no-tts.pc.in b/cmake/sherpa-onnx-no-tts.pc.in index df193043..4637b6ca 100644 --- a/cmake/sherpa-onnx-no-tts.pc.in +++ b/cmake/sherpa-onnx-no-tts.pc.in @@ -13,4 +13,4 @@ Cflags: -I"${includedir}" # Note: -lcargs is required only for the following file # https://github.com/k2-fsa/sherpa-onnx/blob/master/c-api-examples/decode-file-c-api.c # We add it here so that users don't need to specify -lcargs when compiling decode-file-c-api.c -Libs: -L"${libdir}" -lsherpa-onnx-c-api -lsherpa-onnx-core -lkaldi-decoder-core -lsherpa-onnx-kaldifst-core -lsherpa-onnx-fst -lkaldi-native-fbank-core -lcargs -lonnxruntime -Wl,-rpath,${libdir} @SHERPA_ONNX_PKG_CONFIG_EXTRA_LIBS@ +Libs: -L"${libdir}" -lsherpa-onnx-c-api -lsherpa-onnx-core -lkaldi-decoder-core -lsherpa-onnx-kaldifst-core -lsherpa-onnx-fst -lkaldi-native-fbank-core -lonnxruntime -Wl,-rpath,${libdir} @SHERPA_ONNX_PKG_WITH_CARGS@ @SHERPA_ONNX_PKG_CONFIG_EXTRA_LIBS@ diff --git a/cmake/sherpa-onnx.pc.in b/cmake/sherpa-onnx.pc.in index 0870f3ae..fd37eeeb 100644 --- a/cmake/sherpa-onnx.pc.in +++ b/cmake/sherpa-onnx.pc.in @@ -13,4 +13,4 @@ Cflags: -I"${includedir}" # Note: -lcargs is required only for the following file # https://github.com/k2-fsa/sherpa-onnx/blob/master/c-api-examples/decode-file-c-api.c # We add it here so that users don't need to specify -lcargs when compiling decode-file-c-api.c -Libs: -L"${libdir}" -lsherpa-onnx-c-api -lsherpa-onnx-core -lkaldi-decoder-core -lsherpa-onnx-kaldifst-core -lsherpa-onnx-fstfar -lsherpa-onnx-fst -lkaldi-native-fbank-core -lpiper_phonemize -lespeak-ng -lucd -lcargs -lonnxruntime -Wl,-rpath,${libdir} @SHERPA_ONNX_PKG_CONFIG_EXTRA_LIBS@ +Libs: -L"${libdir}" -lsherpa-onnx-c-api -lsherpa-onnx-core -lkaldi-decoder-core -lsherpa-onnx-kaldifst-core -lsherpa-onnx-fstfar -lsherpa-onnx-fst -lkaldi-native-fbank-core -lpiper_phonemize -lespeak-ng -lucd -lonnxruntime -Wl,-rpath,${libdir} @SHERPA_ONNX_PKG_WITH_CARGS@ @SHERPA_ONNX_PKG_CONFIG_EXTRA_LIBS@ diff --git a/nodejs-addon-examples/README.md b/nodejs-addon-examples/README.md new file mode 100644 index 00000000..28dd0ff0 --- /dev/null +++ b/nodejs-addon-examples/README.md @@ -0,0 +1,34 @@ +# Introduction + +Note: You need `Node >= 10`. + +This repo contains examples for NodeJS. +It uses [node-addon-api](https://github.com/nodejs/node-addon-api) to wrap +`sherpa-onnx` for NodeJS and it supports multiple threads. + +Note: [../nodejs-examples](../nodejs-examples) uses WebAssembly to wrap +`sherpa-onnx` for NodeJS and it does not support multiple threads. + +Before you continue, please first run + +```bash +npm install + +# For macOS x64 +export DYLD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-darwin-x64:$DYLD_LIBRARY_PATH + +# For macOS arm64 +export DYLD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-darwin-arm64:$DYLD_LIBRARY_PATH + +# For Linux x64 +export LD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-linux-x64:$LD_LIBRARY_PATH +``` + + +## Streaming speech recognition with zipformer transducer +```bash +wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 +tar xvf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 +rm sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2 + +``` diff --git a/nodejs-addon-examples/package.json b/nodejs-addon-examples/package.json new file mode 100644 index 00000000..d69336d1 --- /dev/null +++ b/nodejs-addon-examples/package.json @@ -0,0 +1,6 @@ +{ + "dependencies": { + "sherpa-onnx-node": "*", + "perf_hooks": "*" + } +} diff --git a/nodejs-addon-examples/test_asr_streaming_transducer.js b/nodejs-addon-examples/test_asr_streaming_transducer.js new file mode 100644 index 00000000..a92c9aca --- /dev/null +++ b/nodejs-addon-examples/test_asr_streaming_transducer.js @@ -0,0 +1,57 @@ +// Copyright (c) 2024 Xiaomi Corporation +const sherpa_onnx = require('sherpa-onnx-node'); +const performance = require('perf_hooks').performance; + + +// Please download test files from +// https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models +const config = { + 'featConfig': { + 'sampleRate': 16000, + 'featureDim': 80, + }, + 'modelConfig': { + 'transducer': { + 'encoder': + './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/encoder-epoch-99-avg-1.onnx', + 'decoder': + './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/decoder-epoch-99-avg-1.onnx', + 'joiner': + './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/joiner-epoch-99-avg-1.onnx', + }, + 'tokens': + './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/tokens.txt', + 'numThreads': 2, + 'provider': 'cpu', + 'debug': 1, + 'modelType': 'zipformer', + } +}; + +const waveFilename = + './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/test_wavs/0.wav'; + +const recognizer = new sherpa_onnx.OnlineRecognizer(config); +console.log('Started') +let start = performance.now(); +const stream = recognizer.createStream(); +const wave = sherpa_onnx.readWave(waveFilename); +stream.acceptWaveform(wave.samples, wave.sampleRate); + +const tailPadding = new Float32Array(wave.sampleRate * 0.4); +stream.acceptWaveform(tailPadding, wave.sampleRate); + +while (recognizer.isReady(stream)) { + recognizer.decode(stream); +} +result = recognizer.getResult(stream) +let stop = performance.now(); +console.log('Done') + +const elapsed_seconds = (stop - start) / 1000; +const duration = wave.samples.length / wave.sampleRate; +const real_time_factor = elapsed_seconds / duration; +console.log('Wave duration', duration.toFixed(3), 'secodns') +console.log('Elapsed', elapsed_seconds.toFixed(3), 'secodns') +console.log('RTF', real_time_factor.toFixed(3)) +console.log('result', result.text) diff --git a/nodejs-examples/README.md b/nodejs-examples/README.md index 4c3df2a6..9f6ac9b5 100644 --- a/nodejs-examples/README.md +++ b/nodejs-examples/README.md @@ -1,7 +1,15 @@ # Introduction +Note: You need `Node >= 18`. + This directory contains nodejs examples for [sherpa-onnx](https://github.com/k2-fsa/sherpa-onnx). +It uses WebAssembly to wrap `sherpa-onnx` for NodeJS and it does not support multiple threads. + +Note: [../nodejs-addon-examples](../nodejs-addon-examples) uses +[node-addon-api](https://github.com/nodejs/node-addon-api) to wrap +`sherpa-onnx` for NodeJS and it supports multiple threads. + Before you continue, please first run ```bash @@ -13,7 +21,6 @@ npm i In the following, we describe how to use [sherpa-onnx](https://github.com/k2-fsa/sherpa-onnx) for text-to-speech and speech-to-text. -Note: You need `Node >= 18`. # Text-to-speech diff --git a/scripts/node-addon-api/README.md b/scripts/node-addon-api/README.md index 6ed366ea..f9b3cf0b 100644 --- a/scripts/node-addon-api/README.md +++ b/scripts/node-addon-api/README.md @@ -1,3 +1,23 @@ # Introduction This folder contains `node-addon-api` wrapper for `sherpa-onnx`. + +Caution: This folder is for developer only. + +## Usage + +```bash +git clone https://github.com/k2-fsa/sherpa-onnx +cd sherpa-onnx +mkdir build +cd build +cmake -DCMAKE_INSTALL_PREFIX=./install -DBUILD_SHARED_LIBS=ON .. +make -j install +export PKG_CONFIG_PATH=$PWD/install:$PKG_CONFIG_PATH +cd ../scripts/node-addon-api/ + +./node_modules/.bin/node-gyp build --verbose + +# see test/test_asr_streaming_transducer.js +# for usages +``` diff --git a/scripts/node-addon-api/binding.gyp b/scripts/node-addon-api/binding.gyp index 1294188b..b54cc95e 100644 --- a/scripts/node-addon-api/binding.gyp +++ b/scripts/node-addon-api/binding.gyp @@ -1,7 +1,7 @@ { 'targets': [ { - 'target_name': 'sherpa-onnx-node-addon-api-native', + 'target_name': 'sherpa-onnx', 'sources': [ 'src/sherpa-onnx-node-addon-api.cc', 'src/streaming-asr.cc', @@ -9,7 +9,7 @@ ], 'include_dirs': [ "