I've had this idea for awhile after I played with GLB. If you've ever noticed what happens when you play a sound that's already playing you'll know that it cuts off playback and starts it back over. Let's say you want to play a cute little sound whenever your character picks up a coin or something. When the pick up several coins at once, the sound effect doesn't sound very clean. What this code will do is allow you to specify how many instances of a sound can be played at once. So rather than it being cut-off, you could hear the same sound played again, overlapping what's currently already playing.
REM ================================================
REM Title: Sound Functions
REM Author: Phaelax
REM Date: Apr 4, 2011
REM ================================================
Global __SoundIndex = 0
Type Sound
dbSoundNumber as integer
buffer as integer
EndType
dim __Sound(0) as Sound
snd1 = loadSound("H:\Media\Audio\SFX\greet.wav", 6)
snd2 = loadSound("H:\Media\Audio\SFX\launchgame.wav", 3)
repeat
if rightkey() = 1 and flag = 0 then playSound(snd1) : flag = 1
if rightkey() = 0 then flag = 0
if leftkey() = 1 and flag2 = 0 then playSound(snd2) : flag2 = 1
if leftkey() = 0 then flag2 = 0
until spacekey()
end
REM ===========================================
REM path$ - Path to the wav file to load
REM buffer - How many instances of this sound
REM can be played at the same time
REM ===========================================
function loadSound(path$, buffer)
snd = __SoundIndex + 1
load sound path$, snd
for i = snd+1 to (snd+buffer-1)
clone sound i, snd
next i
array insert at bottom __Sound()
k = array count(__Sound()) - 1
__Sound(k).dbSoundNumber = snd
__Sound(k).buffer = buffer
inc __SoundIndex, buffer
endfunction k
REM ===========================================
REM Plays a specified sound.
REM It will play the next available free instance
REM of the sound. If all instances are currently
REM playing, it will cut off the first instance
REM and replay it from the beginning.
REM ===========================================
function playSound(snd)
for i = __Sound(snd).dbSoundNumber to __Sound(snd).dbSoundNumber+__Sound(snd).buffer-1
if sound playing(i) = 0 then play sound i : exitfunction
next i
play sound __Sound(snd).dbSoundNumber
endfunction
The Internet: Where men are men, women are men, and children are federal agents