@voice-ping/cognitive-services-speech
Version:
VoicePing Cognitive Services Speech SDK for JavaScript forked from Microsoft
128 lines (126 loc) • 7.01 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Exports_1 = require("../common.browser/Exports");
var Exports_2 = require("../common/Exports");
var Exports_3 = require("../sdk/Exports");
/**
* Implements methods for speaker recognition classes, sending requests to endpoint
* and parsing response into expected format
* @class SpeakerIdMessageAdapter
*/
var SpeakerIdMessageAdapter = /** @class */ (function () {
function SpeakerIdMessageAdapter(config) {
var endpoint = config.parameters.getProperty(Exports_3.PropertyId.SpeechServiceConnection_Endpoint, undefined);
if (!endpoint) {
var region = config.parameters.getProperty(Exports_3.PropertyId.SpeechServiceConnection_Region, "westus");
var hostSuffix = (region && region.toLowerCase().startsWith("china")) ? ".azure.cn" : ".microsoft.com";
var host = config.parameters.getProperty(Exports_3.PropertyId.SpeechServiceConnection_Host, "https://" + region + ".api.cognitive" + hostSuffix + "/speaker/{mode}/v2.0/{dependency}");
endpoint = host + "/profiles";
}
this.privUri = endpoint;
var options = Exports_1.RestConfigBase.requestOptions;
options.headers[Exports_1.RestConfigBase.configParams.subscriptionKey] = config.parameters.getProperty(Exports_3.PropertyId.SpeechServiceConnection_Key, undefined);
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.
*/
SpeakerIdMessageAdapter.prototype.createProfile = function (profileType, lang) {
var uri = this.getOperationUri(profileType);
this.privRestAdapter.setHeaders(Exports_1.RestConfigBase.configParams.contentTypeKey, "application/json");
return this.privRestAdapter.request(Exports_1.RestRequestType.Post, uri, {}, { 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.
*/
SpeakerIdMessageAdapter.prototype.createEnrollment = function (profile, audioSource) {
var _this = this;
this.privRestAdapter.setHeaders(Exports_1.RestConfigBase.configParams.contentTypeKey, "multipart/form-data");
var uri = this.getOperationUri(profile.profileType) + "/" + profile.profileId + "/enrollments";
return audioSource.blob.onSuccessContinueWithPromise(function (result) {
return _this.privRestAdapter.request(Exports_1.RestRequestType.File, uri, { 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.
*/
SpeakerIdMessageAdapter.prototype.verifySpeaker = function (model, audioSource) {
var _this = this;
this.privRestAdapter.setHeaders(Exports_1.RestConfigBase.configParams.contentTypeKey, "multipart/form-data");
var uri = this.getOperationUri(model.voiceProfile.profileType) + "/" + model.voiceProfile.profileId + "/verify";
return audioSource.blob.continueWithPromise(function (result) {
if (result.isError) {
var response = new Exports_2.Deferred();
response.resolve({ data: result.error });
return response.promise();
}
return _this.privRestAdapter.request(Exports_1.RestRequestType.File, uri, { ignoreMinLength: "true" }, null, result.result);
});
};
/**
* 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.
*/
SpeakerIdMessageAdapter.prototype.identifySpeaker = function (model, audioSource) {
var _this = this;
this.privRestAdapter.setHeaders(Exports_1.RestConfigBase.configParams.contentTypeKey, "multipart/form-data");
var uri = this.getOperationUri(Exports_3.VoiceProfileType.TextIndependentIdentification) + "/identifySingleSpeaker";
return audioSource.blob.continueWithPromise(function (result) {
if (result.isError) {
var response = new Exports_2.Deferred();
response.resolve({ data: result.error });
return response.promise();
}
return _this.privRestAdapter.request(Exports_1.RestRequestType.File, uri, { profileIds: model.voiceProfileIds, ignoreMinLength: "true" }, null, result.result);
});
};
/**
* Sends delete profile request to endpoint.
* @function
* @param {VoiceProfile} profile - voice profile to delete.
* @public
* @returns {Promise<IRestResponse>} rest response to deletion request
*/
SpeakerIdMessageAdapter.prototype.deleteProfile = function (profile) {
var uri = this.getOperationUri(profile.profileType) + "/" + profile.profileId;
return this.privRestAdapter.request(Exports_1.RestRequestType.Delete, uri, {});
};
/**
* 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
*/
SpeakerIdMessageAdapter.prototype.resetProfile = function (profile) {
var uri = this.getOperationUri(profile.profileType) + "/" + profile.profileId + "/reset";
return this.privRestAdapter.request(Exports_1.RestRequestType.Post, uri, {});
};
SpeakerIdMessageAdapter.prototype.getOperationUri = function (profileType) {
var mode = profileType === Exports_3.VoiceProfileType.TextIndependentIdentification ? "identification" : "verification";
var dependency = profileType === Exports_3.VoiceProfileType.TextDependentVerification ? "text-dependent" : "text-independent";
return this.privUri.replace("{mode}", mode).replace("{dependency}", dependency);
};
return SpeakerIdMessageAdapter;
}());
exports.SpeakerIdMessageAdapter = SpeakerIdMessageAdapter;
//# sourceMappingURL=SpeakerIdMessageAdapter.js.map