forked from introlab/odas_web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaudio-recorder.js
187 lines (132 loc) · 4.89 KB
/
audio-recorder.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
const streamToText = require('./stream-to-text.js');
const path = require('path');
const wav = require('wav')
const EventEmitter = require('events');
const appSettings = require('./settings.js').appSettings;
/*
* Audio parameters
*/
const bitNumber = 16
/*
End of parameters
*/
// Audio Recorder
exports.AudioRecorder = class AudioRecorder extends EventEmitter {
constructor(index, suffix) {
super();
this.active = false;
this.hold = false;
this.index = index;
this.recordingEnabled = false;
this.workspacePath = '';
this.suffix = suffix;
this.buffer = undefined;
this.writer = undefined;
this.path = undefined;
this.transcripter = new streamToText.Transcripter();
this.transcripter.on('data', data => {
this.emit('fuzzy-transcript', this.path, data);
console.log(data);
});
this.transcripter.on('error', err => {
console.error(err);
});
}
receive(data) {
if(this.active) {
if(this.hold) {
if(typeof(this.buffer) !== 'undefined') {
this.buffer = Buffer.concat([this.buffer, data]);
}
else {
this.buffer = data;
}
}
else {
try {
if(typeof(this.buffer) !== 'undefined') {
data = Buffer.concat([this.buffer, data]);
this.buffer = undefined;
console.log(`Wrote samples buffered in writer ${this.index}`);
}
if( !this.writer.write(data)) {
console.warn(`Write stream ${this.index} is full\nHolding samples...`);
this.hold = true;
}
}
catch(err) {
console.error(`Couldn't write to recorder ${this.index}`);
console.warn(err);
this.stopRecording();
}
}
this.transcripter.putData(data);
}
}
startRecording(id) {
if(this.recordingEnabled) {
if(typeof(this.writer) !== 'undefined') { // Verify that previous recording is cleared
setTimeout(() => {
this.startRecording(id);
console.log(`Recorder ${id} was defined. Retrying...`);
}, 100);
return;
}
console.log(`Recorder ${this.index} started`)
console.log(`Recorder ${this.index} was ${this.active} active`)
let now = new Date()
let dateString = ""
dateString += now.getFullYear() + "-"
dateString += (now.getMonth()+1) + "-"
dateString += now.getDate()
let timeString = ""
timeString += now.getHours() + "-"
timeString += now.getMinutes() + "-"
timeString += now.getSeconds() + "-"
timeString += now.getMilliseconds()
let datetimeString = dateString + "_" + timeString
let filename = path.join(this.workspacePath, `ODAS_${id}_${datetimeString}_${this.suffix}.wav`)
this.path = filename
try {
this.writer = new wav.FileWriter(filename,{channels:1, sampleRate:appSettings.sampleRate, bitDepth:bitNumber});
this.emit('fuzzy-recording', filename);
this.writer.on('drain', () => { // Release hold when write stream is cleared
console.log(`Writer ${this.index} is empty.\nResuming...`);
this.hold = false;
});
this.active = true;
this.hold = false;
this.buffer = undefined;
this.transcripter.start();
}
catch(err) {
console.error(`Failed to start recorder ${this.index}`);
console.log(err);
this.writer = undefined;
}
}
}
stopRecording() {
if(this.active) {
this.active = false;
console.log(`Recorder ${this.index} ended`);
this.transcripter.stop();
console.log(`Registering header on recorder ${this.index}`);
this.writer.end();
this.writer.on('header',(header) => {
console.log(`Registered header on recorder ${this.index}`);
this.emit('add-recording', this.writer.path);
this.writer = undefined;
console.log(`Recorder ${this.index} undefined`);
});
}
}
enableRecording(workspacePath) {
this.recordingEnabled = true;
this.workspacePath = workspacePath;
}
disableRecording() {
this.recordingEnabled = false;
this.stopRecording();
}
}