-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecorder.js
198 lines (174 loc) · 4.74 KB
/
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
188
189
190
191
192
193
194
195
196
197
198
var AudioRecorder = (function(){
var arrayTypes = {
8: Int8Array,
16: Int16Array,
32: Float32Array,
64: Float64Array
};
function Int8toInt16(source){
var i, len = source.length,
dest = new Int16Array(len);
for(i = 0; i < len; i++){ dest[i] = source[i] << 8; }
return dest;
}
function Int16toInt8(source){
var i, len = source.length,
dest = new Int8Array(len);
for(i = 0; i < len; i++){ dest[i] = source[i] >> 8; }
return dest;
}
function IntToFloat(dtype,max){
return function(source){
var i, len = source.length,
dest = new dtype(len);
for(i = 0; i < len; i++){ dest[i] = source[i] / max; }
return dest;
};
}
function FloatToInt(dtype,max){
return function(source){
var i, len = source.length,
dest = new dtype(len);
for(i = 0; i < len; i++){ dest[i] = Math.round(source[i] * max); }
return dest;
};
}
function calcDepthConversion(from,to){
if(from === to){ return function(a){ return a; }; }
switch(from){
case 8:
if(to === 16){ return Int8toInt16; }
return IntToFloat(to === 32?Float32Array:Float64Array, 128);
case 16:
if(to === 8){ return Int16toInt8; }
return IntToFloat(to === 32?Float32Array:Float64Array, 32768);
case 32:
if(to === 8){ return FloatToInt(Int8Array, 128); }
if(to === 16){ return FloatToInt(Int16Array, 32768); }
return function(source){ return new Float64Array(source); };
case 64:
if(to === 8){ return FloatToInt(Int8Array, 128); }
if(to === 16){ return FloatToInt(Int16Array, 32768); }
return function(source){ return new Float32Array(source); };
}
}
function Recorder(sp,ep){
var that = this;
this.events = {};
this.recording = false;
this.source = null;
this.encoder = null;
this.resampler = null;
this.queued = 1;
this.finished = 0;
function enc_suc(data){
that.finished++;
that.emit('data',data);
}
function enc_err(err){
that.emit('error',err);
}
return Promise.all([sp, ep]).then(function(arr){
var dconv, resampler,
source = arr[0],
encoder = arr[1];
if(source.channels !== encoder.channels){
throw new Error("Channel Mismatch.");
}
if(!arrayTypes.hasOwnProperty(source.bitrate)){
throw new Error("Invalid Source Bitrate.");
}
if(!arrayTypes.hasOwnProperty(encoder.bitrate)){
throw new Error("Invalid Encoder Bitrate.");
}
dconv = calcDepthConversion(source.bitrate,encoder.bitrate);
//resample at the lower bitrate to save memory
if(source.bitrate < encoder.bitrate){
resampler = new Resampler({
channels: source.channels,
bitrate: encoder.bitrate,
from: source.samplerate,
to: encoder.samplerate,
bufferSize: encoder.bufferSize
});
resampler.on('data',function(inputs){
that.queued++;
encoder.encode(inputs)
.then(enc_suc,enc_err);
});
source.pipe(function(inputs){
if(!that.recording){ return; }
//convert to encoder bitrate before resampling
resampler.append(inputs.map(dconv));
});
}else{
resampler = new Resampler({
channels: source.channels,
bitrate: source.bitrate,
from: source.samplerate,
to: encoder.samplerate,
bufferSize: encoder.bufferSize
});
resampler.on('data',function(inputs){
that.queued++;
//convert to encoder bitrate after resampling
encoder.encode(inputs.map(dconv))
.then(enc_suc,enc_err);
});
source.pipe(function(inputs){
if(!that.recording){ return; }
resampler.append(inputs);
});
}
that.source = source;
that.encoder = encoder;
that.resampler = resampler;
return that;
});
}
Recorder.prototype.on = function(ename, handler){
if(!this.events.hasOwnProperty(ename)){
this.events[ename] = [handler];
}else{
this.events[ename].push(handler);
}
};
Recorder.prototype.off = function(ename, handler){
var i, evlist = this.events[ename];
if(!evlist){ return; }
i = evlist.indexOf(handler);
if(~i){ evlist.splice(i,1); }
};
Recorder.prototype.emit = function(ename, obj){
var evlist = this.events[ename];
if(!evlist){ return; }
evlist.forEach(function(h){ h.call(this, obj); }, this);
};
Recorder.prototype.record = function(){
this.recording = true;
};
Recorder.prototype.pause = function(){
this.recording = false;
};
Recorder.prototype.reset = function(hard){
this.recording = false;
this.queued = 1;
this.finished = 0;
this.resampler.reset(true);
this.encoder.reset(hard);
};
Recorder.prototype.finish = function(){
//TODO: Make sure resmapler is flushed
var that = this,
endp = this.encoder.end();
endp.then(function(arr){
var frame = arr[0],
blob = arr[1];
that.finished++;
that.emit('data', arr[0]);
that.emit('end', arr[1]);
});
return endp.then(function(arr){ return arr[1]; });
};
return Recorder;
}());