-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnode-red-contrib-mic.js
170 lines (144 loc) · 3.92 KB
/
node-red-contrib-mic.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
module.exports = function(RED) {
var Mic = require('mic');
var micInstanceGlobal = null;
//define state recording and set it to node
const nodeStatusRecording = {fill : "red", shape : "ring", text : "recording"};
const nodeStatusPaused = {fill : "red", shape : "dot", text : "paused"};
const nodeStatusSilence = {fill : "green", shape : "dot", text : "silence.."};
function initMic(node) {
var micInstance = new Mic({
'rate' : node.config.rate,
'channels' : node.config.channels,
'device' : node.config.device,
'debug' : (node.config.debug == "true"),
'exitOnSilence' : node.config.silenceTimeout
});
var micAudioStream = micInstance.getAudioStream();
if (node.msg == undefined)
node.msg = {};
if (node.config.outputPayloadType == "buffer") {
micAudioStream.on('data', function(data) {
node.msg.payload = data;
node.msg.event = "data";
node.send(node.msg);
});
}
micAudioStream.on('error', function(err) {
node.log("Error in stream: " + err);
});
micAudioStream.on('startComplete', function() {
node.log("Stream event startComplete");
node.status(nodeStatusRecording);
if (node.config.outputPayloadType == "stream") {
node.msg.payload = micAudioStream;
node.send(node.msg);
}
});
micAudioStream.on('stopComplete', function() {
node.log("Stream event stopComplete");
node.status({});
if (node.config.outputPayloadType == "stream") {
micAudioStream.unpipe();
} else {
node.msg.payload = Buffer.alloc(0);
node.msg.event = "end";
node.send(node.msg);
}
});
micAudioStream.on('pauseComplete', function() {
node.log("Stream event pauseComplete");
node.status(nodeStatusPaused);
});
micAudioStream.on('resumeComplete', function() {
node.log("Stream event resumeComplete");
node.status(nodeStatusRecording);
});
micAudioStream.on('silence', function() {
node.log("Stream event silence");
node.status(nodeStatusSilence);
if (node.config.exitOnSilence == "true")
node.stopRecord();
});
return micInstance;
}
/*******/
// Mic //
/*******/
function Mic_Node(config) {
RED.nodes.createNode(this, config);
var node = this;
node.config = config;
node.on('input', function(msg) {
node.log("Event input: " + msg.payload);
node.msg = msg;
switch (msg.payload) {
case "pause":
node.pauseRecord();
break;
case "resume":
node.resumeRecord();
break;
case "stop":
node.stopRecord();
break;
case "start":
default:
node.startRecord();
break;
}
});
node.on('close', function() {
node.log("Event close");
node.stopRecord();
});
node.startRecord = function() {
if (micInstanceGlobal != null) {
micInstanceGlobal.getAudioStream().on('audioProcessExitComplete', function() {
node.log("Stream event processExitComplete");
micInstanceGlobal = initMic(node);
micInstanceGlobal.start();
});
micInstanceGlobal.stop();
} else {
micInstanceGlobal = initMic(node);
micInstanceGlobal.start();
}
}
node.stopRecord = function() {
if (micInstanceGlobal != null) {
micInstanceGlobal.stop();
micInstanceGlobal = null;
}
}
node.pauseRecord = function() {
if (micInstanceGlobal != null) {
micInstanceGlobal.pause();
}
}
node.resumeRecord = function() {
if (micInstanceGlobal != null) {
micInstanceGlobal.resume();
}
}
}
RED.nodes.registerType("Mic", Mic_Node);
RED.httpAdmin.post("/Mic/:id/:state", RED.auth.needsPermission("debug.write"), function(req, res) {
var node = RED.nodes.getNode(req.params.id);
var state = req.params.state;
if (node !== null && typeof node !== "undefined") {
if (state === "enable") {
this.active = true;
node.startRecord();
res.sendStatus(200);
} else if (state === "disable") {
node.stopRecord();
this.active = false;
res.sendStatus(201);
} else {
res.sendStatus(404);
}
} else {
res.sendStatus(404);
}
});
}