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 !

Great idea for a tutorial, what's it for and how exactly does it work etc? Could you explain further with more text and comments, etc etc.

Cheers
Clyde

Hi clyde :) happy to ear you :)

It's really simple ! first you preload you sound with :

SFX_LoadSound("mywav","tada.wav")


Note : You can preload multiple sound !!!

And next you call sound playing, not with global var, but simply
with her name ! with :

SFX_PlaySound("mywav")


Example with multi sounds :) ? ok :)

First, imagine you want to play 20 sounds ! you must have
20 global (or other method) example :

MySound1:Tsound=Loadsound(blablabla)
....
MySound20:Tsound=Loadsound(blablabla)


Etc ... Ok ? it's a little bit heavy ? with my lib your preload all your
sounds in one time !

SFX_LoadSound("mywav01","tada.wav")
....

SFX_LoadSound("mywav20","tada.wav")

and if you want to play your sound make this :

While  Not KeyHit(KEY_ESCAPE)
    Cls

	If MouseHit(1) Then
		SFX_PlaySound("mywav01",true)
	EndIf


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

	If MouseHit(3) Then
		SFX_PlaySound("mywav15",true)
	EndIf

    Flip

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


I hope this explain help you to clarify my lib :)

Note : About SFX_PlaySound("mywav03",true)

The true parameter is here to force to stop the sound before replay it !