Browse Source

Use Uint8Array for input data 's16' format

Avoid converting input data to Float32Array
master
guest271314 3 years ago
parent
commit
b2fcb06f65
No account linked to committer's email address
1 changed files with 5 additions and 15 deletions
  1. 5
    15
      chromium_extension/AudioStream.js

+ 5
- 15
chromium_extension/AudioStream.js View File

@@ -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') {

Loading…
Cancel
Save