473,569 Members | 2,772 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

pyFMOD writing a callback function in Python

Hi,

I am using the FMOD audio-library with the pyFMOD python bindings. pyFMOD uses
ctypes. It is possible to register callback functions with FMOD that are
called at certain points in the processing pipeline or when certain events
happen.

I am expecially interested in the one that fires when a currently playing
stream ends. This is what the declaration in pyFMOD looks like:

_FSOUND_Stream_ SetEndCallback =
getattr(fmod,"_ FSOUND_Stream_S etEndCallback@1 2")
_FSOUND_Stream_ SetEndCallback. restype = c_byte
def FSOUND_Stream_S etEndCallback(s tream, callback, userdata):
result = _FSOUND_Stream_ SetEndCallback( c_int(stream), c_int(callback) ,
c_int(userdata) )
if not result: raise fmod_exception( )

I cannot make it work, however. I tried:

def _sound_end_call back(stream,buf ,len,userdata):
print "_sound_end_cal lback(): Stream has reached the end."

as simplest possible callback function. I am registering it like this:

pyFMOD.FSOUND_S tream_SetEndCal lback(_currentt rack,_sound_end _callback,0)

And this is how my program dies:

File "d:\projekte\ec lipse\workspace \gettone\getton esound.py", line 175, in
sound_tick
pyFMOD.FSOUND_S tream_SetEndCal lback(_currentt rack,_sound_end _callback,0)
File "c:\programme\P ython23\lib\sit e-packages\pyFMOD .py", line 690, in
FSOUND_Stream_S etEndCallback
result = _FSOUND_Stream_ SetEndCallback( c_int(stream), c_int(callback) ,
c_int(userdata) )
TypeError: int expected instead of function instance

I am very new to Python and have zero idea what the problem is nor how to
solve it. In some of my other languages I would have to explicitly make a
function pointer and possibly have to cast that to an int to pass it to
SetEndCallback, but that seems very inappropriate in Python...

Ciao, MM
--
Marian Aldenhövel, Rosenhain 23, 53123 Bonn. +49 228 624013.
http://www.marian-aldenhoevel.de
"Wir brauchen keine Opposition, wir sind bereits Demokraten."
Jul 18 '05 #1
6 2578
Marian Aldenhövel <ma****@mba-software.de> writes:
Hi,

I am using the FMOD audio-library with the pyFMOD python bindings. pyFMOD uses
ctypes.
I was looking into this recently, because another poster also asked
about pyFMOD: which FMOD version do you use? I was only able to find
fmodapi374.zip (for windows), and that version doesn't seem to work with
the pyFMOD release I found.
It is possible to register callback functions with FMOD that are
called at certain points in the processing pipeline or when certain events
happen.

I am expecially interested in the one that fires when a currently playing
stream ends. This is what the declaration in pyFMOD looks like:

_FSOUND_Stream_ SetEndCallback =
getattr(fmod,"_ FSOUND_Stream_S etEndCallback@1 2")
_FSOUND_Stream_ SetEndCallback. restype = c_byte
def FSOUND_Stream_S etEndCallback(s tream, callback, userdata):
result = _FSOUND_Stream_ SetEndCallback( c_int(stream), c_int(callback) ,
c_int(userdata) )
if not result: raise fmod_exception( )

I cannot make it work, however. I tried:

def _sound_end_call back(stream,buf ,len,userdata):
print "_sound_end_cal lback(): Stream has reached the end."

as simplest possible callback function. I am registering it like this:

pyFMOD.FSOUND_S tream_SetEndCal lback(_currentt rack,_sound_end _callback,0)

And this is how my program dies:

