Search code examples
c++audiosignal-processingsuperpowered

Explain C++ code from Superpowered CrossExample for Android


This code occurs in the CrossExample from superpowered.com:

static void playerEventCallbackA(void *clientData, SuperpoweredAdvancedAudioPlayerEvent event, void * __unused value) {
    if (event == SuperpoweredAdvancedAudioPlayerEvent_LoadSuccess) {

        SuperpoweredAdvancedAudioPlayer *playerA = *((SuperpoweredAdvancedAudioPlayer **)clientData);

        playerA->setBpm(126.0f);
        playerA->setFirstBeatMs(353);
        playerA->setPosition(playerA->firstBeatMs, false, false);
    };
}    

    playerA = new SuperpoweredAdvancedAudioPlayer(&playerA , playerEventCallbackA, samplerate, 0);

    playerA->open(path, fileAoffset, fileAlength);

Can anyone help me understand the first line inside the if statement? In particular, how do I interpret the right hand side of the assignment?

*((SuperpoweredAdvancedAudioPlayer **)clientData)


Solution

  • clientData is passed as a void pointer. Later, it is casted to pointer to pointer of type SuperpoweredAdvancedAudioPlayer. I suposse that clientData was of that type before passing it to the function, that's why the cast is needed. I don't know why this void* is used instead of SuperpoweredAdvancedAudioPlayer**. Not good, but not rare.

    A var named playerA is a pointer to an object of type SuperpoweredAdvancedAudioPlayer. Dereferencing the pointer to pointer you get a pointer to a SuperpoweredAdvancedAudioPlayer object, just the same type as playerA.

    Note that playerAis declared only inside the if-block, its life ends there. There's another playerA outside the block, that must be declared before assigning it to anything.