UNPKG

@voice-ping/cognitive-services-speech

Version:

VoicePing Cognitive Services Speech SDK for JavaScript forked from Microsoft

135 lines (133 loc) 5.79 kB
"use strict"; // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); var Exports_1 = require("../common.speech/Exports"); var Contracts_1 = require("./Contracts"); var Exports_2 = require("./Exports"); /** * Defines SpeakerRecognizer class for Speaker Recognition * Handles operations from user for Voice Profile operations (e.g. createProfile, deleteProfile) * @class SpeakerRecognizer */ var SpeakerRecognizer = /** @class */ (function () { /** * SpeakerRecognizer constructor. * @constructor * @param {SpeechConfig} speechConfig - An set of initial properties for this recognizer (authentication key, region, &c) */ function SpeakerRecognizer(speechConfig, audioConfig) { var speechConfigImpl = speechConfig; Contracts_1.Contracts.throwIfNull(speechConfigImpl, "speechConfig"); this.privAudioConfigImpl = audioConfig; Contracts_1.Contracts.throwIfNull(this.privAudioConfigImpl, "audioConfig"); this.privProperties = speechConfigImpl.properties.clone(); this.implSRSetup(); } Object.defineProperty(SpeakerRecognizer.prototype, "authorizationToken", { /** * Gets the authorization token used to communicate with the service. * @member SpeakerRecognizer.prototype.authorizationToken * @function * @public * @returns {string} Authorization token. */ get: function () { return this.properties.getProperty(Exports_2.PropertyId.SpeechServiceAuthorization_Token); }, /** * Gets/Sets the authorization token used to communicate with the service. * @member SpeakerRecognizer.prototype.authorizationToken * @function * @public * @param {string} token - Authorization token. */ set: function (token) { Contracts_1.Contracts.throwIfNullOrWhitespace(token, "token"); this.properties.setProperty(Exports_2.PropertyId.SpeechServiceAuthorization_Token, token); }, enumerable: true, configurable: true }); Object.defineProperty(SpeakerRecognizer.prototype, "properties", { /** * The collection of properties and their values defined for this SpeakerRecognizer. * @member SpeakerRecognizer.prototype.properties * @function * @public * @returns {PropertyCollection} The collection of properties and their values defined for this SpeakerRecognizer. */ get: function () { return this.privProperties; }, enumerable: true, configurable: true }); /** * Get recognition result for model using given audio * @member SpeakerRecognizer.prototype.recognizeOnceAsync * @function * @public * @param {SpeakerIdentificationModel} model Model containing Voice Profiles to be identified * @param cb - Callback invoked once result is returned. * @param err - Callback invoked in case of an error. */ SpeakerRecognizer.prototype.recognizeOnceAsync = function (model, cb, err) { var _this = this; if (model instanceof Exports_2.SpeakerIdentificationModel) { this.privAdapter.identifySpeaker(model, this.privAudioConfigImpl).continueWith(function (promiseResult) { _this.handleResultCallbacks(promiseResult, Exports_2.SpeakerRecognitionResultType.Identify, undefined, cb, err); }); } else if (model instanceof Exports_2.SpeakerVerificationModel) { this.privAdapter.verifySpeaker(model, this.privAudioConfigImpl).continueWith(function (promiseResult) { _this.handleResultCallbacks(promiseResult, Exports_2.SpeakerRecognitionResultType.Verify, model.voiceProfile.profileId, cb, err); }); } else { throw new Error("SpeakerRecognizer.recognizeOnce: Unexpected model type"); } }; /** * Included for compatibility * @member SpeakerRecognizer.prototype.close * @function * @public */ SpeakerRecognizer.prototype.close = function () { return; }; // Does class setup, swiped from Recognizer. SpeakerRecognizer.prototype.implSRSetup = function () { var osPlatform = (typeof window !== "undefined") ? "Browser" : "Node"; var osName = "unknown"; var osVersion = "unknown"; if (typeof navigator !== "undefined") { osPlatform = osPlatform + "/" + navigator.platform; osName = navigator.userAgent; osVersion = navigator.appVersion; } var recognizerConfig = new Exports_1.SpeakerRecognitionConfig(new Exports_1.Context(new Exports_1.OS(osPlatform, osName, osVersion)), this.privProperties); this.privAdapter = new Exports_1.SpeakerIdMessageAdapter(recognizerConfig); }; SpeakerRecognizer.prototype.handleResultCallbacks = function (promiseResult, resultType, profileId, cb, err) { try { if (promiseResult.isError) { if (!!err) { err(promiseResult.error); } } else if (promiseResult.isCompleted && !!cb) { cb(new Exports_2.SpeakerRecognitionResult(resultType, promiseResult.result.data, profileId, promiseResult.result.ok ? Exports_2.ResultReason.RecognizedSpeaker : Exports_2.ResultReason.Canceled)); } } catch (e) { if (!!err) { err(e); } } }; return SpeakerRecognizer; }()); exports.SpeakerRecognizer = SpeakerRecognizer; //# sourceMappingURL=SpeakerRecognizer.js.map