| 87 | } |
| 88 | |
| 89 | process(inputs: Array<Array<Float32Array<ArrayBuffer>>>): boolean { |
| 90 | if (this.#isStopped) { |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | const [input] = inputs; |
| 95 | if (input == null) { |
| 96 | throw new Error(`Invalid input count: ${inputs.length}`); |
| 97 | } |
| 98 | |
| 99 | const [channel] = input; |
| 100 | if (channel == null) { |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | for (const sample of channel) { |
| 105 | this.#peakSquares += sample ** 2; |
| 106 | this.#peakSamples += 1; |
| 107 | if (this.#peakSamples < PEAK_EVERY) { |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | const peak = Math.min( |
| 112 | 1, |
| 113 | Math.max(0, Math.sqrt(this.#peakSquares / this.#peakSamples)) |
| 114 | ); |
| 115 | this.#window[this.#windowOffset] = peak; |
| 116 | this.#windowOffset = (this.#windowOffset + 1) % WINDOW_SIZE; |
| 117 | |
| 118 | let max = 1e-23; |
| 119 | for (const oldPeak of this.#window) { |
| 120 | max = Math.max(max, oldPeak); |
| 121 | } |
| 122 | |
| 123 | this.#previousMax *= MAX_DECAY; |
| 124 | if (this.#previousMax < max) { |
| 125 | this.#previousMax = max; |
| 126 | } else { |
| 127 | max = this.#previousMax; |
| 128 | } |
| 129 | |
| 130 | this.#peakSquares = 0; |
| 131 | this.#peakSamples = 0; |
| 132 | |
| 133 | this.port.postMessage({ |
| 134 | type: 'peak', |
| 135 | peak: peak / max, |
| 136 | } satisfies WorkletMessageType); |
| 137 | } |
| 138 | |
| 139 | const shared = this.#encoder.encode(channel); |
| 140 | if (shared.length === 0) { |
| 141 | return true; |
| 142 | } |
| 143 | |
| 144 | const copy = new Uint8Array(shared); |
| 145 | this.port.postMessage( |
| 146 | { |