[{"id":"e273b97f.97c148","type":"tab","label":"Pi_2_bleSPEAKER","disabled":false,"info":""},{"id":"866572cd.1cabb","type":"comment","z":"e273b97f.97c148","name":"https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en&q=Hello+World","info":"https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en&q=Hello+World","x":480,"y":120,"wires":[]},{"id":"a58a79e3.5db398","type":"comment","z":"e273b97f.97c148","name":"Voice communication between e.g.Alexa and RPI","info":"# Output Delay to Bluetooth Speaker\n## **ffplay**\n 5-6 seconds\n## **cvlc**\n less than 3 seconds\n## **aplay**\n less than 1 second\n \n# Therefore best to convert mp3 to wav for use\n","x":540,"y":40,"wires":[]},{"id":"5ccbf570.ecdf9c","type":"wemo-emulator","z":"e273b97f.97c148","name":"","friendlyName":"My Pi","serial":"1","port":"34087","onTopic":"Hello Pi Zero","onPayload":"Hello, Pi Zero is now ON","offTopic":"Good Bye Pi Zero","offPayload":"Goodbye My Pi","x":200,"y":380,"wires":[["597a0b09.032f54"]]},{"id":"4d138d69.57e704","type":"comment","z":"e273b97f.97c148","name":"tell Alexa: Turn off My pi","info":"","x":200,"y":440,"wires":[]},{"id":"4a1502b.eda03fc","type":"comment","z":"e273b97f.97c148","name":"tell Alexa: Turn on My pi","info":"","x":200,"y":340,"wires":[]},{"id":"b1e32597.2c78a8","type":"inject","z":"e273b97f.97c148","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Goodbye","payloadType":"str","x":180,"y":280,"wires":[["597a0b09.032f54"]]},{"id":"67bd15e7.7e05cc","type":"inject","z":"e273b97f.97c148","name":"","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Hello","payloadType":"str","x":190,"y":220,"wires":[["597a0b09.032f54"]]},{"id":"bae924d9.b41608","type":"play audio","z":"e273b97f.97c148","name":"","voice":"0","x":850,"y":240,"wires":[]},{"id":"597a0b09.032f54","type":"function","z":"e273b97f.97c148","name":"TTS","func":"msg.url =\"https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en&q=\"+msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":390,"y":240,"wires":[["1a67f7f4.ceebf8","30704c6d15cd1368"]]},{"id":"1a67f7f4.ceebf8","type":"http request","z":"e273b97f.97c148","name":"Download mp3","method":"GET","ret":"bin","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":560,"y":240,"wires":[["fb043dc9.03987","bae924d9.b41608"]]},{"id":"5aa18234.1e1cfc","type":"exec","z":"e273b97f.97c148","command":"DISPLAY=:0 ffplay ","addpay":"payload","append":" >/dev/null 2>&1 -vn -autoexit -nodisp","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":850,"y":340,"wires":[[],[],["fcb0628317c8900a"]]},{"id":"fb043dc9.03987","type":"file","z":"e273b97f.97c148","name":"File write to Buffer","filename":"/home/pi/Desktop/audio.mp3","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":570,"y":280,"wires":[["8f1d43cfb19b69bf"]]},{"id":"819b3cfeb9606e49","type":"comment","z":"e273b97f.97c148","name":"Output on Browser","info":"","x":870,"y":200,"wires":[]},{"id":"bc3fe0386551d616","type":"comment","z":"e273b97f.97c148","name":"Output mp3 to Bluetooth Speaker","info":"","x":870,"y":300,"wires":[]},{"id":"a1f9ee40e25fa20d","type":"comment","z":"e273b97f.97c148","name":"ffplay Desktop/audio.mp3 >/dev/null 2>&1 -vn -autoexit -nodisp","info":"So by specifying > /dev/null 2>&1 you tell the system that you want to redirect the command's standard output into /dev/null, then redirect standard error into standard output (which in turn is redirected into /dev/null). This has the effect of silencing the command by throwing away all of its output that is sent to either stdout or stderr.","x":940,"y":380,"wires":[]},{"id":"6dd3fddb77516621","type":"exec","z":"e273b97f.97c148","command":"DISPLAY=:0 aplay","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":590,"y":700,"wires":[[],[],["b8f719b643171b25"]]},{"id":"9739b27be27bd415","type":"exec","z":"e273b97f.97c148","command":"DISPLAY=:0 ffplay","addpay":"payload","append":">/dev/null 2>&1 -vn -autoexit -nodisp -infbuf","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":590,"y":820,"wires":[[],[],["5ebc114217dbbe28"]]},{"id":"f67bc7636a06da18","type":"inject","z":"e273b97f.97c148","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Desktop/audio.mp3 ","payloadType":"str","x":230,"y":820,"wires":[["7b5a966485e2598e"]]},{"id":"7f7b33f409c4ec51","type":"inject","z":"e273b97f.97c148","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Desktop/audio.wav","payloadType":"str","x":230,"y":700,"wires":[["3b00b443ab48f4fd"]]},{"id":"e618a1223a958607","type":"exec","z":"e273b97f.97c148","command":"DISPLAY=:0 cvlc ","addpay":"payload","append":">/dev/null 2>&1 --play-and-exit","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":590,"y":760,"wires":[[],[],["9910a669ea46f618"]]},{"id":"8724b2a3ad73cd8c","type":"inject","z":"e273b97f.97c148","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Desktop/audio.mp3 ","payloadType":"str","x":230,"y":760,"wires":[["c21cc4ef3ea5342f"]]},{"id":"3b00b443ab48f4fd","type":"ui_button","z":"e273b97f.97c148","name":"","group":"0e9c5f30215bb24c","order":2,"width":0,"height":0,"passthru":true,"label":"aplay wav","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"Desktop/audio.wav","payloadType":"str","topic":"topic","topicType":"msg","x":400,"y":700,"wires":[["6dd3fddb77516621"]]},{"id":"c21cc4ef3ea5342f","type":"ui_button","z":"e273b97f.97c148","name":"","group":"0e9c5f30215bb24c","order":3,"width":0,"height":0,"passthru":true,"label":"cvlc mp3","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"Desktop/audio.mp3 ","payloadType":"str","topic":"topic","topicType":"msg","x":400,"y":760,"wires":[["e618a1223a958607"]]},{"id":"7b5a966485e2598e","type":"ui_button","z":"e273b97f.97c148","name":"","group":"0e9c5f30215bb24c","order":4,"width":0,"height":0,"passthru":true,"label":"ffplay mp3","tooltip":"","color":"","bgcolor":"","className":"","icon":"","payload":"Desktop/audio.mp3 ","payloadType":"str","topic":"topic","topicType":"msg","x":410,"y":820,"wires":[["9739b27be27bd415"]]},{"id":"69f00d4149f72637","type":"ui_text_input","z":"e273b97f.97c148","name":"Saved to mp3","label":"Saved to mp3","tooltip":"","group":"0e9c5f30215bb24c","order":1,"width":0,"height":0,"passthru":false,"mode":"text","delay":"0","topic":"topic","sendOnBlur":false,"className":"","topicType":"msg","x":200,"y":160,"wires":[["597a0b09.032f54"]]},{"id":"be8dde74a6affb79","type":"comment","z":"e273b97f.97c148","name":"and Convert to wav","info":"","x":550,"y":400,"wires":[]},{"id":"fb642b1de20d7af0","type":"exec","z":"e273b97f.97c148","command":"ffmpeg -y -i","addpay":"payload","append":"Desktop/audio.wav","useSpawn":"false","timer":"","winHide":true,"oldrc":false,"name":"","x":550,"y":440,"wires":[["bbdb135076df91fd"],[],[]]},{"id":"8f1d43cfb19b69bf","type":"change","z":"e273b97f.97c148","name":"Set msg.payload \\n to Buffer file","rules":[{"t":"set","p":"payload","pt":"msg","to":"Desktop/audio.mp3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":570,"y":340,"wires":[["fb642b1de20d7af0","5aa18234.1e1cfc"]]},{"id":"bbdb135076df91fd","type":"exec","z":"e273b97f.97c148","command":"DISPLAY=:0 aplay","addpay":"","append":"Desktop/audio.wav","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":570,"y":520,"wires":[[],[],["00ef3f60401554dd"]]},{"id":"f14fac21b4f26f8b","type":"comment","z":"e273b97f.97c148","name":"aplay Desktop/audio.wav to Bluetooth Speaker","info":"","x":570,"y":580,"wires":[]},{"id":"36a4798f455775ce","type":"ui_text_input","z":"e273b97f.97c148","name":"Saved","label":"Saved: APLAY as wav after saving as mp3","tooltip":"","group":"0e9c5f30215bb24c","order":6,"width":0,"height":0,"passthru":true,"mode":"text","delay":"0","topic":"topic","sendOnBlur":false,"className":"","topicType":"msg","x":170,"y":1000,"wires":[["ecd390911b63923d"]]},{"id":"ecd390911b63923d","type":"function","z":"e273b97f.97c148","name":"TTS","func":"msg.url =\"https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en&q=\"+msg.payload;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":350,"y":1000,"wires":[["d346e0ad93496732","6f4e70853167febe"]]},{"id":"d346e0ad93496732","type":"http request","z":"e273b97f.97c148","name":"Download mp3","method":"GET","ret":"bin","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":520,"y":1000,"wires":[["e84b49c4b011db9a","05fa4c5d6e71c57a","bc3cdad30a0078cb"]]},{"id":"e84b49c4b011db9a","type":"play audio","z":"e273b97f.97c148","name":"","voice":"0","x":750,"y":1000,"wires":[]},{"id":"f247a50823a43ed1","type":"change","z":"e273b97f.97c148","name":"Set msg.payload \\n to Buffer file","rules":[{"t":"set","p":"payload","pt":"msg","to":"Desktop/audio.mp3","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":530,"y":1100,"wires":[["df0f86374d3b2553"]]},{"id":"59b4a592c39a0f2f","type":"comment","z":"e273b97f.97c148","name":"Save Text to file, EXEC converts and outputs as wav by aplay","info":"ffmpeg -y -i Desktop/audio.mp3 -f wav - | aplay - \n","x":240,"y":920,"wires":[]},{"id":"bc3cdad30a0078cb","type":"file","z":"e273b97f.97c148","name":"File write to Buffer","filename":"/home/pi/Desktop/audio.mp3","appendNewline":false,"createDir":false,"overwriteFile":"true","encoding":"none","x":530,"y":1040,"wires":[["f247a50823a43ed1"]]},{"id":"b8f719b643171b25","type":"debug","z":"e273b97f.97c148","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":700,"wires":[]},{"id":"9910a669ea46f618","type":"debug","z":"e273b97f.97c148","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":760,"wires":[]},{"id":"5ebc114217dbbe28","type":"debug","z":"e273b97f.97c148","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":820,"wires":[]},{"id":"fcb0628317c8900a","type":"debug","z":"e273b97f.97c148","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":1050,"y":340,"wires":[]},{"id":"00ef3f60401554dd","type":"debug","z":"e273b97f.97c148","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":790,"y":520,"wires":[]},{"id":"30704c6d15cd1368","type":"debug","z":"e273b97f.97c148","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":550,"y":180,"wires":[]},{"id":"510fd79235320044","type":"inject","z":"e273b97f.97c148","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"Dynamic Output","payloadType":"str","x":150,"y":1080,"wires":[["36a4798f455775ce"]]},{"id":"05fa4c5d6e71c57a","type":"debug","z":"e273b97f.97c148","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":1040,"wires":[]},{"id":"c7e34a41c159088e","type":"debug","z":"e273b97f.97c148","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":1160,"wires":[]},{"id":"df0f86374d3b2553","type":"exec","z":"e273b97f.97c148","command":"ffmpeg -i ","addpay":"payload","append":" -f wav - | DISPLAY=:0 aplay","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":500,"y":1160,"wires":[["c7e34a41c159088e"],[],[]]},{"id":"421cf47fca038cb0","type":"exec","z":"e273b97f.97c148","command":"ffmpeg -i ","addpay":"payload","append":"-y -f wav - | DISPLAY=:0 aplay","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":460,"y":1300,"wires":[[],[],[]]},{"id":"6f4e70853167febe","type":"debug","z":"e273b97f.97c148","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":730,"y":960,"wires":[]},{"id":"0feb550ad55ffceb","type":"function","z":"e273b97f.97c148","name":"","func":"var url = \"'\"+msg.url+\"'\";\nmsg.payload = url;\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":330,"y":1120,"wires":[[]]},{"id":"88bc7ff238ee11b3","type":"ui_text_input","z":"e273b97f.97c148","name":"Dynamic","label":"Dynamic: APLAY as wav after coverting from mp3","tooltip":"","group":"0e9c5f30215bb24c","order":6,"width":0,"height":0,"passthru":true,"mode":"text","delay":"0","topic":"topic","sendOnBlur":false,"className":"","topicType":"msg","x":120,"y":1300,"wires":[["b14ec78481324685"]]},{"id":"a7fc2b93acb28f67","type":"play audio","z":"e273b97f.97c148","name":"","voice":"0","x":670,"y":1360,"wires":[]},{"id":"a444e30475b8f12e","type":"http request","z":"e273b97f.97c148","name":"Download mp3","method":"GET","ret":"bin","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","senderr":false,"x":480,"y":1360,"wires":[["a7fc2b93acb28f67"]]},{"id":"b14ec78481324685","type":"function","z":"e273b97f.97c148","name":"TTS","func":"msg.payload = encodeURIComponent(msg.payload.trim());// replace spaces with %20 for url GET REQUEST\nmsg.url =\"https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en&q=\"+msg.payload;\nmsg.payload = \"'\"+msg.url+\"'\";// need enclose REQUEST in quotes\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":270,"y":1300,"wires":[["a444e30475b8f12e","421cf47fca038cb0"]]},{"id":"27c818b43c17264c","type":"comment","z":"e273b97f.97c148","name":"Dynamic, ffmpeg convert and ouput mp3 as wav to aplay","info":"","x":270,"y":1240,"wires":[]},{"id":"21e367111b96ca4f","type":"ui_spacer","z":"e273b97f.97c148","name":"spacer","group":"0e9c5f30215bb24c","order":5,"width":1,"height":1},{"id":"261ed9809d91b303","type":"ui_spacer","z":"e273b97f.97c148","name":"spacer","group":"0e9c5f30215bb24c","order":1,"width":1,"height":1},{"id":"0e9c5f30215bb24c","type":"ui_group","name":"Text to Sound via Google Translate","tab":"b6152d595c55a32b","order":1,"disp":true,"width":"6","collapse":false,"className":""},{"id":"b6152d595c55a32b","type":"ui_tab","name":"Pi_2_bleSPEAKER","icon":"dashboard","disabled":false,"hidden":false}]
The above Flow uses Google Translate to convert text to sound which is output as an mp3. Outputing the mp3 through ffplay introduces a time lag of 5-7 seconds. This can be reduced to about 3 second by using cvlc for mp3 output, but best results are achieved by converting the mp3 to a wav by ffmpeg, either by saving the mp3 to file before conversion, or on the fly with ffmpeg piping wav to aplay. However when done on the fly the spaces in the url get request must be replaced by %20 in the msg.payload within the TTS function, and then enclosed in quotes.
TTS function node
msg.payload = encodeURIComponent(msg.payload.trim());// replace spaces with %20 for url GET REQUEST
msg.url ="https://translate.google.com/translate_tts?ie=UTF-8&client=tw-ob&tl=en&q="+msg.payload;
msg.payload = "'"+msg.url+"'";// need to enclose REQUEST in quotes for node red
return msg;
ffmpeg exec node
If output is needed to the browser then can HTTP request node to download the mp3 and use node-red-contrib-play-audio for output to the browser.
Download MP3