UNPKG

@picovoice/leopard-react-native

Version:

Picovoice Leopard React Native binding

167 lines (159 loc) 7.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _reactNative = require("react-native"); var LeopardErrors = _interopRequireWildcard(require("./leopard_errors")); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(arg) { var key = _toPrimitive(arg, "string"); return typeof key === "symbol" ? key : String(key); } function _toPrimitive(input, hint) { if (typeof input !== "object" || input === null) return input; var prim = input[Symbol.toPrimitive]; if (prim !== undefined) { var res = prim.call(input, hint || "default"); if (typeof res !== "object") return res; throw new TypeError("@@toPrimitive must return a primitive value."); } return (hint === "string" ? String : Number)(input); } // // Copyright 2022-2023 Picovoice Inc. // // You may not use this file except in compliance with the license. A copy of the license is located in the "LICENSE" // file accompanying this source. // // Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on // an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the // specific language governing permissions and limitations under the License. // const RCTLeopard = _reactNative.NativeModules.PvLeopard; class Leopard { /** * Static creator for initializing Leopard given the model path. * @param accessKey AccessKey obtained from Picovoice Console (https://console.picovoice.ai/). * @param modelPath Path to the file containing model parameters. * @param options Optional configuration arguments. * @param options.enableAutomaticPunctuation Set to `true` to enable automatic punctuation insertion. * @param options.enableDiarization Set to `true` to enable speaker diarization, which allows Leopard to differentiate speakers * as part of the transcription process. Word metadata will include a `speakerTag` to identify unique speakers. * @returns An instance of the engine. */ static async create(accessKey, modelPath, options = {}) { const { enableAutomaticPunctuation = false, enableDiarization = false } = options; try { let { handle, sampleRate, version } = await RCTLeopard.create(accessKey, modelPath, enableAutomaticPunctuation, enableDiarization); return new Leopard(handle, sampleRate, version); } catch (err) { if (err instanceof LeopardErrors.LeopardError) { throw err; } else { const nativeError = err; throw this.codeToError(nativeError.code, nativeError.message); } } } constructor(handle, sampleRate, version) { _defineProperty(this, "_handle", void 0); _defineProperty(this, "_sampleRate", void 0); _defineProperty(this, "_version", void 0); this._handle = handle; this._sampleRate = sampleRate; this._version = version; } /** * Process a frame of audio with the speech-to-text engine. * @param frame An array of 16-bit pcm samples. The audio needs to have a sample rate equal to `.sampleRate` and be 16-bit * linearly-encoded. This function operates on single-channel audio. If you wish to process data in a different * sample rate or format consider using `.processFile`. * @returns {Promise<LeopardTranscript>} LeopardTranscript object which contains the transcription results of the engine. */ async process(frame) { if (frame === undefined) { throw new LeopardErrors.LeopardInvalidArgumentError('Frame array provided to process() is undefined or null'); } // sample the first frame to check for non-integer values if (!Number.isInteger(frame[0])) { throw new LeopardErrors.LeopardInvalidArgumentError(`Non-integer frame values provided to process(): ${frame[0]}. Leopard requires 16-bit integers`); } try { return await RCTLeopard.process(this._handle, frame); } catch (err) { const nativeError = err; throw Leopard.codeToError(nativeError.code, nativeError.message); } } /** * Process a frame of audio with the speech-to-text engine. * @param audioPath Absolute path to the audio file. The supported formats are: `3gp (AMR)`, `FLAC`, `MP3`, * `MP4/m4a (AAC)`, `Ogg`, `WAV` and `WebM`. * @returns {Promise<LeopardTranscript>>} LeopardTranscript object which contains the transcription results of the engine. */ async processFile(audioPath) { if (audioPath === undefined || audioPath === '') { throw new LeopardErrors.LeopardInvalidArgumentError('Audio path provided is not set.'); } try { return await RCTLeopard.processFile(this._handle, audioPath); } catch (err) { const nativeError = err; throw Leopard.codeToError(nativeError.code, nativeError.message); } } /** * Frees memory that was allocated for Leopard */ async delete() { return RCTLeopard.delete(this._handle); } /** * Get the audio sample rate required by Leopard. * @returns Required sample rate. */ get sampleRate() { return this._sampleRate; } /** * Gets the version number of the Leopard library. * @returns Version of Leopard */ get version() { return this._version; } /** * Gets the Error type given a code. * @param code Code name of native Error. * @param message Detailed message of the error. */ static codeToError(code, message) { switch (code) { case 'LeopardException': return new LeopardErrors.LeopardError(message); case 'LeopardMemoryException': return new LeopardErrors.LeopardMemoryError(message); case 'LeopardIOException': return new LeopardErrors.LeopardIOError(message); case 'LeopardInvalidArgumentException': return new LeopardErrors.LeopardInvalidArgumentError(message); case 'LeopardStopIterationException': return new LeopardErrors.LeopardStopIterationError(message); case 'LeopardKeyException': return new LeopardErrors.LeopardKeyError(message); case 'LeopardInvalidStateException': return new LeopardErrors.LeopardInvalidStateError(message); case 'LeopardRuntimeException': return new LeopardErrors.LeopardRuntimeError(message); case 'LeopardActivationException': return new LeopardErrors.LeopardActivationError(message); case 'LeopardActivationLimitException': return new LeopardErrors.LeopardActivationLimitError(message); case 'LeopardActivationThrottledException': return new LeopardErrors.LeopardActivationThrottledError(message); case 'LeopardActivationRefusedException': return new LeopardErrors.LeopardActivationRefusedError(message); default: throw new Error(`unexpected code: ${code}, message: ${message}`); } } } var _default = exports.default = Leopard; //# sourceMappingURL=leopard.js.map