Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ui_audio.js 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. module.exports = function(RED) {
  2. var ui = require('../ui')(RED);
  3. function uiAudioNode(config) {
  4. RED.nodes.createNode(this,config);
  5. this.voice = config.voice;
  6. this.group = config.group;
  7. this.always = config.always || false;
  8. if (this.group && RED.nodes.getNode(this.group).hasOwnProperty("config")) {
  9. this.tabname = RED.nodes.getNode(RED.nodes.getNode(this.group).config.tab).name;
  10. }
  11. var node = this;
  12. node.status({});
  13. this.on('input', function(msg) {
  14. if (msg.hasOwnProperty("level") && (isNaN(msg.level) || msg.level > 300 || msg.level < 0)) {
  15. delete msg.level;
  16. }
  17. if (msg.reset == true) {
  18. ui.emit('ui-audio', { reset:true, tabname:node.tabname, always:node.always });
  19. }
  20. else if (Buffer.isBuffer(msg.payload)) {
  21. ui.emit('ui-audio', { audio:msg.payload, tabname:node.tabname, always:node.always, vol:msg.level });
  22. }
  23. else if (typeof msg.payload === "string") {
  24. ui.emit('ui-audio', { tts:msg.payload, voice:(node.voice || msg.voice || 0), tabname:node.tabname, always:node.always, vol:msg.level });
  25. }
  26. });
  27. var updateStatus = function(audioStatus) {
  28. if (audioStatus === "complete") {
  29. // When the audio or speech has played completely, clear the node status
  30. node.status({});
  31. }
  32. else if (audioStatus.indexOf("error") === 0) {
  33. node.status({shape:"ring",fill:"red",text:audioStatus});
  34. }
  35. else {
  36. node.status({shape:"dot",fill:"blue",text:audioStatus});
  37. }
  38. };
  39. ui.ev.on('audiostatus', updateStatus);
  40. this.on('close', function() {
  41. ui.ev.removeListener('audiostatus', updateStatus);
  42. })
  43. }
  44. RED.nodes.registerType("ui_audio", uiAudioNode);
  45. }