0

I'm using PortAudioSharp as a C# wrapper for PortAudio (PA). However the question is more general, so I will strip the code a bit for clarity.
PA has a callback, that is invoked when it needs new data. So opening a stream will use this function:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate PaStreamCallbackResult PaStreamCallbackDelegate(IntPtr input, IntPtr userData);

[DllImport("PortAudio.dll")]
public static PaError Pa_OpenStream(out IntPtr stream, PaStreamCallbackDelegate streamCallback, IntPtr userData);

I now have a class, that runs a decoder filling a buffer in that class. I want PA to call back a function from that class to get new data. Code is this:

class CFoo{
  private PaStreamCallbackDelegate _PaStreamCallback;
  void Open(){
      _PaStreamCallback = _ProcessNewData;
      IntPtr myStream;
      Pa_OpenStream(out myStream, _PaStreamCallback, IntPtr.Zero);
  }
  private PaStreamCallbackResult _ProcessNewData(IntPtr input, IntPtr userData){
      var buf = new byte[SIZE];
      // Fill buf ... and then:
      Marshal.Copy(buf, 0, output, buf.Length);
      return PaStreamCallbackResult.paContinue;
  }
}

This seems to work so far. Problem: Sometimes when there is more than 1 stream (1 paused, 1 running) the callback of the wrong stream gets called. I think, the delegate might be the problem.

So questions:
1) Is the above correct? Can I just pass a member function as a delegate to C++?
2) How does this work? If it were C++ instead of C# one would have to make a C detour function, that will convert the userData Pointer into a class and then call the class callback (e.g. ((CFoo*) userData)->_ProcessNewData) Does the runtime use some "magic" so the function from the right instance is called?

Flamefire
  • 5,313
  • 3
  • 35
  • 70
  • Delegates and function pointers are roughly equivalent so in theory this should work. Are you able sometimes two write to two or more streams simultaneously? – Slugart May 03 '14 at 21:22
  • If they were equivalent then this won't work. You cannot pass a function pointer to an objects method as a callback param. I think I can write to 2 streams at the same time. Got to test it though as I'm not completely sure, if it really happens at the same time. – Flamefire May 03 '14 at 22:51
  • http://stackoverflow.com/questions/2738850/are-there-function-pointers-in-c – Slugart May 04 '14 at 05:49

0 Answers0