Code: Alles auswählen
; How to use FMODEX and its FFT with PB
; needs the fmodex.dll inside the program's directory
;
; by Froggerprogger 05.08.06
;- declarations and initialisations
#FSOUND_SOFTWARE = $00000040
Declare updateFFT(Value)
Global soundfile.s
Global logscale.b, resume.b, visual.b
Global fmodsystem, channel
Global Dim FFTArray.f(512)
If OpenLibrary(0, "fmodex.dll") = 0
MessageRequester("error", "Cannot find fmodex.dll")
End
EndIf
If CallFunction(0, "FMOD_System_Create", @fmodsystem)
MessageRequester("error", "Error on FMOD_System_Create")
End
EndIf
InitSprite()
logscale = 1
visual = 1
;- open a window
hwnd = OpenWindow(1,0,0,520,265,"FMODEX | FFT-Spectrum Visualization 1.0 | by Froggerprogger", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(hwnd,4,40,512,200,0,0,0)
CreateSprite(1,512,200,0)
;- create menu + gadgets
If CallFunction(0, "FMOD_System_Init", fmodsystem, 16, 0, 0)
MessageRequester("error", "Error on FMOD_System_Init")
End
EndIf
If hwnd <> 0 And CreateMenu(1,hwnd) And CreateGadgetList(hwnd)
MenuItem(1, "play")
MenuTitle("options")
MenuItem(2, "toggle y-scale")
SetMenuItemState(1,2,logscale)
MenuItem(3, "toggle style")
SetMenuItemState(1,3,visual)
resume=1
TextGadget(1,5,10,512,20,"Music-File: "+soundfile,0)
EndIf
;- main loop
hthread=CreateThread(@updateFFT(),0)
While resume
event = WindowEvent()
If event
Select event
Case #PB_Event_Menu
Select EventMenu()
Case 1 ;(Play)
temp_filename.s = OpenFileRequester("","","PCM-MusicFiles (*.mp3, *.wav, *.mp2, *.ogg, *.raw)|*.mp3;*.wav;*.mp2;*.ogg;*.raw|*.*|*.*",0,0)
If temp_filename
soundfile.s = temp_filename
SetGadgetText(1,soundfile)
EndIf
CallFunction(0, "FMOD_System_CreateStream", fmodsystem, @soundfile, #FSOUND_SOFTWARE, 0, @hstream)
CallFunction(0, "FMOD_System_PlaySound", fmodsystem, 0, hstream, 0, @channel)
IsSoundPlaying = 1
Case 2 ;(options|logarithmic y-scale)
logscale!1
SetMenuItemState(1,2,logscale)
Case 3 ;(options|toggle style)
visual!1
SetMenuItemState(1,3,visual)
EndSelect
Case #PB_Event_CloseWindow ;(close window & exit)
resume=0
WaitThread(hthread)
CallFunction(0, "FMOD_System_Release", fmodsystem) ;shut down FSOUND
Default
EndSelect
Else
Delay(1)
EndIf
; get the current cpu-usage
CallFunction(0, "FMOD_System_GetCPUUsage", fmodsystem, 0, 0, 0, @cpu.f)
cpuusage.s="CPU: " + StrF(cpu, 3) + "%"
Wend
;- the drawing-callback
;The following procedure is called as a thread and updates the fft-visualisation
Procedure updateFFT(Value)
Shared logscale, resume, visual
Shared resume, cpuusage, IsSoundPlaying
While resume
; fill the FFT-data of the given channel into FFTArray()
CallFunction(0, "FMOD_Channel_GetSpectrum", channel, FFTArray(), 512, 0, 0)
; display the FFT-data
StartDrawing(SpriteOutput(1))
If visual = 1
Box(0,0,512,200,$000000)
For actscale=1 To 25
If logscale = 1
actdBline_y.f = Pow(0.0000001, 1 / actscale) * 199
Else
actdBline_y.f = 199-199/actscale
EndIf
Box(0,actdBline_y,511,1,RGB(150-actscale*6,150-actscale*6,150-actscale*6))
Next
Else
Box(0,0,512,200,$FF0000)
EndIf
Select visual
Case 0
If IsSoundPlaying
For i=0 To 511
If logscale = 1
actfftvalue.f = Pow(0.0000001, FFTArray(i)) * 199
Else
actfftvalue.f = 199-FFTArray(i)*199
EndIf
Box(i,0,1,1+actfftvalue,$000000+actfftvalue)
Next
EndIf
Case 1
If IsSoundPlaying
For i=0 To 511
If logscale = 1
actfftvalue.f = 199-Pow(0.0000001, FFTArray(i)) * 199
Else
actfftvalue.f = FFTArray(i)*199
EndIf
Box(i,200,1,-1-actfftvalue, (255-actfftvalue) * $000100 + actfftvalue * $000001)
Next
EndIf
EndSelect
DrawingMode(1)
FrontColor($FFFFFF)
DrawText(420 , 4, cpuusage)
StopDrawing()
FlipBuffers()
DisplaySprite(1,0,0)
; wait for 20ms
Delay(20)
Wend
EndProcedure