Begin to add node-addon-api for sherpa-onnx (#826)
This commit is contained in:
106
.github/workflows/test-nodejs-addon-api.yaml
vendored
Normal file
106
.github/workflows/test-nodejs-addon-api.yaml
vendored
Normal file
@@ -0,0 +1,106 @@
|
|||||||
|
name: test-node-addon-api
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
paths:
|
||||||
|
- '.github/workflows/test-node-addon-api.yaml'
|
||||||
|
- 'CMakeLists.txt'
|
||||||
|
- 'cmake/**'
|
||||||
|
- 'sherpa-onnx/csrc/*'
|
||||||
|
- 'sherpa-onnx/c-api/*'
|
||||||
|
- 'scripts/node-addon-api/**'
|
||||||
|
|
||||||
|
pull_request:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
paths:
|
||||||
|
- '.github/workflows/test-node-addon-api.yaml'
|
||||||
|
- 'CMakeLists.txt'
|
||||||
|
- 'cmake/**'
|
||||||
|
- 'sherpa-onnx/csrc/*'
|
||||||
|
- 'sherpa-onnx/c-api/*'
|
||||||
|
- 'scripts/node-addon-api/**'
|
||||||
|
|
||||||
|
workflow_dispatch:
|
||||||
|
|
||||||
|
concurrency:
|
||||||
|
group: test-node-addon-api-${{ github.ref }}
|
||||||
|
cancel-in-progress: true
|
||||||
|
|
||||||
|
permissions:
|
||||||
|
contents: read
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
test-node-addon-api:
|
||||||
|
name: ${{ matrix.os }}
|
||||||
|
runs-on: ${{ matrix.os }}
|
||||||
|
strategy:
|
||||||
|
fail-fast: false
|
||||||
|
matrix:
|
||||||
|
os: [macos-latest, 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: |
|
||||||
|
mkdir build
|
||||||
|
cd build
|
||||||
|
cmake -DCMAKE_INSTALL_PREFIX=/tmp/sherpa-onnx -DBUILD_SHARED_LIBS=ON ..
|
||||||
|
make -j
|
||||||
|
make install
|
||||||
|
|
||||||
|
- name: Build node-addon-api package
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
cd scripts/node-addon-api
|
||||||
|
|
||||||
|
export PKG_CONFIG_PATH=/tmp/sherpa-onnx:$PKG_CONFIG_PATH
|
||||||
|
|
||||||
|
ls -lh /tmp/sherpa-onnx
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
- name: Test streaming transducer
|
||||||
|
shell: bash
|
||||||
|
run: |
|
||||||
|
cd scripts/node-addon-api
|
||||||
|
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/test_asr_streaming_transducer.js
|
||||||
|
|
||||||
|
rm -rf sherpa-onnx-streaming-zipformer-bilingual-zh-en-2023-02-20
|
||||||
2
.gitignore
vendored
2
.gitignore
vendored
@@ -102,3 +102,5 @@ sherpa-onnx-kws-zipformer-wenetspeech-3.3M-2024-01-01
|
|||||||
*.tar.bz2
|
*.tar.bz2
|
||||||
*.zip
|
*.zip
|
||||||
sherpa-onnx-ced-*
|
sherpa-onnx-ced-*
|
||||||
|
node_modules
|
||||||
|
package-lock.json
|
||||||
|
|||||||
3
scripts/node-addon-api/README.md
Normal file
3
scripts/node-addon-api/README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Introduction
|
||||||
|
|
||||||
|
This folder contains `node-addon-api` wrapper for `sherpa-onnx`.
|
||||||
35
scripts/node-addon-api/binding.gyp
Normal file
35
scripts/node-addon-api/binding.gyp
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
{
|
||||||
|
'targets': [
|
||||||
|
{
|
||||||
|
'target_name': 'sherpa-onnx-node-addon-api-native',
|
||||||
|
'sources': [
|
||||||
|
'src/sherpa-onnx-node-addon-api.cc',
|
||||||
|
'src/streaming-asr.cc',
|
||||||
|
'src/wave-reader.cc'
|
||||||
|
],
|
||||||
|
'include_dirs': [
|
||||||
|
"<!@(node -p \"require('node-addon-api').include\")",
|
||||||
|
"<!@(a=$(pkg-config --cflags sherpa-onnx);echo ${a:2})"
|
||||||
|
],
|
||||||
|
'dependencies': ["<!(node -p \"require('node-addon-api').gyp\")"],
|
||||||
|
'cflags!': [
|
||||||
|
'-fno-exceptions',
|
||||||
|
],
|
||||||
|
'cflags_cc!': [
|
||||||
|
'-fno-exceptions',
|
||||||
|
'-std=c++17'
|
||||||
|
],
|
||||||
|
'libraries': [
|
||||||
|
"<!@(pkg-config --libs sherpa-onnx)"
|
||||||
|
],
|
||||||
|
'xcode_settings': {
|
||||||
|
'GCC_ENABLE_CPP_EXCEPTIONS': 'YES',
|
||||||
|
'CLANG_CXX_LIBRARY': 'libc++',
|
||||||
|
'MACOSX_DEPLOYMENT_TARGET': '10.7'
|
||||||
|
},
|
||||||
|
'msvs_settings': {
|
||||||
|
'VCCLCompilerTool': { 'ExceptionHandling': 1 },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
7
scripts/node-addon-api/lib/sherpa-onnx.js
Normal file
7
scripts/node-addon-api/lib/sherpa-onnx.js
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
const addon = require('bindings')('sherpa-onnx-node-addon-api-native');
|
||||||
|
const streaming_asr = require('./streaming-asr.js');
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
OnlineRecognizer: streaming_asr.OnlineRecognizer,
|
||||||
|
readWave: addon.readWave,
|
||||||
|
}
|
||||||
41
scripts/node-addon-api/lib/streaming-asr.js
Normal file
41
scripts/node-addon-api/lib/streaming-asr.js
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
const addon = require('bindings')('sherpa-onnx-node-addon-api-native');
|
||||||
|
|
||||||
|
class OnlineStream {
|
||||||
|
constructor(handle) {
|
||||||
|
this.handle = handle;
|
||||||
|
}
|
||||||
|
|
||||||
|
// samples is a float32 array containing samples in the range [-1, 1]
|
||||||
|
acceptWaveform(samples, sampleRate) {
|
||||||
|
addon.acceptWaveformOnline(
|
||||||
|
this.handle, {samples: samples, sampleRate: sampleRate})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class OnlineRecognizer {
|
||||||
|
constructor(config) {
|
||||||
|
this.handle = addon.createOnlineRecognizer(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
createStream() {
|
||||||
|
const handle = addon.createOnlineStream(this.handle);
|
||||||
|
return new OnlineStream(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
isReady(stream) {
|
||||||
|
return addon.isOnlineStreamReady(this.handle, stream.handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
decode(stream) {
|
||||||
|
addon.decodeOnlineStream(this.handle, stream.handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
getResult(stream) {
|
||||||
|
const jsonStr =
|
||||||
|
addon.getOnlineStreamResultAsJson(this.handle, stream.handle);
|
||||||
|
|
||||||
|
return JSON.parse(jsonStr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {OnlineRecognizer}
|
||||||
53
scripts/node-addon-api/package.json
Normal file
53
scripts/node-addon-api/package.json
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
{
|
||||||
|
"main": "lib/sherpa-onnx.js",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"description": "Speech-to-text and text-to-speech using Next-gen Kaldi without internet connection",
|
||||||
|
"dependencies": {
|
||||||
|
"node-addon-api": "^1.1.0",
|
||||||
|
"bindings": "^1.5.0",
|
||||||
|
"node-gyp": "^10.1.0"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"test": "node --napi-modules ./test/test_binding.js"
|
||||||
|
},
|
||||||
|
"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",
|
||||||
|
"streaming speech recognition",
|
||||||
|
"speech",
|
||||||
|
"recognition",
|
||||||
|
"vad",
|
||||||
|
"node-addon-api",
|
||||||
|
"speaker id",
|
||||||
|
"language id"
|
||||||
|
],
|
||||||
|
"author": "The next-gen Kaldi team",
|
||||||
|
"license": "Apache-2.0",
|
||||||
|
"gypfile": true,
|
||||||
|
"name": "sherpa-onnx-node-addon-api",
|
||||||
|
"bugs": {
|
||||||
|
"url": "https://github.com/k2-fsa/sherpa-onnx/issues"
|
||||||
|
},
|
||||||
|
"homepage": "https://github.com/k2-fsa/sherpa-onnx#readme"
|
||||||
|
}
|
||||||
16
scripts/node-addon-api/src/sherpa-onnx-node-addon-api.cc
Normal file
16
scripts/node-addon-api/src/sherpa-onnx-node-addon-api.cc
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
// scripts/node-addon-api/src/sherpa-onnx-node-addon-api.cc
|
||||||
|
//
|
||||||
|
// Copyright (c) 2024 Xiaomi Corporation
|
||||||
|
#include "napi.h" // NOLINT
|
||||||
|
|
||||||
|
Napi::Object InitStreamingAsr(Napi::Env env, Napi::Object exports);
|
||||||
|
void InitWaveReader(Napi::Env env, Napi::Object exports);
|
||||||
|
|
||||||
|
Napi::Object Init(Napi::Env env, Napi::Object exports) {
|
||||||
|
InitStreamingAsr(env, exports);
|
||||||
|
InitWaveReader(env, exports);
|
||||||
|
|
||||||
|
return exports;
|
||||||
|
}
|
||||||
|
|
||||||
|
NODE_API_MODULE(addon, Init)
|
||||||
461
scripts/node-addon-api/src/streaming-asr.cc
Normal file
461
scripts/node-addon-api/src/streaming-asr.cc
Normal file
@@ -0,0 +1,461 @@
|
|||||||
|
// scripts/node-addon-api/src/streaming-asr.cc
|
||||||
|
//
|
||||||
|
// Copyright (c) 2024 Xiaomi Corporation
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include "napi.h" // NOLINT
|
||||||
|
#include "sherpa-onnx/c-api/c-api.h"
|
||||||
|
/*
|
||||||
|
{
|
||||||
|
'featConfig': {
|
||||||
|
'sampleRate': 16000,
|
||||||
|
'featureDim': 80,
|
||||||
|
}
|
||||||
|
};
|
||||||
|
*/
|
||||||
|
static SherpaOnnxFeatureConfig GetFeatureConfig(Napi::Object obj) {
|
||||||
|
SherpaOnnxFeatureConfig config;
|
||||||
|
memset(&config, 0, sizeof(config));
|
||||||
|
|
||||||
|
if (!obj.Has("featConfig") || !obj.Get("featConfig").IsObject()) {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
Napi::Object featConfig = obj.Get("featConfig").As<Napi::Object>();
|
||||||
|
|
||||||
|
if (featConfig.Has("sampleRate") && featConfig.Get("sampleRate").IsNumber()) {
|
||||||
|
config.sample_rate =
|
||||||
|
featConfig.Get("sampleRate").As<Napi::Number>().Int32Value();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (featConfig.Has("featureDim") && featConfig.Get("featureDim").IsNumber()) {
|
||||||
|
config.feature_dim =
|
||||||
|
featConfig.Get("featureDim").As<Napi::Number>().Int32Value();
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
{
|
||||||
|
'transducer': {
|
||||||
|
'encoder': './encoder.onnx',
|
||||||
|
'decoder': './decoder.onnx',
|
||||||
|
'joiner': './joiner.onnx',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
|
static SherpaOnnxOnlineTransducerModelConfig GetOnlineTransducerModelConfig(
|
||||||
|
Napi::Object obj) {
|
||||||
|
SherpaOnnxOnlineTransducerModelConfig config;
|
||||||
|
memset(&config, 0, sizeof(config));
|
||||||
|
|
||||||
|
if (!obj.Has("transducer") || !obj.Get("transducer").IsObject()) {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
Napi::Object o = obj.Get("transducer").As<Napi::Object>();
|
||||||
|
|
||||||
|
if (o.Has("encoder") && o.Get("encoder").IsString()) {
|
||||||
|
Napi::String encoder = o.Get("encoder").As<Napi::String>();
|
||||||
|
std::string s = encoder.Utf8Value();
|
||||||
|
char *p = new char[s.size() + 1];
|
||||||
|
std::copy(s.begin(), s.end(), p);
|
||||||
|
p[s.size()] = 0;
|
||||||
|
|
||||||
|
config.encoder = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (o.Has("decoder") && o.Get("decoder").IsString()) {
|
||||||
|
Napi::String decoder = o.Get("decoder").As<Napi::String>();
|
||||||
|
std::string s = decoder.Utf8Value();
|
||||||
|
char *p = new char[s.size() + 1];
|
||||||
|
std::copy(s.begin(), s.end(), p);
|
||||||
|
p[s.size()] = 0;
|
||||||
|
|
||||||
|
config.decoder = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (o.Has("joiner") && o.Get("joiner").IsString()) {
|
||||||
|
Napi::String joiner = o.Get("joiner").As<Napi::String>();
|
||||||
|
std::string s = joiner.Utf8Value();
|
||||||
|
char *p = new char[s.size() + 1];
|
||||||
|
std::copy(s.begin(), s.end(), p);
|
||||||
|
p[s.size()] = 0;
|
||||||
|
|
||||||
|
config.joiner = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
static SherpaOnnxOnlineModelConfig GetOnlineModelConfig(Napi::Object obj) {
|
||||||
|
SherpaOnnxOnlineModelConfig config;
|
||||||
|
memset(&config, 0, sizeof(config));
|
||||||
|
|
||||||
|
if (!obj.Has("modelConfig") || !obj.Get("modelConfig").IsObject()) {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
Napi::Object o = obj.Get("modelConfig").As<Napi::Object>();
|
||||||
|
|
||||||
|
config.transducer = GetOnlineTransducerModelConfig(o);
|
||||||
|
|
||||||
|
if (o.Has("tokens") && o.Get("tokens").IsString()) {
|
||||||
|
Napi::String tokens = o.Get("tokens").As<Napi::String>();
|
||||||
|
std::string s = tokens.Utf8Value();
|
||||||
|
char *p = new char[s.size() + 1];
|
||||||
|
std::copy(s.begin(), s.end(), p);
|
||||||
|
p[s.size()] = 0;
|
||||||
|
|
||||||
|
config.tokens = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (o.Has("numThreads") && o.Get("numThreads").IsNumber()) {
|
||||||
|
config.num_threads = o.Get("numThreads").As<Napi::Number>().Int32Value();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (o.Has("provider") && o.Get("provider").IsString()) {
|
||||||
|
Napi::String provider = o.Get("provider").As<Napi::String>();
|
||||||
|
std::string s = provider.Utf8Value();
|
||||||
|
char *p = new char[s.size() + 1];
|
||||||
|
std::copy(s.begin(), s.end(), p);
|
||||||
|
p[s.size()] = 0;
|
||||||
|
|
||||||
|
config.provider = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (o.Has("debug") && o.Get("debug").IsNumber()) {
|
||||||
|
config.debug = o.Get("debug").As<Napi::Number>().Int32Value();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (o.Has("modelType") && o.Get("modelType").IsString()) {
|
||||||
|
Napi::String model_type = o.Get("modelType").As<Napi::String>();
|
||||||
|
std::string s = model_type.Utf8Value();
|
||||||
|
char *p = new char[s.size() + 1];
|
||||||
|
std::copy(s.begin(), s.end(), p);
|
||||||
|
p[s.size()] = 0;
|
||||||
|
|
||||||
|
config.model_type = p;
|
||||||
|
}
|
||||||
|
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
static Napi::External<SherpaOnnxOnlineRecognizer> CreateOnlineRecognizerWrapper(
|
||||||
|
const Napi::CallbackInfo &info) {
|
||||||
|
Napi::Env env = info.Env();
|
||||||
|
if (info.Length() != 1) {
|
||||||
|
std::ostringstream os;
|
||||||
|
os << "Expect only 1 argument. Given: " << info.Length();
|
||||||
|
|
||||||
|
Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!info[0].IsObject()) {
|
||||||
|
Napi::TypeError::New(env, "Expect an object as the argument")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Napi::Object config = info[0].As<Napi::Object>();
|
||||||
|
SherpaOnnxOnlineRecognizerConfig c;
|
||||||
|
memset(&c, 0, sizeof(c));
|
||||||
|
c.feat_config = GetFeatureConfig(config);
|
||||||
|
c.model_config = GetOnlineModelConfig(config);
|
||||||
|
#if 0
|
||||||
|
printf("encoder: %s\n", c.model_config.transducer.encoder
|
||||||
|
? c.model_config.transducer.encoder
|
||||||
|
: "no");
|
||||||
|
printf("decoder: %s\n", c.model_config.transducer.decoder
|
||||||
|
? c.model_config.transducer.decoder
|
||||||
|
: "no");
|
||||||
|
printf("joiner: %s\n", c.model_config.transducer.joiner
|
||||||
|
? c.model_config.transducer.joiner
|
||||||
|
: "no");
|
||||||
|
|
||||||
|
printf("tokens: %s\n", c.model_config.tokens ? c.model_config.tokens : "no");
|
||||||
|
printf("num_threads: %d\n", c.model_config.num_threads);
|
||||||
|
printf("provider: %s\n",
|
||||||
|
c.model_config.provider ? c.model_config.provider : "no");
|
||||||
|
printf("debug: %d\n", c.model_config.debug);
|
||||||
|
printf("model_type: %s\n",
|
||||||
|
c.model_config.model_type ? c.model_config.model_type : "no");
|
||||||
|
#endif
|
||||||
|
|
||||||
|
SherpaOnnxOnlineRecognizer *recognizer = CreateOnlineRecognizer(&c);
|
||||||
|
|
||||||
|
if (c.model_config.transducer.encoder) {
|
||||||
|
delete[] c.model_config.transducer.encoder;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c.model_config.transducer.decoder) {
|
||||||
|
delete[] c.model_config.transducer.decoder;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c.model_config.transducer.joiner) {
|
||||||
|
delete[] c.model_config.transducer.joiner;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c.model_config.tokens) {
|
||||||
|
delete[] c.model_config.tokens;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c.model_config.provider) {
|
||||||
|
delete[] c.model_config.provider;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (c.model_config.model_type) {
|
||||||
|
delete[] c.model_config.model_type;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!recognizer) {
|
||||||
|
Napi::TypeError::New(env, "Please check your config!")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
return Napi::External<SherpaOnnxOnlineRecognizer>::New(
|
||||||
|
env, recognizer,
|
||||||
|
[](Napi::Env env, SherpaOnnxOnlineRecognizer *recognizer) {
|
||||||
|
DestroyOnlineRecognizer(recognizer);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static Napi::External<SherpaOnnxOnlineStream> CreateOnlineStreamWrapper(
|
||||||
|
const Napi::CallbackInfo &info) {
|
||||||
|
Napi::Env env = info.Env();
|
||||||
|
if (info.Length() != 1) {
|
||||||
|
std::ostringstream os;
|
||||||
|
os << "Expect only 1 argument. Given: " << info.Length();
|
||||||
|
|
||||||
|
Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!info[0].IsExternal()) {
|
||||||
|
Napi::TypeError::New(
|
||||||
|
env, "You should pass a recognizer pointer as the only argument")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
SherpaOnnxOnlineRecognizer *recognizer =
|
||||||
|
info[0].As<Napi::External<SherpaOnnxOnlineRecognizer>>().Data();
|
||||||
|
|
||||||
|
SherpaOnnxOnlineStream *stream = CreateOnlineStream(recognizer);
|
||||||
|
|
||||||
|
return Napi::External<SherpaOnnxOnlineStream>::New(
|
||||||
|
env, stream, [](Napi::Env env, SherpaOnnxOnlineStream *stream) {
|
||||||
|
DestroyOnlineStream(stream);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
static void AcceptWaveformWrapper(const Napi::CallbackInfo &info) {
|
||||||
|
Napi::Env env = info.Env();
|
||||||
|
|
||||||
|
if (info.Length() != 2) {
|
||||||
|
std::ostringstream os;
|
||||||
|
os << "Expect only 2 arguments. Given: " << info.Length();
|
||||||
|
|
||||||
|
Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!info[0].IsExternal()) {
|
||||||
|
Napi::TypeError::New(env, "Argument 0 should be a online stream pointer.")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SherpaOnnxOnlineStream *stream =
|
||||||
|
info[0].As<Napi::External<SherpaOnnxOnlineStream>>().Data();
|
||||||
|
|
||||||
|
if (!info[1].IsObject()) {
|
||||||
|
Napi::TypeError::New(env, "Argument 1 should be an object")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Napi::Object obj = info[1].As<Napi::Object>();
|
||||||
|
|
||||||
|
if (!obj.Has("samples")) {
|
||||||
|
Napi::TypeError::New(env, "The argument object should have a field samples")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!obj.Get("samples").IsTypedArray()) {
|
||||||
|
Napi::TypeError::New(env, "The object['samples'] should be a typed array")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!obj.Has("sampleRate")) {
|
||||||
|
Napi::TypeError::New(env,
|
||||||
|
"The argument object should have a field sampleRate")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!obj.Get("sampleRate").IsNumber()) {
|
||||||
|
Napi::TypeError::New(env, "The object['samples'] should be a number")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Napi::Float32Array samples = obj.Get("samples").As<Napi::Float32Array>();
|
||||||
|
int32_t sample_rate = obj.Get("sampleRate").As<Napi::Number>().Int32Value();
|
||||||
|
|
||||||
|
AcceptWaveform(stream, sample_rate, samples.Data(), samples.ElementLength());
|
||||||
|
}
|
||||||
|
|
||||||
|
static Napi::Boolean IsOnlineStreamReadyWrapper(
|
||||||
|
const Napi::CallbackInfo &info) {
|
||||||
|
Napi::Env env = info.Env();
|
||||||
|
if (info.Length() != 2) {
|
||||||
|
std::ostringstream os;
|
||||||
|
os << "Expect only 2 arguments. Given: " << info.Length();
|
||||||
|
|
||||||
|
Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!info[0].IsExternal()) {
|
||||||
|
Napi::TypeError::New(env,
|
||||||
|
"Argument 0 should be a online recognizer pointer.")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!info[1].IsExternal()) {
|
||||||
|
Napi::TypeError::New(env,
|
||||||
|
"Argument 1 should be a online recognizer pointer.")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
SherpaOnnxOnlineRecognizer *recognizer =
|
||||||
|
info[0].As<Napi::External<SherpaOnnxOnlineRecognizer>>().Data();
|
||||||
|
|
||||||
|
SherpaOnnxOnlineStream *stream =
|
||||||
|
info[1].As<Napi::External<SherpaOnnxOnlineStream>>().Data();
|
||||||
|
|
||||||
|
int32_t is_ready = IsOnlineStreamReady(recognizer, stream);
|
||||||
|
|
||||||
|
return Napi::Boolean::New(env, is_ready);
|
||||||
|
}
|
||||||
|
|
||||||
|
static void DecodeOnlineStreamWrapper(const Napi::CallbackInfo &info) {
|
||||||
|
Napi::Env env = info.Env();
|
||||||
|
if (info.Length() != 2) {
|
||||||
|
std::ostringstream os;
|
||||||
|
os << "Expect only 2 arguments. Given: " << info.Length();
|
||||||
|
|
||||||
|
Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!info[0].IsExternal()) {
|
||||||
|
Napi::TypeError::New(env,
|
||||||
|
"Argument 0 should be a online recognizer pointer.")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!info[1].IsExternal()) {
|
||||||
|
Napi::TypeError::New(env,
|
||||||
|
"Argument 1 should be a online recognizer pointer.")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
SherpaOnnxOnlineRecognizer *recognizer =
|
||||||
|
info[0].As<Napi::External<SherpaOnnxOnlineRecognizer>>().Data();
|
||||||
|
|
||||||
|
SherpaOnnxOnlineStream *stream =
|
||||||
|
info[1].As<Napi::External<SherpaOnnxOnlineStream>>().Data();
|
||||||
|
|
||||||
|
DecodeOnlineStream(recognizer, stream);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Napi::String GetOnlineStreamResultAsJsonWrapper(
|
||||||
|
const Napi::CallbackInfo &info) {
|
||||||
|
Napi::Env env = info.Env();
|
||||||
|
if (info.Length() != 2) {
|
||||||
|
std::ostringstream os;
|
||||||
|
os << "Expect only 2 arguments. Given: " << info.Length();
|
||||||
|
|
||||||
|
Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!info[0].IsExternal()) {
|
||||||
|
Napi::TypeError::New(env,
|
||||||
|
"Argument 0 should be a online recognizer pointer.")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!info[1].IsExternal()) {
|
||||||
|
Napi::TypeError::New(env,
|
||||||
|
"Argument 1 should be a online recognizer pointer.")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
SherpaOnnxOnlineRecognizer *recognizer =
|
||||||
|
info[0].As<Napi::External<SherpaOnnxOnlineRecognizer>>().Data();
|
||||||
|
|
||||||
|
SherpaOnnxOnlineStream *stream =
|
||||||
|
info[1].As<Napi::External<SherpaOnnxOnlineStream>>().Data();
|
||||||
|
|
||||||
|
const char *json = GetOnlineStreamResultAsJson(recognizer, stream);
|
||||||
|
Napi::String s = Napi::String::New(env, json);
|
||||||
|
|
||||||
|
DestroyOnlineStreamResultJson(json);
|
||||||
|
|
||||||
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitStreamingAsr(Napi::Env env, Napi::Object exports) {
|
||||||
|
exports.Set(Napi::String::New(env, "createOnlineRecognizer"),
|
||||||
|
Napi::Function::New(env, CreateOnlineRecognizerWrapper));
|
||||||
|
|
||||||
|
exports.Set(Napi::String::New(env, "createOnlineStream"),
|
||||||
|
Napi::Function::New(env, CreateOnlineStreamWrapper));
|
||||||
|
|
||||||
|
exports.Set(Napi::String::New(env, "acceptWaveformOnline"),
|
||||||
|
Napi::Function::New(env, AcceptWaveformWrapper));
|
||||||
|
|
||||||
|
exports.Set(Napi::String::New(env, "isOnlineStreamReady"),
|
||||||
|
Napi::Function::New(env, IsOnlineStreamReadyWrapper));
|
||||||
|
|
||||||
|
exports.Set(Napi::String::New(env, "decodeOnlineStream"),
|
||||||
|
Napi::Function::New(env, DecodeOnlineStreamWrapper));
|
||||||
|
|
||||||
|
exports.Set(Napi::String::New(env, "getOnlineStreamResultAsJson"),
|
||||||
|
Napi::Function::New(env, GetOnlineStreamResultAsJsonWrapper));
|
||||||
|
}
|
||||||
57
scripts/node-addon-api/src/wave-reader.cc
Normal file
57
scripts/node-addon-api/src/wave-reader.cc
Normal file
@@ -0,0 +1,57 @@
|
|||||||
|
// scripts/node-addon-api/src/wave-reader.cc
|
||||||
|
//
|
||||||
|
// Copyright (c) 2024 Xiaomi Corporation
|
||||||
|
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include "napi.h" // NOLINT
|
||||||
|
#include "sherpa-onnx/c-api/c-api.h"
|
||||||
|
|
||||||
|
static Napi::Object ReadWaveWrapper(const Napi::CallbackInfo &info) {
|
||||||
|
Napi::Env env = info.Env();
|
||||||
|
if (info.Length() != 1) {
|
||||||
|
std::ostringstream os;
|
||||||
|
os << "Expect only 1 argument. Given: " << info.Length();
|
||||||
|
|
||||||
|
Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
if (!info[0].IsString()) {
|
||||||
|
Napi::TypeError::New(env, "Argument should be a string")
|
||||||
|
.ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string filename = info[0].As<Napi::String>().Utf8Value();
|
||||||
|
|
||||||
|
const SherpaOnnxWave *wave = SherpaOnnxReadWave(filename.c_str());
|
||||||
|
if (!wave) {
|
||||||
|
std::ostringstream os;
|
||||||
|
os << "Failed to read '" << filename << "'";
|
||||||
|
Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException();
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
Napi::ArrayBuffer arrayBuffer = Napi::ArrayBuffer::New(
|
||||||
|
env, const_cast<float *>(wave->samples),
|
||||||
|
sizeof(float) * wave->num_samples,
|
||||||
|
[](Napi::Env /*env*/, void * /*data*/, const SherpaOnnxWave *hint) {
|
||||||
|
SherpaOnnxFreeWave(hint);
|
||||||
|
},
|
||||||
|
wave);
|
||||||
|
Napi::Float32Array float32Array =
|
||||||
|
Napi::Float32Array::New(env, wave->num_samples, arrayBuffer, 0);
|
||||||
|
|
||||||
|
Napi::Object obj = Napi::Object::New(env);
|
||||||
|
obj.Set(Napi::String::New(env, "samples"), float32Array);
|
||||||
|
obj.Set(Napi::String::New(env, "sampleRate"), wave->sample_rate);
|
||||||
|
return obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitWaveReader(Napi::Env env, Napi::Object exports) {
|
||||||
|
exports.Set(Napi::String::New(env, "readWave"),
|
||||||
|
Napi::Function::New(env, ReadWaveWrapper));
|
||||||
|
}
|
||||||
53
scripts/node-addon-api/test/test_asr_streaming_transducer.js
Normal file
53
scripts/node-addon-api/test/test_asr_streaming_transducer.js
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
// Copyright (c) 2024 Xiaomi Corporation
|
||||||
|
const sherpa_onnx = require('../lib/sherpa-onnx.js');
|
||||||
|
|
||||||
|
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)
|
||||||
4
scripts/node-addon-api/test/test_binding.js
Normal file
4
scripts/node-addon-api/test/test_binding.js
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
const sherpa_onnx = require('../lib/sherpa-onnx.js');
|
||||||
|
console.log(sherpa_onnx)
|
||||||
|
|
||||||
|
console.log('Tests passed- everything looks OK!');
|
||||||
Reference in New Issue
Block a user