ihhub / fheroes2

fheroes2 is a recreation of Heroes of Might and Magic II game engine.
https://ihhub.github.io/fheroes2/
GNU General Public License v2.0
2.7k stars 375 forks source link

"[ERROR] PlayChunk: No free channels available" appears often #4573

Closed a1exsh closed 2 years ago

a1exsh commented 2 years ago

Preliminary checks

Platform

Linux

Describe the bug

In a version built from recent HEAD the following errors appear on the console:

10:09:19: [DBG_ENGINE]  main:  Free Heroes of Might and Magic II, version: 0.9.9
10:50:12: [ERROR]   PlayChunk:  No free channels available
10:56:28: [ERROR]   PlayChunk:  No free channels available
10:56:28: [ERROR]   PlayChunk:  No free channels available
10:56:28: [ERROR]   PlayChunk:  No free channels available
10:56:28: [ERROR]   PlayChunk:  No free channels available
10:56:28: [ERROR]   PlayChunk:  No free channels available
10:56:28: [ERROR]   PlayChunk:  No free channels available
10:56:28: [ERROR]   PlayChunk:  No free channels available
10:56:28: [ERROR]   PlayChunk:  No free channels available
10:56:28: [ERROR]   PlayChunk:  No free channels available
10:56:56: [ERROR]   PlayChunk:  No free channels available
10:56:56: [ERROR]   PlayChunk:  No free channels available
10:56:58: [ERROR]   PlayChunk:  No free channels available
10:56:59: [ERROR]   PlayChunk:  No free channels available
11:00:33: [ERROR]   PlayChunk:  No free channels available
11:00:33: [ERROR]   PlayChunk:  No free channels available
11:18:15: [ERROR]   PlayChunk:  No free channels available
11:18:16: [ERROR]   PlayChunk:  No free channels available
11:18:22: [ERROR]   PlayChunk:  No free channels available

This was not the case until recently.

Save file

Not sure what triggers it. Will add a save file if I find when the error appears.

Additional info

Haven't noticed any sounds missing so far.

oleg-derevenetz commented 2 years ago

Hi @a1exsh if this issue is reliably reproducible with your setup, it would be nice to perform a git bisect to find commit that introduced it.

ihhub commented 2 years ago

Hi @a1exsh , is it still reproducible on the latest commit from master?

a1exsh commented 2 years ago

Hi @a1exsh , is it still reproducible on the latest commit from master?

Yep. Just built cleanly from 8a25c0d3 and I can reproduce it after a few minutes playing the attached save file. So far I've noticed it being triggered when a hero is moving over a road or a river, but this doesn't always logs this error.

AUTOSAVE.zip

a1exsh commented 2 years ago

It looks like this is not caused by recent changes in fheroes2, but by some update of libraries on my system. I've checked a number of sound-related changes and went as early as to the previous release 0.9.8 and the issue is still reproducible.

a1exsh commented 2 years ago

I was able to reproduce the error on a different machine with Ubuntu 21.10, by building from HEAD as of today.

ihhub commented 2 years ago

I think if you're playing a map where a hero is surrounded by more than 16 objects this error appears.

a1exsh commented 2 years ago

I think if you're playing a map where a hero is surrounded by more than 16 objects this error appears.

Ah, that's a good guess. I wonder can we do something to prevent this? We track the channels ourselves, if I'm not mistaken, so we could avoid playing an extra sound when all channels are busy and log a debug message instead?

oleg-derevenetz commented 2 years ago

We track the channels ourselves, if I'm not mistaken, so we could avoid playing an extra sound when all channels are busy and log a debug message instead?

That's what in fact happens here: extra sound is not played and the message is logged :) We can also try to increase the number of channels.

a1exsh commented 2 years ago

That's what in fact happens here: extra sound is not played and the message is logged

Yes, but an ERROR message is logged, as if something unexpected has happened. I'd prefer this to be a debug message that we can ignore, instead of starting to ignore errors.

