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

212
Views
How to cache a webaudio object properly?

I'm developing a game using javascript and other web technologies. In it, there's a game mode that is basically a tower defense, in which multiple objects may need to make use of the same audio file(.ogg) at the same time. Loading a file and creating a new webaudio for each one of those lags it too much, even if I attempt to stream it instead of a simple sync read, and if I create and save a webaudio in a variable to use multiple times, each time its playing and there is a new request to play said audio, the one that was playing will stop to allow for the new one to play(so, with enough of those, nothing plays at all).

With those issues, I decided to make copies of said webaudio object each time it was gonna be played, but its not only slow to do so, but also creates a minor memory leak(at least the way I did it).

How can I properly cache a webaudio for re-use? Consider that I'm pretty sure I'll need a new one each time because each audio has a position, and thus each of them will play differently, based on player position in relation to object that is playing the audio

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You tagged your question with web-audio-api, but from the body of this question, it seems you are using an HTMLMediaElement <audio> instead of the Web Audio API.

So I'll invite you to do the transition to that Web Audio API.

From there you'll be able to decode once your audio file, keep only once the decoded data as an AudioBuffer, and create many readers that will all hook to that one and only AudioBuffer, without eating any more memory.

const btn = document.querySelector("button")
const context = new AudioContext();
// a GainNode to control the output volume of our audio
const volumeNode = context.createGain();
volumeNode.gain.value = 0.5; // from 0 to 1
volumeNode.connect(context.destination);

fetch("https://dl.dropboxusercontent.com/s/agepbh2agnduknz/camera.mp3")
  // get the resource as an ArrayBuffer
  .then((resp) => resp.arrayBuffer())
  // decode the Audio data from this resource
  .then((buffer) => context.decodeAudioData(buffer))
  // now we have our AudioBuffer object, ready to be played
  .then((audioBuffer) => {
    btn.onclick = (evt) => {
      // allowing an AudioContext to make noise
      // must be required from an user-gesture
      if (context.status === "suspended") {
        context.resume();
      }
      // a very light player object
      const source = context.createBufferSource();
      // a simple pointer to the big AudioBuffer (no copy)
      source.buffer = audioBuffer;
      // connect to our volume node, itself connected to audio output
      source.connect(volumeNode);
      // start playing now
      source.start(0);
    };
    // now you can spam the button!
    btn.disabled = false;
  })
  .catch(console.error);
<button disabled>play</button>

about 4 years ago · Juan Pablo Isaza Report
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!