{"version":3,"sources":["src/common.speech/SpeakerIdMessageAdapter.ts"],"names":[],"mappings":"AAAA,OAAO,EAEH,aAAa,EAIhB,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAGH,YAAY,EACZ,OAAO,EAEV,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAEH,0BAA0B,EAC1B,wBAAwB,EACxB,YAAY,EACZ,gBAAgB,EACnB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,wBAAwB,EAAE,MAAM,WAAW,CAAC;AAErD;;;;GAIG;AACH,qBAAa,uBAAuB;IAChC,OAAO,CAAC,eAAe,CAAqB;IAC5C,OAAO,CAAC,OAAO,CAAS;gBAEL,MAAM,EAAE,wBAAwB;IAiBnD;;;;;;;OAOG;IACI,aAAa,CAAC,WAAW,EAAE,gBAAgB,EAAE,IAAI,EAAE,MAAM,GAC5D,OAAO,CAAC,aAAa,CAAC;IAO1B;;;;;;;OAOG;IACI,gBAAgB,CAAC,OAAO,EAAE,YAAY,EAAE,WAAW,EAAE,YAAY,GACpE,OAAO,CAAC,aAAa,CAAC;IAS1B;;;;;;;OAOG;IACI,aAAa,CAAC,KAAK,EAAE,wBAAwB,EAAE,WAAW,EAAE,YAAY,GAC3E,OAAO,CAAC,aAAa,CAAC;IAc1B;;;;;;;OAOG;IACI,eAAe,CAAC,KAAK,EAAE,0BAA0B,EAAE,WAAW,EAAE,YAAY,GAC/E,OAAO,CAAC,aAAa,CAAC;IAa1B;;;;;;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;CAO1B","file":"SpeakerIdMessageAdapter.d.ts","sourcesContent":["import {\n    IRequestOptions,\n    IRestResponse,\n    RestConfigBase,\n    RestMessageAdapter,\n    RestRequestType,\n} from \"../common.browser/Exports\";\nimport {\n    createNoDashGuid,\n    Deferred,\n    IAudioSource,\n    Promise,\n    PromiseResult,\n} from \"../common/Exports\";\nimport {\n    PropertyId,\n    SpeakerIdentificationModel,\n    SpeakerVerificationModel,\n    VoiceProfile,\n    VoiceProfileType,\n} from \"../sdk/Exports\";\nimport { SpeakerRecognitionConfig } from \"./Exports\";\n\n/**\n * Implements methods for speaker recognition classes, sending requests to endpoint\n * and parsing response into expected format\n * @class SpeakerIdMessageAdapter\n */\nexport class SpeakerIdMessageAdapter {\n    private privRestAdapter: RestMessageAdapter;\n    private privUri: string;\n\n    public constructor(config: SpeakerRecognitionConfig) {\n\n        let endpoint = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Endpoint, undefined);\n        if (!endpoint) {\n            const region: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Region, \"westus\");\n            const hostSuffix: string = (region && region.toLowerCase().startsWith(\"china\")) ? \".azure.cn\" : \".microsoft.com\";\n            const host: string = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Host, \"https://\" + region + \".api.cognitive\" + hostSuffix + \"/speaker/{mode}/v2.0/{dependency}\");\n            endpoint = host + \"/profiles\";\n        }\n        this.privUri = endpoint;\n\n        const options: IRequestOptions = RestConfigBase.requestOptions;\n        options.headers[RestConfigBase.configParams.subscriptionKey] = config.parameters.getProperty(PropertyId.SpeechServiceConnection_Key, undefined);\n\n        this.privRestAdapter = new RestMessageAdapter(options);\n    }\n\n    /**\n     * Sends create profile request to endpoint.\n     * @function\n     * @param {VoiceProfileType} profileType - type of voice profile to create.\n     * @param {string} lang - language/locale of voice profile\n     * @public\n     * @returns {Promise<IRestResponse>} promised rest response containing id of created profile.\n     */\n    public createProfile(profileType: VoiceProfileType, lang: string):\n        Promise<IRestResponse> {\n\n        const uri = this.getOperationUri(profileType);\n        this.privRestAdapter.setHeaders(RestConfigBase.configParams.contentTypeKey, \"application/json\");\n        return this.privRestAdapter.request(RestRequestType.Post, uri, {}, { locale: lang });\n    }\n\n    /**\n     * Sends create enrollment request to endpoint.\n     * @function\n     * @param {VoiceProfile} profileType - voice profile for which to create new enrollment.\n     * @param {IAudioSource} audioSource - audioSource from which to pull data to send\n     * @public\n     * @returns {Promise<IRestResponse>} rest response to enrollment request.\n     */\n    public createEnrollment(profile: VoiceProfile, audioSource: IAudioSource):\n        Promise<IRestResponse> {\n\n        this.privRestAdapter.setHeaders(RestConfigBase.configParams.contentTypeKey, \"multipart/form-data\");\n        const uri = this.getOperationUri(profile.profileType) + \"/\" + profile.profileId + \"/enrollments\";\n        return audioSource.blob.onSuccessContinueWithPromise<IRestResponse>((result: Blob | Buffer): Promise<IRestResponse> => {\n            return this.privRestAdapter.request(RestRequestType.File, uri, { ignoreMinLength: \"true\" }, null, result);\n        });\n    }\n\n    /**\n     * Sends verification request to endpoint.\n     * @function\n     * @param {SpeakerVerificationModel} model - voice model to verify against.\n     * @param {IAudioSource} audioSource - audioSource from which to pull data to send\n     * @public\n     * @returns {Promise<IRestResponse>} rest response to enrollment request.\n     */\n    public verifySpeaker(model: SpeakerVerificationModel, audioSource: IAudioSource):\n        Promise<IRestResponse> {\n\n        this.privRestAdapter.setHeaders(RestConfigBase.configParams.contentTypeKey, \"multipart/form-data\");\n        const uri = this.getOperationUri(model.voiceProfile.profileType) + \"/\" + model.voiceProfile.profileId + \"/verify\";\n        return audioSource.blob.continueWithPromise<IRestResponse>((result: PromiseResult<Blob | Buffer>): Promise<IRestResponse> => {\n            if (result.isError) {\n                const response: Deferred<IRestResponse> = new Deferred<IRestResponse>();\n                response.resolve({ data: result.error } as IRestResponse);\n                return response.promise();\n            }\n            return this.privRestAdapter.request(RestRequestType.File, uri, { ignoreMinLength: \"true\" }, null, result.result);\n        });\n    }\n\n    /**\n     * Sends identification request to endpoint.\n     * @function\n     * @param {SpeakerIdentificationModel} model - voice profiles against which to identify.\n     * @param {IAudioSource} audioSource - audioSource from which to pull data to send\n     * @public\n     * @returns {Promise<IRestResponse>} rest response to enrollment request.\n     */\n    public identifySpeaker(model: SpeakerIdentificationModel, audioSource: IAudioSource):\n        Promise<IRestResponse> {\n\n        this.privRestAdapter.setHeaders(RestConfigBase.configParams.contentTypeKey, \"multipart/form-data\");\n        const uri = this.getOperationUri(VoiceProfileType.TextIndependentIdentification) + \"/identifySingleSpeaker\";\n        return audioSource.blob.continueWithPromise<IRestResponse>((result: PromiseResult<Blob | Buffer>): Promise<IRestResponse> => {\n            if (result.isError) {\n                const response: Deferred<IRestResponse> = new Deferred<IRestResponse>();\n                response.resolve({ data: result.error } as IRestResponse);\n                return response.promise();\n            }\n            return this.privRestAdapter.request(RestRequestType.File, uri, { profileIds: model.voiceProfileIds, ignoreMinLength: \"true\" }, null, result.result);\n        });\n    }\n    /**\n     * Sends delete profile request to endpoint.\n     * @function\n     * @param {VoiceProfile} profile - voice profile to delete.\n     * @public\n     * @returns {Promise<IRestResponse>} rest response to deletion request\n     */\n    public deleteProfile(profile: VoiceProfile): Promise<IRestResponse> {\n\n        const uri = this.getOperationUri(profile.profileType) + \"/\" + profile.profileId;\n        return this.privRestAdapter.request(RestRequestType.Delete, uri, {});\n    }\n\n    /**\n     * Sends reset profile request to endpoint.\n     * @function\n     * @param {VoiceProfile} profile - voice profile to reset enrollments for.\n     * @public\n     * @returns {Promise<IRestResponse>} rest response to reset request\n     */\n    public resetProfile(profile: VoiceProfile): Promise<IRestResponse> {\n\n        const uri = this.getOperationUri(profile.profileType) + \"/\" + profile.profileId + \"/reset\";\n        return this.privRestAdapter.request(RestRequestType.Post, uri, {});\n    }\n\n    private getOperationUri(profileType: VoiceProfileType): string {\n\n        const mode = profileType === VoiceProfileType.TextIndependentIdentification ? \"identification\" : \"verification\";\n        const dependency = profileType === VoiceProfileType.TextDependentVerification ? \"text-dependent\" : \"text-independent\";\n        return this.privUri.replace(\"{mode}\", mode).replace(\"{dependency}\", dependency);\n    }\n\n}\n"]}