ac-microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
1 lines • 10.5 kB
Source Map (JSON)
{"version":3,"sources":["src/common.speech/SpeakerIdMessageAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAEH,aAAa,EAIhB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AACjD,OAAO,EAEH,0BAA0B,EAC1B,wBAAwB,EACxB,YAAY,EACZ,gBAAgB,EACnB,MAAM,gBAAgB,CAAC;AAExB,OAAO,EAAE,wBAAwB,EAAE,MAAM,WAAW,CAAC;AAErD;;;;GAIG;AACH,qBAAa,uBAAuB;IAChC,OAAO,CAAC,eAAe,CAAqB;IAC5C,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,cAAc,CAAS;gBAEZ,MAAM,EAAE,wBAAwB;IAiBnD;;;;;;;OAOG;IACI,aAAa,CAAC,WAAW,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,GAC5D,OAAO,CAAC,aAAa,CAAC;IAM1B;;;;;;;OAOG;IACI,gBAAgB,CAAC,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,YAAY,GACpE,OAAO,CAAC,aAAa,CAAC;IAO1B;;;;;;;OAOG;IACU,aAAa,CAAC,KAAK,EAAE,wBAAwB,EAAE,WAAW,EAAE,YAAY,GACjF,OAAO,CAAC,aAAa,CAAC;IAW1B;;;;;;;OAOG;IACU,eAAe,CAAC,KAAK,EAAE,0BAA0B,EAAE,WAAW,EAAE,YAAY,GACrF,OAAO,CAAC,aAAa,CAAC;IAW1B;;;;;;OAMG;IACI,gBAAgB,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;IAMtE;;;;;;OAMG;IACI,WAAW,CAAC,WAAW,EAAE,gBAAgB,GAAG,OAAO,CAAC,aAAa,CAAC;IAKzE;;;;;;;OAOG;IACI,UAAU,CAAC,WAAW,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;IAKtF;;;;;;OAMG;IACI,aAAa,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;IAMnE;;;;;;OAMG;IACI,YAAY,CAAC,OAAO,EAAE,YAAY,GAAG,OAAO,CAAC,aAAa,CAAC;IAMlE,OAAO,CAAC,eAAe;IAOvB,OAAO,CAAC,cAAc;CAMzB","file":"SpeakerIdMessageAdapter.d.ts","sourcesContent":["import {\r\n IRequestOptions,\r\n IRestResponse,\r\n RestConfigBase,\r\n RestMessageAdapter,\r\n RestRequestType,\r\n} from \"../common.browser/Exports\";\r\nimport { IAudioSource } from \"../common/Exports\";\r\nimport {\r\n PropertyId,\r\n SpeakerIdentificationModel,\r\n SpeakerVerificationModel,\r\n VoiceProfile,\r\n VoiceProfileType,\r\n} from \"../sdk/Exports\";\r\nimport { ConnectionFactoryBase } from \"./ConnectionFactoryBase\";\r\nimport { SpeakerRecognitionConfig } from \"./Exports\";\r\n\r\n/**\r\n * Implements methods for speaker recognition classes, sending requests to endpoint\r\n * and parsing response into expected format\r\n * @class SpeakerIdMessageAdapter\r\n */\r\nexport class SpeakerIdMessageAdapter {\r\n private privRestAdapter: RestMessageAdapter;\r\n private privUri: string;\r\n private privApiVersion: string;\r\n\r\n public constructor(config: SpeakerRecognitionConfig) {\r\n\r\n let endpoint = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Endpoint, undefined);\r\n if (!endpoint) {\r\n const region: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Region, \"westus\");\r\n const hostSuffix: string = ConnectionFactoryBase.getHostSuffix(region);\r\n endpoint = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Host, `https://${region}.api.cognitive${hostSuffix}`);\r\n }\r\n this.privUri = `${endpoint}/speaker-recognition/{mode}/{dependency}/profiles`;\r\n\r\n const options: IRequestOptions = RestConfigBase.requestOptions;\r\n options.headers[RestConfigBase.configParams.subscriptionKey] = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Key, undefined);\r\n this.privApiVersion = config.parameters.getProperty(PropertyId.SpeakerRecognition_Api_Version, \"2021-09-05\");\r\n\r\n this.privRestAdapter = new RestMessageAdapter(options);\r\n }\r\n\r\n /**\r\n * Sends create profile request to endpoint.\r\n * @function\r\n * @param {VoiceProfileType} profileType - type of voice profile to create.\r\n * @param {string} lang - language/locale of voice profile\r\n * @public\r\n * @returns {Promise<IRestResponse>} promised rest response containing id of created profile.\r\n */\r\n public createProfile(profileType: VoiceProfileType, lang: string):\r\n Promise<IRestResponse> {\r\n\r\n const uri = this.getOperationUri(profileType);\r\n return this.privRestAdapter.request(RestRequestType.Post, uri, this.getQueryParams({}), { locale: lang });\r\n }\r\n\r\n /**\r\n * Sends create enrollment request to endpoint.\r\n * @function\r\n * @param {VoiceProfile} profileType - voice profile for which to create new enrollment.\r\n * @param {IAudioSource} audioSource - audioSource from which to pull data to send\r\n * @public\r\n * @returns {Promise<IRestResponse>} rest response to enrollment request.\r\n */\r\n public createEnrollment(profile: VoiceProfile, audioSource: IAudioSource):\r\n Promise<IRestResponse> {\r\n\r\n const uri = this.getOperationUri(profile.profileType) + \"/\" + profile.profileId + \"/enrollments\";\r\n // eslint-disable-next-line @typescript-eslint/no-unsafe-argument\r\n return audioSource.blob.then<IRestResponse>((result: Blob | Buffer): Promise<IRestResponse> => this.privRestAdapter.request(RestRequestType.File, uri, this.getQueryParams({ ignoreMinLength: \"true\" }), null, result));\r\n }\r\n\r\n /**\r\n * Sends verification request to endpoint.\r\n * @function\r\n * @param {SpeakerVerificationModel} model - voice model to verify against.\r\n * @param {IAudioSource} audioSource - audioSource from which to pull data to send\r\n * @public\r\n * @returns {Promise<IRestResponse>} rest response to enrollment request.\r\n */\r\n public async verifySpeaker(model: SpeakerVerificationModel, audioSource: IAudioSource):\r\n Promise<IRestResponse> {\r\n\r\n const uri = this.getOperationUri(model.voiceProfile.profileType) + \"/\" + model.voiceProfile.profileId + \":verify\";\r\n try {\r\n const result: Blob | Buffer = await audioSource.blob;\r\n return this.privRestAdapter.request(RestRequestType.File, uri, this.getQueryParams({ ignoreMinLength: \"true\" }), null, result);\r\n } catch (e) {\r\n return Promise.resolve({ data: e as string } as IRestResponse);\r\n }\r\n }\r\n\r\n /**\r\n * Sends identification request to endpoint.\r\n * @function\r\n * @param {SpeakerIdentificationModel} model - voice profiles against which to identify.\r\n * @param {IAudioSource} audioSource - audioSource from which to pull data to send\r\n * @public\r\n * @returns {Promise<IRestResponse>} rest response to enrollment request.\r\n */\r\n public async identifySpeaker(model: SpeakerIdentificationModel, audioSource: IAudioSource):\r\n Promise<IRestResponse> {\r\n\r\n const uri = this.getOperationUri(VoiceProfileType.TextIndependentIdentification) + \":identifySingleSpeaker\";\r\n try {\r\n const result: Blob | Buffer = await audioSource.blob;\r\n return this.privRestAdapter.request(RestRequestType.File, uri, this.getQueryParams({ profileIds: model.voiceProfileIds, ignoreMinLength: \"true\" }), null, result);\r\n } catch (e) {\r\n return Promise.resolve({ data: e as string } as IRestResponse);\r\n }\r\n }\r\n\r\n /**\r\n * Sends profile status request to endpoint.\r\n * @function\r\n * @param {VoiceProfile} profile - voice profile to check.\r\n * @public\r\n * @returns {Promise<IRestResponse>} rest response to status request\r\n */\r\n public getProfileStatus(profile: VoiceProfile): Promise<IRestResponse> {\r\n\r\n const uri = `${this.getOperationUri(profile.profileType)}/${profile.profileId}`;\r\n return this.privRestAdapter.request(RestRequestType.Get, uri, this.getQueryParams());\r\n }\r\n\r\n /**\r\n * Sends get all profiles request to endpoint.\r\n * @function\r\n * @param {VoiceProfileType} profileType - type of profiles to return list of\r\n * @public\r\n * @returns {Promise<IRestResponse>} promised rest response containing all profiles\r\n */\r\n public getProfiles(profileType: VoiceProfileType): Promise<IRestResponse> {\r\n const uri = this.getOperationUri(profileType);\r\n return this.privRestAdapter.request(RestRequestType.Get, uri, this.getQueryParams());\r\n }\r\n\r\n /**\r\n * Sends get activation/auth phrases request to endpoint.\r\n * @function\r\n * @param {VoiceProfileType} profileType - type of profiles to return phrases for\r\n * @param {string} lang - language/locale of voice profile\r\n * @public\r\n * @returns {Promise<IRestResponse>} promised rest response containing list of valid phrases\r\n */\r\n public getPhrases(profileType: VoiceProfileType, lang: string): Promise<IRestResponse> {\r\n const uri = `${this.getOperationUri(profileType)}`.replace(\"profiles\", \"phrases\") + \"/\" + lang;\r\n return this.privRestAdapter.request(RestRequestType.Get, uri, this.getQueryParams());\r\n }\r\n\r\n /**\r\n * Sends delete profile request to endpoint.\r\n * @function\r\n * @param {VoiceProfile} profile - voice profile to delete.\r\n * @public\r\n * @returns {Promise<IRestResponse>} rest response to deletion request\r\n */\r\n public deleteProfile(profile: VoiceProfile): Promise<IRestResponse> {\r\n\r\n const uri = this.getOperationUri(profile.profileType) + \"/\" + profile.profileId;\r\n return this.privRestAdapter.request(RestRequestType.Delete, uri, this.getQueryParams());\r\n }\r\n\r\n /**\r\n * Sends reset profile request to endpoint.\r\n * @function\r\n * @param {VoiceProfile} profile - voice profile to reset enrollments for.\r\n * @public\r\n * @returns {Promise<IRestResponse>} rest response to reset request\r\n */\r\n public resetProfile(profile: VoiceProfile): Promise<IRestResponse> {\r\n\r\n const uri = this.getOperationUri(profile.profileType) + \"/\" + profile.profileId + \":reset\";\r\n return this.privRestAdapter.request(RestRequestType.Post, uri, this.getQueryParams());\r\n }\r\n\r\n private getOperationUri(profileType: VoiceProfileType): string {\r\n\r\n const mode = profileType === VoiceProfileType.TextIndependentIdentification ? \"identification\" : \"verification\";\r\n const dependency = profileType === VoiceProfileType.TextDependentVerification ? \"text-dependent\" : \"text-independent\";\r\n return this.privUri.replace(\"{mode}\", mode).replace(\"{dependency}\", dependency);\r\n }\r\n\r\n private getQueryParams(params: { [key: string]: any } = {}): { [key: string]: any } {\r\n\r\n params[RestConfigBase.configParams.apiVersion] = this.privApiVersion;\r\n return params;\r\n }\r\n\r\n}\r\n"]}