text-to-speech using Azure Cognitive Service

This flow converts text to speech using the text-to-speech REST API.

The request body is structured as Speech Synthesis Markup Language (SSML), which allows you to choose the voice and language of the response.

This flow requires an Azure Cognitive Services account with a Speech Services resource. If you don't have an account, you can use the free trial to get a subscription key.

This flow requires node-red-contrib-play-audio

[{"id":"aff15848.d0d078","type":"http request","z":"9410f078.57ce7","name":"Issue token","method":"POST","ret":"txt","url":"https://francecentral.api.cognitive.microsoft.com/sts/v1.0/issueToken","tls":"","x":230,"y":180,"wires":[["fc0a4296.bca99"]]},{"id":"18a7189c.7de417","type":"inject","z":"9410f078.57ce7","name":"Sart","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":"","x":97,"y":33,"wires":[["c4bea049.ac4fc"]]},{"id":"fc0a4296.bca99","type":"function","z":"9410f078.57ce7","name":"Prepare request","func":"msg.headers = {};\nmsg.headers['Authorization'] = 'Bearer ' + msg.payload;\nmsg.headers['cache-control'] = 'no-cache';\nmsg.headers['User-Agent']= 'YOUR_RESOURCE_NAME';\nmsg.headers['X-Microsoft-OutputFormat'] = 'riff-24khz-16bit-mono-pcm';\nmsg.headers['Content-Type'] = 'application/ssml+xml';\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":260,"wires":[["5cfd258c.20e81c"]]},{"id":"5380e56f.08192c","type":"http request","z":"9410f078.57ce7","name":"Request cognitive service","method":"POST","ret":"bin","url":"https://francecentral.tts.speech.microsoft.com/cognitiveservices/v1","tls":"","x":730,"y":540,"wires":[["5713c4e3.62bbbc"]]},{"id":"c4bea049.ac4fc","type":"function","z":"9410f078.57ce7","name":"Prepare access token","func":"msg.headers = {};\nmsg.headers['Ocp-Apim-Subscription-Key'] = 'your_key_here';\nreturn msg;","outputs":1,"noerr":0,"x":180,"y":100,"wires":[["aff15848.d0d078"]]},{"id":"8e82bb52.97c488","type":"template","z":"9410f078.57ce7","name":"Request body","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"<speak version='1.0' xml:lang='en-us'>\n    <voice xml:lang='en-us' xml:gender='Male' name='Microsoft Server Speech Text to Speech Voice (en-US, Guy24KRUS)'>\n        {{payload}}\n    </voice>\n</speak>","output":"str","x":560,"y":420,"wires":[["5380e56f.08192c","3712687b.16e938"]]},{"id":"5713c4e3.62bbbc","type":"play audio","z":"9410f078.57ce7","name":"Say text","voice":"1","x":840,"y":620,"wires":[]},{"id":"5cfd258c.20e81c","type":"change","z":"9410f078.57ce7","name":"Text to speech","rules":[{"t":"set","p":"payload","pt":"msg","to":"Hello from Paris","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":340,"wires":[["8e82bb52.97c488"]]},{"id":"3712687b.16e938","type":"debug","z":"9410f078.57ce7","name":"debug msg","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":750,"y":400,"wires":[]},{"id":"7c74ae74.db529","type":"comment","z":"9410f078.57ce7","name":"Update with your subscription key","info":"","x":470,"y":100,"wires":[]},{"id":"bf151af9.198d08","type":"comment","z":"9410f078.57ce7","name":"Update User-Agent with your resource name","info":"","x":630,"y":240,"wires":[]},{"id":"b62ebb94.306748","type":"comment","z":"9410f078.57ce7","name":"Set a text to speech","info":"","x":650,"y":340,"wires":[]},{"id":"578bff3e.7f98d","type":"comment","z":"9410f078.57ce7","name":"Update region/endpoint","info":"","x":440,"y":180,"wires":[]},{"id":"c7d05021.ecbae","type":"comment","z":"9410f078.57ce7","name":"(change voice)","info":"","x":760,"y":440,"wires":[]},{"id":"b737618a.7e6d4","type":"comment","z":"9410f078.57ce7","name":"(change audio output format)","info":"","x":580,"y":280,"wires":[]},{"id":"fb582c2c.fd458","type":"comment","z":"9410f078.57ce7","name":"Update voice by region/endpoint","info":"","x":810,"y":500,"wires":[]}]

Flow Info

Created 6 years, 6 months ago
Rating: not yet rated

Owner

Actions

Rate:

Node Types

Core
  • change (x1)
  • comment (x7)
  • debug (x1)
  • function (x2)
  • http request (x2)
  • inject (x1)
  • template (x1)
Other

Tags

  • TTS
  • Azure
  • Cognitive_service
  • text-to-speech
  • play-audio
Copy this flow JSON to your clipboard and then import into Node-RED using the Import From > Clipboard (Ctrl-I) menu option