js-tts-wrapper
Version:
A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services
91 lines (90 loc) • 2.45 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.SSMLBuilder = void 0;
/**
* SSML Builder class for creating SSML markup
*/
class SSMLBuilder {
constructor() {
Object.defineProperty(this, "ssml", {
enumerable: true,
configurable: true,
writable: true,
value: ""
});
}
/**
* Add text or SSML to the builder
* @param text Text or SSML to add
* @returns The SSML string
*/
add(text) {
// If text doesn't start with <speak>, wrap it
if (text.trim().startsWith("<speak")) {
this.ssml = text;
}
else {
this.ssml = `<speak>${text}</speak>`;
}
return this.ssml;
}
/**
* Add a break to the SSML
* @param time Break duration (e.g., '500ms')
* @returns The SSML builder instance
*/
addBreak(time = "500ms") {
this.ssml = this.ssml.replace("</speak>", `<break time="${time}"/></speak>`);
return this;
}
/**
* Add prosody element to the SSML
* @param text Text to wrap with prosody
* @param rate Speech rate
* @param pitch Speech pitch
* @param volume Speech volume
* @returns The SSML builder instance
*/
addProsody(text, rate, pitch, volume) {
let prosodyAttrs = "";
if (rate)
prosodyAttrs += ` rate="${rate}"`;
if (pitch)
prosodyAttrs += ` pitch="${pitch}"`;
if (volume)
prosodyAttrs += ` volume="${volume}"`;
const prosodyElement = `<prosody${prosodyAttrs}>${text}</prosody>`;
if (this.ssml.includes("<speak>")) {
this.ssml = this.ssml.replace("<speak>", `<speak>${prosodyElement}`);
}
else {
this.ssml = `<speak>${prosodyElement}</speak>`;
}
return this;
}
/**
* Wrap text with speak tags
* @param text Text to wrap
* @returns SSML string with speak tags
*/
wrapWithSpeak(text) {
if (!text.trim().startsWith("<speak")) {
return `<speak>${text}</speak>`;
}
return text;
}
/**
* Clear the SSML content
*/
clearSSML() {
this.ssml = "";
}
/**
* Get the current SSML string
* @returns The current SSML string
*/
toString() {
return this.ssml;
}
}
exports.SSMLBuilder = SSMLBuilder;
;