Author Topic: Sound overlap issue  (Read 2096 times)

0 Members and 1 Guest are viewing this topic.

This topic contains a post which is marked as Best Answer. Press here if you would like to see it.

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • 40wattstudio
Sound overlap issue
« on: March 23, 2020, 02:27:57 pm »
Still working on my top-down shooter and I'm running into a sound issue.
A 2-second sound is supposed to play if the player just taps the spacebar (fire) button. This part works fine.
The problem is when the player holds down the spacebar (fire) button for a longer burst of fire. What ends up happening is that the code to play the sound file again is re-triggered before it has a chance to play through to the end. This results in an "overlapping" of sounds which sounds awful.
I've experimented with delays with mixed results.
How would you guys deal with this issue?

Below is some sample code of what I've tried so far:

Code: QB64: [Select]
  1. SUB multishot
  2.  
  3.     IF _KEYDOWN(32) AND keydelay = 0 AND readyforsound = 1 THEN            ' if spacebar pressed and bullets stopped firing and ready for sound
  4.         readyforsound = 0
  5.         IF issoundon = 1 THEN
  6.             soundlength = _SNDLEN(multilasersound)               'get length of the sound file in seconds
  7.             _SNDVOL multilasersound, soundlevel                     'play sound file with specified sound volume
  8.             _SNDLIMIT multilasersound, soundlength                'stop playing the sound after the calculated duration. I believe this part also counts down???
  9.             _SNDPLAYCOPY multilasersound                              'play the sound
  10.             IF soundlength <= 0 THEN                                     ' if sound file finished playing then
  11.                 readyforsound = 1                                             ' makes it possible for script to be run again
  12.             END IF
  13.         END IF
  14.         numshots = 0
  15.         DO
  16.            '------------------- code that shoots the bullets --------------
  17.              keydelay = 5
  18.         LOOP UNTIL numshots = maxshots
  19.     END IF
  20.     IF keydelay THEN keydelay = keydelay - 1
  21.     multishotscriptran = 1
  22.  
  23.  

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Sound overlap issue
« Reply #1 on: March 23, 2020, 02:36:29 pm »
If you can't shorten sound or shorten it enough, you could time bullets fired st another can't fire until a set time has elapsed or get a recording of many shots firing.

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • 40wattstudio
Re: Sound overlap issue
« Reply #2 on: March 23, 2020, 02:48:58 pm »
If you can't shorten sound or shorten it enough, you could time bullets fired st another can't fire until a set time has elapsed or get a recording of many shots firing.

That's what I was hoping the first line would do . . .

IF _KEYDOWN(32) AND keydelay = 0 AND readyforsound = 1 THEN

. . . prevent firing until readyforsound is true (which is only supposed to be triggered when the sound has finished playing).
But that doesn't seem to be the case for some reason.
Am I understanding correctly that _SNDLIMIT has a countdown timer in the background? That's what the Wiki made it sound like.

Regardless of the code I posted, is there a better way of syncing sound with on-screen action?


Marked as best answer by 40wattstudio on March 23, 2020, 12:05:09 pm

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
Re: Sound overlap issue
« Reply #3 on: March 23, 2020, 03:04:30 pm »
IF _SNDPLAYING(yoursound) THEN _SNDSTOP yoursound: _SNDPLAY yoursound

or

IF NOT _SNDPLAYING(yoursound) THEN _SNDPLAY yoursound
« Last Edit: March 23, 2020, 03:05:42 pm by TerryRitchie »
In order to understand recursion, one must first understand recursion.

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • 40wattstudio
Re: Sound overlap issue
« Reply #4 on: March 23, 2020, 04:04:55 pm »
IF NOT _SNDPLAYING(yoursound) THEN _SNDPLAY yoursound

I tried that last line with some modifications and it almost works as intended. For one, I had to use _SNDPLAY instead of _SNDPLAYCOPY.
If I hold down spacebar, it won't play the next sound until the first sound has finished playing. So that's good to go.
Thanks Terry and bplus!

Offline 40wattstudio

  • Newbie
  • Posts: 82
    • 40wattstudio
Re: Sound overlap issue
« Reply #5 on: March 24, 2020, 10:10:31 am »
Discovered something else last night: A lot of the "sound overlap" issue seems to be related to the use of _LIMIT. Previously I had the main game loop set to _LIMIT 60. Reducing it to _LIMIT 45 or _LIMIT 30 greatly improved the sound performance.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Sound overlap issue
« Reply #6 on: March 24, 2020, 11:30:00 am »
OK if you find you still need faster loops, you can start timing the bullet or is it laser now ;) shooting.

FellippeHeitor

  • Guest
Re: Sound overlap issue
« Reply #7 on: March 24, 2020, 11:32:17 am »
Code: QB64: [Select]
  1. IF TIMER - laserShot > .3 THEN
  2.     'play laser sound here
  3.     laserShot = TIMER

Then your _LIMIT can be higher.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Sound overlap issue
« Reply #8 on: March 24, 2020, 11:37:56 am »
Code: QB64: [Select]
  1. IF TIMER - laserShot > .3 THEN
  2.     'play laser sound here
  3.     laserShot = TIMER

Then your _LIMIT can be higher.

and don't forget after midnight this will turn into a pumpkin ;-))

This might help:
Code: QB64: [Select]
  1. IF TIMER < laserShot THEN laserShot = laserShot - 24 * 60 * 60
  2. IF TIMER - laserShot > .3 THEN
  3.     'play laser sound here
  4.     laserShot = TIMER
  5.  
« Last Edit: March 24, 2020, 11:42:30 am by bplus »

FellippeHeitor

  • Guest
Re: Sound overlap issue
« Reply #9 on: March 24, 2020, 11:43:07 am »
For one hertz it may play the laser sound incorrectly (or not play it), then it'll be solved. Barely something that needs such prevention.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
Re: Sound overlap issue
« Reply #10 on: March 24, 2020, 11:45:07 am »
For one hertz it may play the laser sound incorrectly (or not play it), then it'll be solved. Barely something that needs such prevention.

I don't think laserShot will get changed so for whole day Timer will be less.

FellippeHeitor

  • Guest
Re: Sound overlap issue
« Reply #11 on: March 24, 2020, 11:51:14 am »
Meh, ok.

Anyway... the person playing it overnight will realize something is wrong and will either (i) go to bed or (ii) restart the game to see what broke :-D

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • Steve’s QB64 Archive Forum
Re: Sound overlap issue
« Reply #12 on: March 24, 2020, 12:07:16 pm »
Or just plug in ExtendedTimer (https://www.qb64.org/forum/index.php?topic=1598.0) and not worry about change of day issues at midnight.
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!