Re: I think I got Callbacks now?

new topic     » goto parent     » topic index » view thread      » older message » newer message
Icy_Viking said...

While I am still trying to work with callbacks. I think I am slowly starting to figure them out or how to wrap them. I just want to make sure that I am doing it right.

I really wish library authors would do better to give names to parameters even when they're optional. It goes a long way towards helping consumers of those libraries understand how to use it.

The documentation could spell it out, which would be nice, but in this case it just says "Callback function which will be called each time there's audio data to process" without describing what the audio data is.

If we dig through the source code we can see that those callback parameters all have names:

typedef sfBool (*sfSoundRecorderStartCallback)(void* userData); 
typedef sfBool (*sfSoundRecorderProcessCallback)(const sfInt16* samples, size_t sampleCount, void* userData); 
typedef void   (*sfSoundRecorderStopCallback)(void* userData); 

So yes, you're pretty close. But I think knowing that the const sfInt16* parameter is called "samples" and size_t parameter is called "sampleCount" helps indicate that this is a pointer to an array.

Knowing that, the callback can peek the values from memory so it has something to work with.

public function sfSoundRecorderProcessCallback(atom psamples,atom sampleCount,atom userData) 
	-- read the samples array (Int16 is two-byte signed integer, so use peek2s) 
	sequence samples = peek2s({ psamples, sampleCount }) 
 
	-- do something with the samples here 
 
	return 1 -- onProcessSamples expects true/false response 
end function 
 
public constant sfSoundRecorderProcessCallback_cb = call_back(routine_id("sfSoundRecorderProcessCallback"),{C_POINTER,C_SIZE_T,C_POINTER},C_BOOL) 

That being what it is, one thing that's important is that a wrapper itself isn't necessarily responsible for callbacks; whoever is using the library is going to need to set those up and pass them to the function calls.

One caveat to this is that you could set up a sort-of intermediate callback "handler" that would get called by the library, perhaps to do some kind of setup or cleanup, and would then call the user's function directly.

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu