js-tts-wrapper
Version:
A JavaScript/TypeScript library that provides a unified API for working with multiple cloud-based Text-to-Speech (TTS) services
187 lines (186 loc) • 7.28 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
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");
/**
* Speech Markdown converter using the official speechmarkdown-js library
*
* This module provides functions to convert Speech Markdown to SSML
* using the speechmarkdown-js library (https://github.com/speechmarkdown/speechmarkdown-js)
*/
// Dynamic import for speechmarkdown-js
let SpeechMarkdown = null;
let speechMarkdownLoaded = false;
async function loadSpeechMarkdown() {
if (speechMarkdownLoaded)
return SpeechMarkdown;
try {
if (environment_1.isNode) {
// In Node.js, try to import speechmarkdown-js
const module = await Promise.resolve().then(() => __importStar(require("speechmarkdown-js")));
SpeechMarkdown = module.SpeechMarkdown;
speechMarkdownLoaded = true;
return SpeechMarkdown;
}
// In browser, speechmarkdown-js is not available
console.warn("speechmarkdown-js is not available in browser environments. Speech Markdown features will be limited.");
return null;
}
catch (_error) {
console.warn("speechmarkdown-js not found. Speech Markdown features will be limited. Install with: npm install speechmarkdown-js");
return null;
}
}
/**
* SpeechMarkdownConverter class for converting Speech Markdown to SSML
*/
class SpeechMarkdownConverter {
constructor() {
Object.defineProperty(this, "speechMarkdownInstance", {
enumerable: true,
configurable: true,
writable: true,
value: null
});
}
async ensureInitialized() {
if (!this.speechMarkdownInstance) {
const SpeechMarkdownClass = await loadSpeechMarkdown();
if (SpeechMarkdownClass) {
this.speechMarkdownInstance = new SpeechMarkdownClass();
}
}
return this.speechMarkdownInstance;
}
/**
* Convert Speech Markdown to SSML
*
* @param markdown Speech Markdown text
* @param platform Target platform (amazon-alexa, google-assistant, microsoft-azure, etc.)
* @returns SSML text
*/
async toSSML(markdown, platform = "amazon-alexa") {
const instance = await this.ensureInitialized();
if (!instance) {
// Fallback: return the text wrapped in basic SSML
return `<speak>${markdown}</speak>`;
}
return instance.toSSML(markdown, { platform });
}
/**
* Check if text is Speech Markdown
*
* @param text Text to check
* @returns True if the text contains Speech Markdown syntax
*/
isSpeechMarkdown(text) {
return isSpeechMarkdown(text);
}
/**
* Get the available platforms supported by the Speech Markdown library
*
* @returns Array of platform names
*/
getAvailablePlatforms() {
return getAvailablePlatforms();
}
}
exports.SpeechMarkdownConverter = SpeechMarkdownConverter;
// Create a default converter instance
const defaultConverter = new SpeechMarkdownConverter();
/**
* Convert Speech Markdown to SSML
*
* This function uses the speechmarkdown-js library to convert Speech Markdown syntax to SSML.
* The library supports various Speech Markdown features including:
* - Breaks: [500ms] or [break:"500ms"]
* - Emphasis: *emphasized text*
* - Rate, pitch, volume: (rate:slow), (pitch:high), (volume:loud)
* - And many more (see the speechmarkdown-js documentation)
*
* @param markdown Speech Markdown text
* @param platform Target platform (amazon-alexa, google-assistant, microsoft-azure, etc.)
* @returns SSML text
*/
async function toSSML(markdown, platform = "amazon-alexa") {
return await defaultConverter.toSSML(markdown, platform);
}
/**
* Check if text is Speech Markdown
*
* This function checks if the text contains Speech Markdown syntax patterns.
* It uses regular expressions to detect common Speech Markdown patterns such as:
* - Breaks: [500ms] or [break:"500ms"]
* - Emphasis: *emphasized text*
* - Rate, pitch, volume: (rate:slow), (pitch:high), (volume:loud)
*
* @param text Text to check
* @returns True if the text contains Speech Markdown syntax
*/
function isSpeechMarkdown(text) {
// Use a simple heuristic to check for common Speech Markdown patterns
// This is a simplified version as the library doesn't provide a direct way to check
const patterns = [
/\[\d+m?s\]/, // Breaks
/\[break:"\d+m?s"\]/, // Breaks with quotes
/\*.*?\*/, // Emphasis (short format)
/\(emphasis:(strong|moderate|reduced|none)\)/, // Emphasis
/\(rate:(x-slow|slow|medium|fast|x-fast)\)/, // Rate
/\(pitch:(x-low|low|medium|high|x-high)\)/, // Pitch
/\(volume:(silent|x-soft|soft|medium|loud|x-loud)\)/, // Volume
/\(voice:(\w+)\)/, // Voice
/\(lang:(\w+(-\w+)?)\)/, // Language
/\(\w+:.*?\)/, // Any other Speech Markdown directive
];
return patterns.some((pattern) => pattern.test(text));
}
/**
* Get the available platforms supported by the Speech Markdown library
*
* This function returns the list of platforms supported by the speechmarkdown-js library.
* These platforms have different SSML dialects, and the library will generate
* SSML appropriate for the specified platform.
*
* @returns Array of platform names (amazon-alexa, google-assistant, microsoft-azure)
*/
function getAvailablePlatforms() {
// The library doesn't expose a direct way to get platforms, so we hardcode them
// These are the platforms supported by speechmarkdown-js as of version 1.x
return ["amazon-alexa", "google-assistant", "microsoft-azure"];
}
;