microsoft-cognitiveservices-speech-sdk
Version:
Microsoft Cognitive Services Speech SDK for JavaScript
147 lines (145 loc) • 5.13 kB
JavaScript
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpeechSynthesisRequest = exports.SpeechSynthesisRequestInputStream = void 0;
const Exports_js_1 = require("./Exports.js");
const SpeechSynthesisRequestInputType_js_1 = require("./SpeechSynthesisRequestInputType.js");
const SpeechSynthesisRequestInputStream_js_1 = require("./SpeechSynthesisRequestInputStream.js");
var SpeechSynthesisRequestInputStream_js_2 = require("./SpeechSynthesisRequestInputStream.js");
Object.defineProperty(exports, "SpeechSynthesisRequestInputStream", { enumerable: true, get: function () { return SpeechSynthesisRequestInputStream_js_2.SpeechSynthesisRequestInputStream; } });
/**
* Represents a speech synthesis request with support for text streaming.
* Note: This class is in preview and may be subject to change in future versions.
* @class SpeechSynthesisRequest
*/
class SpeechSynthesisRequest {
/**
* Creates a speech synthesis request.
* @param inputType The input type for the speech synthesis request.
*/
constructor(inputType) {
this.privBufferedTextPieces = [];
this.privStreamClosedBeforeReady = false;
if (inputType !== SpeechSynthesisRequestInputType_js_1.SpeechSynthesisRequestInputType.TextStream) {
throw new Error("Only TextStream input type is supported in this version.");
}
this.privInputType = inputType;
this.privInputStream = new SpeechSynthesisRequestInputStream_js_1.SpeechSynthesisRequestInputStream(this);
this.privProperties = new Exports_js_1.PropertyCollection();
}
/**
* Gets the input type of this request.
*/
get inputType() {
return this.privInputType;
}
/**
* Gets the input stream for writing text pieces.
*/
get inputStream() {
return this.privInputStream;
}
/**
* Gets the properties collection for this request.
*/
get properties() {
return this.privProperties;
}
/**
* Sets the pitch of the voice.
*/
set pitch(value) {
this.privProperties.setProperty(Exports_js_1.PropertyId.SpeechSynthesisRequest_Pitch, value);
}
/**
* Sets the speaking rate of the voice.
*/
set rate(value) {
this.privProperties.setProperty(Exports_js_1.PropertyId.SpeechSynthesisRequest_Rate, value);
}
/**
* Sets the volume of the voice.
*/
set volume(value) {
this.privProperties.setProperty(Exports_js_1.PropertyId.SpeechSynthesisRequest_Volume, value);
}
/**
* Sets the style of the voice.
*/
set style(value) {
this.privProperties.setProperty(Exports_js_1.PropertyId.SpeechSynthesisRequest_Style, value);
}
/**
* Sets the temperature of the voice synthesis.
*/
set temperature(value) {
this.privProperties.setProperty(Exports_js_1.PropertyId.SpeechSynthesisRequest_Temperature, value.toString());
}
/**
* Sets the custom lexicon URL.
*/
set customLexiconUrl(value) {
this.privProperties.setProperty(Exports_js_1.PropertyId.SpeechSynthesisRequest_CustomLexiconUrl, value);
}
/**
* Sets the preferred locales for the voice.
*/
set preferLocales(value) {
this.privProperties.setProperty(Exports_js_1.PropertyId.SpeechSynthesisRequest_PreferLocales, value);
}
/**
* @internal
* Called by InputStream when a text piece is written.
* Buffers text if no callback is registered yet.
*/
onTextPieceReceived(text) {
if (this.privTextPieceCallback) {
this.privTextPieceCallback(text);
}
else {
this.privBufferedTextPieces.push(text);
}
}
/**
* @internal
* Called by InputStream when it is closed.
* Buffers close event if no callback is registered yet.
*/
onInputStreamClosed() {
if (this.privStreamCloseCallback) {
this.privStreamCloseCallback();
}
else {
this.privStreamClosedBeforeReady = true;
}
}
/**
* @internal
* Sets the callback for receiving text pieces.
* Flushes any buffered text pieces immediately.
*/
set onTextPiece(callback) {
this.privTextPieceCallback = callback;
// Flush buffered text pieces
for (const text of this.privBufferedTextPieces) {
callback(text);
}
this.privBufferedTextPieces = [];
}
/**
* @internal
* Sets the callback for stream close events.
* Fires immediately if stream was already closed.
*/
set onClose(callback) {
this.privStreamCloseCallback = callback;
// Fire if stream was already closed before callback was set
if (this.privStreamClosedBeforeReady) {
this.privStreamClosedBeforeReady = false;
callback();
}
}
}
exports.SpeechSynthesisRequest = SpeechSynthesisRequest;
//# sourceMappingURL=SpeechSynthesisRequest.js.map