|
| 1 | +// scripts/node-addon-api/src/resampler.cc |
| 2 | +// |
| 3 | +// Copyright (c) 2026 Xiaomi Corporation |
| 4 | + |
| 5 | +#include <sstream> |
| 6 | + |
| 7 | +#include "macros.h" // NOLINT |
| 8 | +#include "napi.h" // NOLINT |
| 9 | +#include "sherpa-onnx/c-api/c-api.h" |
| 10 | + |
| 11 | +// createLinearResampler(inputSampleRate, outputSampleRate) |
| 12 | +// Returns an External handle to a SherpaOnnxLinearResampler. |
| 13 | +static Napi::External<SherpaOnnxLinearResampler> CreateLinearResamplerWrapper( |
| 14 | + const Napi::CallbackInfo &info) { |
| 15 | + Napi::Env env = info.Env(); |
| 16 | + |
| 17 | + if (info.Length() != 2) { |
| 18 | + std::ostringstream os; |
| 19 | + os << "Expect 2 arguments (inputSampleRate, outputSampleRate). Given: " |
| 20 | + << info.Length(); |
| 21 | + |
| 22 | + Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException(); |
| 23 | + |
| 24 | + return {}; |
| 25 | + } |
| 26 | + |
| 27 | + if (!info[0].IsNumber()) { |
| 28 | + Napi::TypeError::New(env, "Argument 0 should be a number (inputSampleRate)") |
| 29 | + .ThrowAsJavaScriptException(); |
| 30 | + |
| 31 | + return {}; |
| 32 | + } |
| 33 | + |
| 34 | + if (!info[1].IsNumber()) { |
| 35 | + Napi::TypeError::New(env, |
| 36 | + "Argument 1 should be a number (outputSampleRate)") |
| 37 | + .ThrowAsJavaScriptException(); |
| 38 | + |
| 39 | + return {}; |
| 40 | + } |
| 41 | + |
| 42 | + int32_t input_sample_rate = info[0].As<Napi::Number>().Int32Value(); |
| 43 | + int32_t output_sample_rate = info[1].As<Napi::Number>().Int32Value(); |
| 44 | + |
| 45 | + if (input_sample_rate <= 0 || output_sample_rate <= 0) { |
| 46 | + Napi::TypeError::New(env, |
| 47 | + "inputSampleRate and outputSampleRate must be > 0") |
| 48 | + .ThrowAsJavaScriptException(); |
| 49 | + |
| 50 | + return {}; |
| 51 | + } |
| 52 | + |
| 53 | + const SherpaOnnxLinearResampler *p = SherpaOnnxCreateLinearResampler( |
| 54 | + input_sample_rate, output_sample_rate, 0, 0); |
| 55 | + |
| 56 | + return Napi::External<SherpaOnnxLinearResampler>::New( |
| 57 | + env, const_cast<SherpaOnnxLinearResampler *>(p), |
| 58 | + [](Napi::Env env, SherpaOnnxLinearResampler *ptr) { |
| 59 | + SherpaOnnxDestroyLinearResampler(ptr); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +// resampleLinear(resamplerHandle, samples, flush) |
| 64 | +// Returns a Float32Array of resampled samples. |
| 65 | +// flush should be 1 for the final chunk; 0 otherwise. |
| 66 | +static Napi::Float32Array ResampleLinearWrapper( |
| 67 | + const Napi::CallbackInfo &info) { |
| 68 | + Napi::Env env = info.Env(); |
| 69 | + |
| 70 | + if (info.Length() != 3) { |
| 71 | + std::ostringstream os; |
| 72 | + os << "Expect 3 arguments (resampler, samples, flush). Given: " |
| 73 | + << info.Length(); |
| 74 | + |
| 75 | + Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException(); |
| 76 | + |
| 77 | + return {}; |
| 78 | + } |
| 79 | + |
| 80 | + if (!info[0].IsExternal()) { |
| 81 | + Napi::TypeError::New(env, "Argument 0 should be a resampler handle.") |
| 82 | + .ThrowAsJavaScriptException(); |
| 83 | + |
| 84 | + return {}; |
| 85 | + } |
| 86 | + |
| 87 | + if (!info[1].IsTypedArray() || |
| 88 | + info[1].As<Napi::TypedArray>().TypedArrayType() != napi_float32_array) { |
| 89 | + Napi::TypeError::New(env, "Argument 1 should be a Float32Array.") |
| 90 | + .ThrowAsJavaScriptException(); |
| 91 | + |
| 92 | + return {}; |
| 93 | + } |
| 94 | + |
| 95 | + if (!info[2].IsNumber()) { |
| 96 | + Napi::TypeError::New(env, "Argument 2 should be a number (flush: 0 or 1).") |
| 97 | + .ThrowAsJavaScriptException(); |
| 98 | + |
| 99 | + return {}; |
| 100 | + } |
| 101 | + |
| 102 | + SherpaOnnxLinearResampler *p = |
| 103 | + info[0].As<Napi::External<SherpaOnnxLinearResampler>>().Data(); |
| 104 | + |
| 105 | + Napi::Float32Array samples = info[1].As<Napi::Float32Array>(); |
| 106 | + int32_t input_dim = samples.ElementLength(); |
| 107 | + int32_t flush = info[2].As<Napi::Number>().Int32Value(); |
| 108 | + |
| 109 | + const SherpaOnnxResampleOut *out = |
| 110 | + SherpaOnnxLinearResamplerResample(p, samples.Data(), input_dim, flush); |
| 111 | + |
| 112 | + Napi::Float32Array result = Napi::Float32Array::New(env, out->n); |
| 113 | + std::copy(out->samples, out->samples + out->n, result.Data()); |
| 114 | + |
| 115 | + SherpaOnnxLinearResamplerResampleFree(out); |
| 116 | + |
| 117 | + return result; |
| 118 | +} |
| 119 | + |
| 120 | +// linearResamplerReset(resamplerHandle) |
| 121 | +static void LinearResamplerResetWrapper(const Napi::CallbackInfo &info) { |
| 122 | + Napi::Env env = info.Env(); |
| 123 | + |
| 124 | + if (info.Length() != 1) { |
| 125 | + std::ostringstream os; |
| 126 | + os << "Expect 1 argument (resampler). Given: " << info.Length(); |
| 127 | + |
| 128 | + Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException(); |
| 129 | + |
| 130 | + return; |
| 131 | + } |
| 132 | + |
| 133 | + if (!info[0].IsExternal()) { |
| 134 | + Napi::TypeError::New(env, "Argument 0 should be a resampler handle.") |
| 135 | + .ThrowAsJavaScriptException(); |
| 136 | + |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + SherpaOnnxLinearResampler *p = |
| 141 | + info[0].As<Napi::External<SherpaOnnxLinearResampler>>().Data(); |
| 142 | + |
| 143 | + SherpaOnnxLinearResamplerReset(p); |
| 144 | +} |
| 145 | + |
| 146 | +// linearResamplerGetInputSampleRate(resamplerHandle) |
| 147 | +static Napi::Number LinearResamplerGetInputSampleRateWrapper( |
| 148 | + const Napi::CallbackInfo &info) { |
| 149 | + Napi::Env env = info.Env(); |
| 150 | + |
| 151 | + if (info.Length() != 1) { |
| 152 | + std::ostringstream os; |
| 153 | + os << "Expect 1 argument (resampler). Given: " << info.Length(); |
| 154 | + |
| 155 | + Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException(); |
| 156 | + |
| 157 | + return {}; |
| 158 | + } |
| 159 | + |
| 160 | + if (!info[0].IsExternal()) { |
| 161 | + Napi::TypeError::New(env, "Argument 0 should be a resampler handle.") |
| 162 | + .ThrowAsJavaScriptException(); |
| 163 | + |
| 164 | + return {}; |
| 165 | + } |
| 166 | + |
| 167 | + SherpaOnnxLinearResampler *p = |
| 168 | + info[0].As<Napi::External<SherpaOnnxLinearResampler>>().Data(); |
| 169 | + |
| 170 | + return Napi::Number::New( |
| 171 | + env, SherpaOnnxLinearResamplerResampleGetInputSampleRate(p)); |
| 172 | +} |
| 173 | + |
| 174 | +// linearResamplerGetOutputSampleRate(resamplerHandle) |
| 175 | +static Napi::Number LinearResamplerGetOutputSampleRateWrapper( |
| 176 | + const Napi::CallbackInfo &info) { |
| 177 | + Napi::Env env = info.Env(); |
| 178 | + |
| 179 | + if (info.Length() != 1) { |
| 180 | + std::ostringstream os; |
| 181 | + os << "Expect 1 argument (resampler). Given: " << info.Length(); |
| 182 | + |
| 183 | + Napi::TypeError::New(env, os.str()).ThrowAsJavaScriptException(); |
| 184 | + |
| 185 | + return {}; |
| 186 | + } |
| 187 | + |
| 188 | + if (!info[0].IsExternal()) { |
| 189 | + Napi::TypeError::New(env, "Argument 0 should be a resampler handle.") |
| 190 | + .ThrowAsJavaScriptException(); |
| 191 | + |
| 192 | + return {}; |
| 193 | + } |
| 194 | + |
| 195 | + SherpaOnnxLinearResampler *p = |
| 196 | + info[0].As<Napi::External<SherpaOnnxLinearResampler>>().Data(); |
| 197 | + |
| 198 | + return Napi::Number::New( |
| 199 | + env, SherpaOnnxLinearResamplerResampleGetOutputSampleRate(p)); |
| 200 | +} |
| 201 | + |
| 202 | +void InitResampler(Napi::Env env, Napi::Object exports) { |
| 203 | + exports.Set(Napi::String::New(env, "createLinearResampler"), |
| 204 | + Napi::Function::New(env, CreateLinearResamplerWrapper)); |
| 205 | + |
| 206 | + exports.Set(Napi::String::New(env, "resampleLinear"), |
| 207 | + Napi::Function::New(env, ResampleLinearWrapper)); |
| 208 | + |
| 209 | + exports.Set(Napi::String::New(env, "linearResamplerReset"), |
| 210 | + Napi::Function::New(env, LinearResamplerResetWrapper)); |
| 211 | + |
| 212 | + exports.Set(Napi::String::New(env, "linearResamplerGetInputSampleRate"), |
| 213 | + Napi::Function::New(env, LinearResamplerGetInputSampleRateWrapper)); |
| 214 | + |
| 215 | + exports.Set( |
| 216 | + Napi::String::New(env, "linearResamplerGetOutputSampleRate"), |
| 217 | + Napi::Function::New(env, LinearResamplerGetOutputSampleRateWrapper)); |
| 218 | +} |
0 commit comments