Sound Effects Deteriorate Issue

Hello,

My game has an issue where the quality of the sound effects start out good, then deteriorate as the level goes on. Here is a very short video demonstrating the issue: https://www.youtube.com/watch?v=zdnmOriUJ0Y

I am loading and playing the sounds as instructed in the phaser docs.

In preload:
this.load.audio('whoosh1', "assets/sound_effects/whoosh1.mp3")
this.load.audio('whoosh2', "assets/sound_effects/whoosh2.mp3")
this.load.audio('whoosh3', "assets/sound_effects/whoosh3.mp3")

In the player class constructor:
this.whoosh1 = scene.sound.add('whoosh1')
this.whoosh2 = scene.sound.add('whoosh2')
this.whoosh3 = scene.sound.add('whoosh3')
this.whooshes = [this.whoosh1, this.whoosh2, this.whoosh3]

The sound effects are played from my throw method:
throw(){
this.weapon = new Weapon(this.scene) // object thrown
var whoosh_num = Math.floor(Math.random() * this.whooshes.length)
this.whooshes[whoosh_num].play();
}

And the throw method is being called in my player finite state machine (inside the update method):

if(this.player.state=='stand_right'){
  this.player.setVelocityX(0);
  this.player.anims.play('stand_right',true);
  if(!this.player.body.touching.down && !this.player.body.onFloor()){
    this.player.state = 'jump_right'}
  else if(Phaser.Input.Keyboard.JustDown(this.key_jump)){
    this.player.setVelocityY(-750);
    this.jump_sound.play()
    this.player.state = 'jump_right';}
  else if(this.key_right.isDown){
    this.player.state = 'run_right'}
  else if(this.key_left.isDown){
    this.player.state = 'run_left'}
  else if(Phaser.Input.Keyboard.JustDown(this.key_throw)){
    this.scene.time_at_throw_start = Math.trunc(this.scene.time.now)
    this.player.state='stand_right_and_throw'
    this.throw()}
  else if(this.key_down.isDown){
    this.player.state = 'duck_right'}
}

The frame rate does not decrease, it stays at 60 fps even as the sound quality gets worse.

Any idea what might be causing this? I’ve searched Google for days and haven’t found a single instance of someone having a similar issue.

Hi,
First thing, throw is a reserved word in js
https://www.w3schools.com/js/js_reserved.asp

Secondly, when you play a sound from update, update can call throw multiple times, you use JustDown so it must not happens, but check it so you are sure.

No error message in dev tools?

I have changed the method name to throwWeapon(), it has the same behavior.

I am sure it is only being called once because I have a log statement before the throw, and it shows up in the console once per throw.

There is no error message in dev tools.

I would first try to print which sounds are playing in the manager, e.g., in update().

Upon printing the sound and looking under manager -> sounds, I see that it does have a large array of sounds in there. It starts out with about 20-25 (first throw) and after about 1 minute of throwing this number is up to 1500, what does this mean? Is it actually playing 1500 sounds at once at that point?

They’re probably not all playing but it’s still too many.

Can you log each sound.add()?

That is what I was logging to see the above object in my image. The problem appears to go away when I clear that array ( sound_name.manager.sounds = []) before playing the sound. Is it ok to do this?

I think you’ll want to prevent the problem if possible. :slight_smile:

From the code shown there should be only 3 sounds in the manager, ever.

Add something like

console.log('add whoosh1');
this.whoosh1 = scene.sound.add('whoosh1');
// …

everywhere you use sound.add().

1 Like

I am loading a lot of sounds I didn’t tell you about, and I was adding them to the scene in the constructor of the player’s projectile. That was why there were so many in the manager after throwing for a while. I moved the sound.add() statements to the player’s constructor and the issue is resolved. Thanks samme :slight_smile:

@samme thanks for this answer.

I am also experiencing sound deterioration over the course of my game, but the sound manager has only grown from 19 to 270 sounds after 15 minutes. Is this likely to be the source of my deterioration?
(It becomes noticeable when that count is about 180, but as they say correlation does not prove causation)

SOLVED
The 270 sounds in the manager certainly did degrade the sound playback…
I found it useful to purge the sound manager as each scene wakes.
My sound count went up and down, but never exceeded 50.
The deterioration is ended and the sounds everywhere are brilliant!
Holy cow , the music (by Noah Jacobson-Carroll) sounds spectacular and subtle!!

this.sound.removeAll() ought to be the right method, but it was not found at runtime.(??!)
So I used dumb brute force -

if( this?.sound?.sounds?.length ) 
    this.sound.sounds.forEach( s=> this.sound.remove(s) );