UNPKG

ssmd

Version:

Speech Synthesis Markdown (SSMD) is a lightweight alternative syntax for Speech Synthesis Markup Language (SSML)

107 lines (97 loc) 2.38 kB
const R = require("ramda"); const AmazonSpeech = require("ssml-builder/amazon_speech"); const markdownParser = require("./markdownParser"); const xmlFormatter = require('xml-formatter'); const defaultConfig = { outputSpeakTag: true, prettyPrint: true, headingLevels: { 1: [{ tag: "emphasis", value: 'strong' }, { tag: "pause", value: '100ms' }, ], 2: [{ tag: "emphasis", value: 'moderate' }, { tag: "pause", value: '75ms' }, ], 3: [{ tag: "emphasis", value: 'reduced' }, { tag: "pause", value: '50ms' }, ], } } const ssmd = (text, config = {}) => { if (config === false) { config = { outputSpeakTag: false, prettyPrint: false } } const configMerged = R.mergeDeepRight(defaultConfig, config); const { outputSpeakTag, prettyPrint } = configMerged; const parseGeneric = (separator, tag, subCall) => text => { const outputParts = R.compose( R.filter(R.identity), R.map(R.trim), R.map(splitText => subCall(splitText)), R.split(separator) )(text); let outputText; if (outputParts.length === 1) { outputText = R.head(outputParts); } else { // Join each part with the provided tag outputText = `<${tag}>${R.join(`</${tag}>${separator}<${tag}>`)( outputParts )}</${tag}>`; } return outputText; }; const parseSentence = sentence => R.reduce( (s, { type, text }) => { if (type === "text") { s.push(text); return s; } s.push(type(new AmazonSpeech(), configMerged).ssml(true)); return s; }, [], markdownParser.toTree() && markdownParser.toTree(sentence) ).join(""); // Will add s tag for each sentence const parseSentences = parseGeneric("\n", "s", parseSentence); // Will add p tag for each paragraph const parseParagraphs = parseGeneric("\n\n", "p", parseSentences); let ssmlOutput = parseParagraphs(text); if (outputSpeakTag) { ssmlOutput = `<speak>${ssmlOutput}</speak>` } if (prettyPrint) { ssmlOutput = xmlFormatter(ssmlOutput) || ssmlOutput; // in case xmlFormatter return empty string } return ssmlOutput; }; module.exports = ssmd;