Jump to content

Degrading sound


timetocode
 Share

Recommended Posts

This is probably a browser thing and not a babylon thing... but I'm getting this thing where the game sound effects degrade the more players I add to a game.

I have a player character and I attach ~20 sounds to its mesh. It is a first person shooter and each gun has about that many sounds between gunshots, reloading, and a handful of variants of each. In the development version these are all wav files which are far larger than whatever I'll convert them to later.

As I load more players, the gun of the first player starts to sound worse and worse. The weird part is none of the other players are making any sound.. they're all holding still and not firing their gun. At first it just gets a little tinny or crunchy, but as the player count goes up the sound crunches so much that it eventually goes almost almost silent. It sounds like a game lagging very badly, but the FPS is remaining 120+

Any idea why this is happening? All that is occurring on the sound level is that I'm cloning more and more of the gun sounds, I'm not actually playing anything more than one player worth of a sounds.

I tried to reproduce this on the babylon playground but even cloning the violin music 3000 times didn't change a thing -- it sounds the same no matter how many clones are around. Maybe I'm causing some memory problem in my audio hardware with the wavs...? The biggest is about 475 KB.

Link to comment
Share on other sites

It's not because the camera is further from the player when you have more players?  When you attach a sound to the mesh (ie: sound.attachToMesh(mesh)) then the sound comes from the location/distance of the mesh.  I load quite a few sounds in my game attached to many meshes and have not found any issues.  Any way you can reproduce as a PG otherwise?

Link to comment
Share on other sites

I was finally able to reproduce it in a PG.

https://www.babylonjs-playground.com/#8B9YRN#1

This is based off of the playground where violin music is playing inside of spheres. As the camera enters the sphere, the music becomes audible.

In this playground I have one purple dome with music playing on loop, and then 100 purple domes that have a clone of the music linked to their mesh, but the music is intentionally not being played.  

The purple dome that is playing music is separate from all of the others. It is the only one playing music.

If we keep increasing the number of silent domes (see the for loop around line ~45) eventually the sound will degrade, even though we're only playing a single wav on loop. I left the PG on 100 silent domes b/c I don't want to crash people's computers. Personally I have to increase this number to 1200(!!) before I get the sound problems that are occurring in my game. So to reproduce, change the count on line 45 and then walk into the isolated sphere.

That 1200 is a big number but this problem happens in my game at around 300 sounds (even though 298 of the sounds aren't playing). These are just sounds that are attached to meshes and aren't playing, but they can get triggered later.

 

Link to comment
Share on other sites

Now that I have this reproduced, I think I'm realizing that this idea was never going to work due to the number of sounds. I did a little bit more testing with a 500 KB wav file vs a 15 KB mp3, and the degradation seems more related to the number of sounds than their size.

In my game each weapon has like 5-20 sounds, and a player can hold a few weapons. The walk/run cycle has like 16 sounds per material (concrete, wood, grass, etc). It can add up to about ~100 possible sounds per player, though 99% of them aren't playing at any given point. BJS seems totally fine with that many sounds, but it looks like attachToMesh is not designed for this. I read the source code, and it looks like it *might* be viable if it would check if the sound is playing before rebuilding the matrices. Currently it does some fairly expensive work, even for non-playing sounds (setPosition and computeWorldMatrix fill the profiler when stress tested with cloned sounds on moving meshes).

I'm going to test positioning the sounds manually at the time that they are played, and not having them move along with the mesh. If that doesn't work I guess I'll pool them in addition.

 

Edit: definitely need a pool

Edit#2

at 21 players firing the same automatic rifle, the pool brought the active number of sound instances from 210 down to 59

Here's an ultra simple auto-expanding pool if anyone wants. Usage is just to use 'get' for short-lived sounds and it will handle allocation and releasing on its own. It never deallocs.

class SoundPool {
    constructor() {
        this.scene = null
        this.sounds = {}
    }

    init(scene) {
        this.scene = scene
    }

    allocate(name) {
        const sound = BABYLON.Sound.FromAtlas(name, name, this.scene)
        sound.onEndedObservable.add(() => {
            this.release(sound)
        })
        this.sounds[name].push(sound)
    }

    get(name) {
        if (!this.sounds[name]) {
            this.sounds[name] = []
        }
        if (this.sounds[name].length === 0) {
            this.allocate(name)
        }
        return this.sounds[name].pop()
    }
    release(obj) {
        this.sounds[obj.name].push(obj)
    }
}

const singleton = new SoundPool()
module.exports = singleton

BABYLON.Sound.FromAtlas is a little wrapper that clones sounds without making additional xhrs (e.g. 'sounds/foo.mp3', clones it and applies new options)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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