Page 1 of 1

sinemouse 0.1 (how to use callbacks in FMOD)

Posted: Fri May 02, 2003 4:02 pm
by Froggerprogger
Here's a mini-sinewave-generator, whose frequence is controlled by the mouse-y-value.
It just shows how to handle a custom stream via a callback.
As mentioned in the code, you'll need the stdcall-fmod.dll and the fmod-pb-import.

Code: Select all

; >> Sinemouse 0.1
; FMOD-callback-example by Froggerprogger (02. May 03)
; You'll need the PB-Import for the fmod.dll and the fmod.dll itself in it's stdcall-version to run this code.

;____________ Declaration
Declare.l Buffercallback(hStream.l, BufferPointer.l, length.l, param.l) 
Declare mb(a.s,b.s,c.l) 
Global samplerate.l
Global bits.l
Global channels.l
Global max_samplevalue.l
Global angle.f
Global fq.l
Global mpos.POINT

;____________ Initialization
#pi = 3.14159265 
#FMOD_MONO = $20:#FMOD_STEREO = $40:#FMOD_16BIT = $10 ;so there's no need for the .res-file
samplerate = 44100 : bits = 16 : channels = 2 
If channels = 2:wavetype = #FMOD_STEREO | #FMOD_16BIT:Else:wavetype = #FMOD_MONO | #FMOD_16BIT:EndIf 
buffer_size = 2048 * Int(bits/8) * channels
max_samplevalue = Int(Pow(2,bits)/2) - 1  

FSOUND_SetBufferSize(100) ; -> de- or increase, if you want or need to
If FSOUND_Init(samplerate,1,0) = 0 : mb("","Couldn't initialize FSOUND.",0):End:EndIf 

;____________ Create and start the callback-stream
hStream.l = FSOUND_Stream_Create(@BufferCallback(), buffer_size, wavetype, samplerate, 0) 
        If hStream = 0 : mb("","Cannot create stream.",0):End:EndIf 
hChannel.l = FSOUND_Stream_Play(-1, hStream)
        If hChannel = 0 : mb("","Cannot play stream.",0):End:EndIf 

;____________ Open a window and show some data on it
hWnd = OpenWindow(1,0,0,240,20,#PB_WINDOW_SYSTEMMENU|#PB_WINDOW_SCREENCENTERED,"Sinemouse 0.1")
CreateGadgetList(hWnd)
TextGadget(1,0,0,150,20,"")
TextGadget(2,150,0,20,20,"=")
TextGadget(3,170,0,70,20,"",#PB_Text_Right)

;____________ ||: Beginning of main loop
Repeat
  SetGadgetText(1,"Y-Mouseposition: " + Str(mpos\y))
  SetGadgetText(3,Str(fq)+" Hz")
  Delay(1)
Until WindowEvent() = #PB_Event_CloseWindow
;____________ End of main loop :||


;____________ Quit
FSOUND_Stream_Stop(hStream)
FSOUND_Close()
End



;============================================ PROCEDURES ============================================

;___________ Buffer-Callback 
Procedure.l BufferCallback(*hStream.l, *BufferPointer.l, length.l, param.l)  
    sample_act=0  
    sample_last=length-1 
    bytes_per_sample = Int(bits/8) * channels

        While sample_act < sample_last
            GetCursorPos_(mpos)
            fq.l = mpos\y * 2 + 90 ; -> so frequences from 90 Hz to (screenheight*2) Hz
            angle.f + 2 * #pi * (fq / samplerate) ; -> increase radian measure for the 'complex pointer'
            If angle > 2 * #pi : angle - 2*#pi : EndIf ; -> avoid big (+ inaccurate ) floats
            signed_word.w = Int(max_samplevalue * Sin(angle)) ; -> calculate the real (sine) part of the cp

            For offset=0 To channels -1; (ok, not really stereo, just 'multichannel-mono' ;-)
              PokeW(*BufferPointer + sample_act+Int(bits/8) * offset, signed_word) ; -> poke the same actual value for each channel
            Next 
            sample_act + bytes_per_sample ; -> next sample
        Wend 
    ProcedureReturn 1 
EndProcedure 

;_________ MB (this one really spares time !)
Procedure MB(a.s , b.s, c.l)
  MessageRequester(a,b,c) 
EndProcedure 

Re: sinemouse 0.1 (how to use callbacks in FMOD)

Posted: Sat Jun 14, 2003 8:56 pm
by idStefke
Hi

This example doesn't compile with PB v3.70 and give the next
compile error:

FSOUND_SetBufferSize(100) ==> Is not an function, array, linked list

Can you please help me with an worked version

I installed FMOD.DLL into the same directory where my application is running

Kind regards
Stephane

Posted: Sat Jun 14, 2003 11:59 pm
by Jon
:D Works fine here, really cute. I may have to install this on mates computers with no window, just to annoy them.

Posted: Sun Jun 15, 2003 10:29 am
by Froggerprogger
@idStefke
Try the link above and download the PureFmod1.1 - Lib which includes fmod 3.61 (in it's stdcall-version(!)) and the PB-Import-File.

1. Copy fmod.dll in the \compilers - directory of PureBasic, because if you run a program from the editor, it will start from here.
If you release your program, you have to copy fmod.dll into the directory specified by your code .

2. Copy the file FMOD DLL IMPORT from PureBasic\PureLibraries\UserLibraries into the UserLibraries-directory on your harddisk to be able to call the fmod-API directly in your code.

3. Now it should work.

BTW: In about some weeks there will come a new fmod-import for PB for fmod 3.63. :D