Changes between Version 2 and Version 3 of tips/c++


Ignore:
Timestamp:
12/17/06 11:54:12 (4 years ago)
Author:
bjornroche
Comment:

added sample code to C++ tip page

Legend:

Unmodified
Added
Removed
Modified
  • tips/c++

    v2 v3  
    1313}}} 
    1414 
    15 You do not need to use any special bindings to use !PortAudio in C++ code. The only difficulty is creating a callback function. The best way to do that, is to use a static function, which appears to the compiler a a normal C function, and have the static function call a non-static function. (You can assign a pointer to your class as the userData argument when creating a stream). This topic is covered on many C++ web sites, so try googling for the following terms: 
     15You do not need to use any special bindings to use !PortAudio in C++ code. The only difficulty most people have is creating a callback function. The best way to do that is to use a static function, which appears to the compiler a a normal C function, and have the static function call a non-static function. (You can assign a pointer to your class as the userData argument when creating a stream). 
    1616 
    17 function pointer c++ static 
     17For example, your .h file might look like this: 
     18 
     19{{{ 
     20class MyPortaudioClass 
     21{ 
     22[snip] 
     23 
     24int myMemberCallback(const void *input, void *output, 
     25  unsigned long frameCount, 
     26  const PaStreamCallbackTimeInfo* timeInfo, 
     27  PaStreamCallbackFlags statusFlags); 
     28 
     29static int myPaCallback( 
     30  const void *input, void *output, 
     31  unsigned long frameCount, 
     32  const PaStreamCallbackTimeInfo* timeInfo, 
     33  PaStreamCallbackFlags statusFlags, 
     34  void *userData ) 
     35{ 
     36  return ((MyPortaudioClass*)userData) 
     37     ->myMemberCallback(input, output, frameCount, timeInfo, statusFlags); 
     38} 
     39 
     40[snip] 
     41}; 
     42}}} 
     43 
     44and your .cpp file might look like this: 
     45 
     46{{{ 
     47int MyPortaudioClass::myMemberCallback(const void *input, void *output, 
     48  unsigned long frameCount, 
     49  const PaStreamCallbackTimeInfo* timeInfo, 
     50  PaStreamCallbackFlags statusFlags) 
     51{ 
     52  // Do what you need (having access to every part of MyPortaudioClass) 
     53 
     54  return paContinue; 
     55} 
     56}}} 
     57 
     58Once that's setup, you can open your stream using "this" as your userData: 
     59 
     60{{{ 
     61PaError pa_error = Pa_OpenStream( 
     62                    [snip]... 
     63                    &MyPortaudioClass::myPaCallback,      // streamCallback 
     64                    this);   // userData 
     65}}} 
     66 
     67You could also just use a reference to your class in your static function like (make sure the static function is a member of the class so 
     68that you have access to "private" declared variables): 
     69 
     70{{{ 
     71MyPortaudioClass* pThis = (MyPortaudioClass*)userData; 
     72}}} 
     73 
     74but this adds some complexity to the code. 
     75 
     76This topic is covered on many C++ web sites, so try googling for the following terms: function pointer c++ static 
     77 
     78 
     79Thanks to Robert Bielik for the sample code.