Fangjun Kuang
Committed by GitHub

Publish node-addon-api wrapper for sherpa-onnx as npm packages (#829)

  1 +# Introduction
  2 +
  3 +Please see [sherpa-onnx-node](https://www.npmjs.com/package/sherpa-onnx-node)
  1 +# Introduction
  2 +
  3 +Please see
  4 +https://github.com/k2-fsa/sherpa-onnx/blob/master/nodejs-addon-examples/README.md
  5 +for usages.
  6 +
  7 +
  8 +||Method|Support multiple threads|Minimum required node version|
  9 +|---|---|---|---|
  10 +|this package| https://github.com/nodejs/node-addon-api | Yes | v10|
  11 +|https://www.npmjs.com/package/sherpa-onnx| WebAssembly | No | v18|
  1 +module.exports = require('./sherpa-onnx.node');
  1 +# Introduction
  2 +
  3 +See also
  4 +
  5 + - https://github.com/WonderInventions/node-webrtc/blob/develop/package.json
  6 + - https://stackoverflow.com/questions/15176082/npm-package-json-os-specific-dependency
  7 + - https://github.com/WonderInventions/node-webrtc/blob/develop/lib/binding.js
  8 + - cross-compiling https://github.com/nodejs/node-gyp/issues/829#issuecomment-665527032
  1 +{
  2 + "name": "sherpa-onnx-PLATFORM-ARCH",
  3 + "version": "SHERPA_ONNX_VERSION",
  4 + "description": "Speech-to-text and text-to-speech using Next-gen Kaldi without internet connection",
  5 + "main": "index.js",
  6 + "scripts": {
  7 + "test": "echo \"Error: no test specified\" && exit 1"
  8 + },
  9 + "repository": {
  10 + "type": "git",
  11 + "url": "git+https://github.com/k2-fsa/sherpa-onnx.git"
  12 + },
  13 + "keywords": [
  14 + "speech to text",
  15 + "text to speech",
  16 + "transcription",
  17 + "real-time speech recognition",
  18 + "without internet connection",
  19 + "embedded systems",
  20 + "open source",
  21 + "zipformer",
  22 + "asr",
  23 + "tts",
  24 + "stt",
  25 + "c++",
  26 + "onnxruntime",
  27 + "onnx",
  28 + "ai",
  29 + "next-gen kaldi",
  30 + "offline",
  31 + "privacy",
  32 + "open source",
  33 + "vad",
  34 + "speaker id",
  35 + "language id",
  36 + "node-addon-api",
  37 + "streaming speech recognition",
  38 + "speech",
  39 + "recognition"
  40 + ],
  41 + "author": "The next-gen Kaldi team",
  42 + "license": "Apache-2.0",
  43 + "bugs": {
  44 + "url": "https://github.com/k2-fsa/sherpa-onnx/issues"
  45 + },
  46 + "homepage": "https://github.com/k2-fsa/sherpa-onnx#readme",
  47 + "os": [
  48 + "PLATFORM"
  49 + ],
  50 + "cpu": [
  51 + "ARCH"
  52 + ]
  53 +}
  1 +{
  2 + "name": "sherpa-onnx-node",
  3 + "version": "SHERPA_ONNX_VERSION",
  4 + "description": "Speech-to-text and text-to-speech using Next-gen Kaldi without internet connection",
  5 + "main": "sherpa-onnx.js",
  6 + "scripts": {
  7 + "test": "echo \"Error: no test specified\" && exit 1"
  8 + },
  9 + "repository": {
  10 + "type": "git",
  11 + "url": "git+https://github.com/k2-fsa/sherpa-onnx.git"
  12 + },
  13 + "keywords": [
  14 + "speech to text",
  15 + "text to speech",
  16 + "transcription",
  17 + "real-time speech recognition",
  18 + "without internet connection",
  19 + "embedded systems",
  20 + "open source",
  21 + "zipformer",
  22 + "asr",
  23 + "tts",
  24 + "stt",
  25 + "c++",
  26 + "onnxruntime",
  27 + "onnx",
  28 + "ai",
  29 + "next-gen kaldi",
  30 + "offline",
  31 + "privacy",
  32 + "open source",
  33 + "vad",
  34 + "speaker id",
  35 + "language id",
  36 + "node-addon-api",
  37 + "streaming speech recognition",
  38 + "speech",
  39 + "recognition"
  40 + ],
  41 + "author": "The next-gen Kaldi team",
  42 + "license": "Apache-2.0",
  43 + "bugs": {
  44 + "url": "https://github.com/k2-fsa/sherpa-onnx/issues"
  45 + },
  46 + "homepage": "https://github.com/k2-fsa/sherpa-onnx#readme",
  47 + "optionalDependencies": {
  48 + "sherpa-onnx-darwin-arm64": "^SHERPA_ONNX_VERSION",
  49 + "sherpa-onnx-darwin-x64": "^SHERPA_ONNX_VERSION",
  50 + "sherpa-onnx-linux-x64": "^SHERPA_ONNX_VERSION"
  51 + }
  52 +}
  1 +#!/usr/bin/env bash
  2 +
  3 +sherpa_onnx_dir=$PWD
  4 +echo "sherpa_onnx_dir: $sherpa_onnx_dir"
  5 +
  6 +src_dir=$sherpa_onnx_dir/.github/scripts/node-addon
  7 +
  8 +platform=$(node -p "require('os').platform()")
  9 +arch=$(node -p "require('os').arch()")
  10 +
  11 +SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
  12 +echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION"
  13 +
  14 +if [ -z $owner ]; then
  15 + owner=k2-fsa
  16 +fi
  17 +
  18 +sed -i.bak s/SHERPA_ONNX_VERSION/$SHERPA_ONNX_VERSION/g $src_dir/package-optional.json
  19 +sed -i.bak s/k2-fsa/$owner/g $src_dir/package-optional.json
  20 +sed -i.bak s/PLATFORM/$platform/g $src_dir/package-optional.json
  21 +sed -i.bak s/ARCH/$arch/g $src_dir/package-optional.json
  22 +
  23 +git diff $src_dir/package-optional.json
  24 +
  25 +dst=$sherpa_onnx_dir/sherpa-onnx-node
  26 +mkdir -p $dst
  27 +
  28 +cp $src_dir/package-optional.json $dst/package.json
  29 +cp $src_dir/index.js $dst/
  30 +cp $src_dir/README-optional.md $dst/README.md
  31 +
  32 +cp -fv build/install/lib/lib* $dst/ || true
  33 +cp -fv build/install/lib/*dll $dst/ || true
  34 +
  35 +cp scripts/node-addon-api/build/Release/sherpa-onnx.node $dst/
  36 +
  37 +ls $dst
  1 +#!/usr/bin/env bash
  2 +
  3 +set -ex
  4 +
  5 +d=nodejs-addon-examples
  6 +echo "dir: $d"
  7 +cd $d
  8 +npm install --verbose
  9 +git status
  10 +ls -lh
  11 +ls -lh node_modules
  12 +
  13 +export DYLD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-darwin-x64:$DYLD_LIBRARY_PATH
  14 +export DYLD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-darwin-arm64:$DYLD_LIBRARY_PATH
  15 +export LD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-linux-x64:$LD_LIBRARY_PATH
  16 +
  17 +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
  18 +tar xvf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
  19 +rm sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
  20 +
  21 +node test_asr_streaming_transducer.js
  22 +
  23 +rm -rf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20
  1 +name: npm-addon-linux-x64
  2 +
  3 +on:
  4 + workflow_dispatch:
  5 +
  6 +concurrency:
  7 + group: npm-addon-linux-x64-${{ github.ref }}
  8 + cancel-in-progress: true
  9 +
  10 +permissions:
  11 + contents: read
  12 + id-token: write
  13 +
  14 +jobs:
  15 + npm-addon-linux-x64:
  16 + runs-on: ${{ matrix.os }}
  17 + strategy:
  18 + fail-fast: false
  19 + matrix:
  20 + os: [ubuntu-latest]
  21 + python-version: ["3.8"]
  22 +
  23 + steps:
  24 + - uses: actions/checkout@v4
  25 + with:
  26 + fetch-depth: 0
  27 +
  28 + - name: Setup Python ${{ matrix.python-version }}
  29 + uses: actions/setup-python@v5
  30 + with:
  31 + python-version: ${{ matrix.python-version }}
  32 +
  33 + - uses: actions/setup-node@v4
  34 + with:
  35 + registry-url: 'https://registry.npmjs.org'
  36 +
  37 + - name: Display node version
  38 + shell: bash
  39 + run: |
  40 + node --version
  41 +
  42 + - name: Build sherpa-onnx
  43 + uses: addnab/docker-run-action@v3
  44 + with:
  45 + image: quay.io/pypa/manylinux2014_x86_64
  46 + options: |
  47 + --volume ${{ github.workspace }}/:/shared/
  48 + shell: bash
  49 + run: |
  50 + uname -a
  51 + gcc --version
  52 + cmake --version
  53 + cd /shared
  54 +
  55 + mkdir build
  56 + cd build
  57 + cmake \
  58 + -DCMAKE_INSTALL_PREFIX=./install \
  59 + -DBUILD_SHARED_LIBS=ON \
  60 + -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
  61 + -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
  62 + -DSHERPA_ONNX_ENABLE_BINARY=OFF \
  63 + ..
  64 + make -j install
  65 +
  66 + - name: Build sherpa-onnx node-addon
  67 + shell: bash
  68 + run: |
  69 + export PKG_CONFIG_PATH=$PWD/build/install:$PKG_CONFIG_PATH
  70 + sudo mkdir /shared
  71 + sudo ln -s $PWD/build /shared/
  72 +
  73 + ls -lh /shared/build
  74 +
  75 + pkg-config --cflags sherpa-onnx
  76 + pkg-config --libs sherpa-onnx
  77 +
  78 + cd scripts/node-addon-api/
  79 +
  80 + npm i
  81 +
  82 + ./node_modules/.bin/node-gyp configure build --verbose
  83 +
  84 + - name: Prepare for publish
  85 + shell: bash
  86 + run: |
  87 + export PKG_CONFIG_PATH=$PWD/build/install:$PKG_CONFIG_PATH
  88 +
  89 + owner=${{ github.repository_owner }}
  90 + export owner
  91 +
  92 +
  93 + echo "---"
  94 + ls -lh build/install/lib/
  95 + sudo chown -R runner ./build
  96 + echo "---"
  97 + ls -lh build/install/lib/
  98 + echo "---"
  99 +
  100 + # find build/install/lib/ -maxdepth 1 -type l
  101 + # find build/install/lib/ -maxdepth 1 -type l -delete
  102 + #
  103 + # echo "---"
  104 + # ls -lh build/install/lib/
  105 +
  106 + .github/scripts/node-addon/run.sh
  107 +
  108 + - name: Display files to be published
  109 + shell: bash
  110 + run: |
  111 + ls -lh ./sherpa-onnx-node
  112 +
  113 + - name: Publish
  114 + shell: bash
  115 + env:
  116 + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
  117 + run: |
  118 + cd ./sherpa-onnx-node
  119 + npm install
  120 + npm ci
  121 + # see https://docs.npmjs.com/generating-provenance-statements
  122 + npm publish --provenance --access public
  1 +name: npm-addon-macos
  2 +
  3 +on:
  4 + workflow_dispatch:
  5 +
  6 +concurrency:
  7 + group: npm-addon-macos-${{ github.ref }}
  8 + cancel-in-progress: true
  9 +
  10 +permissions:
  11 + contents: read
  12 + id-token: write
  13 +
  14 +jobs:
  15 + npm-addon-macos:
  16 + runs-on: ${{ matrix.os }}
  17 + strategy:
  18 + fail-fast: false
  19 + matrix:
  20 + os: [macos-11, macos-14]
  21 + python-version: ["3.8"]
  22 +
  23 + steps:
  24 + - uses: actions/checkout@v4
  25 + with:
  26 + fetch-depth: 0
  27 +
  28 + - name: Setup Python ${{ matrix.python-version }}
  29 + uses: actions/setup-python@v5
  30 + with:
  31 + python-version: ${{ matrix.python-version }}
  32 +
  33 + - uses: actions/setup-node@v4
  34 + with:
  35 + registry-url: 'https://registry.npmjs.org'
  36 +
  37 + - name: Display node version
  38 + shell: bash
  39 + run: |
  40 + node --version
  41 +
  42 + - name: ccache
  43 + uses: hendrikmuhs/ccache-action@v1.2
  44 + with:
  45 + key: ${{ matrix.os }}-release-shared
  46 +
  47 + - name: Build sherpa-onnx
  48 + shell: bash
  49 + run: |
  50 + export CMAKE_CXX_COMPILER_LAUNCHER=ccache
  51 + export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
  52 +
  53 + mkdir build
  54 + cd build
  55 + cmake \
  56 + -DCMAKE_INSTALL_PREFIX=./install \
  57 + -DBUILD_SHARED_LIBS=ON \
  58 + -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
  59 + -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
  60 + -DSHERPA_ONNX_ENABLE_BINARY=OFF \
  61 + ..
  62 + make -j install
  63 +
  64 +
  65 +
  66 + - name: Build sherpa-onnx node-addon
  67 + shell: bash
  68 + run: |
  69 + export PKG_CONFIG_PATH=$PWD/build/install:$PKG_CONFIG_PATH
  70 +
  71 + pkg-config --cflags sherpa-onnx
  72 + pkg-config --libs sherpa-onnx
  73 +
  74 + cd scripts/node-addon-api/
  75 +
  76 + npm i
  77 +
  78 + ./node_modules/.bin/node-gyp configure build --verbose
  79 +
  80 + - name: Prepare for publish
  81 + shell: bash
  82 + run: |
  83 + export PKG_CONFIG_PATH=$PWD/build/install:$PKG_CONFIG_PATH
  84 +
  85 + owner=${{ github.repository_owner }}
  86 + export owner
  87 +
  88 +
  89 + ls -lh build/install/lib/
  90 + echo "---"
  91 +
  92 + # find build/install/lib/ -maxdepth 1 -type l
  93 + # find build/install/lib/ -maxdepth 1 -type l -delete
  94 +
  95 + # echo "---"
  96 + # ls -lh build/install/lib/
  97 +
  98 + .github/scripts/node-addon/run.sh
  99 +
  100 + - name: Display files to be published
  101 + shell: bash
  102 + run: |
  103 + ls -lh ./sherpa-onnx-node
  104 +
  105 + - name: Publish
  106 + shell: bash
  107 + env:
  108 + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
  109 + run: |
  110 + cd ./sherpa-onnx-node
  111 + npm install
  112 + npm ci
  113 + # see https://docs.npmjs.com/generating-provenance-statements
  114 + npm publish --provenance --access public
  1 +name: npm-addon
  2 +
  3 +on:
  4 + workflow_dispatch:
  5 +
  6 +concurrency:
  7 + group: npm-addon-${{ github.ref }}
  8 + cancel-in-progress: true
  9 +
  10 +permissions:
  11 + contents: read
  12 + id-token: write
  13 +
  14 +jobs:
  15 + npm-addon:
  16 + runs-on: ${{ matrix.os }}
  17 + strategy:
  18 + fail-fast: false
  19 + matrix:
  20 + os: [ubuntu-latest]
  21 + python-version: ["3.8"]
  22 +
  23 + steps:
  24 + - uses: actions/checkout@v4
  25 + with:
  26 + fetch-depth: 0
  27 +
  28 + - name: Setup Python ${{ matrix.python-version }}
  29 + uses: actions/setup-python@v5
  30 + with:
  31 + python-version: ${{ matrix.python-version }}
  32 +
  33 + - uses: actions/setup-node@v4
  34 + with:
  35 + registry-url: 'https://registry.npmjs.org'
  36 +
  37 + - name: Display node version
  38 + shell: bash
  39 + run: |
  40 + node --version
  41 +
  42 + - name: ccache
  43 + uses: hendrikmuhs/ccache-action@v1.2
  44 + with:
  45 + key: ${{ matrix.os }}-release-shared
  46 +
  47 + - name: Prepare for publish
  48 + shell: bash
  49 + run: |
  50 + owner=${{ github.repository_owner }}
  51 + export owner
  52 +
  53 + SHERPA_ONNX_VERSION=$(grep "SHERPA_ONNX_VERSION" ./CMakeLists.txt | cut -d " " -f 2 | cut -d '"' -f 2)
  54 + echo "SHERPA_ONNX_VERSION $SHERPA_ONNX_VERSION"
  55 +
  56 + src_dir=.github/scripts/node-addon
  57 + sed -i.bak s/SHERPA_ONNX_VERSION/$SHERPA_ONNX_VERSION/g $src_dir/package.json
  58 + sed -i.bak s/k2-fsa/$owner/g $src_dir/package.json
  59 +
  60 + dst=sherpa-onnx-node
  61 + mkdir $dst
  62 + cp $src_dir/package.json $dst/
  63 + cp $src_dir/README.md $dst/
  64 + cp scripts/node-addon-api/lib/*.js $dst/
  65 +
  66 + - name: Display files to be published
  67 + shell: bash
  68 + run: |
  69 + ls -lh ./sherpa-onnx-node
  70 +
  71 + - name: Publish
  72 + shell: bash
  73 + env:
  74 + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
  75 + run: |
  76 + cd ./sherpa-onnx-node
  77 + npm install
  78 + npm ci
  79 + # see https://docs.npmjs.com/generating-provenance-statements
  80 + npm publish --provenance --access public
@@ -5,7 +5,7 @@ on: @@ -5,7 +5,7 @@ on:
5 branches: 5 branches:
6 - master 6 - master
7 paths: 7 paths:
8 - - '.github/workflows/test-node-addon-api.yaml' 8 + - '.github/workflows/test-nodejs-addon-api.yaml'
9 - 'CMakeLists.txt' 9 - 'CMakeLists.txt'
10 - 'cmake/**' 10 - 'cmake/**'
11 - 'sherpa-onnx/csrc/*' 11 - 'sherpa-onnx/csrc/*'
@@ -16,7 +16,7 @@ on: @@ -16,7 +16,7 @@ on:
16 branches: 16 branches:
17 - master 17 - master
18 paths: 18 paths:
19 - - '.github/workflows/test-node-addon-api.yaml' 19 + - '.github/workflows/test-nodejs-addon-api.yaml'
20 - 'CMakeLists.txt' 20 - 'CMakeLists.txt'
21 - 'cmake/**' 21 - 'cmake/**'
22 - 'sherpa-onnx/csrc/*' 22 - 'sherpa-onnx/csrc/*'
@@ -34,12 +34,13 @@ permissions: @@ -34,12 +34,13 @@ permissions:
34 34
35 jobs: 35 jobs:
36 test-node-addon-api: 36 test-node-addon-api:
37 - name: ${{ matrix.os }} 37 + name: ${{ matrix.os }} ${{ matrix.node-version }}
38 runs-on: ${{ matrix.os }} 38 runs-on: ${{ matrix.os }}
39 strategy: 39 strategy:
40 fail-fast: false 40 fail-fast: false
41 matrix: 41 matrix:
42 - os: [macos-latest, macos-14] 42 + os: [macos-11, macos-14, ubuntu-20.04, ubuntu-22.04]
  43 + node-version: ["16", "17", "18", "19", "21", "22"]
43 python-version: ["3.8"] 44 python-version: ["3.8"]
44 45
45 steps: 46 steps:
@@ -55,6 +56,7 @@ jobs: @@ -55,6 +56,7 @@ jobs:
55 - uses: actions/setup-node@v4 56 - uses: actions/setup-node@v4
56 with: 57 with:
57 registry-url: 'https://registry.npmjs.org' 58 registry-url: 'https://registry.npmjs.org'
  59 + node-version: ${{ matrix.node-version }}
58 60
59 - name: Display node version 61 - name: Display node version
60 shell: bash 62 shell: bash
@@ -69,9 +71,18 @@ jobs: @@ -69,9 +71,18 @@ jobs:
69 - name: Build sherpa-onnx 71 - name: Build sherpa-onnx
70 shell: bash 72 shell: bash
71 run: | 73 run: |
  74 + export CMAKE_CXX_COMPILER_LAUNCHER=ccache
  75 + export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH"
  76 +
72 mkdir build 77 mkdir build
73 cd build 78 cd build
74 - cmake -DCMAKE_INSTALL_PREFIX=/tmp/sherpa-onnx -DBUILD_SHARED_LIBS=ON .. 79 + cmake \
  80 + -DCMAKE_INSTALL_PREFIX=/tmp/sherpa-onnx \
  81 + -DBUILD_SHARED_LIBS=ON \
  82 + -DSHERPA_ONNX_ENABLE_WEBSOCKET=OFF \
  83 + -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \
  84 + -DSHERPA_ONNX_ENABLE_BINARY=OFF \
  85 + ..
75 make -j 86 make -j
76 make install 87 make install
77 88
@@ -87,8 +98,6 @@ jobs: @@ -87,8 +98,6 @@ jobs:
87 pkg-config --cflags sherpa-onnx 98 pkg-config --cflags sherpa-onnx
88 pkg-config --libs sherpa-onnx 99 pkg-config --libs sherpa-onnx
89 100
90 - a=$(pkg-config --cflags sherpa-onnx);a=${a:2};echo $a  
91 -  
92 npm i 101 npm i
93 102
94 ./node_modules/.bin/node-gyp configure build --verbose 103 ./node_modules/.bin/node-gyp configure build --verbose
  1 +name: test-node-addon-npm
  2 +
  3 +on:
  4 + push:
  5 + branches:
  6 + - master
  7 + paths:
  8 + - '.github/workflows/test-nodejs-addon-npm.yaml'
  9 + - 'CMakeLists.txt'
  10 + - 'cmake/**'
  11 + - 'sherpa-onnx/csrc/*'
  12 + - 'sherpa-onnx/c-api/*'
  13 + - 'scripts/node-addon-api/**'
  14 + - 'scripts/node-addon-api/*.js'
  15 + - 'nodejs-addon-examples/package.json'
  16 +
  17 + pull_request:
  18 + branches:
  19 + - master
  20 + paths:
  21 + - '.github/workflows/test-nodejs-addon-npm.yaml'
  22 + - 'CMakeLists.txt'
  23 + - 'cmake/**'
  24 + - 'sherpa-onnx/csrc/*'
  25 + - 'sherpa-onnx/c-api/*'
  26 + - 'scripts/node-addon-api/*.js'
  27 + - 'nodejs-addon-examples/package.json'
  28 +
  29 + workflow_dispatch:
  30 +
  31 +concurrency:
  32 + group: test-node-addon-npm-${{ github.ref }}
  33 + cancel-in-progress: true
  34 +
  35 +permissions:
  36 + contents: read
  37 +
  38 +jobs:
  39 + test-node-addon-npm:
  40 + name: ${{ matrix.os }} node v${{ matrix.node-version }}
  41 + runs-on: ${{ matrix.os }}
  42 + strategy:
  43 + fail-fast: false
  44 + matrix:
  45 + os: [macos-11, macos-14, ubuntu-20.04, ubuntu-22.04]
  46 + node-version: ["16", "17", "18", "19", "21", "22"]
  47 +
  48 + steps:
  49 + - uses: actions/checkout@v4
  50 + with:
  51 + fetch-depth: 0
  52 +
  53 + - uses: actions/setup-node@v4
  54 + with:
  55 + registry-url: 'https://registry.npmjs.org'
  56 + node-version: ${{ matrix.node-version }}
  57 +
  58 + - name: Display node version
  59 + shell: bash
  60 + run: |
  61 + node --version
  62 +
  63 + - name: Run tests
  64 + shell: bash
  65 + run: |
  66 + .github/scripts/test-nodejs-addon-npm.sh
@@ -266,6 +266,7 @@ endif() @@ -266,6 +266,7 @@ endif()
266 add_subdirectory(sherpa-onnx) 266 add_subdirectory(sherpa-onnx)
267 267
268 if(SHERPA_ONNX_ENABLE_C_API AND SHERPA_ONNX_ENABLE_BINARY) 268 if(SHERPA_ONNX_ENABLE_C_API AND SHERPA_ONNX_ENABLE_BINARY)
  269 + set(SHERPA_ONNX_PKG_WITH_CARGS "-lcargs")
269 add_subdirectory(c-api-examples) 270 add_subdirectory(c-api-examples)
270 endif() 271 endif()
271 272
@@ -13,4 +13,4 @@ Cflags: -I"${includedir}" @@ -13,4 +13,4 @@ Cflags: -I"${includedir}"
13 # Note: -lcargs is required only for the following file 13 # Note: -lcargs is required only for the following file
14 # https://github.com/k2-fsa/sherpa-onnx/blob/master/c-api-examples/decode-file-c-api.c 14 # https://github.com/k2-fsa/sherpa-onnx/blob/master/c-api-examples/decode-file-c-api.c
15 # We add it here so that users don't need to specify -lcargs when compiling decode-file-c-api.c 15 # We add it here so that users don't need to specify -lcargs when compiling decode-file-c-api.c
16 -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@ 16 +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@
@@ -13,4 +13,4 @@ Cflags: -I"${includedir}" @@ -13,4 +13,4 @@ Cflags: -I"${includedir}"
13 # Note: -lcargs is required only for the following file 13 # Note: -lcargs is required only for the following file
14 # https://github.com/k2-fsa/sherpa-onnx/blob/master/c-api-examples/decode-file-c-api.c 14 # https://github.com/k2-fsa/sherpa-onnx/blob/master/c-api-examples/decode-file-c-api.c
15 # We add it here so that users don't need to specify -lcargs when compiling decode-file-c-api.c 15 # We add it here so that users don't need to specify -lcargs when compiling decode-file-c-api.c
16 -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@ 16 +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@
  1 +# Introduction
  2 +
  3 +Note: You need `Node >= 10`.
  4 +
  5 +This repo contains examples for NodeJS.
  6 +It uses [node-addon-api](https://github.com/nodejs/node-addon-api) to wrap
  7 +`sherpa-onnx` for NodeJS and it supports multiple threads.
  8 +
  9 +Note: [../nodejs-examples](../nodejs-examples) uses WebAssembly to wrap
  10 +`sherpa-onnx` for NodeJS and it does not support multiple threads.
  11 +
  12 +Before you continue, please first run
  13 +
  14 +```bash
  15 +npm install
  16 +
  17 +# For macOS x64
  18 +export DYLD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-darwin-x64:$DYLD_LIBRARY_PATH
  19 +
  20 +# For macOS arm64
  21 +export DYLD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-darwin-arm64:$DYLD_LIBRARY_PATH
  22 +
  23 +# For Linux x64
  24 +export LD_LIBRARY_PATH=$PWD/node_modules/sherpa-onnx-linux-x64:$LD_LIBRARY_PATH
  25 +```
  26 +
  27 +
  28 +## Streaming speech recognition with zipformer transducer
  29 +```bash
  30 +wget https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
  31 +tar xvf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
  32 +rm sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20.tar.bz2
  33 +
  34 +```
  1 +{
  2 + "dependencies": {
  3 + "sherpa-onnx-node": "*",
  4 + "perf_hooks": "*"
  5 + }
  6 +}
  1 +// Copyright (c) 2024 Xiaomi Corporation
  2 +const sherpa_onnx = require('sherpa-onnx-node');
  3 +const performance = require('perf_hooks').performance;
  4 +
  5 +
  6 +// Please download test files from
  7 +// https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models
  8 +const config = {
  9 + 'featConfig': {
  10 + 'sampleRate': 16000,
  11 + 'featureDim': 80,
  12 + },
  13 + 'modelConfig': {
  14 + 'transducer': {
  15 + 'encoder':
  16 + './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/encoder-epoch-99-avg-1.onnx',
  17 + 'decoder':
  18 + './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/decoder-epoch-99-avg-1.onnx',
  19 + 'joiner':
  20 + './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/joiner-epoch-99-avg-1.onnx',
  21 + },
  22 + 'tokens':
  23 + './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/tokens.txt',
  24 + 'numThreads': 2,
  25 + 'provider': 'cpu',
  26 + 'debug': 1,
  27 + 'modelType': 'zipformer',
  28 + }
  29 +};
  30 +
  31 +const waveFilename =
  32 + './sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20/test_wavs/0.wav';
  33 +
  34 +const recognizer = new sherpa_onnx.OnlineRecognizer(config);
  35 +console.log('Started')
  36 +let start = performance.now();
  37 +const stream = recognizer.createStream();
  38 +const wave = sherpa_onnx.readWave(waveFilename);
  39 +stream.acceptWaveform(wave.samples, wave.sampleRate);
  40 +
  41 +const tailPadding = new Float32Array(wave.sampleRate * 0.4);
  42 +stream.acceptWaveform(tailPadding, wave.sampleRate);
  43 +
  44 +while (recognizer.isReady(stream)) {
  45 + recognizer.decode(stream);
  46 +}
  47 +result = recognizer.getResult(stream)
  48 +let stop = performance.now();
  49 +console.log('Done')
  50 +
  51 +const elapsed_seconds = (stop - start) / 1000;
  52 +const duration = wave.samples.length / wave.sampleRate;
  53 +const real_time_factor = elapsed_seconds / duration;
  54 +console.log('Wave duration', duration.toFixed(3), 'secodns')
  55 +console.log('Elapsed', elapsed_seconds.toFixed(3), 'secodns')
  56 +console.log('RTF', real_time_factor.toFixed(3))
  57 +console.log('result', result.text)
1 # Introduction 1 # Introduction
2 2
  3 +Note: You need `Node >= 18`.
  4 +
3 This directory contains nodejs examples for [sherpa-onnx](https://github.com/k2-fsa/sherpa-onnx). 5 This directory contains nodejs examples for [sherpa-onnx](https://github.com/k2-fsa/sherpa-onnx).
4 6
  7 +It uses WebAssembly to wrap `sherpa-onnx` for NodeJS and it does not support multiple threads.
  8 +
  9 +Note: [../nodejs-addon-examples](../nodejs-addon-examples) uses
  10 +[node-addon-api](https://github.com/nodejs/node-addon-api) to wrap
  11 +`sherpa-onnx` for NodeJS and it supports multiple threads.
  12 +
5 Before you continue, please first run 13 Before you continue, please first run
6 14
7 ```bash 15 ```bash
@@ -13,7 +21,6 @@ npm i @@ -13,7 +21,6 @@ npm i
13 In the following, we describe how to use [sherpa-onnx](https://github.com/k2-fsa/sherpa-onnx) 21 In the following, we describe how to use [sherpa-onnx](https://github.com/k2-fsa/sherpa-onnx)
14 for text-to-speech and speech-to-text. 22 for text-to-speech and speech-to-text.
15 23
16 -Note: You need `Node >= 18`.  
17 24
18 # Text-to-speech 25 # Text-to-speech
19 26
1 # Introduction 1 # Introduction
2 2
3 This folder contains `node-addon-api` wrapper for `sherpa-onnx`. 3 This folder contains `node-addon-api` wrapper for `sherpa-onnx`.
  4 +
  5 +Caution: This folder is for developer only.
  6 +
  7 +## Usage
  8 +
  9 +```bash
  10 +git clone https://github.com/k2-fsa/sherpa-onnx
  11 +cd sherpa-onnx
  12 +mkdir build
  13 +cd build
  14 +cmake -DCMAKE_INSTALL_PREFIX=./install -DBUILD_SHARED_LIBS=ON ..
  15 +make -j install
  16 +export PKG_CONFIG_PATH=$PWD/install:$PKG_CONFIG_PATH
  17 +cd ../scripts/node-addon-api/
  18 +
  19 +./node_modules/.bin/node-gyp build --verbose
  20 +
  21 +# see test/test_asr_streaming_transducer.js
  22 +# for usages
  23 +```
1 { 1 {
2 'targets': [ 2 'targets': [
3 { 3 {
4 - 'target_name': 'sherpa-onnx-node-addon-api-native', 4 + 'target_name': 'sherpa-onnx',
5 'sources': [ 5 'sources': [
6 'src/sherpa-onnx-node-addon-api.cc', 6 'src/sherpa-onnx-node-addon-api.cc',
7 'src/streaming-asr.cc', 7 'src/streaming-asr.cc',
@@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
9 ], 9 ],
10 'include_dirs': [ 10 'include_dirs': [
11 "<!@(node -p \"require('node-addon-api').include\")", 11 "<!@(node -p \"require('node-addon-api').include\")",
12 - "<!@(a=$(pkg-config --cflags sherpa-onnx);echo ${a:2})" 12 + "<!@(pkg-config --variable=includedir sherpa-onnx)"
13 ], 13 ],
14 'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"], 14 'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
15 'cflags!': [ 15 'cflags!': [
  1 +const os = require('os');
  2 +const platform_arch = `${os.platform()}-${os.arch()}`;
  3 +const possible_paths = [
  4 + '../build/Release/sherpa-onnx.node',
  5 + '../build/Debug/sherpa-onnx.node',
  6 + `./node_modules/sherpa-onnx-${platform_arch}/sherpa-onnx.node`,
  7 + `../sherpa-onnx-${platform_arch}/sherpa-onnx.node`,
  8 +];
  9 +
  10 +let found = false;
  11 +for (const p of possible_paths) {
  12 + try {
  13 + module.exports = require(p);
  14 + found = true;
  15 + break;
  16 + } catch (error) {
  17 + // do nothing; try the next option
  18 + ;
  19 + }
  20 +}
  21 +
  22 +if (!found) {
  23 + let msg =
  24 + `Could not find sherpa-onnx. Tried\n\n ${possible_paths.join('\n ')}\n`
  25 + if (os.platform() == 'darwin' && process.env.DYLD_LIBRARY_PATH &&
  26 + !process.env.DYLD_LIBRARY_PATH.includes(
  27 + `node_modules/sherpa-onnx-${platform_arch}`)) {
  28 + msg +=
  29 + 'Please remeber to set the following environment variable and try again:\n';
  30 +
  31 + msg += `export DYLD_LIBRARY_PATH=${
  32 + process.env.PWD}/node_modules/sherpa-onnx-${platform_arch}`;
  33 +
  34 + msg += ':$DYLD_LIBRARY_PATH\n';
  35 + }
  36 +
  37 + if (os.platform() == 'linux' && process.env.LD_LIBRARY_PATH &&
  38 + !process.env.LD_LIBRARY_PATH.includes(
  39 + `node_modules/sherpa-onnx-${platform_arch}`)) {
  40 + msg +=
  41 + 'Please remeber to set the following environment variable and try again:\n';
  42 +
  43 + msg += `export LD_LIBRARY_PATH=${
  44 + process.env.PWD}/node_modules/sherpa-onnx-${platform_arch}`;
  45 +
  46 + msg += ':$LD_LIBRARY_PATH\n';
  47 + }
  48 +
  49 + throw new Error(msg)
  50 +}
1 -const addon = require('bindings')('sherpa-onnx-node-addon-api-native'); 1 +const addon = require('./addon.js')
2 const streaming_asr = require('./streaming-asr.js'); 2 const streaming_asr = require('./streaming-asr.js');
3 3
4 module.exports = { 4 module.exports = {
1 -const addon = require('bindings')('sherpa-onnx-node-addon-api-native'); 1 +const addon = require('./addon.js');
2 2
3 class OnlineStream { 3 class OnlineStream {
4 constructor(handle) { 4 constructor(handle) {
@@ -5,7 +5,8 @@ @@ -5,7 +5,8 @@
5 "dependencies": { 5 "dependencies": {
6 "node-addon-api": "^1.1.0", 6 "node-addon-api": "^1.1.0",
7 "bindings": "^1.5.0", 7 "bindings": "^1.5.0",
8 - "node-gyp": "^10.1.0" 8 + "perf_hooks": "*",
  9 + "node-gyp": "^8.3.0"
9 }, 10 },
10 "scripts": { 11 "scripts": {
11 "test": "node --napi-modules ./test/test_binding.js" 12 "test": "node --napi-modules ./test/test_binding.js"
1 // Copyright (c) 2024 Xiaomi Corporation 1 // Copyright (c) 2024 Xiaomi Corporation
2 const sherpa_onnx = require('../lib/sherpa-onnx.js'); 2 const sherpa_onnx = require('../lib/sherpa-onnx.js');
  3 +const performance = require('perf_hooks').performance;
3 4
  5 +// Please download test files from
  6 +// https://github.com/k2-fsa/sherpa-onnx/releases/tag/asr-models
4 const config = { 7 const config = {
5 'featConfig': { 8 'featConfig': {
6 'sampleRate': 16000, 9 'sampleRate': 16000,