Search code examples
iosobjective-cobjective-c-blocksbass

Objective-C callback block for BASS FX


I'm trying to use the audio library BASS in my application. I'm trying to use a method which triggers a callback when it detects a beat in the music.

This is my current code:

void* (^myBlock)(DWORD, double, void *) = ^(DWORD handle, double time, void *user) {
    return nil;
};

BASS_FX_BPM_BeatDecodeGet(bpmStream, 0.0, playBackDuration, BASS_FX_BPM_BKGRND, myBlock,NULL);

The callback is defined in the header file as:

typedef void (CALLBACK BPMBEATPROC)(DWORD chan, double beatpos, void *user);

The error message is:

Passing 'void *(^)(DWORD, double, void *)' to parameter of incompatible type 'BPMBEATPROC *' (aka 'void (*)(DWORD, double, void *)')

I'm pretty sure the block only needs a small modification, but I'm not familiar with Objective-C.


Solution

  • You are passing a block that returns a void* to a parameter that expects a function pointer that returns nothing (void).

    You need to declare your block as a function returning void and pass it as the callback:

    void myFunction(DWORD handle, double time, void *user) {
        //function code here
    }
    

    And then pass it to the library just like the block:

    BASS_FX_BPM_BeatDecodeGet(bpmStream, 0.0, playBackDuration, BASS_FX_BPM_BKGRND, myFunction,NULL);
    

    For more information about function pointers, see How do function pointers in C work?.