Browse Source

fix sampleRate issues in flac and wav

j8
Radium Zheng 7 years ago
parent
commit
e03126e422

+ 42
- 3
react/features/local-recording/recording/WavAdapter.js View File

139
         return Promise.resolve();
139
         return Promise.resolve();
140
     }
140
     }
141
 
141
 
142
+    /**
143
+     * Implements {@link RecordingAdapter#setMicDevice()}.
144
+     *
145
+     * @inheritdoc
146
+     */
147
+    setMicDevice(micDeviceId) {
148
+        return this._replaceMic(micDeviceId);
149
+    }
150
+
151
+    /**
152
+     * Replaces the current microphone MediaStream.
153
+     *
154
+     * @param {*} micDeviceId - New microphone ID.
155
+     * @returns {Promise}
156
+     */
157
+    _replaceMic(micDeviceId) {
158
+        if (this._audioContext && this._audioProcessingNode) {
159
+            return new Promise((resolve, reject) => {
160
+                this._getAudioStream(micDeviceId).then(newStream => {
161
+                    const newSource = this._audioContext
162
+                        .createMediaStreamSource(newStream);
163
+
164
+                    this._audioSource.disconnect();
165
+                    newSource.connect(this._audioProcessingNode);
166
+                    this._stream = newStream;
167
+                    this._audioSource = newSource;
168
+                    resolve();
169
+                })
170
+                .catch(() => {
171
+                    reject();
172
+                });
173
+            });
174
+        }
175
+
176
+        return Promise.resolve();
177
+    }
178
+
142
     /**
179
     /**
143
      * Creates a WAVE file header.
180
      * Creates a WAVE file header.
144
      *
181
      *
209
             this._getAudioStream(micDeviceId)
246
             this._getAudioStream(micDeviceId)
210
             .then(stream => {
247
             .then(stream => {
211
                 this._stream = stream;
248
                 this._stream = stream;
212
-                this._audioContext = new AudioContext();
249
+                this._audioContext = new AudioContext({
250
+                    sampleRate: WAV_SAMPLE_RATE
251
+                });
213
                 this._audioSource
252
                 this._audioSource
214
                     = this._audioContext.createMediaStreamSource(stream);
253
                     = this._audioContext.createMediaStreamSource(stream);
215
                 this._audioProcessingNode
254
                 this._audioProcessingNode
255
      *
294
      *
256
      * @private
295
      * @private
257
      * @param {*} buffers - The stored buffers.
296
      * @param {*} buffers - The stored buffers.
258
-     * @param {*} length - Total length (in bytes).
297
+     * @param {*} length - Total length (number of samples).
259
      * @returns {Blob}
298
      * @returns {Blob}
260
      */
299
      */
261
     _exportMonoWAV(buffers, length) {
300
     _exportMonoWAV(buffers, length) {
265
         //  ...
304
         //  ...
266
         //  buffers[n] = Float32Array object (audio data)
305
         //  buffers[n] = Float32Array object (audio data)
267
 
306
 
268
-        const dataLength = length * 2; // why multiply by 2 here?
307
+        const dataLength = length * 2; // each sample = 16 bit = 2 bytes
269
         const buffer = new ArrayBuffer(44 + dataLength);
308
         const buffer = new ArrayBuffer(44 + dataLength);
270
         const view = new DataView(buffer);
309
         const view = new DataView(buffer);
271
 
310
 

+ 22
- 1
react/features/local-recording/recording/flac/FlacAdapter.js View File

16
  */
16
  */
17
 export class FlacAdapter extends RecordingAdapter {
17
 export class FlacAdapter extends RecordingAdapter {
18
 
18
 
19
+    /**
20
+     * Instance of flacEncodeWorker.
21
+     */
19
     _encoder = null;
22
     _encoder = null;
23
+
24
+    /**
25
+     * The {@code AudioContext} instance.
26
+     */
20
     _audioContext = null;
27
     _audioContext = null;
28
+
29
+    /**
30
+     * The {@code ScriptProcessorNode} instance.
31
+     */
21
     _audioProcessingNode = null;
32
     _audioProcessingNode = null;
33
+
34
+    /**
35
+     * The {@code MediaStreamAudioSourceNode} instance.
36
+     */
22
     _audioSource = null;
37
     _audioSource = null;
38
+
39
+    /**
40
+     * The {@code MediaStream} instance, representing the current audio device.
41
+     */
23
     _stream = null;
42
     _stream = null;
24
 
43
 
25
     /**
44
     /**
206
             this._getAudioStream(micDeviceId)
225
             this._getAudioStream(micDeviceId)
207
             .then(stream => {
226
             .then(stream => {
208
                 this._stream = stream;
227
                 this._stream = stream;
209
-                this._audioContext = new AudioContext();
228
+                this._audioContext = new AudioContext({
229
+                    sampleRate: 44100
230
+                });
210
                 this._audioSource
231
                 this._audioSource
211
                     = this._audioContext.createMediaStreamSource(stream);
232
                     = this._audioContext.createMediaStreamSource(stream);
212
                 this._audioProcessingNode
233
                 this._audioProcessingNode

Loading…
Cancel
Save