Skip to content

Instantly share code, notes, and snippets.

@scargill
Last active May 8, 2024 12:27
Show Gist options
  • Save scargill/2a662d571d3b3d3d1de4ad344b7edb02 to your computer and use it in GitHub Desktop.
Save scargill/2a662d571d3b3d3d1de4ad344b7edb02 to your computer and use it in GitHub Desktop.
Node-Red flow for audio - rpi4 and 5 - Docker version
// Comments not allowed in JSON - do NOT copy these comments
// No comments in Github please - I don't visit that often
// https://tech.scargill.net/aws-polly-speech-on-rpi4-and-rpi5-using-node-red-and-docker/
[
{
"id": "d889a35bfcc63c2f",
"type": "exec",
"z": "98fc211d841d9a2d",
"command": "ssh host",
"addpay": "payload",
"append": "",
"useSpawn": "false",
"timer": "",
"winHide": false,
"oldrc": false,
"name": "mpg123 on host",
"x": 1340,
"y": 340,
"wires": [
[
"2cc43b1538cd8d17"
],
[],
[]
]
},
{
"id": "a51be2c7bd5a93c4",
"type": "exec",
"z": "98fc211d841d9a2d",
"command": "ssh host",
"addpay": "payload",
"append": "",
"useSpawn": "false",
"timer": "",
"winHide": false,
"oldrc": false,
"name": "aws in container exec node",
"x": 1380,
"y": 180,
"wires": [
[
"e8f1df220b9a0e3b"
],
[],
[]
]
},
{
"id": "1738bb65.3471d5",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "50%",
"repeat": "",
"crontab": "",
"once": false,
"topic": "",
"payload": "50",
"payloadType": "str",
"x": 390,
"y": 180,
"wires": [
[
"0497724a81f21b0e"
]
]
},
{
"id": "10e04ec3.32a121",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "25%",
"repeat": "",
"crontab": "",
"once": false,
"topic": "",
"payload": "25",
"payloadType": "str",
"x": 390,
"y": 140,
"wires": [
[
"0497724a81f21b0e"
]
]
},
{
"id": "8f283f57.3b399",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "Red Alert",
"props": [
{
"p": "payload",
"v": "red alert",
"vt": "str"
},
{
"p": "topic",
"v": "amy",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"topic": "amy",
"payload": "red alert",
"payloadType": "str",
"x": 760,
"y": 220,
"wires": [
[
"c6b58278606d97a5"
]
]
},
{
"id": "2b461c2.7bd40e4",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "Comms beep",
"props": [
{
"p": "payload",
"v": "comms beep",
"vt": "str"
},
{
"p": "topic",
"v": "amy",
"vt": "string"
}
],
"repeat": "",
"crontab": "",
"once": false,
"topic": "amy",
"payload": "comms beep",
"payloadType": "str",
"x": 770,
"y": 260,
"wires": [
[
"c6b58278606d97a5"
]
]
},
{
"id": "e928e8.25ad7718",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "Hailing frequencies open",
"repeat": "",
"crontab": "",
"once": false,
"topic": "amy",
"payload": "hailing frequencies open",
"payloadType": "str",
"x": 810,
"y": 300,
"wires": [
[
"c6b58278606d97a5"
]
]
},
{
"id": "2d98b3aa.d7284c",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "Keypress",
"props": [
{
"p": "payload",
"v": "keypress",
"vt": "str"
},
{
"p": "topic",
"v": "amy",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"topic": "amy",
"payload": "keypress",
"payloadType": "str",
"x": 760,
"y": 340,
"wires": [
[
"c6b58278606d97a5"
]
]
},
{
"id": "b0d13aff.e60fd8",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "Unable to comply",
"props": [
{
"p": "payload",
"v": "unable to comply",
"vt": "str"
},
{
"p": "topic",
"v": "amy",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"topic": "amy",
"payload": "unable to comply",
"payloadType": "str",
"x": 780,
"y": 380,
"wires": [
[
"c6b58278606d97a5"
]
]
},
{
"id": "7e2cec04.b03ee4",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "0%",
"repeat": "",
"crontab": "",
"once": false,
"topic": "",
"payload": "0",
"payloadType": "str",
"x": 390,
"y": 100,
"wires": [
[
"0497724a81f21b0e"
]
]
},
{
"id": "e4d915f1.5aaea8",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "100%",
"repeat": "",
"crontab": "",
"once": false,
"topic": "",
"payload": "100",
"payloadType": "str",
"x": 390,
"y": 260,
"wires": [
[
"0497724a81f21b0e"
]
]
},
{
"id": "b98733e4.20171",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "75%",
"repeat": "",
"crontab": "",
"once": false,
"topic": "",
"payload": "75",
"payloadType": "str",
"x": 390,
"y": 220,
"wires": [
[
"0497724a81f21b0e"
]
]
},
{
"id": "3134692e.250326",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "Wind Chimes One",
"props": [
{
"p": "payload",
"v": "wind chimes one",
"vt": "str"
},
{
"p": "topic",
"v": "amy",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"topic": "amy",
"payload": "wind chimes one",
"payloadType": "str",
"x": 790,
"y": 420,
"wires": [
[
"c6b58278606d97a5"
]
]
},
{
"id": "94bb09db.a9bad8",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "Powerup 100%",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": "1",
"topic": "",
"payload": "100",
"payloadType": "str",
"x": 420,
"y": 40,
"wires": [
[
"0497724a81f21b0e"
]
]
},
{
"id": "14222a13.e6f206",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "thunder",
"props": [
{
"p": "payload",
"v": "thunder",
"vt": "str"
},
{
"p": "topic",
"v": "amy",
"vt": "string"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": "",
"topic": "amy",
"payload": "thunder",
"payloadType": "str",
"x": 750,
"y": 180,
"wires": [
[
"c6b58278606d97a5"
]
]
},
{
"id": "7f84915b.077af",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "amy",
"payload": "dogs barking",
"payloadType": "str",
"x": 790,
"y": 460,
"wires": [
[
"c6b58278606d97a5"
]
]
},
{
"id": "e8f1df220b9a0e3b",
"type": "function",
"z": "98fc211d841d9a2d",
"name": "Clr creating",
"func": "global.set(\"create_speech_busy\",0);\nmsg.payload=\"\";\nreturn msg;",
"outputs": 1,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1330,
"y": 280,
"wires": [
[
"c6b58278606d97a5"
]
]
},
{
"id": "c6b58278606d97a5",
"type": "function",
"z": "98fc211d841d9a2d",
"name": "process text",
"func": "if (typeof context.arr == \"undefined\" || !(context.arr instanceof Array)) context.arr = [];\nif (typeof global.get(\"speech_busy\") == \"undefined\") global.set(\"speech_busy\", 0);\nif (typeof global.get(\"create_speech_busy\") == \"undefined\") global.set(\"create_speech_busy\", 0);\n\nif (msg.payload !== \"\") context.arr.push(msg.payload);\nif (context.arr.length) {\n msg.payload = context.arr.shift();\n if (msg.payload.indexOf(\".mp3\") == -1) {\n var fs = global.get(\"fs\");\n var mess = msg.payload;\n mess = mess.replace(/'/g, \"\");\n var messfile = mess.toLowerCase();\n messfile = messfile.replace(/[.,\\/#!$%\\^&\\*;:{}=\\-_`~()]/g, \"\");\n messfile = messfile.replace(/ /g, \"_\");\n messfile = \"/data/audio/\" + messfile + \".mp3\";\n\n if (fs.existsSync(messfile)) {\n if (global.get(\"speech_busy\")==1) { context.arr.unshift(msg.payload); return [null, null]; }\n else { global.set(\"speech_busy\", 1); msg.payload = \"mpg123 /root/DockerIOT/nodered\" + messfile; return [null, msg]; }\n }\n else {\n if (global.get(\"create_speech_busy\")==1) { context.arr.unshift(msg.payload); return [null, null]; } else\n {\n context.arr.unshift(msg.payload);\n global.set(\"create_speech_busy\",1);\n var voice = \"Amy\";\n // msg.payload = 'polly synthesize-speech --engine \"neural\" --region eu-west-2 --output-format mp3 --voice-id ' + voice + ' --text \"' + mess + '\" ' + messfile;\n \n msg.payload = \"docker run --rm -i -v /root:/root amazon/aws-cli \\\"polly synthesize-speech --output-format mp3 --engine 'neural' --voice-id \" + voice + \" --region eu-west-2 --text '\" + mess + \"' /root/DockerIOT/nodered\" + messfile + \"\\\"\";\n return [msg, null];\n }\n }\n }\n if (global.get(\"speech_busy\")==1) context.arr.unshift(msg.payload); \n else { global.set(\"speech_busy\", 1); return [null, msg]; } // mp3 or synth \n}\n",
"outputs": "2",
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1130,
"y": 220,
"wires": [
[
"a51be2c7bd5a93c4"
],
[
"d889a35bfcc63c2f"
]
]
},
{
"id": "2cc43b1538cd8d17",
"type": "function",
"z": "98fc211d841d9a2d",
"name": "Clr playing",
"func": "global.set(\"speech_busy\",0);\nmsg.payload=\"\"; return msg;",
"outputs": "1",
"noerr": 0,
"x": 1330,
"y": 440,
"wires": [
[
"c6b58278606d97a5"
]
]
},
{
"id": "af813dae06f48d2e",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "Power up clear speech busy",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": true,
"onceDelay": 0.1,
"topic": "",
"payload": "",
"payloadType": "date",
"x": 880,
"y": 140,
"wires": [
[
"dc4cc669d013eb9f"
]
]
},
{
"id": "dc4cc669d013eb9f",
"type": "function",
"z": "98fc211d841d9a2d",
"name": "Clear both busy flags",
"func": "global.set(\"create_speech_busy\",0);\nglobal.set(\"speech_busy\",0);",
"outputs": 0,
"timeout": "",
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 1120,
"y": 140,
"wires": []
},
{
"id": "a46305c0988fdf5e",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "Have a nice day!",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "amy",
"payload": "Have a nice day!",
"payloadType": "str",
"x": 1060,
"y": 380,
"wires": [
[
"c6b58278606d97a5"
]
]
},
{
"id": "0497724a81f21b0e",
"type": "function",
"z": "98fc211d841d9a2d",
"name": "weird volume relationship 0% to 100%",
"func": "var inv=msg.payload;\nif (inv<5) inv=0;\nelse if (inv<10) inv=50;\nelse if (inv<20) inv=67;\nelse if (inv<30) inv=77;\nelse if (inv<40) inv=84;\nelse if (inv<50) inv=89;\nelse if (inv<60) inv=93;\nelse if (inv<70) inv=96;\nelse if (inv<80) inv=98;\nelse if (inv<90) inv=99;\nelse inv=100;\n\nmsg.payload=\"amixer cset numid=1 -- \" + inv + \"%\";\nreturn msg;\n",
"outputs": 1,
"timeout": 0,
"noerr": 0,
"initialize": "",
"finalize": "",
"libs": [],
"x": 890,
"y": 100,
"wires": [
[
"d797dff4a2ca6b82"
]
]
},
{
"id": "d797dff4a2ca6b82",
"type": "exec",
"z": "98fc211d841d9a2d",
"command": "ssh host",
"addpay": "payload",
"append": "",
"useSpawn": "false",
"timer": "",
"winHide": false,
"name": "amixer volume on host",
"x": 1360,
"y": 100,
"wires": [
[],
[],
[]
]
},
{
"id": "8d0620f76ad8f155",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "enter authorization code",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "amy",
"payload": "enter authorization code",
"payloadType": "str",
"x": 800,
"y": 500,
"wires": [
[
"c6b58278606d97a5"
]
]
},
{
"id": "10e8662aeb1ad2b2",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "Are you having a nice day?",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "amy",
"payload": "Are you having a nice day?",
"payloadType": "str",
"x": 1090,
"y": 440,
"wires": [
[
"c6b58278606d97a5"
]
]
},
{
"id": "068a9af05fb550e2",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "working",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "amy",
"payload": "working",
"payloadType": "str",
"x": 750,
"y": 540,
"wires": [
[
"c6b58278606d97a5"
]
]
},
{
"id": "08210bd0c47a291d",
"type": "inject",
"z": "98fc211d841d9a2d",
"name": "Are you having a nice day, hmm?",
"props": [
{
"p": "payload"
},
{
"p": "topic",
"vt": "str"
}
],
"repeat": "",
"crontab": "",
"once": false,
"onceDelay": 0.1,
"topic": "amy",
"payload": "Are you having a nice day, hmm?",
"payloadType": "str",
"x": 1110,
"y": 500,
"wires": [
[
"c6b58278606d97a5"
]
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment