js-tts-wrapper
Version:
A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services
67 lines (66 loc) • 2.07 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.isSSML = isSSML;
exports.stripSSML = stripSSML;
exports.createProsodyTag = createProsodyTag;
exports.wrapWithSpeakTags = wrapWithSpeakTags;
/**
* Check if text is SSML
* @param text Text to check
* @returns True if the text is SSML
*/
function isSSML(text) {
return text.trim().startsWith("<speak") && text.trim().endsWith("</speak>");
}
/**
* Strip SSML tags from text
* @param ssml SSML text
* @returns Plain text without SSML tags
*/
function stripSSML(ssml) {
// Simple implementation - for production, consider using a proper XML parser
return ssml
.replace(/<speak.*?>/g, "")
.replace(/<\/speak>/g, "")
.replace(/<break.*?\/>/g, " ")
.replace(/<emphasis.*?>(.*?)<\/emphasis>/g, "$1")
.replace(/<prosody.*?>(.*?)<\/prosody>/g, "$1")
.replace(/<voice.*?>(.*?)<\/voice>/g, "$1")
.replace(/<say-as.*?>(.*?)<\/say-as>/g, "$1")
.replace(/<phoneme.*?>(.*?)<\/phoneme>/g, "$1")
.replace(/<sub.*?>(.*?)<\/sub>/g, "$1")
.replace(/<p>(.*?)<\/p>/g, "$1 ")
.replace(/<s>(.*?)<\/s>/g, "$1 ")
.replace(/\s+/g, " ")
.trim();
}
/**
* Create a prosody tag with the given properties
* @param text Text to wrap with prosody
* @param options Speak options
* @returns SSML with prosody tag
*/
function createProsodyTag(text, options) {
if (!options)
return text;
const attrs = [];
if (options.rate)
attrs.push(`rate="${options.rate}"`);
if (options.pitch)
attrs.push(`pitch="${options.pitch}"`);
if (options.volume !== undefined)
attrs.push(`volume="${options.volume}%"`);
if (attrs.length === 0)
return text;
return `<prosody ${attrs.join(" ")}>${text}</prosody>`;
}
/**
* Wrap text with speak tags if not already present
* @param text Text to wrap
* @returns SSML with speak tags
*/
function wrapWithSpeakTags(text) {
if (isSSML(text))
return text;
return `<speak>${text}</speak>`;
}
;