handling sounds

Blitz3D Forums/Blitz3D Programming/handling sounds

cash(Posted 2007) [#1]
sound questions

For example whats the best way to handle playing enemy gunshots so they don`t become one mass of overlapped sounds.
Difficult to explain but for those who have experience with this I am sure you will know what I mean.

Imagine 20 enemies with an event which triggers the gunshot sound. The sound plays and plays again and doesnt always complete before playing again so it becomes a real mess.

Its like I need each enemy to have its own sound handler or something.


Stevie G(Posted 2007) [#2]
Something like this ..

global CHANNELenemyGun
global SOUNDgun = loadsound( "Gun" )

.
.
.

If EnemyShootsGun
   If  not ChannelPlaying( CHANNELenemyGun ) 
        CHANNELenemyGun = PlaySound( SOUNDgun )
   Endif
Endif



PowerPC603(Posted 2007) [#3]
According to the manual, each sound you play, requires you to assign a channel-handle when you play the sound:
; Assign a global variable for the sound 
Global sndPlayerDie
; Load the sound file into memory
sndPlayerDie=LoadSound("sounds/die.wav")
; Play the sound via channel chnDie
chnDie=PlaySound ( sndPlayerDie ) 


In this example (from the manual), your sound will be playing through channel "chnDie".
Then you have two options:
- if an enemy shoots, you check if the channel is already playing (ChannelPlaying), and if not, play the sound through the channel
if the channel is already playing, do nothing (don't play the sound)
- if an enemy shoots, you check if the channel is already playing and if it is, stop the channel using StopChannel and play the sound


jfk EO-11110(Posted 2007) [#4]
You may want to use an additional variable for each sound that contains the min lenght a sound may be played. This way you can repeat a sound at a delay time that makes sense. THis is also useful for automatic firearms.

You may also experiment with the pitch of the sounds. If all enemies got the same gun then it might look a little strange, noless it's gonna solve the repetition problem. Something like

channelpitch chn, realrate+rand(-1000,1000).

Right now I don't know how to get the realrate (reliably), so you best use the same rate for all your sounds, eg. 44.1kHz.


_33(Posted 2007) [#5]
If one was to generate it's own sample, would they absolutely need to perform a LoadSound for blitz3d to play that sample, or is it possible to just hook the address of the bank to PlaySound?


Hujiklo(Posted 2007) [#6]
yes - just play the 'global' variable of the loaded sound using playsound(laadeedaa).

If you're going to have lots of enemies using sounds then you're best generating some new type info upon spawn and handling the sound types using the default enemy AI code.