Hello community,
some days ago, I found a way to read the spectrum of an MP3 file. And before i forget to post this again. Here it comes.
Rem Project: FFTspectrum
Rem Created: 21.11.2008 07:18:30
///////////////////////////////////////////////////////////////////
// Display
///////////////////////////////////////////////////////////////////
Set Display Mode 800,600,16
Sync Rate 0
Sync On
Disable Escapekey
Set Text Font "courier new",1
Set Text Size 13
///////////////////////////////////////////////////////////////////
// UDT to store DLL information
///////////////////////////////////////////////////////////////////
Type msx
handle As Integer
ffthandle As Integer
spectrum As Integer
lof As Integer
fft As Integer
Endtype
Global music As msx
///////////////////////////////////////////////////////////////////
// Array to store FFT-SPECTRUM information
///////////////////////////////////////////////////////////////////
Global Dim eq(63) As Float
///////////////////////////////////////////////////////////////////
// Call needed functions
///////////////////////////////////////////////////////////////////
f_LoadFMOD("tracker.mp3")
f_PlayMusic()
///////////////////////////////////////////////////////////////////
// start reading spectrum
///////////////////////////////////////////////////////////////////
Do
Cls 0
// after calling this function, all fft values are
// stored as float ( 0.0 - 1.0 ) in the eq(x) array
f_GetFFT()
// *****
For i=0 To 63
Box i*5+200,300,i*5+205,300+eq(i)*150
Text 20,i*10,Str$(i)+" "+Str$(eq(i))
Next i
Sync
If Escapekey() Then Exit
Loop
///////////////////////////////////////////////////////////////////
// stop music and delete dll
///////////////////////////////////////////////////////////////////
f_StopMusic()
End
///////////////////////////////////////////////////////////////////
// Functions
///////////////////////////////////////////////////////////////////
Function f_LoadFMOD(file$)
Load Dll "fmod.dll",1
nul=Call Dll(1,"_FSOUND_Init@12",44100,1,0)
music.handle=Call Dll(1,"_FSOUND_Stream_Open@16",file$,0,0,0)
music.lof=Call Dll(1,"_FSOUND_Stream_GetLength@4",music.handle)
Make Memblock 1,256
music.fft=Get Memblock Ptr(1)
Endfunction
Function f_StopMusic()
nul=Call Dll(1,"_FSOUND_Stream_Stop@4",music.handle)
nul=Call Dll(1,"_FSOUND_Stream_Close@4",music.handle)
nul=Call Dll(1,"_FSOUND_Close@0")
Delete Dll 1
Endfunction
Function f_PlayMusic()
nul=Call Dll(1,"_FSOUND_Stream_Play@8",0,music.handle)
music.ffthandle=Call Dll (1,"_FSOUND_DSP_GetFFTUnit@0")
nul=Call Dll (1,"_FSOUND_DSP_SetActive@8",music.ffthandle,-1)
music.spectrum=Call Dll (1,"_FSOUND_DSP_GetSpectrum@0")
Endfunction
Function f_GetFFT()
Copy Memory music.fft,music.spectrum,256
nr=0
For i=0 To 252 Step 4
eq(nr)=Memblock Float(1,i)
Inc nr,1
Next i
Endfunction
Example:
http://home.arcor.de/nasenmaus/FFTspectrum.zip