-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudioWorklet.js
More file actions
89 lines (88 loc) · 2.46 KB
/
audioWorklet.js
File metadata and controls
89 lines (88 loc) · 2.46 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// AudioWorkletGlobalScope
import { CODECS } from "./codecs.js";
console.log(globalThis);
class AudioWorkletStream extends AudioWorkletProcessor {
constructor(options) {
super(options);
Object.assign(this, options.processorOptions);
this.map = new Map();
this.port.onmessage = this.handleStream.bind(this);
}
async handleStream({
data: {
readable,
},
}) {
this.readable = readable;
// Another way to do this is define buffer and view on the class
// slice() the buffer, set in Map, and resize(0) in the for loop
let buffer = new ArrayBuffer(0, { maxByteLength: 512 });
let view = new DataView(buffer);
globalThis.console.log(currentTime, currentFrame);
const processStream = await this.readable.pipeTo(
new WritableStream({
write: async (value) => {
for (
let i = this.index === 0 ? 44 : 0;
i < value.length;
i++, this.index++
) {
if (buffer.byteLength === 512) {
this.map.set(
this.key++,
buffer,
);
buffer = new ArrayBuffer(0, { maxByteLength: 512 });
view = new DataView(buffer);
}
if (buffer.byteLength < 512) {
buffer.resize(buffer.byteLength + 1);
view.setUint8(buffer.byteLength - 1, value[i]);
}
}
},
close() {
console.log("Writable closed.");
},
}),
new ByteLengthQueuingStrategy({
highWaterMark: 512,
}),
).then(() => "Done reading and writing stream.").catch(console.error);
console.log(processStream, currentTime, currentFrame);
}
endOfStream() {
console.log(this.map);
this.port.postMessage({
ended: true,
currentTime,
currentFrame,
});
}
process(inputs, outputs) {
if (this.index < 512) {
return true;
}
if (this.bytes >= this.index) {
this.endOfStream();
return false;
}
const channels = outputs.flat();
const buffer = this.map.get(this.offset);
if (buffer === undefined) {
this.endOfStream();
return false;
}
this.bytes += buffer.byteLength;
const uint16 = new Uint16Array(buffer);
CODECS.get(this.codec)(uint16, channels);
buffer.resize(0);
this.map.delete(this.offset++);
return true;
}
}
try {
registerProcessor("audio-worklet-stream", AudioWorkletStream);
} catch (e) {
console.log(e);
}