-
Notifications
You must be signed in to change notification settings - Fork 11
/
voice2json_transcribestream.js
184 lines (148 loc) · 6.19 KB
/
voice2json_transcribestream.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Copyright 2020 Bart Butenaers & Johannes Kropf
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
module.exports = function(RED) {
var settings = RED.settings;
const { spawn } = require("child_process");
const fs = require("fs");
function Voice2JsonTranscribeStreamNode(config) {
RED.nodes.createNode(this, config);
this.inputField = config.inputField;
this.outputField = config.outputField;
this.profilePath = "";
this.statusTimer = false;
this.statusTimer2 = false;
this.processingNow = false;
this.msgObj = {};
this.outputBufferArr = [];
this.outputBuffer = false;
this.notNow = false;
var node = this;
function node_status(state1 = [], timeout = 0, state2 = []){
if (state1.length !== 0) {
node.status({fill:state1[1],shape:state1[2],text:state1[0]});
} else {
node.status({});
}
if (node.statusTimer !== false) {
clearTimeout(node.statusTimer);
node.statusTimer = false;
}
if (timeout !== 0) {
node.statusTimer = setTimeout(() => {
if (state2.length !== 0) {
node.status({fill:state2[1],shape:state2[2],text:state2[0]});
} else {
node.status({});
}
node.statusTimer = false;
},timeout);
}
}
function notNowWait(){
node.notNow = true;
setTimeout(() => {
node.notNow = false;
}, 2000);
}
function spawnTranscribe(){
let msg = {};
try{
node.transcribeStream = spawn("voice2json",["--profile",node.profilePath,"transcribe-stream","--audio-source","-","--exit-count","1"]);
}
catch (error) {
node_status(["error starting","red","ring"]);
node.error(error);
return;
}
node_status(["transcribing stream","blue","dot"]);
node.transcribeStream.stderr.on('data', (data)=>{
if (data.toString().match(/Ready/)) { return; }
node.error("stderr: " + data.toString());
node_status(["error","red","dot"]);
return;
});
node.transcribeStream.stdin.on('error', (error)=>{
node.warn("stdin error: " + error);
return;
})
node.transcribeStream.on('close', function (code,signal) {
node_status(["finished","green","dot"],1500);
delete node.transcribeStream;
return;
});
node.transcribeStream.stdout.on('data', (data)=>{
if(!node.notNow) { notNowWait(); }
try {
// Set the converted value in the specified message field (of the original input message)
RED.util.setMessageProperty(msg, node.outputField, JSON.parse(data.toString()), true);
} catch(err) {
node.error("Error setting value in msg." + node.outputField + " : " + err.message);
node_status(["error","red","dot"]);
return;
}
node.send(msg);
});
return;
}
function writeStdin(chunk){
try {
node.transcribeStream.stdin.write(chunk);
}
catch (error){
node.error("couldn't write to stdin: " + error);
if (node.transcribeStream) {
node_status(["error writing chunk","red","dot"],1500,["transcribing stream","blue","dot"]);
} else {
node_status(["error writing chunk","red","dot"],1500);
}
}
return;
}
// Retrieve the config node
node.voice2JsonConfig = RED.nodes.getNode(config.voice2JsonConfig);
if (node.voice2JsonConfig) {
// Use the profile path which has been specified in the config node
node.profilePath = node.voice2JsonConfig.profilePath;
//check path
if (!fs.existsSync(node.profilePath)){
node.error("Profile path doesn't exist. Please check the profile path");
node_status(["profile path error","red","dot"]);
return;
}
}
node.on("input", function(msg) {
node.inputMsg = RED.util.getMessageProperty(msg, node.inputField);
if(!node.notNow){
if(!node.transcribeStream){
if(Buffer.isBuffer(node.inputMsg)){
spawnTranscribe();
}
} else {
if(Buffer.isBuffer(node.inputMsg)){
writeStdin(node.inputMsg);
}
}
}
});
node.on("close",function() {
node_status();
if(node.transcribeStream) {
node.transcribeStream.kill();
}
});
}
RED.nodes.registerType("voice2json-transcribe-stream", Voice2JsonTranscribeStreamNode);
}