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

SDLmixer: provide Mix_ChannelFinished working example #86

Open
marcusva opened this issue Aug 23, 2015 · 3 comments
Open

SDLmixer: provide Mix_ChannelFinished working example #86

marcusva opened this issue Aug 23, 2015 · 3 comments

Comments

@marcusva
Copy link
Collaborator

Originally reported by: JP LeBreton (Bitbucket: JPLeBreton, GitHub: JPLeBreton)


I'd like to create a callback when a sound finishes playing, and it doesn't appear to work like anything else I've encountered using pysdl2 so far.

http://www.libsdl.org/projects/SDL_mixer/docs/SDL_mixer.html#SEC37

When I call Mix_ChannelFinished(my_func) where my_func is a standard Python function, I get this error:

#!python


ctypes.ArgumentError: argument 1: <class 'TypeError'>: expected CFunctionType instance instead of method

My attempt to give it a CFunctionType instance looks like this:

#!python

MYFUNCTYPE = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.POINTER(ctypes.c_int))
my_c_func = MYFUNCTYPE(my_python_func)
sdlmixer.Mix_ChannelFinished(my_c_func)

but this gives the error:

#!python

ctypes.ArgumentError: argument 1: <class 'TypeError'>: expected CFunctionType instance instead of CFunctionType

I'm sure I'm doing something very simple wrong here, but it's non-obvious enough that it would be nice to have an example of how to do this properly in pysdl2's excellent docs!


@marcusva
Copy link
Collaborator Author

Original comment by JP LeBreton (Bitbucket: JPLeBreton, GitHub: JPLeBreton):


Attached: script creating a Mix_HookMusicFinished callback for music, and a Mix_ChannelFinished for a sound effect (sample/chunk). The music callback works as expected, but the sound callback segfaults running Mix_ChannelFinished - see the comment.

@marcusva
Copy link
Collaborator Author

Original comment by Marcus von Appen (Bitbucket: marcusva, GitHub: marcusva):


Your Mix_ChannelFinished signature is wrong. It does not return a value and requires a c_int as argument

#!python

MYFUNCTYPE = ctypes.CFUNCTYPE(None, ctypes.c_int)
my_c_func = MYFUNCTYPE(my_python_func)
sdlmixer.Mix_ChannelFinished(my_c_func)

should do the trick.

@marcusva
Copy link
Collaborator Author

Original comment by JP LeBreton (Bitbucket: JPLeBreton, GitHub: JPLeBreton):


Excellent, thank you! Would you like me to clean up my above code and submit it as an example?

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

No branches or pull requests

1 participant