Sound and channels

BlitzMax Forums/BlitzMax Beginners Area/Sound and channels

Pettersson(Posted 2010) [#1]
Hi!

I've never needed to deal with sound before, and hope to get some help with this. I've been testing what I could find online, so I can load and play a sound. But I have no idea what a channel is or how to manage them.

I'm working on a game and a friend of mine has made a great tune. I can play it, but don't know how to make it loop. The game should have sound effects as well. Anyone have code that will help manage these things? I.e. looping a large ogg and simultaneously playing perhaps multiple sound effects.


matibee(Posted 2010) [#2]
Looping: LoadSound accepts a loop flag

Rem
Load and Play a small example wav file with looping.
End Rem

sound=LoadSound("shoot.wav", SOUND_LOOP )
PlaySound sound

Input "Press any key to continue"


PlaySound returns a TChannel object (you don't have to use it) but you can use it to (amongst other things): See if the channel is still playing (ChannelPlaying), pause/resume or stop it (PauseChannel,ResumeChannel and StopChannel) and adjust its volume level (SetChannelVolume).

In most cases you'll just want to PlaySound and ignore the rest.


Pettersson(Posted 2010) [#3]
Excellent! Never occurred to me that looping could be as simple as that.. Seems I'm all set. Thanks!

/Henrik


John G(Posted 2010) [#4]
On a related question, how many sound channels can you typically allocate? I currently have 10 for sound effects each with its own channel. Thanks.


matibee(Posted 2010) [#5]
I generally don't allocate sound channels John. You may have 10 sound effects but what if you want to play one sound effect several times? Like a gun fire effect that's used for a machine gun. The sample may be 0.3 seconds long and you might want to play a burst of machine gun fire: 10 shots in 1 second, 0.1 seconds apart. If you use one channel per sound effect you'll be stopping one sample to start the next instead of letting the sounds accumulate as they should.

I let the system allocate the channels on the fly and only use or keep local instances of them if there's extra work to be done setting volumes or manually looping them etc.


John G(Posted 2010) [#6]
Thanks for feedback matibee. When I do the machine gun, I just use one burst and repeat. Is it faster Allocate Channels and just set the volume or rate once?
Two examples below:
	Global ChanThree:TChannel = AllocChannel()
	Global MachGun:TSound = LoadSound("Incbin::Grafix/MachineG.wav", SOUND_HARDWARE)
	SetChannelVolume ChanThree, 0.5

	Global ChanEight:TChannel = AllocChannel()
	Global MotorG:TSound = LoadSound("Incbin::Grafix/MotorG.wav", SOUND_LOOP | SOUND_HARDWARE)
	ChanEight = CueSound(MotorG)
	SetChannelVolume ChanEight, 0.575
	SetChannelRate ChanEight, 0.5

Thanks


matibee(Posted 2010) [#7]
Is it faster Allocate Channels and just set the volume or rate once?


I wouldn't really know without testing. What I do know is I've wrapped PlaySound into a volume controlled function:

Function PlayVolumeControlledSound( s:TSound, fVolume:Float = 1.0 )
	Local chan:TChannel = CueSound( s )
	SetChannelVolume( chan , _fSoundVolume * fVolume )
	ResumeChannel( chan )
End Function 


_fSoundVolume is a system global, fVolume is the required ratio. This has served me well enough.

I see you're using a motor sound and want to alter its frequency. In that case the above function should be adjusted to return you the TChannel object to work on.