Seguí la documentación del W3C para implementar un "vumeter" (código ) utilizando la Web Audio API de JavaScript, específicamente las interfaces AudioWorkletProcessor y AudioWorkletNode . Me gustaría saber por qué el nivel RMS se compara con el volumen anterior multiplicado por un "factor de suavizado" (lo siento, recién comencé a aprender este tema, así que todo es nuevo para mí):
// Calculate the RMS level and update the volume. rms = Math.sqrt(sum / samples.length); this._volume = Math.max(rms, this._volume * SMOOTHING_FACTOR);Además, ¿cuál es el rango que puede tomar la variable de volumen? ¿Es posible saberlo para que podamos suponer que el volumen será un valor en un rango de 0...100?
Código del método de proceso:
process (inputs, outputs, parameters) { const input = inputs[0]; // Note that the input will be down-mixed to mono; however, if no inputs are // connected then zero channels will be passed in. if (input.length > 0) { const samples = input[0]; let sum = 0; let rms = 0; // Calculated the squared-sum. for (let i = 0; i < samples.length; ++i) sum += samples[i] * samples[i]; // Calculate the RMS level and update the volume. rms = Math.sqrt(sum / samples.length); this._volume = Math.max(rms, this._volume * SMOOTHING_FACTOR); // Update and sync the volume property with the main thread. this._nextUpdateFrame -= samples.length; if (this._nextUpdateFrame < 0) { this._nextUpdateFrame += this.intervalInFrames; this.port.postMessage({volume: this._volume}); } } // Keep on processing if the volume is above a threshold, so that // disconnecting inputs does not immediately cause the meter to stop // computing its smoothed value. return this._volume >= MINIMUM_VALUE; }