js-tts-wrapper
Version:
A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services
68 lines (67 loc) • 2.6 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.SpeechMarkdownConverter = void 0;
exports.toSSML = toSSML;
exports.isSpeechMarkdown = isSpeechMarkdown;
exports.getAvailablePlatforms = getAvailablePlatforms;
const environment_1 = require("../utils/environment");
const speechmarkdown_js_1 = require("speechmarkdown-js");
/**
* Browser-specific Speech Markdown converter that statically imports
* speechmarkdown-js so it can be bundled into the browser build.
*/
// Ensure we are in a browser build context; this file should only be used
// from src/browser.ts and the rollup browser bundle.
if (!environment_1.isBrowser) {
// No-op warning; this module is meant for browser bundles only
// eslint-disable-next-line no-console
console.warn("converter-browser loaded outside browser; consider using converter.ts instead");
}
class SpeechMarkdownConverter {
constructor() {
Object.defineProperty(this, "speechMarkdownInstance", {
enumerable: true,
configurable: true,
writable: true,
value: void 0
});
// Statically imported class; safe to instantiate for browser bundle
this.speechMarkdownInstance = new speechmarkdown_js_1.SpeechMarkdown();
}
async toSSML(markdown, platform = "amazon-alexa") {
return this.speechMarkdownInstance.toSSML(markdown, { platform });
}
isSpeechMarkdown(text) {
return isSpeechMarkdown(text);
}
getAvailablePlatforms() {
return getAvailablePlatforms();
}
}
exports.SpeechMarkdownConverter = SpeechMarkdownConverter;
const defaultConverter = new SpeechMarkdownConverter();
async function toSSML(markdown, platform = "amazon-alexa") {
return await defaultConverter.toSSML(markdown, platform);
}
function isSpeechMarkdown(text) {
// Keep parity with the detection heuristic from converter.ts
const patterns = [
/\[\d+m?s\]/,
/\[break:"[^"\]]+"\]/,
/\+\+.*?\+\+/,
/\+.*?\+/,
/~.*?~/,
/-.*?-/,
/\(.*?\)\[emphasis(:"(strong|moderate|reduced|none)")?\]/,
/\(.*?\)\[rate:"(x-slow|slow|medium|fast|x-fast)"\]/,
/\(.*?\)\[pitch:"(x-low|low|medium|high|x-high)"\]/,
/\(.*?\)\[volume:"(silent|x-soft|soft|medium|loud|x-loud)"\]/,
/\(.*?\)\[voice:".*?"\]/,
/\(.*?\)\[lang:".*?"\]/,
/\(.*?\)\[\w+:"?.*?"?\]/,
];
return patterns.some((pattern) => pattern.test(text));
}
function getAvailablePlatforms() {
return ["amazon-alexa", "google-assistant", "microsoft-azure"];
}
;