@picovoice/rhino-react-native
Version:
Picovoice Rhino React Native binding
181 lines (172 loc) • 8.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _reactNativeVoiceProcessor = require("@picovoice/react-native-voice-processor");
var _rhino = _interopRequireDefault(require("./rhino"));
var RhinoErrors = _interopRequireWildcard(require("./rhino_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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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 2020-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.
//
class RhinoManager {
/**
* Creates an instance of the Rhino Manager.
* @param accessKey AccessKey obtained from Picovoice Console (https://console.picovoice.ai/.
* @param contextPath Absolute path to context file.
* @param inferenceCallback A callback for when Rhino has made an intent inference
* @param processErrorCallback Reports errors that are encountered while the engine is processing audio.
* @param modelPath Path to the file containing model parameters. If not set it will be set to the default location.
* @param sensitivity Inference sensitivity. A higher sensitivity value results in fewer misses at the cost of (potentially) increasing the erroneous inference rate.
* Sensitivity should be a floating-point number within [0, 1].
* @param endpointDurationSec Endpoint duration in seconds. An endpoint is a chunk of silence at the end of an
* utterance that marks the end of spoken command. It should be a positive number within [0.5, 5]. A lower endpoint
* duration reduces delay and improves responsiveness. A higher endpoint duration assures Rhino doesn't return inference
* preemptively in case the user pauses before finishing the request.
* @param requireEndpoint If set to `true`, Rhino requires an endpoint (a chunk of silence) after the spoken command.
* If set to `false`, Rhino tries to detect silence, but if it cannot, it still will provide inference regardless. Set
* to `false` only if operating in an environment with overlapping speech (e.g. people talking in the background).
* @returns An instance of the Rhino Manager
*/
static async create(accessKey, contextPath, inferenceCallback, processErrorCallback, modelPath) {
let sensitivity = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : 0.5;
let endpointDurationSec = arguments.length > 6 && arguments[6] !== undefined ? arguments[6] : 1.0;
let requireEndpoint = arguments.length > 7 && arguments[7] !== undefined ? arguments[7] : true;
let rhino = await _rhino.default.create(accessKey, contextPath, modelPath, sensitivity, endpointDurationSec, requireEndpoint);
return new RhinoManager(rhino, inferenceCallback, processErrorCallback);
}
constructor(rhino, inferenceCallback, processErrorCallback) {
_defineProperty(this, "_voiceProcessor", void 0);
_defineProperty(this, "_errorListener", void 0);
_defineProperty(this, "_frameListener", void 0);
_defineProperty(this, "_rhino", void 0);
_defineProperty(this, "_isListening", false);
this._rhino = rhino;
this._voiceProcessor = _reactNativeVoiceProcessor.VoiceProcessor.instance;
this._frameListener = async frame => {
if (this._rhino === null || !this._isListening) {
return;
}
try {
let inference = await this._rhino.process(frame);
if (inference.isFinalized) {
inferenceCallback(inference);
await this._stop();
}
} catch (e) {
if (processErrorCallback) {
processErrorCallback(e);
} else {
console.error(e);
}
}
};
this._errorListener = error => {
if (processErrorCallback) {
processErrorCallback(new RhinoErrors.RhinoError(error.message));
} else {
console.error(error);
}
};
if (typeof inferenceCallback !== 'function') {
throw new RhinoErrors.RhinoInvalidArgumentError("'inferenceCallback' must be a function type");
}
}
/**
* Opens audio input stream and sends audio frames to Rhino.
*/
async process() {
if (this._isListening) {
return;
}
if (this._rhino === null) {
throw new RhinoErrors.RhinoInvalidStateError('Cannot start Rhino - resources have already been released');
}
if (await this._voiceProcessor.hasRecordAudioPermission()) {
this._voiceProcessor.addFrameListener(this._frameListener);
this._voiceProcessor.addErrorListener(this._errorListener);
try {
await this._voiceProcessor.start(this._rhino.frameLength, this._rhino.sampleRate);
} catch (e) {
throw new RhinoErrors.RhinoRuntimeError(`Failed to start audio recording: ${e.message}`);
}
} else {
throw new RhinoErrors.RhinoRuntimeError('User did not give permission to record audio.');
}
this._isListening = true;
}
/**
* Closes audio stream.
*/
async _stop() {
if (!this._isListening) {
return;
}
this._voiceProcessor.removeErrorListener(this._errorListener);
this._voiceProcessor.removeFrameListener(this._frameListener);
if (this._voiceProcessor.numFrameListeners === 0) {
try {
await this._voiceProcessor.stop();
} catch (e) {
throw new RhinoErrors.RhinoRuntimeError(`Failed to stop audio recording: ${e.message}`);
}
}
this._isListening = false;
}
/**
* Releases resources and listeners.
*/
async delete() {
if (this._rhino !== null) {
await this._rhino.delete();
this._rhino = null;
}
}
/**
* Gets the source of the Rhino context in YAML format. Shows the list of intents,
* which expressions map to those intents, as well as slots and their possible values.
* @returns The context YAML
*/
get contextInfo() {
var _this$_rhino;
return (_this$_rhino = this._rhino) === null || _this$_rhino === void 0 ? void 0 : _this$_rhino.contextInfo;
}
/**
* Gets the required number of audio samples per frame.
* @returns Required frame length.
*/
get frameLength() {
var _this$_rhino2;
return (_this$_rhino2 = this._rhino) === null || _this$_rhino2 === void 0 ? void 0 : _this$_rhino2.frameLength;
}
/**
* Get the audio sample rate required by Rhino.
* @returns Required sample rate.
*/
get sampleRate() {
var _this$_rhino3;
return (_this$_rhino3 = this._rhino) === null || _this$_rhino3 === void 0 ? void 0 : _this$_rhino3.sampleRate;
}
/**
* Gets the version number of the Rhino library.
* @returns Version of Rhino
*/
get version() {
var _this$_rhino4;
return (_this$_rhino4 = this._rhino) === null || _this$_rhino4 === void 0 ? void 0 : _this$_rhino4.version;
}
}
var _default = exports.default = RhinoManager;
//# sourceMappingURL=rhino_manager.js.map