-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathaudio-worklet.js
More file actions
175 lines (146 loc) · 4.24 KB
/
audio-worklet.js
File metadata and controls
175 lines (146 loc) · 4.24 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
class BufferQueue {
constructor() {
this._length = 0;
this._buffers = [];
this._hasPushed = false;
}
push(buffer) {
this._buffers.push(buffer);
this._length += buffer.length;
this._hasPushed = true;
}
clear() {
this._length = 0;
this._buffers = [];
this._hasPushed = false;
}
get size() {
return this._buffers.length;
}
read() {
if (!this._hasPushed) {
return null;
}
if (this._buffers.length === 0) {
this._hasPushed = false;
return null;
}
// Worklet process requires 128 samples of 32-bit float https://developer.mozilla.org/en-US/docs/Web/API/AudioWorkletProcessor/process
const needed = 128;
const output = new Float32Array(needed);
if (this._length > 0 && this._length < needed) {
let offset = 0;
while (this._buffers.length && offset < this._length) {
const buf = this._buffers[0];
const take = Math.min(buf.length, this._length - offset);
output.set(buf.subarray(0, take), offset);
if (take === buf.length) {
this._buffers.shift();
} else {
this._buffers[0] = buf.subarray(take);
}
offset += take;
}
this._length = 0;
this._hasPushed = false;
return output;
}
let offset = 0;
while (offset < needed) {
const buf = this._buffers[0];
const take = Math.min(buf.length, needed - offset);
output.set(buf.subarray(0, take), offset);
if (take === buf.length) {
this._buffers.shift();
} else {
this._buffers[0] = buf.subarray(take);
}
this._length -= take;
offset += take;
}
return output;
}
}
class AudioStreamProcessor extends AudioWorkletProcessor {
constructor() {
super();
this._bq = new BufferQueue();
this.port.onmessage = (e) => {
switch (e.data?.type) {
case 'audio':
this._bq.push(new Float32Array(e.data.data));
if (this._fadeOutActive) {
this._fadeOutActive = false;
this._fadeOutCounter = 0;
}
this.port.postMessage({ type: 'queueLength', length: this._bq.size });
break;
case 'end':
this._shouldStop = true;
break;
case 'fadeAndClear':
this._fadeOutActive = true;
this._fadeOutCounter = 0;
break;
case 'clear':
this._bq.clear();
this._shouldStop = false;
break;
}
};
this._shouldStop = false;
this._fadeOutDurationMs = 30;
// sampleRate is part of AudioWorkletGlobalScope
// eslint-disable-next-line no-undef
this._sampleRate = sampleRate;
this._fadeOutSamplesCount = Math.floor(
(this._fadeOutDurationMs * this._sampleRate) / 1000,
);
this._fadeOutActive = false;
this._fadeOutCounter = 0;
}
process(inputs, outputs) {
const output = outputs[0];
const frames = output[0].length;
const channels = output.length;
const block = this._bq.read();
this.port.postMessage({ type: 'queueLength', length: this._bq.size });
if (block) {
for (let ch = 0; ch < channels; ch++) {
const out = output[ch];
for (let i = 0; i < frames; i++) {
let sample = block[i * channels + ch] ?? 0;
// Apply fade out if active
if (this._fadeOutActive) {
const fadeProgress =
this._fadeOutCounter / this._fadeOutSamplesCount;
const gain = 1 - Math.min(fadeProgress, 1);
sample *= gain;
}
out[i] = sample;
}
}
// If we're currently fading out,
// increment the counter and end if complete
if (this._fadeOutActive) {
this._fadeOutCounter += frames;
if (this._fadeOutCounter >= this._fadeOutSamplesCount) {
this._fadeOutActive = false;
this._fadeOutCounter = 0;
this._bq.clear();
this.port.postMessage({ type: 'ended' });
}
}
return true;
}
if (this._shouldStop) {
this.port.postMessage({ type: 'ended' });
return false;
}
for (let ch = 0; ch < channels; ch++) {
output[ch].fill(0);
}
return true;
}
}
registerProcessor('audio-processor', AudioStreamProcessor);