|
| 1 | +// Copyright (c) 2023 Xiaomi Corporation (authors: Fangjun Kuang) |
| 2 | +// |
| 3 | +// Please use |
| 4 | +// |
| 5 | +// npm install ffi-napi ref-struct-napi |
| 6 | +// |
| 7 | +// before you use this file |
| 8 | +// |
| 9 | +// |
| 10 | +// Please use node 13. node 16, 18, 20, and 21 are known not working. |
| 11 | +// See also |
| 12 | +// https://github.com/node-ffi-napi/node-ffi-napi/issues/244 |
| 13 | +// and |
| 14 | +// https://github.com/node-ffi-napi/node-ffi-napi/issues/97 |
| 15 | +'use strict' |
| 16 | + |
| 17 | +const debug = require('debug')('sherpa-ncnn'); |
| 18 | +const os = require('os'); |
| 19 | +const path = require('path'); |
| 20 | +const ffi = require('ffi-napi'); |
| 21 | +const ref = require('ref-napi'); |
| 22 | +const fs = require('fs'); |
| 23 | + |
| 24 | +const StructType = require('ref-struct-napi'); |
| 25 | +const cstring = ref.types.CString; |
| 26 | +const int32_t = ref.types.int32; |
| 27 | +const float = ref.types.float; |
| 28 | +const floatPtr = ref.refType(float); |
| 29 | + |
| 30 | +const RecognizerPtr = ref.refType(ref.types.void); |
| 31 | +const StreamPtr = ref.refType(ref.types.void); |
| 32 | +const SherpaNcnnModelConfig = StructType({ |
| 33 | + 'encoderParam': cstring, |
| 34 | + 'encoderBin': cstring, |
| 35 | + 'decoderParam': cstring, |
| 36 | + 'decoderBin': cstring, |
| 37 | + 'joinerParam': cstring, |
| 38 | + 'joinerBin': cstring, |
| 39 | + 'tokens': cstring, |
| 40 | + 'useVulkanCompute': int32_t, |
| 41 | + 'numThreads': int32_t, |
| 42 | +}); |
| 43 | + |
| 44 | +const SherpaNcnnDecoderConfig = StructType({ |
| 45 | + 'decodingMethod': cstring, |
| 46 | + 'numActivePaths': int32_t, |
| 47 | +}); |
| 48 | + |
| 49 | +const SherpaNcnnFeatureExtractorConfig = StructType({ |
| 50 | + 'sampleRate': float, |
| 51 | + 'featureDim': int32_t, |
| 52 | +}); |
| 53 | + |
| 54 | +const SherpaNcnnRecognizerConfig = StructType({ |
| 55 | + 'featConfig': SherpaNcnnFeatureExtractorConfig, |
| 56 | + 'modelConfig': SherpaNcnnModelConfig, |
| 57 | + 'decoderConfig': SherpaNcnnDecoderConfig, |
| 58 | + 'enableEndpoint': int32_t, |
| 59 | + 'rule1MinTrailingSilence': float, |
| 60 | + 'rule2MinTrailingSilence': float, |
| 61 | + 'rule3MinUtteranceLength': float, |
| 62 | + 'hotwordsFile': cstring, |
| 63 | + 'hotwordsScore': cstring, |
| 64 | +}); |
| 65 | + |
| 66 | +const SherpaNcnnResult = StructType({ |
| 67 | + 'text': cstring, |
| 68 | + 'tokens': cstring, |
| 69 | + 'timestamps': floatPtr, |
| 70 | + 'count': int32_t, |
| 71 | +}); |
| 72 | + |
| 73 | + |
| 74 | +const ResultPtr = ref.refType(SherpaNcnnResult); |
| 75 | +const RecognizerConfigPtr = ref.refType(SherpaNcnnRecognizerConfig) |
| 76 | + |
| 77 | +let soname; |
| 78 | +if (os.platform() == 'win32') { |
| 79 | + soname = path.join(__dirname, 'install', 'lib', 'sherpa-ncnn-c-api.dll'); |
| 80 | +} else if (os.platform() == 'darwin') { |
| 81 | + soname = path.join(__dirname, 'install', 'lib', 'libsherpa-ncnn-c-api.dylib'); |
| 82 | +} else if (os.platform() == 'linux') { |
| 83 | + soname = path.join(__dirname, 'install', 'lib', 'libsherpa-ncnn-c-api.so'); |
| 84 | +} else { |
| 85 | + throw new Error(`Unsupported platform ${os.platform()}`); |
| 86 | +} |
| 87 | +if (!fs.existsSync(soname)) { |
| 88 | + throw new Error(`Cannot find file ${soname}. Please make sure you have run |
| 89 | + ./build.sh`); |
| 90 | +} |
| 91 | + |
| 92 | +debug('soname ', soname) |
| 93 | + |
| 94 | +const libsherpa_ncnn = ffi.Library(soname, { |
| 95 | + 'CreateRecognizer': [RecognizerPtr, [RecognizerConfigPtr]], |
| 96 | + 'DestroyRecognizer': ['void', [RecognizerPtr]], |
| 97 | + 'CreateStream': [StreamPtr, [RecognizerPtr]], |
| 98 | + 'DestroyStream': ['void', [StreamPtr]], |
| 99 | + 'AcceptWaveform': ['void', [StreamPtr, float, floatPtr, int32_t]], |
| 100 | + 'IsReady': [int32_t, [RecognizerPtr, StreamPtr]], |
| 101 | + 'Decode': ['void', [RecognizerPtr, StreamPtr]], |
| 102 | + 'GetResult': [ResultPtr, [RecognizerPtr, StreamPtr]], |
| 103 | + 'DestroyResult': ['void', [ResultPtr]], |
| 104 | + 'Reset': ['void', [RecognizerPtr, StreamPtr]], |
| 105 | + 'InputFinished': ['void', [StreamPtr]], |
| 106 | + 'IsEndpoint': [int32_t, [RecognizerPtr, StreamPtr]], |
| 107 | +}); |
| 108 | + |
| 109 | +class Recognizer { |
| 110 | + /** |
| 111 | + * @param {SherpaNcnnRecognizerConfig} config Configuration for the recognizer |
| 112 | + * |
| 113 | + * The user has to invoke this.free() at the end to avoid memory leak. |
| 114 | + */ |
| 115 | + constructor(config) { |
| 116 | + this.recognizer_handle = libsherpa_ncnn.CreateRecognizer(config.ref()); |
| 117 | + this.stream_handle = libsherpa_ncnn.CreateStream(this.recognizer_handle); |
| 118 | + } |
| 119 | + |
| 120 | + free() { |
| 121 | + if (this.stream_handle) { |
| 122 | + libsherpa_ncnn.DestroyStream(this.stream_handle); |
| 123 | + this.stream_handle = null; |
| 124 | + } |
| 125 | + |
| 126 | + libsherpa_ncnn.DestroyRecognizer(this.recognizer_handle); |
| 127 | + this.handle = null; |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * @param {bool} true to create a new stream |
| 132 | + */ |
| 133 | + reset(recreate) { |
| 134 | + if (recreate) { |
| 135 | + libsherpa_ncnn.DestroyStream(this.stream_handle); |
| 136 | + this.stream_handle = libsherpa_ncnn.CreateStream(this.recognizer_handle); |
| 137 | + return; |
| 138 | + } |
| 139 | + libsherpa_ncnn.Reset(this.recognizer_handle, this.stream_handle) |
| 140 | + } |
| 141 | + /** |
| 142 | + * @param {float} Sample rate of the input data |
| 143 | + * @param {float[]} A 1-d float array containing audio samples. It should be |
| 144 | + * in the range [-1, 1]. |
| 145 | + */ |
| 146 | + acceptWaveform(sampleRate, samples) { |
| 147 | + libsherpa_ncnn.AcceptWaveform( |
| 148 | + this.stream_handle, sampleRate, samples, samples.length); |
| 149 | + } |
| 150 | + |
| 151 | + isReady() { |
| 152 | + return libsherpa_ncnn.IsReady(this.recognizer_handle, this.stream_handle); |
| 153 | + } |
| 154 | + |
| 155 | + decode() { |
| 156 | + libsherpa_ncnn.Decode(this.recognizer_handle, this.stream_handle); |
| 157 | + } |
| 158 | + |
| 159 | + getResult() { |
| 160 | + const h = |
| 161 | + libsherpa_ncnn.GetResult(this.recognizer_handle, this.stream_handle); |
| 162 | + const text = Buffer.from(h.deref().text, 'utf-8').toString(); |
| 163 | + libsherpa_ncnn.DestroyResult(h); |
| 164 | + return text; |
| 165 | + } |
| 166 | +}; |
| 167 | + |
| 168 | +// alias |
| 169 | + |
| 170 | +const ModelConfig = SherpaNcnnModelConfig; |
| 171 | +const DecoderConfig = SherpaNcnnDecoderConfig; |
| 172 | +const FeatureConfig = SherpaNcnnFeatureExtractorConfig; |
| 173 | +const RecognizerConfig = SherpaNcnnRecognizerConfig; |
| 174 | + |
| 175 | +module.exports = { |
| 176 | + FeatureConfig, |
| 177 | + ModelConfig, |
| 178 | + DecoderConfig, |
| 179 | + Recognizer, |
| 180 | + RecognizerConfig, |
| 181 | +}; |
0 commit comments