summaryrefslogtreecommitdiff
path: root/scripts/volume_processor.js
blob: 9765e7b9c76143b9e23ecf88ce07cc9b98d162a5 (plain)
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
class VolumeProcessor extends AudioWorkletProcessor {
    constructor() {
        super();
        this.volume = 0;
    }

    process(inputs) {
        const input = inputs[0];
        if (input.length > 0) {
            const channelData = input[0];
            let sum = 0;

            for (let i = 0; i < channelData.length; i++) {
                sum += channelData[i] * channelData[i];
            }

            this.volume = Math.sqrt(sum / channelData.length);

            // Отправляем громкость в основной поток
            this.port.postMessage(this.volume);
        }

        return true;
    }
}

registerProcessor('volume_processor', VolumeProcessor);