1

I have a simple game that creates lots of tiny sound effects. It works fine for the first 1 or 2 minutes, but starts rapidly slowing down and lagging soon after. It seems like my computer runs out of RAM? Disabling just the sound effects or simply not triggering them doesn't lag the game at all anymore.

Here's an example of how I would create a sound effect in my game. Is there anything that should be done differently? Or maybe something that clears whatever sound cache is eating up the RAM?

function preload(){
  soundFormats("wav","mp3");
  sound1 = loadSound("sound1.wav");
}

function setup(){
  createCanvas(window.innerWidth, window.innerHeight, WEBGL);
}

var X;
var a = [];
a[0] = "something something 50 something";
a[1] = "something -150 something something";
function soundEffect(value){
  X = parseInt(a[value]);
  if (X<0) sound1.pan(-0.5);
  if (X>0) sound1.pan(0.5);
  if (X==0) sound1.pan(0);
  sound1.jump(0.06);
  sound1.setVolume(0.1);
  sound1.play();
}

function mousePressed(){
  soundEffect(0);
}
function mouseReleased(){
  soundEffect(1);
}

function draw(){
  //Game visuals go here
}
p2r3
  • 11
  • 2
  • could you show us more code? How is x being determined? – Luke Garrigan Feb 05 '19 at 09:05
  • @LukeGarrigan, I added some more info on how and why this code works. – p2r3 Feb 05 '19 at 14:37
  • 2
    Can you please post a [mcve] that demonstrates the problem? – Kevin Workman Feb 05 '19 at 15:05
  • @JohnColeman Yep, you're right. Fixed that typo, and added more code. Is this okay? – p2r3 Feb 05 '19 at 20:02
  • it looks like you're actually loading the wav every for itteration... So your `for` loop is using a new instance of a loaded wav file.. There be dragons.. BUT i don't know p5 that well, i could very well be wrong – Pogrindis Feb 05 '19 at 20:05
  • @Pogrindis And here I am, trying to write easy-to-understand code that functions as closely to my game as possibly possible, when random internet people come along and point out all the typos I can't notice myself. I assure you that was not in my actual game code. Thanks, the example has been updated. – p2r3 Feb 05 '19 at 20:13
  • Your `X` in the sample code is `NaN`, a [mcve] should have the property that someone can take your code and reproduce your problem. Your code does run (even though `X` makes little sense), but it doesn't reproduce your problem. – John Coleman Feb 05 '19 at 21:13
  • @JohnColeman The framerate starts dropping when the sound effects have been triggered enough times. That's the problem, and how to reproduce it. – p2r3 Feb 05 '19 at 21:20
  • Are you saying that the very code which you posted above has this problem? If so, I don't see it when I run it. – John Coleman Feb 05 '19 at 21:23
  • @JohnColeman Yes, running this exact code with no modifications starts to soak up resources after spamming the mouse button for under a minute. My system becomes so unresponsive that trying to close the tab or the browser itself takes forever. I'm on Ubuntu 18.04, Firefox, 4GB of RAM, Intel i3-6100. – p2r3 Feb 06 '19 at 12:18
  • Okay -- I can reproduce it now. I looked at my RAM usage and it went up by 10GB in about a minute. There seems to be a memory leak in which `.play()` creates an object which is not garbage-collected. If the documentation doesn't shed any light on what is happening, perhaps you could file a bug report. – John Coleman Feb 06 '19 at 12:36
  • There is a bug report concerning what seams to be the issue: https://github.com/processing/p5.js-sound/issues/88 (though a subsequent release claims to have fixed it) – John Coleman Feb 06 '19 at 14:12
  • @JohnColeman Well thanks for the help. I'll just try combining multiple sound effects into one for now, so that it doesn't start lagging right away. Nothing else I can do it seems. – p2r3 Feb 06 '19 at 18:10
  • Perhaps p5.sound is overkill for your use anyway. Sound is often used in HTML5 games. The p5.dom library might be useful here. See this question: https://stackoverflow.com/q/9419263/4996248 – John Coleman Feb 07 '19 at 02:23
  • Have you tried .stop()? Before you try the too .jump()? It's possible it's still playing, when your performing the .jump(). It's just a wild guess, but it can be easily checked. – Peter Bode Mar 22 '19 at 14:54

0 Answers0