C API: playdate_sound_synth::setGenerator() has incorrect API

typedef int (*synthRenderFunc)(void* userdata, ...);
typedef void (*synthNoteOnFunc)(void* userdata, ...);
typedef void (*synthReleaseFunc)(void* userdata, ...);
typedef int (*synthSetParameterFunc)(void* userdata, ...);
typedef void (*synthDeallocFunc)(void* userdata);

void (*setGenerator)(PDSynth* synth, synthRenderFunc* render, synthNoteOnFunc* noteOn, synthReleaseFunc* release, synthSetParameterFunc* setparam, synthDeallocFunc* dealloc, void* userdata);

Notice that render, etc. are declared as a pointer to their fn typedefs. But the typedef also defines a function pointer type.

So the function parameters are actually typed as pointers to function pointers. The correct signature should be:

void (*setGenerator)(PDSynth* synth, synthRenderFunc render, synthNoteOnFunc noteOn, synthReleaseFunc release, synthSetParameterFunc setparam, synthDeallocFunc dealloc, void* userdata);

I also am seeing the simulator crash (on windows11) no matter how I call this function, with a fn pointer or a fn pointer-pointer, as soon as I playNote() afterward. The unusual-ness here has me wondering if there's some issue, and I wonder if you could confirm that this function does indeed work through the C API?

Nope, I totally goofed this. Looks like I threw the function in as a sort of outline then forgot to fill in the details. :confused: It's crashing because I wasn't even copying those functions to the internal struct so it's calling a null value. What a mess.

That render function is what the internals look like but there's no good reason to expose all that in the user API. You should be able to just put your samples in a buffer and let the system deal with amplitude scaling. I'll fix that up and add a demo to the Examples folder, maybe a simple FM synth. I'll try and get that into the next update.

1 Like

Diving into this more, I see lots of ways to make it work better--beyond, y'know, just making it work at all and not crash. We're wrapping up the 1.10 release right now so I don't have time to do all the things I'd like to here, but I expect 1.11 won't be far behind. If you'd like to test it out before then, let me know and I can send you a beta build when I have something working.

I would love to beta test, please let me know!

I didn't wind up pushing a beta for this, obvs. Sorry! I've started a tutorial thread for setGenerator() here: pd->sound->synth->setGenerator() tutorial Hope it's helpful!