client/browser/audio-worklet.js (3420B)
1 // One producer (the mixer worker) and one consumer (this audio render thread). 2 // Header words: read, write, paused, ready, flush-before, underruns. 3 class AOAudioProcessor extends AudioWorkletProcessor { 4 constructor(options) { 5 super(); 6 const {buffer, offset, capacity, channels, prefill} = options.processorOptions; 7 this.state = new Int32Array(buffer, offset, 6); 8 this.samples = new Float32Array(buffer, offset + 24, capacity * channels); 9 this.capacity = capacity; 10 this.channels = channels; 11 this.prefill = prefill; 12 this.flush = 0; 13 this.waiting = true; 14 this.closed = false; 15 this.closing = false; 16 this.gain = 0; 17 this.last = new Float32Array(channels); 18 this.start = new Float32Array(channels); 19 this.port.onmessage = () => { 20 this.closing = true; 21 this.closeIfSilent(); 22 }; 23 Atomics.store(this.state, 3, 1); 24 } 25 26 closeIfSilent() { 27 for (let channel = 0; channel < this.channels; ++channel) 28 if (this.last[channel]) return; 29 this.closed = true; 30 // No further shared-memory access after acknowledging close. 31 this.port.postMessage('closed'); 32 } 33 34 process(inputs, outputs) { 35 if (this.closed) return false; 36 const output = outputs[0]; 37 if (this.closing) { 38 for (let frame = 0; frame < output[0].length; ++frame) { 39 for (let channel = 0; channel < this.channels; ++channel) { 40 this.last[channel] *= 0.95; 41 if (Math.abs(this.last[channel]) < 1e-6) this.last[channel] = 0; 42 output[channel][frame] = this.last[channel]; 43 } 44 } 45 this.closeIfSilent(); 46 return !this.closed; 47 } 48 let read = Atomics.load(this.state, 0) >>> 0; 49 const paused = Atomics.load(this.state, 2); 50 const flush = Atomics.load(this.state, 4) >>> 0; 51 const write = Atomics.load(this.state, 1) >>> 0; 52 if (flush !== this.flush) { 53 read = flush; 54 this.flush = flush; 55 this.waiting = true; 56 } 57 let available = (write - read) >>> 0; 58 if (!paused && this.waiting && available >= this.prefill) { 59 this.waiting = false; 60 this.gain = 0; 61 this.start.set(this.last); 62 } 63 64 // Fade at buffer exhaustion and on pause; resume only after replenishing. 65 // The render callback allocates nothing and never waits for another thread. 66 for (let frame = 0; frame < output[0].length; ++frame) { 67 if (!paused && !this.waiting && available) { 68 this.gain = Math.min(1, this.gain + 1 / 128); 69 const index = (read & (this.capacity - 1)) * this.channels; 70 for (let channel = 0; channel < this.channels; ++channel) { 71 this.last[channel] = (this.samples[index + channel] * this.gain + 72 this.start[channel] * (1 - this.gain)) * Math.min(1, available / 128); 73 output[channel][frame] = this.last[channel]; 74 } 75 read = (read + 1) >>> 0; 76 --available; 77 } else { 78 for (let channel = 0; channel < this.channels; ++channel) { 79 this.last[channel] *= 0.95; 80 if (Math.abs(this.last[channel]) < 1e-6) this.last[channel] = 0; 81 output[channel][frame] = this.last[channel]; 82 } 83 this.gain = 0; 84 if (!paused && !this.waiting) { 85 Atomics.add(this.state, 5, 1); 86 this.waiting = true; 87 } 88 } 89 } 90 Atomics.store(this.state, 0, read); 91 return true; 92 } 93 } 94 95 registerProcessor('ao-audio', AOAudioProcessor);