oleg-derevenetz commented 2 years ago

Yes, but an ERROR message is logged, as if something unexpected has happened.

This is really a bit unexpected. However, it seems that maximum number of channels in Mix_AllocateChannels() depends just on available RAM and not on some audio hardware limitations, so we can try to just increase number of channels from 16 to, say, 32 here:

https://github.com/ihhub/fheroes2/blob/e956c44bf37aa9dde518ae76059fdad3669db685/src/fheroes2/game/fheroes2.cpp#L194

You can try it yourself and see is this solves an issue.

a1exsh commented 2 years ago

increase number of channels from 16 to, say, 32

I'd rather not do that: 16 sounds at once is probably already too much — adding more will likely result in the player hearing just noise.

Maybe we should consider less object in the hero's vicinity? How did we came to use all 16 channels in the first place?

ihhub commented 2 years ago

Originally this number was 8. Then we increased it to 16. What we need to do is to limit the number of objects from where we play sounds. There's a function to scan nearby objects around a hero and then play sounds from them according to distance. We need to modify that part. The current implementation simply doesn't take into account that 2 objects might have the same sound and it doesn't check if the object is near than another one.

oleg-derevenetz commented 2 years ago

The environment sound mixer scans 7x7 area around a hero or castle to find objects with ambient sounds, and the farther the object is from the hero or castle, the lower the volume. It would be interesting though to try to increase number of channels and hear the result in the cases where all these "no free channels" messages arrive.

a1exsh commented 2 years ago

The current implementation simply doesn't take into account that 2 objects might have the same sound

Well, that's not necessarily bad, as in stereo the sounds may be coming from different directions. I don't immediately see why we should change this part.

and it doesn't check if the object is near than another one

That. I guess we are just considering an area too big to be relevant (the farthest objects occupying the channels, but producing sounds to quiet to be noticable). What if we sort objects by distance before playing the sounds — closest first?

ihhub commented 2 years ago

Yes, closest objects should be first.

ihhub commented 2 years ago

Hi @a1exsh and @oleg-derevenetz , please take a look at #4706.

a1exsh commented 2 years ago

@ihhub This error message wasn't appearing for a while, but now I'm constantly seeing it again after building from HEAD...

oleg-derevenetz commented 2 years ago

Hi @a1exsh Is there any way to attach a savefile with instructions on how to reproduce this (so that at least on your system it was 100% reproducible)? Something like "load this savefile and move Roxana 3 tiles to the right"?

a1exsh commented 2 years ago

@oleg-derevenetz @ihhub hm, I cannot reproduce it anymore after another clean build from HEAD... sorry for the noise: it must be some artifact of checking out other branches. I need to rest more ;)

a1exsh commented 2 years ago

Sorry, I have to re-open this, but this time I've got a reproducer! :)

This time the error is logged when in the battle:

  1. Load the attached save file: vh1_0070.zip
  2. Attack the town of the red player.
  3. Restart the battle in manual mode, if auto-battle was active.
  4. Ensure combat animation speed is set to maximum (10).
  5. Start auto-battle and observe console output.
oleg-derevenetz commented 2 years ago

Aha. Although this message does not appear to me, I can assume the mechanism of its appearance. When a sufficiently high animation speed is selected, some unit sounds (for example, footsteps) overlap each other, and the new sound starts playing before the old one finishes playing. Thus, many identical sounds can be played simultaneously (creating a some sort of a rush effect) and many sound channels can be busy playing those sounds. This is mostly harmless.

a1exsh commented 2 years ago

Sure, I understand that it's harmless, so maybe we should demote it to a warning or even a debug message? Otherwise it may make it easy to miss real errors.

oleg-derevenetz commented 2 years ago

As for me, I'd rather increase the number of channels (from 16 to, say, 32) :) AFAIK this costs just a some amount of RAM, nothing more.