forked from faradayio/Recorderjs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrecorder.js
More file actions
222 lines (195 loc) · 7.48 KB
/
recorder.js
File metadata and controls
222 lines (195 loc) · 7.48 KB
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
(function (root, factory) {
if (typeof exports === 'object') {
// CommonJS
module.exports = factory();
} else {
// Global Variables
root.Recorder = factory();
}
})(this, function () {
var Recorder = function (source, cfg) {
var config = cfg || {};
var bufferLen = config.bufferLen || 4096;
this.context = source.context;
if (config.channelType == 'mono') {
this.node = (this.context.createScriptProcessor || this.context.createJavaScriptNode).call(this.context, bufferLen, 1, 1);
} else {
this.node = (this.context.createScriptProcessor || this.context.createJavaScriptNode).call(this.context, bufferLen, 2, 2);
}
var recLength = 0,
recBuffersL = [],
recBuffersR = [],
sampleRate;
//init sampleRate
sampleRate = this.context.sampleRate;
var recording = false;
var self = this;
function mergeBuffers(recBuffers, recLength) {
var result = new Float32Array(recLength);
var offset = 0;
for (var i = 0; i < recBuffers.length; i++) {
result.set(recBuffers[i], offset);
offset += recBuffers[i].length;
}
return result;
}
function interleave(inputL, inputR) {
var length = inputL.length + inputR.length;
var result = new Float32Array(length);
var index = 0,
inputIndex = 0;
while (index < length) {
result[index++] = inputL[inputIndex];
result[index++] = inputR[inputIndex];
inputIndex++;
}
return result;
}
function floatTo16BitPCM(output, offset, input) {
for (var i = 0; i < input.length; i++, offset += 2) {
var s = Math.max(-1, Math.min(1, input[i]));
output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
}
}
function writeString(view, offset, string) {
for (var i = 0; i < string.length; i++) {
view.setUint8(offset + i, string.charCodeAt(i));
}
}
function encodeWAV(samples) {
var buffer = new ArrayBuffer(44 + samples.length * 2);
var view = new DataView(buffer);
/* RIFF identifier */
writeString(view, 0, 'RIFF');
/* RIFF chunk length */
view.setUint32(4, 36 + samples.length * 2, true);
/* RIFF type */
writeString(view, 8, 'WAVE');
/* format chunk identifier */
writeString(view, 12, 'fmt ');
/* format chunk length */
view.setUint32(16, 16, true);
/* sample format (raw) */
view.setUint16(20, 1, true);
if (config.channelType === 'mono') {
/* channel count */
view.setUint16(22, 1, true);
/* sample rate */
view.setUint32(24, sampleRate, true);
/* byte rate (sample rate * block align) */
view.setUint32(28, sampleRate * 2, true);
/* block align (channel count * bytes per sample) */
view.setUint16(32, 2, true);
} else {
/* channel count */
view.setUint16(22, 2, true);
/* sample rate */
view.setUint32(24, sampleRate, true);
/* byte rate (sample rate * block align) */
view.setUint32(28, sampleRate * 4, true);
/* block align (channel count * bytes per sample) */
view.setUint16(32, 4, true);
}
/* bits per sample */
view.setUint16(34, 16, true);
/* data chunk identifier */
writeString(view, 36, 'data');
/* data chunk length */
view.setUint32(40, samples.length * 2, true);
floatTo16BitPCM(view, 44, samples);
return view;
}
function rec(left, right) {
recBuffersL.push(left);
recBuffersR.push(right);
recLength += left.length;
}
function recMono(left) {
recBuffersL.push(left);
recLength += left.length;
}
function onAudioProcessMono(e) {
if (!recording) return;
self.ondata && self.ondata(e.inputBuffer.getChannelData(0));
var left = e.inputBuffer.getChannelData(0);
recMono(new Float32Array(left));
}
function onAudioProcessStereo(e) {
if (!recording) return;
self.ondata && self.ondata(e.inputBuffer.getChannelData(0));
var left, right;
left = e.inputBuffer.getChannelData(0);
right = e.inputBuffer.getChannelData(1);
rec(new Float32Array(left), new Float32Array(right));
}
if (config.channelType === 'mono') {
this.node.onaudioprocess = onAudioProcessMono;
} else {
this.node.onaudioprocess = onAudioProcessStereo;
}
this.configure = function (cfg) {
for (var prop in cfg) {
if (cfg.hasOwnProperty(prop)) {
config[prop] = cfg[prop];
}
}
};
this.record = function () {
recording = true;
};
this.stop = function () {
recording = false;
};
this.clear = function () {
recLength = 0;
recBuffersL = [];
recBuffersR = [];
};
this.getBuffer = function (cb) {
var buffers = [];
buffers.push(mergeBuffers(recBuffersL, recLength));
if (config.channelType !== 'mono') {
buffers.push(mergeBuffers(recBuffersR, recLength));
}
if (typeof cb === 'function') {
cb(buffers);
} else {
throw new Error('There is no callback function to export buffers.');
}
};
this.exportWAV = function (cb, type) {
//currCallback = cb || config.callback;
type = type || config.type || 'audio/wav';
var bufferL = mergeBuffers(recBuffersL, recLength);
var bufferR = null;
var interleaved = null;
if (config.channelType !== 'mono') {
bufferR = mergeBuffers(recBuffersR, recLength);
interleaved = interleave(bufferL, bufferR);
}
var dataview = encodeWAV(interleaved || bufferL);
var audioBlob = new Blob([dataview], {type: type});
if (typeof cb === 'function') {
cb(audioBlob);
} else {
throw new Error('There is no callback function to export file.');
}
};
this.shutdown = function () {
source.disconnect();
this.node.disconnect();
};
source.connect(this.node);
this.node.connect(this.context.destination); //this should not be necessary
};
Recorder.forceDownload = function (blob, filename) {
var url = (window.URL || window.webkitURL).createObjectURL(blob);
var link = window.document.createElement('a');
link.href = url;
link.download = filename || 'output.wav';
var click = document.createEvent("Event");
click.initEvent("click", true, true);
link.dispatchEvent(click);
};
return Recorder;
});