Here's some code from Claymore Island ..
Firstly, I load in all the sound files I'm going to use (so each WAV, just once).
Here's a function I use for playing back a sound:
`Plays back a one off sound if a free sound slot is available
function SND_PlaySound(snd,x#,z#,random,volumedivider#)
diffx# = PLA_Player(0).x#-x#
if abs(diffx#) < Const_SoundRange
diffz# = PLA_Player(0).z#-z#
if abs(diffz#) < Const_SoundRange
set vector3 1,diffx#,0,diffz#
if length vector3(1) < Const_SoundRange
soundslot = SND_GetNextFreeSound()
if soundslot = -1 then exitfunction
clone sound soundslot,snd
SND_SetSoundVolumeAndPan(soundslot,x#,z#,distance#,volumedivider#)
set sound speed soundslot,44100-random+rnd(random*2)
play sound soundslot
endif
endif
endif
endfunction
I dont use 3D sounds in Claymore Island, as I have this simple function which sets the volume and pan instead for stereo only.
function SND_SetSoundVolumeAndPan(snd,x#,z#,distance#,volumedivider#)
`PLAY AT FULL VOLUME, ELSE SCALE DOWN THE VOLUME
if distance# < Const_VolumeFullRange
volume = 100
else
volume = 100+Const_VolumeFullRange-distance#/5.0
if volume < 0 then volume = 0
endif
volume = int(volume/volumedivider#)
set sound volume snd,volume
`NOW SET SOUND PAN
`Set sound vector
set vector3 1,x#-PLA_Player(0).x#,0,z#-PLA_Player(0).z#
`Set camera normal vector
set vector3 2,sin(camera angle y()+90),0,cos(camera angle y()+90)
`Calculate how far the sound is left or right of the camera
pan# = int(dot product vector3(1,2)*Const_SoundPanRange)
if pan# < -8000 then pan# = -8000
if pan# > 8000 then pan# = 8000
set sound pan snd,pan#
endfunction
For sound slots I would use something like this:
function SND_GetNextFreeSound()
for snd = Const_SoundPositionMin to Const_SoundPositionMax
if sound exist(snd)
if sound playing(snd) = 0
delete sound snd
exitfunction snd
endif
else
exitfunction snd
endif
next snd
endfunction -1
It also checks for the sound playing and if its not playing, it deletes it, otherwise you'll be leaving a lot of unnecessary sound data in memory. If you're cloning a new sound every time someone fires a bullet without deleting the old sound data, then you'll eat up memory quite fast.