Here we go! My Soundclone/Instancing solution:
remstart
Project : SoundClone
Author : Steve "Agent" Bonfield
Created : 13/04/2011 06:19:00 AM
Abstract: The intent of this project is to create a cloned/instanced sound system that never cuts off a sound currently being
played even if a duplicate soundplay is triggered.
remend
global NumVoices = 5
global NumSoundsPlaying = 0
sync on
sync rate 0
Load_Resources()
` Main Loop
do
Draw_Hud()
Handle_Input()
Sound_Upkeep()
sync
loop
end
` Draw the elements of the UI display
function Draw_Hud()
cls
print "Press a number from 1-4 to play a sound."
print
print "NumVoices: ", NumVoices
print "NumSoundsPlaying: ", NumSoundsPlaying
endfunction
` Poll the keyboard and respond to user commands
function Handle_Input()
GetKey = scancode()
while scancode()
` Wait for keyup
endwhile
if GetKey = 2 then Play_Sound(1, 100)
if GetKey = 3 then Play_Sound(2, 100)
if GetKey = 4 then Play_Sound(3, 100)
if GetKey = 5 then Play_Sound(4, 100)
endfunction
` Upkeep sound plays and deal with resource ID recycles
function Sound_Upkeep()
NumSoundsPlaying = 0
for x = 100 to 99 + NumVoices
if sound exist(x)
if sound playing(x)
` Count the voices in use for HUD display
inc NumSoundsPlaying
else
` Recycle expired sound IDs.
delete sound x
endif
endif
next x
endfunction
` Play a sound
function Play_Sound(WhichSound, Vol)
WhichVoice = 0
for x = 100 to 99 + NumVoices
if sound exist(x) = 0 then WhichVoice = x
next x
if WhichVoice
clone sound WhichVoice, WhichSound
play sound WhichVoice
set sound volume WhichVoice, Vol
else
` All voices are currently in use. Just a placeholder, as this project will take no action in response to such a case.
endif
endfunction
` Precache resources used in the program
function Load_Resources()
load sound "Sound1.wav", 1
load sound "Sound2.wav", 2
load sound "Sound3.wav", 3
load sound "Sound4.wav", 4
endfunction
Changing the number of voices is as easy as adjusting the variable with the obvious name at the very first line of functional code. If you want to use a different range of sounds for your actual plays, change the 100 and the 99 in the FOR...NEXT loop in Play_Sound() and Sound_Upkeep().
Should be a simple matter to modify Play_Sound() to accept additional parameters for more complex 3D soundplays.