Inconsistent sounds solution

Having a small issue with OpenAL where anything below 1 intervals per second I get inconsistent sounds.

Response from Michalis
Short answer: The problem comes from OpenAL mixing mechanism (limit on how many sounds can be reliably mixed at given time) combined with the fact your BulletShot.wav includes a long period of almost-silence at the end. This causes the need to mix a lot of sounds at given time, when the Timer.IntervalSeconds is very small. Details: With Timer.IntervalSeconds equal 0.05 , you’re firing 20 times per second. The length of your sound file is 2 seconds (01.99 below):

Input File : ‘BulletShot.wav’
Channels : 2
Sample Rate : 48000
Precision : 16-bit
Duration : 00:00:01.99 = 95616 samples ~ 149.4 CDDA sectors
File Size : 383k
Bit Rate : 1.54M
Sample Encoding: 16-bit Signed Integer PCM

This means that, after 2 seconds, there are almost 40 sounds playing at the same time. Most of them are silence, but still take resources. And OpenAL, as most sound engines, has internally a hard limit on the number of sounds it can really mix. See how it works:

  • Play with examples/audio/test_sound_source_allocator/ (note: I have just pushed a few improvements to this example, so you may want to update the engine from GitHub.)
  • load there your sound ‘BulletShot.wav’,
  • and hit “Play” button very quickly.

The example has limit “Max allocated sources” set to 6, to show quickly what the limit means. The real limit, by default, is 16. This is still < 40.


  1. (Not advised): Increase the limit. Set SoundEngine.MaxAllocatedSources := 40.
    Do it e.g. in ApplicationInitialize.

Not advised, because there’s no guarantee that OpenAL will actually support 40 (if not, we will actually have less than 40). And the limit is there for a good reason: mixing many sources is a performance problem, and user probably doesn’t hear (clearly) 40 sounds at the same time anyway.

That said, it does work to improve the audio in this case. At least with OpenAL on Linux right now.

  1. (Advised): Make the sound shorter.
    I’ve made an extreme example of it, cutting the sound length to 0.33, and I did it “brutally” by just cutting of the sound. See Audacity screenshots.
    SoundEngine.MaxAllocatedSources makes the problem go away. It is probably possible to make it better than I did: you can e.g. fade out sound at the end, not cut it abruptly. And maybe 0.33 is too drastic, maybe 0.5 is OK. So you could have a bit better sound quality. My “brutal cut” means the sound has no audible “tail” at the end, it just stops.

  2. (Advised if possible): If at all possible.
    Do not play “rapid fire” this way. Instead, mix numerous sounds into a longer sound file, e.g. “10 shots” into one sound file. This way we will not have to mix so many sounds at the same time – to play 10 shots, you would play one WAV, not 10.

Thank you very much for reposting it here – this will help others this way :slight_smile:

I’ll add that I have added a somewhat rephrased version of this answer also into our manual, Sound | Manual | Castle Game Engine .

Hopefully this makes things clear if anyone else stumbles on this problem :slight_smile:

1 Like