Sound library : How to use sound by name ?

BlitzMax Forums/BlitzMax Tutorials/Sound library : How to use sound by name ?

Hi :)

I'm making a little sound library this is the code :) you can tell sound
with name for easy using :)

Global SFX_SoundList:TList=CreateList() 

' -------------
' Type des sons
' -------------
Type SFX_Sound
	Field Name:String
	Field Sound:Int
	Field Channel:Int
	Field Volume:Float
EndType

' ---------------------------
' Permet d'initialiser un son
' ---------------------------
Function SFX_LoadSound(SoundName:String,SoundFile:String)
	NewSound:SFX_Sound=New SFX_Sound
	NewSound.Name=SoundName
	NewSound.Sound=LoadSound(SoundFile)
	NewSound.Channel=AllocChannel()	
	NewSound.Volume=1
	ListAddLast SFX_SoundList,NewSound
End Function

' ----------------------------
' Fonction pour jouer un son : Si le paramètre force est active le son est stoppé puis rejoué
' ----------------------------
Function SFX_PlaySound(Name:String,Force:Byte=False)
	For Local S:SFX_Sound = EachIn SFX_SoundList
		If Upper(S.Name)=Upper(Name) Then		
			If Force=False Then
				If ChannelPlaying(S.Channel)<>True Then
					S.Channel=CueSound(S.Sound)
				EndIf
				
				SetChannelVolume(S.Channel,S.Volume)
				ResumeChannel S.Channel
			Else
				If ChannelPlaying(S.Channel)=True Then
					StopChannel(S.Channel)
				EndIf
				
				S.Channel=CueSound(S.Sound)
				
				SetChannelVolume(S.Channel,S.Volume)
				ResumeChannel S.Channel
			EndIf
		EndIf
	Next	
End Function

' ----------------------------------------
' Fonction pour changer le volume d'un son
' ----------------------------------------
Function SFX_SetSoundVolume(Name:String,Volume:Float=1.0)
	For Local S:SFX_Sound = EachIn SFX_SoundList
		If Upper(S.Name)=Upper(Name) Then
			S.Volume=Volume
			SetChannelVolume(S.Channel,S.Volume)
			ResumeChannel S.Channel
		EndIf
	Next
End Function


How to use ? :

include "Inc_Sound.bmx"

Graphics 640,480,0

SFX_LoadSound("mywav","tada.wav")
SFX_SetSoundVolume("w",0.5)

While  Not KeyHit(KEY_ESCAPE)

    Cls

	If MouseHit(1) Then
	
		SFX_PlaySound("mywav")
		
	EndIf


	If MouseHit(2) Then
	
		SFX_PlaySound("mywav",true)
		
	EndIf

    Flip

    ' On n'oublie pas de vide la memoire ....
    FlushMem()
Wend 


Its only the start :) you can improve this lib !