diff options
| author | ilotterytea <iltsu@alright.party> | 2024-06-16 02:25:23 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-06-16 02:25:23 +0500 |
| commit | 5bb3f74b909d4d1eed8635ff4b9b9d383bf0abb2 (patch) | |
| tree | 440fe47381e76ae4b6b410dbe609b7d06c8780b8 /scripts/volume_processor.js | |
| parent | 9a5cdeba44779ec0c6c297e917a8d4ae0ede70ab (diff) | |
feat: listen microphone and show dB level
Diffstat (limited to 'scripts/volume_processor.js')
| -rw-r--r-- | scripts/volume_processor.js | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/scripts/volume_processor.js b/scripts/volume_processor.js new file mode 100644 index 0000000..9765e7b --- /dev/null +++ b/scripts/volume_processor.js @@ -0,0 +1,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); |
