-
Notifications
You must be signed in to change notification settings - Fork 5
/
audioWorklet.js
57 lines (57 loc) · 1.73 KB
/
audioWorklet.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { CODECS } from './codecs.js';
class AudioDataWorkletStream extends AudioWorkletProcessor {
constructor(options) {
super(options);
Object.assign(this, options.processorOptions, {
uint8: new Uint8Array(70982024),
});
this.port.onmessage = this.appendBuffers.bind(this);
}
async appendBuffers({ data: { readable } }) {
globalThis.console.log(currentTime, currentFrame);
let index = 0;
const reader = readable.getReader();
const stream = async ({ value, done }) => {
if (done) {
await reader.closed;
return 'read/write done';
}
for (let i = !index ? 44 : 0; i < value.length; i++) {
this.uint8[index++] = value[i];
// accumulate 1 second of data to avoid glitches at beginning of playback
if (index === 44100) {
this.port.postMessage({ start: true });
}
}
return stream(await reader.read());
};
const processStream = await stream(await reader.read());
console.log(processStream, currentTime, currentFrame);
Object.assign(this, { readable });
}
endOfStream() {
this.port.postMessage({
ended: true,
currentTime,
currentFrame,
});
}
process(inputs, outputs) {
if (this.offset >= this.uint8.length) {
this.endOfStream();
return false;
}
const channels = outputs.flat();
const uint8 = new Uint8Array(512);
for (let i = 0; i < 512; i++, this.offset++) {
if (this.offset >= this.uint8.length) {
break;
}
uint8[i] = this.uint8[this.offset];
}
const uint16 = new Uint16Array(uint8.buffer);
CODECS.get(this.codec)(uint16, channels);
return true;
}
}
registerProcessor('audio-data-worklet-stream', AudioDataWorkletStream);