sfx with bass

BlitzMax Forums/Brucey's Modules/sfx with bass

jkrankie(Posted 2008) [#1]
What would be the best way of using bass to load and play sound effects (explosion etc.)? currently i use the built in blitzmax command for sounds, and i use a function which allocates a new channel to a sound every time one is needed to be played (which could be several times a second, my game is an arena shooter).

Cheers
Charlie


plash(Posted 2008) [#2]
and i use a function which allocates a new channel to a sound every time one is needed to be played (which could be several times a second, my game is an arena shooter).
Use a single channel for such events (particles, collision.. etc). If there isn't a limit on the number of channels you can have.. I don't think it really matters.


Brucey(Posted 2008) [#3]
AFAIK, there's no limit... it mixes it all together for you. So I believe in theory you can load up your samples and have them ready to play whenever.


jkrankie(Posted 2008) [#4]
right, cool. i'll take a look at this tomorrow. hopefully it'll be able to play the files quick enough with some of the effects added to so that explosions can sound slightly different from one another.

This is a really cool module, especially as you can get ftt and waveform data etc. from playing channels. this means i can have some cool audio reactive backgrounds (like these, only more interactive: http://www.blitzbasic.com/Community/posts.php?topic=82146#925865 ) in my game, along with (hopefully) enemy spawns being in time with various parts of the music.

Cheers
Charlie


jkrankie(Posted 2008) [#5]
so something like this would be ok?

SuperStrict

Import BaH.Bass
Import brl.GLMax2D

Incbin "LockNLoad4.wav"

If Not TBass.Init(- 1, 44100, 0, Null, Null) Then
	DebugLog "Can't initialize device : " + TBass.ErrorGetCode()
	End
End If

Local stream:TBassChannel = New TBassStream.CreateMem(IncbinPtr("LockNLoad4.wav"), IncbinLen("LockNLoad4.wav"), 0)

If Not stream.Play(False) Then
		DebugLog "can't play... : "  + TBass.ErrorGetCode()
	Else
	
	Graphics 640, 480, 0
	
	
		For Local i:Int = 0 To 100
			
			stream:TBassChannel = New TBassStream.CreateMem(IncbinPtr("LockNLoad4.wav"), IncbinLen("LockNLoad4.wav"), 0)
			stream.Play(False)
			
			DrawText i, 10, 10
			Flip; Cls
			Delay 50
		next
	
	
End If

EndGraphics()

TBass.Free()

End


only i'd probably have a pool of channels.

Cheers
Charlie


Brucey(Posted 2008) [#6]
Having a rummage around the documentation... I think, for your purposes you will be better suited using a TBassSample.

You can call the function :

TBassSample.SampleMemLoad(mem:Byte Ptr, length:Int, maxPlaybacks:Int, flags:Int)

or

New TBassSample.MemLoad(mem:Byte Ptr, length:Int, maxPlaybacks:Int, flags:Int)

maxPlaybacks parameter is

Maximum number of simultaneous playbacks... 1 (min) - 65535 (max). Use one of the BASS_SAMPLE_OVER flags to choose the override decider, in the case of there being no free channel available for playback (ie. the sample is already playing max times).


You can then use GetChannel() on the sample every time you want to generate that sound, and channel.Play() it. BASS will allocate channels on the fly for you.

Yes, I really need to sort out the documentation, as there is a lot I haven't done yet.

Let BASS manage channels for you :-)
(unless you have reasons not to!)


jkrankie(Posted 2008) [#7]
so this would be better then? or do i need an array of channels?

SuperStrict

Import BaH.Bass
Import brl.GLMax2D

Incbin "CelestaDown.wav"

If Not TBass.Init(- 1, 44100, 0, Null, Null) Then
	DebugLog "Can't initialize device : " + TBass.ErrorGetCode()
	End
End If

Local stream:TBassSample = New TBassSample.MemLoad(IncbinPtr("CelestaDown.wav"), IncbinLen("CelestaDown.wav"), 65535, BASS_SAMPLE_OVER_POS)
Local channel:TBassChannel
Graphics 640, 480, 0
	
For Local i:Int = 0 To 100
			
	channel = stream.GetChannel(True)
	channel.Play(False)
	DrawText i, 10, 10
	Flip; Cls
	Delay 100
	
Next
delay 500
EndGraphics()

TBass.Free()

End


Also, have you spotted this one yet Brucey?

http://www.un4seen.com/forum/?topic=7943.0

that might be cool to wrap once it's out of beta.


Brucey(Posted 2008) [#8]
so this would be better then?

Yes.

do i need an array of channels?

You may get away with calling GetChannel + Play on demand, rather than keeping a list of them.
The channels free themselves too - or are reclaimed for re-use.