Jump to content

Audio Static after Many Plays of a Sound Effect [FIXED]


Recommended Posts

I have been working on a tile-based rogue-like exploration game that is supposed to be very relaxing and peaceful. A lot of it is procedurally generated, and I wanted to procedurally generate the music, too.


So I loaded two octaves of notes in the pentatonic scale:

this.game.load.audio('notec', ['../wp-content/themes/toolbox/js/assets/music/notec.ogg','../wp-content/themes/toolbox/js/assets/music/notec.mp3']);this.game.load.audio('noted', ['../wp-content/themes/toolbox/js/assets/music/noted.ogg','../wp-content/themes/toolbox/js/assets/music/noted.mp3']);this.game.load.audio('notee', ['../wp-content/themes/toolbox/js/assets/music/notee.ogg','../wp-content/themes/toolbox/js/assets/music/notee.mp3']);this.game.load.audio('noteg', ['../wp-content/themes/toolbox/js/assets/music/noteg.ogg','../wp-content/themes/toolbox/js/assets/music/noteg.mp3']);this.game.load.audio('notea', ['../wp-content/themes/toolbox/js/assets/music/notea.ogg','../wp-content/themes/toolbox/js/assets/music/notea.mp3']);this.game.load.audio('notec2', ['../wp-content/themes/toolbox/js/assets/music/notec2.ogg','../wp-content/themes/toolbox/js/assets/music/notec2.mp3']);this.game.load.audio('noted2', ['../wp-content/themes/toolbox/js/assets/music/noted2.ogg','../wp-content/themes/toolbox/js/assets/music/noted2.mp3']);this.game.load.audio('notee2', ['../wp-content/themes/toolbox/js/assets/music/notee2.ogg','../wp-content/themes/toolbox/js/assets/music/notee2.mp3']);this.game.load.audio('noteg2', ['../wp-content/themes/toolbox/js/assets/music/noteg2.ogg','../wp-content/themes/toolbox/js/assets/music/noteg2.mp3']);this.game.load.audio('notea2', ['../wp-content/themes/toolbox/js/assets/music/notea2.ogg','../wp-content/themes/toolbox/js/assets/music/notea2.mp3']);

And then I called a function every time the person took a step:

    sound_track: function() { // random music generation    	if ((stepcounter % 2) == 1) {	 	   	ckey = game.add.audio('notec');   		 	dkey = game.add.audio('noted');   		 	ekey = game.add.audio('notee');   		 	gkey = game.add.audio('noteg');   		 	akey = game.add.audio('notea');   		 	c2key = game.add.audio('notec');   	 		d2key = game.add.audio('noted');    		e2key = game.add.audio('notee');    		g2key = game.add.audio('noteg');    		a2key = game.add.audio('notea');    		ckey.volume = .3;   		 	dkey.volume = .3;   		 	ekey.volume = .3;   		 	gkey.volume = .3;   		 	akey.volume = .3;   		 	c2key.volume = .3;   	 		d2key.volume = .3;    		e2key.volume = .3;    		g2key.volume = .3;    		a2key.volume = .3;    		console.log("this part's working");    		var randtime = Math.floor(Math.random()*8)+1;    		var randmusic = Math.floor(Math.random()*12)+1;  	 	 	if ((randtime == 1) || ((stepcounter % 4) == 1)) { // guaranteed beat  	 	 		console.log(randmusic);    			if (randmusic == 1 || randmusic == 11) ckey.play();    			if (randmusic == 2) dkey.play();    			if (randmusic == 3) ekey.play();    			if (randmusic == 4) gkey.play();    			if (randmusic == 5) akey.play();    			if (randmusic == 6) c2key.play();    			if (randmusic == 7) d2key.play();    			if (randmusic == 8) e2key.play();    			if (randmusic == 9) g2key.play();    			if (randmusic == 10) a2key.play();    		}    	}	        },

Which seemed to work fine! (Albeit a bit random: I need to write a few more rules, perhaps a bassline, to give the song more structure. But it's already very zen!)


Anyway, this works great for the first hundred or so steps, but then the audio starts to get all staticky. Eventually, it stopped playing at all, and I had to reload the window. I thought maybe I could fix this by making the files shorter and more compressed, and I did that, and it seemed to help a bit, but the audio still got pretty corrupted-sounded and eventually stopped all-together.


This affects all audio played by the code, even sound effects which have not been played more than a few times, and does not go away if I wait.

I am not a programmer, and this is just a side project for me. So it's very possible that I am doing something silly, or stupid, or preposterously round-about. If so, please correct me! I have really enjoyed the experience of coding in Phaser, and it's been a lot of fun, but this kind of bug (which throws no errors in the console!) totally perplexes me, as it falls outside the realm of internal code-logic and into the realm of technical know-how, of which I have none.

(If you would like to test the game out, you can find it a http://www.koanoftheday.com/trails, and the password is trails. It's not completely finished, though, so there might be other bugs!)

Also, a PS: I have lurked in these forums and used people's advice to solve a number of issues already. This seems like a great place, and you all seem like great people, and I'm excited to finally register and start contributing (even if my first contribution is a request for help!). Thank you all.


EDIT: Well, I think I've figured it out! The problem was that I was doing the "ckey = game.add.audio('notec');" stuff (I'm not actually sure what this is called, only how it functions!) every single time the sound effects were called. This was overloading something and making the sound sound horrible.

Hopefully somebody else will do the same stupid thing and this post will serve as helpful information to them!

Edited by gregkarber
Link to comment
Share on other sites

I should add that I experience this problem in Firefox, Chrome, and Safari, and that the distortion generally starts around the 500th step (which is displayed in the console) and gets intolerable by about step 560, which means that it starts at about the (500/4+500/2/6)th play of a sound effect, or about the 167th time. This seems much too low for a sort of fundamental wall.


Thank you very much for any help!

Link to comment
Share on other sites

I just re-tested it in Google Chrome, and now it's happening around the 950 step mark (or after about 300 sound-effect cues). I think the increase might have happened since I converted the sound files to lower-fidelity versions. And sometimes now it doesn't get distorted, it just decides to stop playing all-together. Once this happens, no sound effects work.


I see some people include end-point parameters in their sound cues. Perhaps doing that will help? I honestly have no experience in audio programming and so do not even know where to begin. Is overlapping causing this kind of corruption?


Thank you very much for testing it out!

EDIT: Just saw your suggestion! Will do.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...