Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Stopping _SNDPLAYCOPY ?Possible?
#11
(08-07-2024, 05:36 PM)DSMan195276 Wrote: I haven't tested this code, but wouldn't something like this work?

Code: (Select All)
ReDim HandleCache(20) As Long


Sub PlaySound (handle As Long)
    For i = 1 to UBOUND(HandleCache)
        If HandleCache(i) <= 0 _OrElse Not _SndPlaying(HandleCache(i)) Then
            _SndClose HandleCache(i)
            HandleCache(i) = _SndCopy(handle)
            _SndPlay HandleCache(i)
           
            Exit Sub
        End If
    Next

    ReDim HandleCache(UBOUND(HandleCache + 1)) As Long

    HandleCache(UBOUND(HandleCache)) = _SndCopy(handle)
    _SndPlay HandleCache(UBOUND(HandleCache))
End Sub

Sub KillSounds ()
    For i = 1 to Ubound(HandleCache)
        If HandleCache(i) > 0 Then
            _SndClose HandleCache(i)
            HandleCache(i) = -1
        End If
    Next
End Sub

And then you just replace the `_SndPlayCopy` calls with `PlaySound` calls and call `KillSounds` when you're done. It's not super pretty but it doesn't seem all that bad overall.
At first glance that looks about right, however, that's a lot of overhead when dealing with something like a fast-paced game with lots of sprite movements, animations, and sounds working all at once. Just as an example let's say you have 10 enemy combatants coming at you firing their machine guns at 3 rounds per second. That's 30 handles produced every second. If the sound clip is one second long you'll have up to 90 handles floating around that need looked after, not to mention the overhead of _SNDCOPY and _SNDCLOSE doing their thing. These are the scenarios I'm running into.

Off the top of my head a game I used to play a lot was BeJeweled 3 and it suffered from sound overlap when you had many explosions and movement sounds going on all at once. It can be quite distracting when as a player you notice these things. If they would have stopped all the other identical explosion sounds when say a lighting jewel was activated it would have sounded so much better instead of the muddy mess of having the many explosion sounds finish out while the lightning sound effect was going on.
There are two ways to write error-free programs; only the third one works.
QB64 Tutorial
Reply


Messages In This Thread
RE: Stopping _SNDPLAYCOPY ?Possible? - by a740g - 08-07-2024, 02:48 PM
RE: Stopping _SNDPLAYCOPY ?Possible? - by Pete - 08-07-2024, 03:44 PM
RE: Stopping _SNDPLAYCOPY ?Possible? - by TerryRitchie - 08-07-2024, 07:24 PM
RE: Stopping _SNDPLAYCOPY ?Possible? - by Pete - 08-07-2024, 11:16 PM



Users browsing this thread: 14 Guest(s)