Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switched audio callback from function pointer to std::function #451

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ERSUCC
Copy link
Contributor

@ERSUCC ERSUCC commented Jan 20, 2025

This change allows member functions to be used as audio callbacks with std::bind, which can reduce the amount of user data that needs to be passed into the callback as an input.

@sonoro1234
Copy link
Contributor

Given that the arguments are the same, I cant see how it would "reduce the amount of user data that needs to be passed into the callback as an input.". Perhaps an example would help.
Also: C binding would be more complicated.

@ERSUCC
Copy link
Contributor Author

ERSUCC commented Jan 22, 2025

Hi Victor,

In my project, I had a bunch of audio-related state saved in a class. With the original C-style function pointers, I was not able to put the audio callback in the class as well. To get the data from the class to the callback, I had to put it into a struct and pass it into the user data parameter of the audio callback, and then I had to recreate the struct from void* in the callback itself. With std::function, I was able to bind a member function as the callback, and I was then able to directly access the audio state data without needing to use the user data parameter. It's not a huge deal, but the code is slicker and more expandable since I don't have to duplicate the necessary state in a separate user data struct. Hopefully that helped, but let me know if it still doesn't make sense; I may be misunderstanding something.

In regards to C binding: I did a quick test with the new std::function callback code and I was still able to use my old C-style callback code without changing anything. Did I just encounter a special case? I only made this PR because it seemed like it wouldn't affect any code using C-style function pointers. Also, since RtAudioErrorCallback is already an std::function, would it make sense for both callbacks to match?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants