MCPcopy Create free account
hub / github.com/GoogleCloudPlatform/nodejs-docs-samples / textToSsml

Function textToSsml

texttospeech/ssmlAddresses.js:93–119  ·  view source on GitHub ↗

* Generates SSML text from plaintext. * * Given an input filename, this function converts the contents of the input text file * into a String of tagged SSML text. This function formats the SSML String so that, * when synthesized, the synthetic audio will pause for two seconds between eac

(inputFile)

Source from the content-addressed store, hash-verified

91 *
92 */
93 function textToSsml(inputFile) {
94 let rawLines = '';
95 // Read input file
96 try {
97 rawLines = fs.readFileSync(inputFile, 'utf8');
98 } catch (e) {
99 console.log('Error:', e.stack);
100 return;
101 }
102
103 // Replace special characters with HTML Ampersand Character Codes
104 // These codes prevent the API from confusing text with SSML tags
105 // For example, '<' --> '&lt;' and '&' --> '&amp;'
106 let escapedLines = rawLines;
107 escapedLines = escapedLines.replace(/&/g, '&amp;');
108 escapedLines = escapedLines.replace(/"/g, '&quot;');
109 escapedLines = escapedLines.replace(/</g, '&lt;');
110 escapedLines = escapedLines.replace(/>/g, '&gt;');
111
112 // Convert plaintext to SSML
113 // Tag SSML so that there is a 2 second pause between each address
114 const expandedNewline = escapedLines.replace(/\n/g, '\n<break time="2s"/>');
115 const ssml = '<speak>' + expandedNewline + '</speak>';
116
117 // Return the concatenated String of SSML
118 return ssml;
119 }
120 // [END tts_ssml_address_ssml]
121 const ssml = textToSsml(inFile);
122 await ssmlToAudio(ssml, outFile);

Callers 1

mainFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected