Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

137
Vistas
Check volume of audio in real time

I want to log the volume level of an audio file as its playing, to see if it hits a threshold. I tried using the .createAnalyzer function but can't figure it out. I have the function "repeat" to check every 0.1 seconds for a new volume, I just don't know how to actually get the volume.

const audioContext = new AudioContext();
const audiodiv = document.querySelector('.audiodiv')

const buffer = audioContext.createBuffer(
    1,
    audioContext.sampleRate * 1,
    audioContext.sampleRate
);

const channelData = buffer.getChannelData(0)

for (let i = 0; i < buffer.length; i++){
    channelData[i] = Math.random() * 2 - 1;
}
//OUTPUT VOLUME
const primaryGainControl = audioContext.createGain()
primaryGainControl.gain.setValueAtTime(0.01, 0);
primaryGainControl.connect(audioContext.destination);
//SAMPLE
const songbutton = document.createElement('button')
songbutton.innerText = 'song'
songbutton.addEventListener('click', async () => {
    const response = await fetch('testsong.wav')
    const soundBuffer = await response.arrayBuffer()
    const songBuffer = await audioContext.decodeAudioData(soundBuffer)
    
    const songSource = audioContext.createBufferSource()
    songSource.buffer = songBuffer


    const songSource2 = audioContext.createBufferSource()
    songSource2.buffer = songBuffer

    songSource.playbackRate.setValueAtTime(1, 0)
    songSource2.playbackRate.setValueAtTime(1, 0)

    const songGain = audioContext.createGain()
    songGain.gain.setValueAtTime(0.01, 0)
    

    const analyserNode = audioContext.createAnalyser();
    analyserNode.connect(primaryGainControl)
    
    repeat()
    function repeat(){
        setTimeout(() => {
        console.log(analyzerNode.VOLUME)

        repeat()
        }, 100);
    }


    console.log(analyserNode.frequencyBinCount)

    songSource.connect(primaryGainControl)
    songSource2.connect(analyserNode)
    songSource2.start()
    
    console.log(songSource2.amplitude)
    
})
audiodiv.appendChild(songbutton)
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda