-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
257 lines (241 loc) · 7.98 KB
/
server.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/* Dependencies : socket.io */
var http = require('http');
var io = require('socket.io');
var fs = require('fs');
var url = require('url');
var net = require('net');
var exec = require('child_process').exec;
var spawn = require('child_process').spawn;
var process = require('child_process');
//spawn it once, and then force it to continue spawning
//when it closes
function runKaldiScript(){
console.log("spawning example2");
var example2 = spawn('bash', ['../scripts/example2.sh']);
example2.stdout.on('data', function(chunk){
var returnedText = chunk.toString();
console.log(returnedText);
});
example2.stdout.on('end', function(){
console.log('ending example2');
runKaldiScript();
});
}
runKaldiScript();
var server = http.createServer(function(req,rep){
console.log('connection');
var path = url.parse(req.url).pathname;
console.log(path);
switch(path) {
// Capture audio from mic and send it back to server
case '/':
fs.readFile(__dirname+"/index.html", function(error,data){
if (error) {
console.log('error');
}
rep.writeHead(200, {'Content-Type':'text/html'});
rep.write(data, 'utf8');
rep.end();
});
break;
// Working version where audio is captured, downsampled, and sent to server via socket
// Kaldi decodes and server sends result back to the html
case '/offline.html':
fs.readFile(__dirname+"/offline.html", function(error,data){
if (error) {
console.log('error');
}
rep.writeHead(200, {'Content-Type':'text/html'});
rep.write(data, 'utf8');
rep.end();
});
break;
// Javascript files used with new_index.html
case '/recorder.js':
fs.readFile(__dirname+"/recorder.js", function(error,data){
if (error) {
console.log('error');
}
rep.writeHead(200, {'Content-Type':'text/javascript'});
rep.write(data, 'utf8');
rep.end();
});
break;
case '/resampler.js':
fs.readFile(__dirname+"/resampler.js", function(error,data){
if (error) {
console.log('error');
}
rep.writeHead(200, {'Content-Type':'text/javascript'});
rep.write(data, 'utf8');
rep.end();
});
break;
case '/recorderWorker.js':
fs.readFile(__dirname+"/recorderWorker.js", function(error,data){
if (error) {
console.log('error');
}
rep.writeHead(200, {'Content-Type':'text/javascript'});
rep.write(data, 'utf8');
rep.end();
});
break;
// END OF ACTIVE CODE
// START DEMO ROUTES
// sample invocations of command line process
// http://stackoverflow.com/questions/20643470/execute-a-command-line-binary-in-node-js
// execute_all: fetches complete output
// execute_online: receives output as streams
case '/execute_all':
exec('bash ../example.sh',
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
if (error !== null) {
console.log('exec error: ' + error);
}
});
rep.writeHead(200, {'Content-Type':'text/html'});
rep.write("<html><body>Executing command ... fetch complete output</body></html>");
rep.end();
break;
case '/execute_online':
// command and its list of args
var child = spawn('bash',['../example.sh'] );
child.stdout.on('data', function(chunk) {
var returnedText = chunk.toString();
// need to parse buffer data bytes into ascii
console.log(returnedText);
console.log(chunk.length());
});
rep.writeHead(200, {'Content-Type':'text/html'});
rep.write("<html><body>Executing command ... fetch in chunks</body></html>");
rep.end();
break;
// example of writing to a file using fs
case '/write':
fs.writeFile("./test.txt", "Hey there!", function(err) {
if(err) {
console.log(err);
} else {
console.log("The file was saved!");
}
});
rep.writeHead(200, {'Content-Type':'text/html'});
rep.write("<html><body>Writng</body></html>");
rep.end();
break;
// This file was part of the starter code but abandoned in favor of
// new_index.html
case '/audio.html':
fs.readFile(__dirname+"/audio.html", function(error,data){
if (error) {
console.log('error');
}
rep.writeHead(200, {'Content-Type':'text/html'});
rep.write(data, 'utf8');
rep.end();
});
break;
// An visual example of using user media with video
case '/video.html':
fs.readFile(__dirname+"/video.html", function(error,data){
if (error) {
console.log('error');
}
rep.writeHead(200, {'Content-Type':'text/html'});
rep.write(data, 'utf8');
rep.end();
});
break;
// END DEMO ROUTES
default:
console.log("default path");
rep.writeHead(404);
rep.write("opps this doesn't exist - 404");
break;
}
});
server.listen(8080);
io = io.listen(server);
// Turn debug logging off
io.set('log level', 1);
io.sockets.on('connection',function (socket) {
// used in index.html
console.log('connection begun');
var child1 = spawn('sox',['-r', '44100', '-t', 'raw','-e','unsigned-integer','-b', '16','-','-r', '16k','-t', 'raw','-e', 'unsigned-integer','-b','16','-']);
// Error handling for process termination
child1.on('close', function (code) {
if (code !== 0) {
console.log('child1 process exited with code ' + code);
}
});
child1.stderr.on('data', function (data) {
console.log(' stderr: ' + data);
});
// Here comes the pipeline from index.html
// Get the sound as floats between -1 and 1, scale to 16 bits unsigned i.e. 0 ... 65535
// build a buffer of bytes, i.e. high end byte, low end byte
// turn it into a string using code cribbed from the web and pass it to sox
socket.on('data', function (data) {
// console.log(data);
console.log("passing to sox with rate " + data.rate);
// take the sound data
var sendthis = data.audio;
var packedform = [];
for (x in sendthis) {
var byte = 32767 * sendthis[x] + 32768;
var hi = (byte & 0xff00) >> 8;
var lo = byte & 0xff;
packedform.push(hi);
packedform.push(lo);
}
child1.stdin.write(String.fromCharCode.apply(null, packedform));
});
child1.stdout.on('data', function (data){
console.log("sox output data, writing to kaldi");
var client = new net.Socket();
client.connect(3000,function(){
client.write(data);
console.log('connected to 3000 :)');
});
client.on('data', function(data2){
console.log('DATA: ' + data2);
console.log('TEXT: ' + data2.toString());
// fs.readFile(__dirname+"/index.html", function(error,data3){
// if (error) {
// console.log('error');
// }
// rep.writeHead(200, {'Content-Type':'text/html'});
// rep.write(data3, 'utf8');
// rep.write(data2.toString());
// rep.end();
// });
}).on('error', function(err){
if (err.code == 'ECONNREFUSED') {
console.log('refused at 3000');
client.connect(10, function(){
console.log('CONNECTED TO 3000: next take');
});
}else {
console.log('ERR: ' + err);
}
}).on('close', function(){
console.log('connection to 3000 closed');
});
});
//used in offline.html
socket.on('wav', function (data) {
fs.writeFile('speech.wav', data.str, 'binary');
// run the kaldi decode script
// using the king solomon models
var child = spawn('bash', ['../scripts/offline_king_sol.sh']);
child.stdout.on('data', function(chunk) {
// need to parse buffer data bytes into ascii
var returnedText = chunk.toString();
console.log(returnedText);
// send back to html file to display for user
socket.emit("decode", {'result': returnedText});
});
});
});