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

This commit is contained in:
Fangjun Kuang
2024-05-04 13:27:39 +08:00
committed by GitHub
parent 2f9553d838
commit 4f758e6cd3
27 changed files with 773 additions and 15 deletions

View File

@@ -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
```

View File

@@ -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': [
"<!@(node -p \"require('node-addon-api').include\")",
"<!@(a=$(pkg-config --cflags sherpa-onnx);echo ${a:2})"
"<!@(pkg-config --variable=includedir sherpa-onnx)"
],
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
'cflags!': [

View File

@@ -0,0 +1,50 @@
const os = require('os');
const platform_arch = `${os.platform()}-${os.arch()}`;
const possible_paths = [
'../build/Release/sherpa-onnx.node',
'../build/Debug/sherpa-onnx.node',
`./node_modules/sherpa-onnx-${platform_arch}/sherpa-onnx.node`,
`../sherpa-onnx-${platform_arch}/sherpa-onnx.node`,
];
let found = false;
for (const p of possible_paths) {
try {
module.exports = require(p);
found = true;
break;
} catch (error) {
// do nothing; try the next option
;
}
}
if (!found) {
let msg =
`Could not find sherpa-onnx. Tried\n\n ${possible_paths.join('\n ')}\n`
if (os.platform() == 'darwin' && process.env.DYLD_LIBRARY_PATH &&
!process.env.DYLD_LIBRARY_PATH.includes(
`node_modules/sherpa-onnx-${platform_arch}`)) {
msg +=
'Please remeber to set the following environment variable and try again:\n';
msg += `export DYLD_LIBRARY_PATH=${
process.env.PWD}/node_modules/sherpa-onnx-${platform_arch}`;
msg += ':$DYLD_LIBRARY_PATH\n';
}
if (os.platform() == 'linux' && process.env.LD_LIBRARY_PATH &&
!process.env.LD_LIBRARY_PATH.includes(
`node_modules/sherpa-onnx-${platform_arch}`)) {
msg +=
'Please remeber to set the following environment variable and try again:\n';
msg += `export LD_LIBRARY_PATH=${
process.env.PWD}/node_modules/sherpa-onnx-${platform_arch}`;
msg += ':$LD_LIBRARY_PATH\n';
}
throw new Error(msg)
}

View File

@@ -1,4 +1,4 @@
const addon = require('bindings')('sherpa-onnx-node-addon-api-native');
const addon = require('./addon.js')
const streaming_asr = require('./streaming-asr.js');
module.exports = {

View File

@@ -1,4 +1,4 @@
const addon = require('bindings')('sherpa-onnx-node-addon-api-native');
const addon = require('./addon.js');
class OnlineStream {
constructor(handle) {

View File

@@ -5,7 +5,8 @@
"dependencies": {
"node-addon-api": "^1.1.0",
"bindings": "^1.5.0",
"node-gyp": "^10.1.0"
"perf_hooks": "*",
"node-gyp": "^8.3.0"
},
"scripts": {
"test": "node --napi-modules ./test/test_binding.js"

View File

@@ -1,6 +1,9 @@
// Copyright (c) 2024 Xiaomi Corporation
const sherpa_onnx = require('../lib/sherpa-onnx.js');
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,