forked from introlab/odas_web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recordings.js
144 lines (107 loc) · 3.15 KB
/
recordings.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
const fs = require('fs')
const path = require('path')
const url = require('url')
const net = require('net')
const AudioRecorder = require('./audio-recorder.js').AudioRecorder
/*
* Audio parameters
*/
const bitNumber = 16
const nChannels = 4
/*
* Parse arguments
*/
const portNumber = parseInt(process.argv[2]);
const suffix = process.argv[3];
/*
* Construct audio recorders
*/
const audioRecorders = [];
for(i=0; i<nChannels; i++) {
let recorder = new AudioRecorder(i, suffix);
// Relay messages from recorder to main process
recorder.on('fuzzy-transcript', (filename, data) => {
process.send({event:'fuzzy-transcript', filename:filename, data:data});
});
recorder.on('fuzzy-recording', filename => {
process.send({event:'fuzzy-recording', filename:filename});
});
recorder.on('add-recording', filename => {
process.send({event:'add-recording', filename:filename});
})
audioRecorders.push(recorder);
}
/*
* Construct audio socket server
*/
class AudioSocket {
constructor() {
this.port = portNumber;
this.connected = false;
this.server = net.createServer()
this.server.on('connection', (conn) => {
var remoteAddress = conn.remoteAddress + ':' + conn.remotePort
console.log('new client connection from %s', remoteAddress)
this.connected = true;
conn.on('data', (data) => {
let offset = 0
let jump = bitNumber/8
let delta = jump-1
try {
while(offset < data.length) {
audioRecorders.forEach((recorder, index) => {
recorder.receive(new Buffer.from(data.slice(offset + index*jump, offset + index*jump + delta +1)));
})
offset += (bitNumber / 8)*nChannels
}
} catch(e) {
console.log(e)
}
})
conn.once('close', () => {
console.log('connection from %s closed', remoteAddress)
this.connected = false;
audioRecorders.forEach((recorder) => {
recorder.stopRecording();
});
})
conn.on('error', (err) => {
console.log('Connection %s error: %s', remoteAddress, err.message)
this.connected = false;
audioRecorders.forEach((recorder) => {
recorder.stopRecording();
});
})
})
this.server.listen(this.port, () => {
console.log('server listening to %j', this.server.address())
})
}
}
const audioServer = new AudioSocket()
/*
* Control recorder according to commands received
*/
process.on('message', m => {
switch(m.event) {
case 'new-recording':
audioRecorders[m.index].startRecording(m.id);
break;
case 'end-recording':
audioRecorders[m.index].stopRecording();
break;
case 'start-recording':
audioRecorders.forEach(recorder => {
recorder.enableRecording(m.workspace);
});
break;
case 'stop-recording':
audioRecorders.forEach(recorder => {
recorder.disableRecording();
});
break;
default:
console.warn(`Unhandled main process message ${m}`);
break;
}
});