I'm actually coding a program in C++ using the library PortAudio.
This library is using a callback function tu manage audio input and output.
In C++, I implemented this callback function in my class "Audio" and I can't send it to Pa_OpenDefaultStream(). The compiler says "this argument is incompatible with parameter of type PaStreamCallback*" with this line :

3 Answers
3

You need to implement a plain global function with the same signature as PaStreamCallback.

It looks like PaStreamCallback takes an argument, void* userData. This is what's usually called a context argument. Without knowledge of PortAudio I guess that you can use this to represent your class instance. When you call Pa_OpenDefaultStream, pass "this" for the userData pointer, where you wrote NULL in your example.

Erik Olson's answer works great when you have the opportunity to pass in "user data" or context. If this isn't possible, you'll probably need an ugly hack to get ahold of your this pointer from within the callback. I've seen the class with the callback also have a static copy of this pointer, which would work only if you've got only one instance of the class. Ugggggly but it works.

This ugliness is an argument for all APIs to have an elegant way of dealing with this problem, ideally without breaking type safety using the reinterpret cast.