Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

75
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!