Real-time audio input

BlitzMax Forums/BlitzMax Programming/Real-time audio input

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.

No, I've left the callback function just as you've written it, except for changing the multiplication factor for Buf[n] and using a variable for the for..to limit instead of 255.

All of my calculations, drawing, etc are contained in the main loop. Here's the code, so far:

Import axe.portaudio                    
Include "particles.bmx"              
'Include "explosions.bmx"
'Include "screenflash.bmx"

Global astream:Byte Ptr

''''''''''''''''''''''''' stuff missing from port audio
Extern
	Function Pa_GetDefaultOutputDeviceID:Int()
EndExtern

Const paInt8	:Int=32
Const paUInt8	:Int=64
Const paInt16	:Int=2
'''''''''''''''''''''''''' end of missing stuff

Graphics 1024, 768 
AutoMidHandle True
Global fpb:Int = 8
Global Buf:Float[fpb]
Global gscl:Float = 1
Global imgPart = LoadImage("part64.png")
Global rot:Int = 0

error( Pa_Initialize() ,"init")

' set the mix buffer running....
playstreamPA(astream,myCallback)	

Global avgBuf:Double        

'---Set the seed of the RNG
SeedRnd MilliSecs()          

'---create a timer
CreateTimer 30
gotime = 0
While WaitEvent()                

	Select EventID()                    
	
		Case EVENT_TIMERTICK 
	        avgBuf = 0
			Cls
			For Local n:Int=0 To fpb -1
	            SetColor(0,127,0)          
	            SetRotation 0
	            SetScale 1,1
	            SetAlpha 0.7
	            SetBlend ALPHABLEND                                         
				SetColor(255,0,0)  
				If gotime = 0 Then 
					TParticle.Create(512+Abs(Buf[n])*Cos(n*45+rot), 384+Abs(Buf[n])*Sin(n*45+rot), Rand(2,8)*4, 255,127,127, 0.9, 8, imgPart, Rand(0,3))     
				                                                                                                                                                
					'TParticle.Create(512+(Buf[n]), 52+728*n/fpb, Rand(1,16)*4, 255,127,127, 0.9, 4, imgPart, Rand(0,3))     
				EndIf
				avgBuf:+Abs(Buf[n])
			Next
			avgBuf = avgBuf/fpb
			TParticle.Create(0,0,avgBuf, 63,63, 255, 0.5, 4, imgPart, 1)   
			TParticle.Create(0,768,avgBuf, 63,63, 255, 0.5, 4, imgPart, 1)    
			TParticle.Create(1024,0,avgBuf, 63,63, 255, 0.5, 4, imgPart, 1)    
			TParticle.Create(1024,768,avgBuf, 63,63, 255, 0.5, 4, imgPart, 1)     
			'DrawText String(avgBuf), 0, 100
			'SetColor 0,0,avgBuf
			'SetClsColor 0,0,avgBuf
			TParticle.DrawAll
			Flip   
			TParticle.UpdateAll 
			rot :+ 2    
			gotime:+1
			If gotime = 2 gotime = 0          
			
		Case EVENT_KEYDOWN
			If EventData() = KEY_ESCAPE   
					End	
			End If      
			 
	End Select


Wend


Pa_CloseStream(astream)

End



Function myCallback:Int( inputBuffer:Float Ptr, outputBuffer:Short Ptr,..
                           framesPerBuffer:Int,outTime:Double, userData:Byte Ptr )
	For Local n:Int=0 To fpb-1
		Buf[n]=inputBuffer[0]*1024
		inputbuffer:+1  
	Next
EndFunction

                                                                                                                        


Function error(e:Int,s:String)
	If e=0 Then Return
	Pa_Terminate()
	Print "error "+e+" in "+s
	Print "message "+String.FromCString(Pa_GetErrorText(e))
	End
EndFunction	


Function playstreamPA(Stream:Byte Ptr Var,callback:Byte Ptr)

	error(.. 
	Pa_OpenStream( VarPtr Stream,Pa_GetDefaultOutputDeviceID(),1,paFloat32,Null,..
						paNoDevice,0,paInt8,Null,44100,fpb,0,0,..
			              			Callback,Null )..
	,"open stream")	
	
	error( Pa_StartStream( Stream ),"start stream" )
	Print "playing"



EndFunction