UNPKG

ac-microsoft-cognitiveservices-speech-sdk

Version:
156 lines (154 loc) 7.76 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SpeakerIdMessageAdapter = void 0; const Exports_1 = require("../common.browser/Exports"); const Exports_2 = require("../sdk/Exports"); const ConnectionFactoryBase_1 = require("./ConnectionFactoryBase"); /** * Implements methods for speaker recognition classes, sending requests to endpoint * and parsing response into expected format * @class SpeakerIdMessageAdapter */ class SpeakerIdMessageAdapter { constructor(config) { let endpoint = config.parameters.getProperty(Exports_2.PropertyId.SpeechServiceConnection_Endpoint, undefined); if (!endpoint) { const region = config.parameters.getProperty(Exports_2.PropertyId.SpeechServiceConnection_Region, "westus"); const hostSuffix = ConnectionFactoryBase_1.ConnectionFactoryBase.getHostSuffix(region); endpoint = config.parameters.getProperty(Exports_2.PropertyId.SpeechServiceConnection_Host, `https://${region}.api.cognitive${hostSuffix}`); } this.privUri = `${endpoint}/speaker-recognition/{mode}/{dependency}/profiles`; const options = Exports_1.RestConfigBase.requestOptions; options.headers[Exports_1.RestConfigBase.configParams.subscriptionKey] = config.parameters.getProperty(Exports_2.PropertyId.SpeechServiceConnection_Key, undefined); this.privApiVersion = config.parameters.getProperty(Exports_2.PropertyId.SpeakerRecognition_Api_Version, "2021-09-05"); this.privRestAdapter = new Exports_1.RestMessageAdapter(options); } /** * Sends create profile request to endpoint. * @function * @param {VoiceProfileType} profileType - type of voice profile to create. * @param {string} lang - language/locale of voice profile * @public * @returns {Promise<IRestResponse>} promised rest response containing id of created profile. */ createProfile(profileType, lang) { const uri = this.getOperationUri(profileType); return this.privRestAdapter.request(Exports_1.RestRequestType.Post, uri, this.getQueryParams({}), { locale: lang }); } /** * Sends create enrollment request to endpoint. * @function * @param {VoiceProfile} profileType - voice profile for which to create new enrollment. * @param {IAudioSource} audioSource - audioSource from which to pull data to send * @public * @returns {Promise<IRestResponse>} rest response to enrollment request. */ createEnrollment(profile, audioSource) { const uri = this.getOperationUri(profile.profileType) + "/" + profile.profileId + "/enrollments"; // eslint-disable-next-line @typescript-eslint/no-unsafe-argument return audioSource.blob.then((result) => this.privRestAdapter.request(Exports_1.RestRequestType.File, uri, this.getQueryParams({ ignoreMinLength: "true" }), null, result)); } /** * Sends verification request to endpoint. * @function * @param {SpeakerVerificationModel} model - voice model to verify against. * @param {IAudioSource} audioSource - audioSource from which to pull data to send * @public * @returns {Promise<IRestResponse>} rest response to enrollment request. */ async verifySpeaker(model, audioSource) { const uri = this.getOperationUri(model.voiceProfile.profileType) + "/" + model.voiceProfile.profileId + ":verify"; try { const result = await audioSource.blob; return this.privRestAdapter.request(Exports_1.RestRequestType.File, uri, this.getQueryParams({ ignoreMinLength: "true" }), null, result); } catch (e) { return Promise.resolve({ data: e }); } } /** * Sends identification request to endpoint. * @function * @param {SpeakerIdentificationModel} model - voice profiles against which to identify. * @param {IAudioSource} audioSource - audioSource from which to pull data to send * @public * @returns {Promise<IRestResponse>} rest response to enrollment request. */ async identifySpeaker(model, audioSource) { const uri = this.getOperationUri(Exports_2.VoiceProfileType.TextIndependentIdentification) + ":identifySingleSpeaker"; try { const result = await audioSource.blob; return this.privRestAdapter.request(Exports_1.RestRequestType.File, uri, this.getQueryParams({ profileIds: model.voiceProfileIds, ignoreMinLength: "true" }), null, result); } catch (e) { return Promise.resolve({ data: e }); } } /** * Sends profile status request to endpoint. * @function * @param {VoiceProfile} profile - voice profile to check. * @public * @returns {Promise<IRestResponse>} rest response to status request */ getProfileStatus(profile) { const uri = `${this.getOperationUri(profile.profileType)}/${profile.profileId}`; return this.privRestAdapter.request(Exports_1.RestRequestType.Get, uri, this.getQueryParams()); } /** * Sends get all profiles request to endpoint. * @function * @param {VoiceProfileType} profileType - type of profiles to return list of * @public * @returns {Promise<IRestResponse>} promised rest response containing all profiles */ getProfiles(profileType) { const uri = this.getOperationUri(profileType); return this.privRestAdapter.request(Exports_1.RestRequestType.Get, uri, this.getQueryParams()); } /** * Sends get activation/auth phrases request to endpoint. * @function * @param {VoiceProfileType} profileType - type of profiles to return phrases for * @param {string} lang - language/locale of voice profile * @public * @returns {Promise<IRestResponse>} promised rest response containing list of valid phrases */ getPhrases(profileType, lang) { const uri = `${this.getOperationUri(profileType)}`.replace("profiles", "phrases") + "/" + lang; return this.privRestAdapter.request(Exports_1.RestRequestType.Get, uri, this.getQueryParams()); } /** * Sends delete profile request to endpoint. * @function * @param {VoiceProfile} profile - voice profile to delete. * @public * @returns {Promise<IRestResponse>} rest response to deletion request */ deleteProfile(profile) { const uri = this.getOperationUri(profile.profileType) + "/" + profile.profileId; return this.privRestAdapter.request(Exports_1.RestRequestType.Delete, uri, this.getQueryParams()); } /** * Sends reset profile request to endpoint. * @function * @param {VoiceProfile} profile - voice profile to reset enrollments for. * @public * @returns {Promise<IRestResponse>} rest response to reset request */ resetProfile(profile) { const uri = this.getOperationUri(profile.profileType) + "/" + profile.profileId + ":reset"; return this.privRestAdapter.request(Exports_1.RestRequestType.Post, uri, this.getQueryParams()); } getOperationUri(profileType) { const mode = profileType === Exports_2.VoiceProfileType.TextIndependentIdentification ? "identification" : "verification"; const dependency = profileType === Exports_2.VoiceProfileType.TextDependentVerification ? "text-dependent" : "text-independent"; return this.privUri.replace("{mode}", mode).replace("{dependency}", dependency); } getQueryParams(params = {}) { params[Exports_1.RestConfigBase.configParams.apiVersion] = this.privApiVersion; return params; } } exports.SpeakerIdMessageAdapter = SpeakerIdMessageAdapter; //# sourceMappingURL=SpeakerIdMessageAdapter.js.map