2019-08-21 14:50:00 +00:00
|
|
|
import logger from '../logger';
|
2020-05-20 10:57:03 +00:00
|
|
|
|
2018-08-07 23:15:15 +00:00
|
|
|
import { AbstractAudioContextAdapter } from './AbstractAudioContextAdapter';
|
2018-06-19 16:43:33 +00:00
|
|
|
|
|
|
|
const WAV_BITS_PER_SAMPLE = 16;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recording adapter for raw WAVE format.
|
|
|
|
*/
|
2018-08-07 23:15:15 +00:00
|
|
|
export class WavAdapter extends AbstractAudioContextAdapter {
|
2018-06-19 16:43:33 +00:00
|
|
|
|
2018-07-10 11:20:36 +00:00
|
|
|
/**
|
2018-08-01 01:37:33 +00:00
|
|
|
* Length of the WAVE file, in number of samples.
|
2018-07-10 11:20:36 +00:00
|
|
|
*/
|
2018-06-19 16:43:33 +00:00
|
|
|
_wavLength = 0;
|
2018-07-10 11:20:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The {@code ArrayBuffer}s that stores the PCM bits.
|
|
|
|
*/
|
2018-06-19 16:43:33 +00:00
|
|
|
_wavBuffers = [];
|
2018-07-10 11:20:36 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether or not the {@code WavAdapter} is in a ready state.
|
|
|
|
*/
|
2018-06-19 16:43:33 +00:00
|
|
|
_isInitialized = false;
|
|
|
|
|
2018-07-10 11:20:36 +00:00
|
|
|
/**
|
|
|
|
* Initialization promise.
|
|
|
|
*/
|
|
|
|
_initPromise = null;
|
|
|
|
|
2018-06-19 16:43:33 +00:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
2018-08-07 23:15:15 +00:00
|
|
|
this._onAudioProcess = this._onAudioProcess.bind(this);
|
2018-06-19 16:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements {@link RecordingAdapter#start()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2018-07-31 09:53:22 +00:00
|
|
|
start(micDeviceId) {
|
2018-07-10 11:20:36 +00:00
|
|
|
if (!this._initPromise) {
|
2018-07-31 09:53:22 +00:00
|
|
|
this._initPromise = this._initialize(micDeviceId);
|
2018-07-10 11:20:36 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 14:52:11 +00:00
|
|
|
return this._initPromise.then(() => {
|
|
|
|
this._wavBuffers = [];
|
|
|
|
this._wavLength = 0;
|
2018-06-19 16:43:33 +00:00
|
|
|
|
2018-08-07 23:15:15 +00:00
|
|
|
this._connectAudioGraph();
|
2018-07-25 14:52:11 +00:00
|
|
|
});
|
2018-06-19 16:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements {@link RecordingAdapter#stop()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
stop() {
|
2018-08-07 23:15:15 +00:00
|
|
|
this._disconnectAudioGraph();
|
2018-06-19 16:43:33 +00:00
|
|
|
this._data = this._exportMonoWAV(this._wavBuffers, this._wavLength);
|
2018-07-10 11:20:36 +00:00
|
|
|
this._audioProcessingNode = null;
|
|
|
|
this._audioSource = null;
|
|
|
|
this._isInitialized = false;
|
2018-06-19 16:43:33 +00:00
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-08-03 16:06:11 +00:00
|
|
|
* Implements {@link RecordingAdapter#exportRecordedData()}.
|
2018-06-19 16:43:33 +00:00
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
2018-08-03 16:06:11 +00:00
|
|
|
exportRecordedData() {
|
2018-06-19 16:43:33 +00:00
|
|
|
if (this._data !== null) {
|
2018-08-03 16:06:11 +00:00
|
|
|
return Promise.resolve({
|
|
|
|
data: this._data,
|
|
|
|
format: 'wav'
|
|
|
|
});
|
2018-06-19 16:43:33 +00:00
|
|
|
}
|
2018-08-03 16:06:11 +00:00
|
|
|
|
|
|
|
return Promise.reject('No audio data recorded.');
|
2018-06-19 16:43:33 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 14:52:11 +00:00
|
|
|
/**
|
|
|
|
* Implements {@link RecordingAdapter#setMuted()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
setMuted(muted) {
|
|
|
|
const shouldEnable = !muted;
|
|
|
|
|
|
|
|
if (!this._stream) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
const track = this._stream.getAudioTracks()[0];
|
|
|
|
|
|
|
|
if (!track) {
|
|
|
|
logger.error('Cannot mute/unmute. Track not found!');
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (track.enabled !== shouldEnable) {
|
|
|
|
track.enabled = shouldEnable;
|
|
|
|
logger.log(muted ? 'Mute' : 'Unmute');
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2018-07-31 21:56:54 +00:00
|
|
|
/**
|
|
|
|
* Implements {@link RecordingAdapter#setMicDevice()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
setMicDevice(micDeviceId) {
|
|
|
|
return this._replaceMic(micDeviceId);
|
|
|
|
}
|
|
|
|
|
2018-06-19 16:43:33 +00:00
|
|
|
/**
|
|
|
|
* Creates a WAVE file header.
|
|
|
|
*
|
|
|
|
* @private
|
2018-08-01 01:37:33 +00:00
|
|
|
* @param {number} dataLength - Length of the payload (PCM data), in bytes.
|
2018-06-19 16:43:33 +00:00
|
|
|
* @returns {Uint8Array}
|
|
|
|
*/
|
2018-08-01 01:37:33 +00:00
|
|
|
_createWavHeader(dataLength) {
|
2018-06-19 16:43:33 +00:00
|
|
|
// adapted from
|
|
|
|
// https://github.com/mmig/speech-to-flac/blob/master/encoder.js
|
|
|
|
|
|
|
|
// ref: http://soundfile.sapp.org/doc/WaveFormat/
|
|
|
|
|
|
|
|
// create our WAVE file header
|
|
|
|
const buffer = new ArrayBuffer(44);
|
|
|
|
const view = new DataView(buffer);
|
|
|
|
|
|
|
|
// RIFF chunk descriptor
|
|
|
|
writeUTFBytes(view, 0, 'RIFF');
|
|
|
|
|
|
|
|
// set file size at the end
|
|
|
|
writeUTFBytes(view, 8, 'WAVE');
|
|
|
|
|
|
|
|
// FMT sub-chunk
|
|
|
|
writeUTFBytes(view, 12, 'fmt ');
|
|
|
|
view.setUint32(16, 16, true);
|
|
|
|
view.setUint16(20, 1, true);
|
|
|
|
|
|
|
|
// NumChannels
|
|
|
|
view.setUint16(22, 1, true);
|
|
|
|
|
|
|
|
// SampleRate
|
2018-08-07 23:15:15 +00:00
|
|
|
view.setUint32(24, this._sampleRate, true);
|
2018-06-19 16:43:33 +00:00
|
|
|
|
|
|
|
// ByteRate
|
|
|
|
view.setUint32(28,
|
2018-08-07 23:15:15 +00:00
|
|
|
Number(this._sampleRate) * 1 * WAV_BITS_PER_SAMPLE / 8, true);
|
2018-06-19 16:43:33 +00:00
|
|
|
|
|
|
|
// BlockAlign
|
|
|
|
view.setUint16(32, 1 * Number(WAV_BITS_PER_SAMPLE) / 8, true);
|
|
|
|
|
|
|
|
view.setUint16(34, WAV_BITS_PER_SAMPLE, true);
|
|
|
|
|
|
|
|
// data sub-chunk
|
|
|
|
writeUTFBytes(view, 36, 'data');
|
|
|
|
|
2018-08-01 01:37:33 +00:00
|
|
|
// file length
|
|
|
|
view.setUint32(4, 32 + dataLength, true);
|
2018-06-19 16:43:33 +00:00
|
|
|
|
2018-08-01 01:37:33 +00:00
|
|
|
// data chunk length
|
|
|
|
view.setUint32(40, dataLength, true);
|
2018-06-19 16:43:33 +00:00
|
|
|
|
|
|
|
return new Uint8Array(buffer);
|
|
|
|
}
|
|
|
|
|
2018-07-10 11:20:36 +00:00
|
|
|
/**
|
|
|
|
* Initialize the adapter.
|
|
|
|
*
|
|
|
|
* @private
|
2018-07-31 09:53:22 +00:00
|
|
|
* @param {string} micDeviceId - The current microphone device ID.
|
2018-07-10 11:20:36 +00:00
|
|
|
* @returns {Promise}
|
|
|
|
*/
|
2018-07-31 09:53:22 +00:00
|
|
|
_initialize(micDeviceId) {
|
2018-07-10 11:20:36 +00:00
|
|
|
if (this._isInitialized) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2018-08-07 23:15:15 +00:00
|
|
|
return this._initializeAudioContext(micDeviceId, this._onAudioProcess)
|
|
|
|
.then(() => {
|
2018-07-10 11:20:36 +00:00
|
|
|
this._isInitialized = true;
|
|
|
|
});
|
|
|
|
}
|
2018-06-19 16:43:33 +00:00
|
|
|
|
|
|
|
/**
|
2018-08-07 23:15:15 +00:00
|
|
|
* Callback function for handling AudioProcessingEvents.
|
2018-06-19 16:43:33 +00:00
|
|
|
*
|
|
|
|
* @private
|
2018-08-07 23:15:15 +00:00
|
|
|
* @param {AudioProcessingEvent} e - The event containing the raw PCM.
|
2018-06-19 16:43:33 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2018-08-07 23:15:15 +00:00
|
|
|
_onAudioProcess(e) {
|
|
|
|
// See: https://developer.mozilla.org/en-US/docs/Web/API/
|
|
|
|
// AudioBuffer/getChannelData
|
|
|
|
// The returned value is an Float32Array.
|
|
|
|
const channelLeft = e.inputBuffer.getChannelData(0);
|
|
|
|
|
2018-07-25 14:52:11 +00:00
|
|
|
// Need to copy the Float32Array:
|
|
|
|
// unlike passing to WebWorker, this data is passed by reference,
|
|
|
|
// so we need to copy it, otherwise the resulting audio file will be
|
|
|
|
// just repeating the last segment.
|
2018-08-07 23:15:15 +00:00
|
|
|
this._wavBuffers.push(new Float32Array(channelLeft));
|
|
|
|
this._wavLength += channelLeft.length;
|
2018-06-19 16:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Combines buffers and export to a wav file.
|
|
|
|
*
|
|
|
|
* @private
|
2018-08-01 01:37:33 +00:00
|
|
|
* @param {Float32Array[]} buffers - The stored buffers.
|
|
|
|
* @param {number} length - Total length (number of samples).
|
2018-06-19 16:43:33 +00:00
|
|
|
* @returns {Blob}
|
|
|
|
*/
|
|
|
|
_exportMonoWAV(buffers, length) {
|
2018-07-31 21:56:54 +00:00
|
|
|
const dataLength = length * 2; // each sample = 16 bit = 2 bytes
|
2018-06-19 16:43:33 +00:00
|
|
|
const buffer = new ArrayBuffer(44 + dataLength);
|
|
|
|
const view = new DataView(buffer);
|
|
|
|
|
|
|
|
// copy WAV header data into the array buffer
|
2018-08-01 01:37:33 +00:00
|
|
|
const header = this._createWavHeader(dataLength);
|
2018-06-19 16:43:33 +00:00
|
|
|
const len = header.length;
|
|
|
|
|
|
|
|
for (let i = 0; i < len; ++i) {
|
|
|
|
view.setUint8(i, header[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write audio data
|
|
|
|
floatTo16BitPCM(view, 44, buffers);
|
|
|
|
|
|
|
|
return new Blob([ view ], { type: 'audio/wav' });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function. Writes a UTF string to memory
|
|
|
|
* using big endianness. Required by WAVE headers.
|
|
|
|
*
|
|
|
|
* @param {ArrayBuffer} view - The view to memory.
|
2018-08-08 01:58:38 +00:00
|
|
|
* @param {number} offset - Offset.
|
|
|
|
* @param {string} string - The string to be written.
|
2018-06-19 16:43:33 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function writeUTFBytes(view, offset, string) {
|
|
|
|
const lng = string.length;
|
|
|
|
|
|
|
|
// convert to big endianness
|
|
|
|
for (let i = 0; i < lng; ++i) {
|
|
|
|
view.setUint8(offset + i, string.charCodeAt(i));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function for converting Float32Array to Int16Array.
|
|
|
|
*
|
2018-08-08 01:58:38 +00:00
|
|
|
* @param {DataView} output - View to the output buffer.
|
|
|
|
* @param {number} offset - The offset in output buffer to write from.
|
|
|
|
* @param {Float32Array[]} inputBuffers - The input buffers.
|
2018-06-19 16:43:33 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
function floatTo16BitPCM(output, offset, inputBuffers) {
|
|
|
|
|
2018-08-01 01:37:33 +00:00
|
|
|
let i, j;
|
|
|
|
let input, s, sampleCount;
|
|
|
|
const bufferCount = inputBuffers.length;
|
2018-06-19 16:43:33 +00:00
|
|
|
let o = offset;
|
|
|
|
|
2018-08-01 01:37:33 +00:00
|
|
|
for (i = 0; i < bufferCount; ++i) {
|
|
|
|
input = inputBuffers[i];
|
|
|
|
sampleCount = input.length;
|
|
|
|
for (j = 0; j < sampleCount; ++j, o += 2) {
|
|
|
|
s = Math.max(-1, Math.min(1, input[j]));
|
2018-06-19 16:43:33 +00:00
|
|
|
output.setInt16(o, s < 0 ? s * 0x8000 : s * 0x7FFF, true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|