From b2fcb06f6573c81ab55dfef67e83abd113418b20 Mon Sep 17 00:00:00 2001 From: guest271314 Date: Sat, 21 Aug 2021 11:18:05 -0700 Subject: [PATCH] Use Uint8Array for input data 's16' format Avoid converting input data to Float32Array --- chromium_extension/AudioStream.js | 20 +++++--------------- 1 file changed, 5 insertions(+), 15 deletions(-) diff --git a/chromium_extension/AudioStream.js b/chromium_extension/AudioStream.js index a6c831d5..1683d74b 100644 --- a/chromium_extension/AudioStream.js +++ b/chromium_extension/AudioStream.js @@ -183,7 +183,7 @@ class AudioStream { close: async () => { console.log('Done writing input stream.'); if (channelData.length) { - this.inputController.enqueue(new Uint8Array(channelData)); + this.inputController.enqueue(new Uint8Array(channelData.splice(0, channelData.length))); } this.inputController.close(); this.source.postMessage('Done writing input stream.', '*'); @@ -210,24 +210,14 @@ class AudioStream { new Promise((resolve) => (this.stream.oninactive = resolve)), new Promise((resolve) => (this.ac.onstatechange = resolve)), ]); - } - const uint16 = new Uint16Array(value.buffer); - // https://stackoverflow.com/a/35248852 - const floats = new Float32Array(this.channelDataLength / 2); - for (let i = 0; i < uint16.length; i++) { - const int = uint16[i]; - // If the high bit is on, then it is a negative number, and actually counts backwards. - const float = - int >= 0x8000 ? -(0x10000 - int) / 0x8000 : int / 0x7fff; - floats[i] = float; - } + } const frame = new AudioData({ - format: 'FLTP', + format: 's16', sampleRate: 22050, numberOfChannels: 1, - numberOfFrames: 220, + numberOfFrames: value.length / 2, timestamp, - data: floats, + data: value, }); this.duration += (frame.duration / 10**6); if (this.recorder && this.recorder.state === 'inactive') {