Difficulty writing a CallBack for PortAudio

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Al
Reply | Threaded
Open this post in threaded view
|

Difficulty writing a CallBack for PortAudio

Al
I am experimenting with PortAudio to record the microphone.
So far I was successfull recording using what PortAudio call Blocking Read/Write.

I am trying to implement a CallBack function for PortAudio.
So far my attempts were not successfull.
I need a little help on how to properly declare the function in Lazarus and invoke it in the OpenStream.

So far this is what I made.  But it does not compile.

Any hint/help would be appreciated

Thanks



the error is:
main_di.pas(330,28) Error: Incompatible types: got "<address of function(Pointer;Pointer;Word;PaStreamCallbackTimeInfo;LongWord;Pointer):LongInt;Register>" expected "PPaStreamCallback"

Here is the code (partial)

Function MyCallBack ( inputbuffer: Pointer; OutputBuffer : Pointer; FramesPerBuffer : Word;      timeInfo:PaStreamCallBackTimeINfo;
                      StatusFlags : PaStreamCallBAckFlags; UserData: Pointer ): Integer ;

Begin
   // the callback just copy the data in a main thread buffer defined as Array[0..NbPoints-1] of Int16 ;
    Move(inputBuffer^,UserData^,FramesPerBuffer);
    DataReady := true ;
    Result := 0 ;
End;




procedure TForm1.Button4Click(Sender: TObject);
var i: Integer    ;
     max : longint;
begin
  InputParameters.Device :=  ComboBox1.ItemIndex ;
  inputParameters.channelCount := 1 ;  //Mono
  inputParameters.sampleFormat := paInt16; // +paNonInterleaved; //paInt16;
  inputParameters.suggestedLatency := (Pa_GetDeviceInfo( inputParameters.device)^.defaultLowInputLatency); //0; (* ignored by Pa_IsFormatSupported() *)
  inputParameters.hostApiSpecificStreamInfo := nil;

   err := Pa_OpenStream(
                @Mystream,
                @inputParameters,
                Nil,
                samplerate,
                NbPoints ,
                paNoFlag, //flags that can be used to define dither, clip settings and more
                @MyCallBack, //your callback function
                Nil); //data to be passed to callback. In C++, it is frequently (void *)this

  if Err <> PaError(PaNoError) then showmessage('Open ' + Pa_GetErrorText(err));

  Err :=  Pa_StartStream(MyStream);
  if Err <> PaError(PaNoError) then showmessage('Start ' + Pa_GetErrorText(err));

  Repeat until DataReady ;  // wait for the CallBack to complete ;

  ShowMessage('DataReady was triggered') ;

  Pa_StopStream(MyStream) ;
  if Err <> PaError(PaNoError) then showmessage('STOP '+Pa_GetErrorText(err));
  //Finding the max
  max := 0 ;
  for i := 0 to NbPoints-1 do
  begin
    memo1.lines.add(Format('  i = %d ',[i]) +Inttostr(Mybuffer[i]));
    If MyBuffer[i] > Max then Max := MyBuffer[i] ;
  end;

   Memo1.lines.add(Format(' Max = %d ',[Max])) ;

 end;            
Reply | Threaded
Open this post in threaded view
|

Re: Difficulty writing a CallBack for PortAudio

fredvs
Administrator
Hello.

uos dont use the PortAudio Callback feature.

This to avoid some limitation of the method proposed by Portaudio and his callback method.

May I ask you why you need to use PortAudio Callback feature that you could not do with uos?

With uos everything is done before to give the array of sample to Portaudio (that is used only to connect to sound card).

Fre;D
Al
Reply | Threaded
Open this post in threaded view
|

Re: Difficulty writing a CallBack for PortAudio

Al
Have not found a simple way of capturing a mic to a buffer with UOS.
Can you give me the bullet points in doing so?
Reply | Threaded
Open this post in threaded view
|

Re: Difficulty writing a CallBack for PortAudio

fredvs
Administrator
There is consoleplaymemorybuffer.pas and consoleplaymemorystream.pas.

On this demo the input is a audio file but it should be the same idea if you use a mic as input.

Fre;D
Reply | Threaded
Open this post in threaded view
|

Re: Difficulty writing a CallBack for PortAudio

fredvs
Administrator
You may also save the output into a buffer with:

uos_AddIntoMemoryBuffer(PlayerIndex: cint32; outmemory: PDArFloat) : cint32;

and

uos_AddIntoMemoryStream(PlayerIndex: cint32; MemoryStream: TMemoryStream; SampleRate: LongInt;
       SampleFormat: LongInt ; Channels: LongInt; FramesCount: LongInt): LongInt;  


Fre;D

Al
Reply | Threaded
Open this post in threaded view
|

Re: Difficulty writing a CallBack for PortAudio

Al
OK if I understand the example I need to:
Uses uos_flat
Declare a TuosConsole
Create a Console which will:
-Load the libraries
-UOS_create Player
-UOS_AddfromDevIN
-UOS_AddIntoMemoryBuffer
-UOS_free

Do my stuff with the data in the memory buffer.
 
Reply | Threaded
Open this post in threaded view
|

Re: Difficulty writing a CallBack for PortAudio

fredvs
Administrator
You did not explain exactly what you wanted.

But yes, if you only want to catch the samples from the mic into a buffer (array of samples).

Dont forget to add uos_Play(PlayerIndex) at end and use uos_Stop(PlayerIndex) at end of recording.

Please take a look a simplerecorder.lpi demo.
Some way to save are explained:

   PlayerIndex1 := 0 ;
   uos_CreatePlayer(PlayerIndex1);
 
  // saving in a file using a File-Stream:  
  uos_AddIntoFile(PlayerIndex1, Pchar(filenameEdit4.filename));
 
  // saving in a Memory-Buffer:  
  // SetLength(thebuffer, 0);
  // uos_AddIntoMemoryBuffer(PlayerIndex1, @thebuffer);
 
 // saving in a Memory-Stream:  
 // if thememorystream = nil then thememorystream := tmemorystream.create;
 // uos_AddIntoMemoryStream(PlayerIndex1, (thememorystream),-1,-1,-1,-1);
   
  // saving in a file using a Menory-Stream:  
  // uos_AddIntoFileFromMem(PlayerIndex1, Pchar(filenameEdit4.filename));
   
   // uos_AddIntoFile(PlayerIndex1, Pchar(filenameEdit4.filename), -1, -1, 1, -1, -1);
   //// add a Output into wav file (save record) with custom parameters
   
   In1Index := uos_AddFromDevIn(PlayerIndex1);
   /// add Input from mic/aux into IN device with default parameters

   uos_Play(PlayerIndex1);

__________________

Imho, you should try the demos.

Fre;D
Al
Reply | Threaded
Open this post in threaded view
|

Re: Difficulty writing a CallBack for PortAudio

Al
Thank you for the info.  It is much clearer to me now.
I will  try the demos.