fix sampleRate issues in flac and wav

This commit is contained in:
Radium Zheng 2018-08-01 07:56:54 +10:00
parent 61652c69b3
commit e03126e422
2 changed files with 64 additions and 4 deletions

View File

@ -139,6 +139,43 @@ export class WavAdapter extends RecordingAdapter {
return Promise.resolve();
}
/**
* Implements {@link RecordingAdapter#setMicDevice()}.
*
* @inheritdoc
*/
setMicDevice(micDeviceId) {
return this._replaceMic(micDeviceId);
}
/**
* Replaces the current microphone MediaStream.
*
* @param {*} micDeviceId - New microphone ID.
* @returns {Promise}
*/
_replaceMic(micDeviceId) {
if (this._audioContext && this._audioProcessingNode) {
return new Promise((resolve, reject) => {
this._getAudioStream(micDeviceId).then(newStream => {
const newSource = this._audioContext
.createMediaStreamSource(newStream);
this._audioSource.disconnect();
newSource.connect(this._audioProcessingNode);
this._stream = newStream;
this._audioSource = newSource;
resolve();
})
.catch(() => {
reject();
});
});
}
return Promise.resolve();
}
/**
* Creates a WAVE file header.
*
@ -209,7 +246,9 @@ export class WavAdapter extends RecordingAdapter {
this._getAudioStream(micDeviceId)
.then(stream => {
this._stream = stream;
this._audioContext = new AudioContext();
this._audioContext = new AudioContext({
sampleRate: WAV_SAMPLE_RATE
});
this._audioSource
= this._audioContext.createMediaStreamSource(stream);
this._audioProcessingNode
@ -255,7 +294,7 @@ export class WavAdapter extends RecordingAdapter {
*
* @private
* @param {*} buffers - The stored buffers.
* @param {*} length - Total length (in bytes).
* @param {*} length - Total length (number of samples).
* @returns {Blob}
*/
_exportMonoWAV(buffers, length) {
@ -265,7 +304,7 @@ export class WavAdapter extends RecordingAdapter {
// ...
// buffers[n] = Float32Array object (audio data)
const dataLength = length * 2; // why multiply by 2 here?
const dataLength = length * 2; // each sample = 16 bit = 2 bytes
const buffer = new ArrayBuffer(44 + dataLength);
const view = new DataView(buffer);

View File

@ -16,10 +16,29 @@ const logger = require('jitsi-meet-logger').getLogger(__filename);
*/
export class FlacAdapter extends RecordingAdapter {
/**
* Instance of flacEncodeWorker.
*/
_encoder = null;
/**
* The {@code AudioContext} instance.
*/
_audioContext = null;
/**
* The {@code ScriptProcessorNode} instance.
*/
_audioProcessingNode = null;
/**
* The {@code MediaStreamAudioSourceNode} instance.
*/
_audioSource = null;
/**
* The {@code MediaStream} instance, representing the current audio device.
*/
_stream = null;
/**
@ -206,7 +225,9 @@ export class FlacAdapter extends RecordingAdapter {
this._getAudioStream(micDeviceId)
.then(stream => {
this._stream = stream;
this._audioContext = new AudioContext();
this._audioContext = new AudioContext({
sampleRate: 44100
});
this._audioSource
= this._audioContext.createMediaStreamSource(stream);
this._audioProcessingNode