File "d:\projekte\ec lipse\workspace \gettone\getton esound.py", line
175, in sound_tick
pyFMOD.FSOUND_S tream_SetEndCal lback(_currentt rack,_sound_end _callback,0)
File "c:\programme\P ython23\lib\sit e-packages\pyFMOD .py", line 690,
in FSOUND_Stream_S etEndCallback
result = _FSOUND_Stream_ SetEndCallback( c_int(stream),
c_int(callback) , c_int(userdata) )
TypeError: int expected instead of function instance
The first problem is that the FSOUND_Stream_S etEndCallback function, as
given, converts all parameters to integers.
Second, you cannot pass a Python function directly as callback, you have
to create a function prototype first which specifies calling convention,
return type, and argument types.
Third, you instantiate the prototype with a Python callable, which
creates the C callable callback function, and use that in the
FSOUND_Stream_S etEndCallback call.
All in all, it should look similar (I can't test it!) to this code:

_FSOUND_Stream_ SetEndCallback = getattr(fmod,"_ FSOUND_Stream_S etEndCallback@1 2")
_FSOUND_Stream_ SetEndCallback. restype = c_byte
def FSOUND_Stream_S etEndCallback(s tream, callback, userdata):
result = _FSOUND_Stream_ SetEndCallback( c_int(stream), callback,
c_int(userdata) )
if not result: raise fmod_exception( )

# from the FMOD header file:
##typedef signed char (F_CALLBACKAPI *FSOUND_STREAMC ALLBACK)
## (FSOUND_STREAM *stream, void *buff, int len, void *userdata);

# funtion prototype
FSOUND_STREAMCA LLBACK = WINFUNCTYPE(c_c har, c_int, c_void_p, c_int, c_void_p)

# create callback function
callback_functi on = FSOUND_STREAMCA LLBACK(_sound_e nd_callback)

# and then put it to work:

FSOUND_Stream_S etEndCallback(s tream, callback_functi on, userdata)
You should also be aware that you have to keep the callback_functi on
object alive *as long as the FMOD library is using it*! If you don't,
it will probably crash.
I am very new to Python and have zero idea what the problem is nor how to
solve it. In some of my other languages I would have to explicitly make a
function pointer and possibly have to cast that to an int to pass it to
SetEndCallback, but that seems very inappropriate in Python...


Thomas
Jul 18 '05 #2
Check out pySonic, a new FMOD wrapper written with Pyrex. Much more Pythonic.

gb
Jul 18 '05 #3
Hi,
I was only able to find fmodapi374.zip (for windows), and that version doesn't
seem to work with the pyFMOD release I found.
I found that too. But I could easily fix pyFMOD to use the FMOD 374. A few of
the exports have been renamed and parameters have been added to others. As the
total size of all parameters are encoded in the exported names all of these
errors are caught when importing so you can fix them one by one by looking it
up in the FMOD-Docs.
All in all, it should look similar (I can't test it!) to this code:
I will try that when I get around to playing with the stuff again.
You should also be aware that you have to keep the callback_functi on
object alive *as long as the FMOD library is using it*!


While very obvious I am prone to forget that sometime :-).

Ciao, MM
--
Marian Aldenhövel, Rosenhain 23, 53123 Bonn. +49 228 624013.
http://www.marian-aldenhoevel.de
"Wir brauchen keine Opposition, wir sind bereits Demokraten."
Jul 18 '05 #4
Hi,
Check out pySonic, a new FMOD wrapper written with Pyrex. Much more Pythonic.


I have only found Win32-Downloads. The same is true for pyFMOD. What options
do I have to make it work on Linux?

Ciao, MM
--
Marian Aldenhövel, Rosenhain 23, 53123 Bonn. +49 228 624013.
http://www.marian-aldenhoevel.de
"Wir brauchen keine Opposition, wir sind bereits Demokraten."
Jul 18 '05 #5
Any opinion on pyFMOD vs. pySonic?

Jul 18 '05 #6
I haven't tried it on Linux but I believe it should work. FMOD works on Linux as does Pyrex.
I don't think there is any win32 specific code. Grab the source and try building it. You'll
likely have to fool with the libraries and includes in setup.py.

gb

Marian Aldenh?vel <ma****@mba-software.de> wrote:
Hi,
Check out pySonic, a new FMOD wrapper written with Pyrex. Much more Pythonic.

I have only found Win32-Downloads. The same is true for pyFMOD. What options
do I have to make it work on Linux?


Jul 18 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1008
by: Tian | last post by:
I am trying to use pyFMOD, I have installed all other supporting libraries (hopefully), such as ctypes, numarray, I also installed FMOD itself and copied its DLL files to python/DLLs. When I try to import pyFMOD in python, i got this message: >>> import pyFMOD Traceback (most recent call last): File "<pyshell#39>", line 1, in -toplevel-...
1
2392
by: Tian | last post by:
I am trying to use pyfmod in python to manipulate sound. i have installed pyfmod, ctype, numarray (if they are necessary), i also copied fmod.dll to python/DLLs as well as windows/system32, but when i tried import pyFMOD I got: Traceback (most recent call last):
10
3670
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. Andrew dalke@dalkescientific.com
0
2702
by: Bill Davy | last post by:
I am working with MSVC6 on Windows XP. I have created an MSVC project called SHIP I have a file SHIP.i with "%module SHIP" as the first line (file is below). I run SHIP.i through SWIG 1.3.24 to obtain SHIP_wrap.cpp and SHIP.py; the latter contains the line "import _SHIP". I compile SHIP_wrap.cpp and a bunch of files into a DLL which I...
267
10578
by: Xah Lee | last post by:
Python, Lambda, and Guido van Rossum Xah Lee, 2006-05-05 In this post, i'd like to deconstruct one of Guido's recent blog about lambda in Python. In Guido's blog written in 2006-02-10 at http://www.artima.com/weblogs/viewpost.jsp?thread=147358
16
1274
by: Karl Kofnarson | last post by:
Hi, while writing my last program I came upon the problem of accessing a common local variable by a bunch of functions. I wanted to have a function which would, depending on some argument, return other functions all having access to the same variable. An OO approach would do but why not try out closures... So here is a simplified example...
0
2098
by: Tim Spens | last post by:
--- On Fri, 6/27/08, Tim Spens <t_spens@yahoo.comwrote: I think I know where the problem is but I'm unsure how to fix it. When I call Register_Handler(...) from python via callback.setHandler1(callback1) this only seems to affect pythons ability to trigger an "event" in c. PyObject *Handler is always NULL even after I call...
0
7694
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8118
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5504
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.