Sound bug or am I missing something?

BlitzMax Forums/BlitzMax Beginners Area/Sound bug or am I missing something?

QuickSilva(Posted 2009) [#1]
I am trying to keep my code as modular as possible by including my sounds inside the type from which they are used, for example, all sounds related to the player are in the TPlayer type, bullet sounds are in the TBullet type and so on.

The problem that I am getting is that when I switch over to OpenAL this no longer works. Here is an example of the problem. This doesn`t work and I cannot see why? If you comment out the SetAudioDriver ("OpenAL") it works again.

Can anyone see a reason for this? Am I doing something wrong?

Thanks for any help,
Jason.

(You will need to use your own .wav file.)


Type TTest
	Global SoundFX:TSound=LoadSound("SoundFX.wav")
End Type

Graphics 640,480

SetAudioDriver ("OpenAL")

While Not KeyHit(KEY_ESCAPE)
	Cls
	
	If KeyHit(KEY_SPACE) Then PlaySound(TTest.SoundFX)
	Flip
Wend

End




grable(Posted 2009) [#2]
You probably need to call SetAudioDriver BEFORE any calls to LoadSound and the like...


Gabriel(Posted 2009) [#3]
It's generally considered a bad practice to initalize anything other than literals (and constants) in a static variable (eg: Type Global) like that because there are no guarantees when it will be executed. In this case, as Grable points out, it's more than likely being executed before you set a driver.


jkrankie(Posted 2009) [#4]
Is there really any point in having a sound as a static variable? Like Gabriel says, Static stuff is pretty much just for constants.

Load it as a global variable and play it as-and-when, unless you have a particularly need for doing things this way. Personally i tend to have a function that accepts tSound and tchannel parameters, along with volume and rate that returns whatever channel i'm using (so i can test if it's playing or not etc.).

Cheers
Charlie


Grey Alien(Posted 2009) [#5]
Yep that's the problem, the Global is initialised as part of the type before any code is run, then the code changes the sound driver to OpenAL. So you need to make a system where you load the sounds in after you've changed the sound driver.


QuickSilva(Posted 2009) [#6]
It`s not a problem, I just wanted to know if it was BlitzMax related or just a bad programming practice that I was using.

I think that I first saw sounds loaded in a type in a tutorial for BMax that I was looking at. Obviously this was the wrong way to tackle the problem.

Thanks though for the pointers. I`m still learning so it`s good to know about these things.

Jason.