I am making a simple Javascript game in which, when an object collides with a wall, it plays a “dull” sound. This sound volume depends on the speed of the subject (higher speed => louder than sound).
Play function:
playSound = function(id, vol) //ID of the sound in the sounds array, volume/loudness of the sound { if (vol)
As I call it:
playSound(2, Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV); //self.TV stands for terminal velocity. This calculates the actual speed using the basic Pythagora theorem and then divides it by self.TV, which results in a number from 0 to self.TV. 2 is the id of the sound I want to play.
Everything works very well in Chrome. In Firefox, however, every time there is a collision with a wall (=> playSound ), the pause lasts for almost half a second! At first I thought the problems were in Math.sqrt , but I was wrong. Here's how I tested it:
//playSound(2, 1); //2 Is the id of the sound I want to play, and 1 is max loudness Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV; Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV; Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV;
This completely eliminated the collision delay and made me believe that Math.sqrt not causing any problems. To be sure, I did this:
playSound(2, 1); //2 Is the id of the sound I want to play, and 1 is max loudness //Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV; //Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV; //Math.sqrt(p.vx*p.vx + p.vy*p.vy)/self.TV;
And the backlog is back! Now I am sure that sound reproduction is causing problems. I'm right? Why is this happening? How to fix it?
performance javascript html5 html5-audio audio
corazza
source share