OpenAL

BlitzMax Forums/BlitzMax Programming/OpenAL

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Dont worry I've done a little 2D example for ya.
'Import MaxMod.ModPlayer

EnableOpenALAudio()
SetAudioDriver "openal"

Graphics 800,32,0

' TSoundBuffer is like TSound in blitzmax
Local Sound:TSoundBuffer = TSoundBuffer.Load(RequestFile(""))
If Not Sound RuntimeError "unable to open buffer"

' TSoundSource is like TChannel
Local Source:TSoundSource = TSoundSource.Create(Sound,True)
If Not Source RuntimeError "unable to create source"
Source.Play()

Local Length:Int = Sound.GetLength()
Local XD:Double = 800!/Length
Local Pos:Double
Repeat
	Cls
	If MouseHit(1) Source.SetPlayPosition((Length/800!)*MouseX())
	Pos = XD*Source.GetPlayPosition()
	DrawLine(pos,0,pos,32)
	Flip
Until KeyDown(KEY_ESCAPE) Or AppTerminate()

' _________________________________________________________________________

Type TSoundBuffer

	Field AS:TAudioSample
	Field Buffer:Int

	Function Load:TSoundBuffer(Url:Object)

		Local AS:TAudioSample = LoadAudioSample(url)
		If Not AS Return Null
		
		Local This:TSoundBuffer = New TSoundBuffer
		This.AS = AS
		alGenBuffers(1, Varptr(This.buffer) )

		Local format:Int = AL_FORMAT_STEREO16
		Select AS.Format
			Case SF_MONO8		; format = AL_FORMAT_MONO8
			Case SF_STEREO8		; format = AL_FORMAT_STEREO8
			Case SF_MONO16LE	; format = AL_FORMAT_MONO16
			Case SF_STEREO16LE	; format = AL_FORMAT_STEREO16
		EndSelect
		Local size:Int = AS.Length*bytespersample[as.format]
		alBufferData(This.Buffer, format, AS.Samples, size, AS.Hertz)
		
		Return This

	EndFunction
	' _____________________________________________________________________

	Method Delete()
		alDeleteBuffers(1,Varptr(Buffer))
		AS = Null
		GCCollect()
	EndMethod
	' _____________________________________________________________________

	Method GetLength:Int()
		Local value:Int
		alGetBufferi(Buffer,AL_SIZE,Varptr(value))
		Return value/bytespersample[as.format]
	EndMethod
	' _____________________________________________________________________

	Method GetFrequency:Int()
		Local value:Int
		alGetBufferi(Buffer,AL_FREQUENCY,Varptr(value))
		Return value
	EndMethod
	' _____________________________________________________________________

	Method GetBits:Int()
		Local value:Int
		alGetBufferi(Buffer,AL_BITS,Varptr(value))
		Return value
	EndMethod
	' _____________________________________________________________________

	Method GetChannels:Int()
		Local value:Int
		alGetBufferi(Buffer,AL_Channels,Varptr(value))
		Return value
	EndMethod
	
EndType

' _________________________________________________________________________

Type TSoundSource

	Field Buf:TSoundBuffer
	Field Source:Int

	Function Create:TSoundSource(SoundBuffer:TSoundBuffer,Loop:Int=False)
		If Not SoundBuffer Return Null
		Local This:TSoundSource = New TSoundSource
		This.Buf = SoundBuffer
		alGenSources(1, Varptr(This.Source) )
	    	alSourcei(This.Source, AL_BUFFER, SoundBuffer.Buffer)
	    	This.SetLoop(Loop)
		Return This
	EndFunction
	' _____________________________________________________________________

	Method Delete()
		alDeleteSources(1,Varptr(Source))
		Buf = Null
		GCCollect()
	EndMethod
	' _____________________________________________________________________

	Method Play()
		alSourcePlay(Source)
	EndMethod
	
	Method Stop()
		alSourceStop(Source)
	EndMethod
	
	Method Pause()
		alSourcePause(Source)
	EndMethod
	
	Method Rewind()
		alSourceRewind(Source)
	EndMethod

	Method GetState:Int()
		Local value:Int
		alGetSourcei(Source,AL_SOURCE_STATE,Varptr(value))
		Return value
	EndMethod
	' _____________________________________________________________________
	
	Method SetPlayPosition(pos:Int)
		alSourcei(Source,AL_SAMPLE_OFFSET,pos)
	EndMethod
	
	Method GetPlayPosition:Int()
		Local pos:Int
		alGetSourcei(Source,AL_SAMPLE_OFFSET,Varptr(pos))
		Return pos
	EndMethod
	' _____________________________________________________________________

	Method SetGain(value:Float=1.0)
		alSourcef(Source, AL_GAIN, 1.0)
	EndMethod
	
	Method GetGain:Float()
		Local value:Float
		alGetSourcef(Source, AL_GAIN, Varptr(value))
		Return value
	EndMethod
	' _____________________________________________________________________

	Method SetPitch(value:Float=1.0)
		alSourcef(Source, AL_PITCH, 1.0)
	EndMethod
	
	Method GetPitch:Float()
		Local value:Float
		alGetSourcef(Source, AL_PITCH, Varptr(value))
		Return value
	EndMethod
	' _____________________________________________________________________

	Field Looping:Int	

	Method SetLoop(bool:Int=True)
		Looping=bool
	    	alSourcei(Source, AL_LOOPING, bool)		
	EndMethod
	
	Method GetLoop:Int()
		Return Looping
	EndMethod

EndType