* 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)
| 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, '<' --> '<' and '&' --> '&' |
| 106 | let escapedLines = rawLines; |
| 107 | escapedLines = escapedLines.replace(/&/g, '&'); |
| 108 | escapedLines = escapedLines.replace(/"/g, '"'); |
| 109 | escapedLines = escapedLines.replace(/</g, '<'); |
| 110 | escapedLines = escapedLines.replace(/>/g, '>'); |
| 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); |