Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

72
Visualizações
Problems controlling the volume of the Web Audio API

I found a script on this forum at this link for looping an audio clip using the Web Audio API, and it works well, however I’m having problems working out how to control the volume of it. Apparently the key is to use “createGain”, but I’ve been having difficulties getting that to work. (I also tried the “Loopify” suggestion on that page, but Loopify doesn’t let you control the volume.)

See in the script where the comment says “START TRYING TO SET THE VOLUME”. I’m think I’m close with it, but it’s just not working. The sample is trying to reduce the volume to half.

// initialize the audio
var actx = new (AudioContext || webkitAudioContext)(), src="lo-fi-dub-beat_70bpm.wav", audioData, srcNode;  

// load the audio
fetch(src, {mode: "cors"}).then(function(resp) {return resp.arrayBuffer()}).then(decode);

// START TRYING TO SET THE VOLUME

var loopAudioGainNode = actx.createGain();
loopAudioGainNode.connect(actx.destination); // <- Perhaps this is needed?
loopAudioGainNode.gain.value = .5;  

// END TRYING TO SET THE VOLUME

// decode the audio and start it playing
function decode (buffer) { actx.decodeAudioData(buffer, playLoop); }

// set a new source node as needed after the audio was stopped
function playLoop (abuffer) {

    if (!audioData) audioData = abuffer;  // create a reference for control buttons

    srcNode = actx.createBufferSource();  // create audio source
    srcNode.buffer = abuffer;             // use decoded buffer
    srcNode.connect(actx.destination);    // create output
    srcNode.loop = true;                  // takes care of perfect looping
    srcNode.start();                      // play...
        
}

Thanks for your help.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda