-
Notifications
You must be signed in to change notification settings - Fork 12
/
Microphone.js
374 lines (325 loc) · 12.1 KB
/
Microphone.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/**
* Copyright 2015 IBM Corp. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the 'License');
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* global OfflineAudioContext */
'use strict';
var utils = require('./utils');
/**
* Captures microphone input from the browser.
* Works at least on latest versions of Firefox and Chrome
*/
function Microphone(_options) {
var options = _options || {};
// we record in mono because the speech recognition service
// does not support stereo.
this.bufferSize = options.bufferSize || 8192;
this.inputChannels = options.inputChannels || 1;
this.outputChannels = options.outputChannels || 1;
this.recording = false;
this.requestedAccess = false;
this.sampleRate = 16000;
// auxiliar buffer to keep unused samples (used when doing downsampling)
this.bufferUnusedSamples = new Float32Array(0);
this.samplesAll = new Float32Array(20000000);
this.samplesAllOffset = 0;
// Chrome or Firefox or IE User media
if (!navigator.getUserMedia) {
navigator.getUserMedia = navigator.webkitGetUserMedia ||
navigator.mozGetUserMedia || navigator.msGetUserMedia;
}
}
/**
* Called when the user reject the use of the michrophone
* @param error The error
*/
Microphone.prototype.onPermissionRejected = function() {
console.log('Microphone.onPermissionRejected()');
this.requestedAccess = false;
this.onError('Permission to access the microphone rejeted.');
};
Microphone.prototype.onError = function(error) {
console.log('Microphone.onError():', error);
};
/**
* Called when the user authorizes the use of the microphone.
* @param {Object} stream The Stream to connect to
*
*/
Microphone.prototype.onMediaStream = function(stream) {
var AudioCtx = window.AudioContext || window.webkitAudioContext;
if (!AudioCtx)
throw new Error('AudioContext not available');
if (!this.audioContext)
this.audioContext = new AudioCtx();
var gain = this.audioContext.createGain();
var audioInput = this.audioContext.createMediaStreamSource(stream);
audioInput.connect(gain);
if (!this.mic) {
this.mic = this.audioContext.createScriptProcessor(this.bufferSize,
this.inputChannels, this.outputChannels);
}
// uncomment the following line if you want to use your microphone sample rate
// this.sampleRate = this.audioContext.sampleRate;
console.log('Microphone.onMediaStream(): sampling rate is:', this.sampleRate);
this.mic.onaudioprocess = this._onaudioprocess.bind(this);
this.stream = stream;
gain.connect(this.mic);
this.mic.connect(this.audioContext.destination);
this.recording = true;
this.requestedAccess = false;
this.onStartRecording();
};
/**
* callback that is being used by the microphone
* to send audio chunks.
* @param {object} data audio
*/
Microphone.prototype._onaudioprocess = function(data) {
if (!this.recording) {
// We speak but we are not recording
return;
}
// Single channel
var chan = data.inputBuffer.getChannelData(0);
// resampler(this.audioContext.sampleRate,data.inputBuffer,this.onAudio);
this.saveData(new Float32Array(chan));
this.onAudio(this._exportDataBufferTo16Khz(new Float32Array(chan)));
// export with microphone mhz, remember to update the this.sampleRate
// with the sample rate from your microphone
// this.onAudio(this._exportDataBuffer(new Float32Array(chan)));
};
/**
* Start the audio recording
*/
Microphone.prototype.record = function() {
if (!navigator.getUserMedia){
this.onError('Browser doesn\'t support microphone input');
return;
}
if (this.requestedAccess) {
return;
}
this.requestedAccess = true;
navigator.getUserMedia({audio: true},
this.onMediaStream.bind(this), // Microphone permission granted
this.onPermissionRejected.bind(this)); // Microphone permission rejected
};
/**
* Stop the audio recording
*/
Microphone.prototype.stop = function() {
if (!this.recording)
return;
if (JSON.parse(localStorage.getItem('playback')))
this.playWav(); /* plays back the audio that was recorded*/
this.recording = false;
this.stream.getTracks()[0].stop();
this.requestedAccess = false;
this.mic.disconnect(0);
this.onStopRecording();
};
/**
* Creates a Blob type: 'audio/l16' with the chunk and downsampling to 16 kHz
* coming from the microphone.
* Explanation for the math: The raw values captured from the Web Audio API are
* in 32-bit Floating Point, between -1 and 1 (per the specification).
* The values for 16-bit PCM range between -32768 and +32767 (16-bit signed integer).
* Multiply to control the volume of the output. We store in little endian.
* @param {Object} buffer Microphone audio chunk
* @return {Blob} 'audio/l16' chunk
* @deprecated This method is depracated
*/
Microphone.prototype._exportDataBufferTo16Khz = function(bufferNewSamples) {
var buffer = null,
newSamples = bufferNewSamples.length,
unusedSamples = this.bufferUnusedSamples.length;
if (unusedSamples > 0) {
buffer = new Float32Array(unusedSamples + newSamples);
for (var i = 0; i < unusedSamples; ++i) {
buffer[i] = this.bufferUnusedSamples[i];
}
for (i = 0; i < newSamples; ++i) {
buffer[unusedSamples + i] = bufferNewSamples[i];
}
} else {
buffer = bufferNewSamples;
}
// downsampling variables
var filter = [
-0.037935, -0.00089024, 0.040173, 0.019989, 0.0047792, -0.058675, -0.056487,
-0.0040653, 0.14527, 0.26927, 0.33913, 0.26927, 0.14527, -0.0040653, -0.056487,
-0.058675, 0.0047792, 0.019989, 0.040173, -0.00089024, -0.037935
],
samplingRateRatio = this.audioContext.sampleRate / 16000,
nOutputSamples = Math.floor((buffer.length - filter.length) / (samplingRateRatio)) + 1,
pcmEncodedBuffer16k = new ArrayBuffer(nOutputSamples * 2),
dataView16k = new DataView(pcmEncodedBuffer16k),
index = 0,
volume = 0x7FFF, // range from 0 to 0x7FFF to control the volume
nOut = 0;
// eslint-disable-next-line no-redeclare
for (var i = 0; i + filter.length - 1 < buffer.length; i = Math.round(samplingRateRatio * nOut)) {
var sample = 0;
for (var j = 0; j < filter.length; ++j) {
sample += buffer[i + j] * filter[j];
}
sample *= volume;
dataView16k.setInt16(index, sample, true); // 'true' -> means little endian
index += 2;
nOut++;
}
var indexSampleAfterLastUsed = Math.round(samplingRateRatio * nOut);
var remaining = buffer.length - indexSampleAfterLastUsed;
if (remaining > 0) {
this.bufferUnusedSamples = new Float32Array(remaining);
for (i = 0; i < remaining; ++i) {
this.bufferUnusedSamples[i] = buffer[indexSampleAfterLastUsed + i];
}
} else {
this.bufferUnusedSamples = new Float32Array(0);
}
return new Blob([dataView16k], {
type: 'audio/l16'
});
};
// // native way of resampling captured audio
// var resampler = function(sampleRate, audioBuffer, callbackProcessAudio) {
//
// console.log('length: ' + audioBuffer.length + ' ' + sampleRate);
// var channels = 1;
// var targetSampleRate = 16000;
// var numSamplesTarget = audioBuffer.length * targetSampleRate / sampleRate;
//
// var offlineContext = new OfflineAudioContext(channels, numSamplesTarget, targetSampleRate);
// var bufferSource = offlineContext.createBufferSource();
// bufferSource.buffer = audioBuffer;
//
// // callback that is called when the resampling finishes
// offlineContext.oncomplete = function(event) {
// var samplesTarget = event.renderedBuffer.getChannelData(0);
// console.log('Done resampling: ' + samplesTarget.length + ' samples produced');
//
// // convert from [-1,1] range of floating point numbers to [-32767,32767] range of integers
// var index = 0;
// var volume = 0x7FFF;
// var pcmEncodedBuffer = new ArrayBuffer(samplesTarget.length * 2); // short integer to byte
// var dataView = new DataView(pcmEncodedBuffer);
// for (var i = 0; i < samplesTarget.length; i++) {
// dataView.setInt16(index, samplesTarget[i] * volume, true);
// index += 2;
// }
//
// // l16 is the MIME type for 16-bit PCM
// callbackProcessAudio(new Blob([dataView], {type: 'audio/l16'}));
// };
//
// bufferSource.connect(offlineContext.destination);
// bufferSource.start(0);
// offlineContext.startRendering();
// };
/**
* Creates a Blob type: 'audio/l16' with the
* chunk coming from the microphone.
*/
// var exportDataBuffer = function(buffer, bufferSize) {
// var pcmEncodedBuffer = null,
// dataView = null,
// index = 0,
// volume = 0x7FFF; // range from 0 to 0x7FFF to control the volume
//
// pcmEncodedBuffer = new ArrayBuffer(bufferSize * 2);
// dataView = new DataView(pcmEncodedBuffer);
//
// /* Explanation for the math: The raw values captured from the Web Audio API are
// * in 32-bit Floating Point, between -1 and 1 (per the specification).
// * The values for 16-bit PCM range between -32768 and +32767 (16-bit signed integer).
// * Multiply to control the volume of the output. We store in little endian.
// */
// for (var i = 0; i < buffer.length; i++) {
// dataView.setInt16(index, buffer[i] * volume, true);
// index += 2;
// }
//
// // l16 is the MIME type for 16-bit PCM
// return new Blob([dataView], {type: 'audio/l16'});
// };
Microphone.prototype._exportDataBuffer = function(buffer){
utils.exportDataBuffer(buffer, this.bufferSize);
};
// Functions used to control Microphone events listeners.
Microphone.prototype.onStartRecording = function() {};
Microphone.prototype.onStopRecording = function() {};
Microphone.prototype.onAudio = function() {};
module.exports = Microphone;
Microphone.prototype.saveData = function(samples) {
for (var i = 0; i < samples.length; ++i) {
this.samplesAll[this.samplesAllOffset + i] = samples[i];
}
this.samplesAllOffset += samples.length;
console.log('samples: ' + this.samplesAllOffset);
};
Microphone.prototype.playWav = function() {
var samples = this.samplesAll.subarray(0, this.samplesAllOffset);
var dataview = this.encodeWav(samples, 1, this.audioContext.sampleRate);
var audioBlob = new Blob([dataview], {type: 'audio/l16'});
var url = window.URL.createObjectURL(audioBlob);
var audio = new Audio();
audio.src = url;
audio.play();
};
Microphone.prototype.encodeWav = function(samples, numChannels, sampleRate) {
console.log('#samples: ' + samples.length);
var buffer = new ArrayBuffer(44 + samples.length * 2);
var view = new DataView(buffer);
/* RIFF identifier */
this.writeString(view, 0, 'RIFF');
/* RIFF chunk length */
view.setUint32(4, 36 + samples.length * 2, true);
/* RIFF type */
this.writeString(view, 8, 'WAVE');
/* format chunk identifier */
this.writeString(view, 12, 'fmt ');
/* format chunk length */
view.setUint32(16, 16, true);
/* sample format (raw) */
view.setUint16(20, 1, true);
/* channel count */
view.setUint16(22, numChannels, 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, numChannels * 2, true);
/* bits per sample */
view.setUint16(34, 16, true);
/* data chunk identifier */
this.writeString(view, 36, 'data');
/* data chunk length */
view.setUint32(40, samples.length * 2, true);
this.floatTo16BitPCM(view, 44, samples);
return view;
};
Microphone.prototype.writeString = function(view, offset, string){
for (var i = 0; i < string.length; i++){
view.setUint8(offset + i, string.charCodeAt(i));
}
};
Microphone.prototype.floatTo16BitPCM = function(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);
}
};