bastibe/SoundCard

Memory Error: Cannot allocate write+execute memory for ffi.callback()

Opened this issue · 1 comments

Hi Bastian,

SoundCard has been extremely helpful in running hearing experiments in our lab. MacOS (since BigSur) and some Linux variants are unhappy with the implementation of the cffi callbacks, in that memory protection disallows these callbacks. Any call to soundcard.default_speaker().play results in the following error:

File ~/miniconda3/lib/python3.10/site-packages/soundcard/coreaudio.py:322, in _Player.__enter__(self)
    318 def __enter__(self):
    319     self._queue = collections.deque()
    321     @_ffi.callback("AURenderCallback")
--> 322     def render_callback(userdata, actionflags, timestamp,
    323                         busnumber, numframes, bufferlist):
    324         for bufferidx in range(bufferlist.mNumberBuffers):
    325             dest = bufferlist.mBuffers[bufferidx]

MemoryError: Cannot allocate write+execute memory for ffi.callback(). You might be running on a system that prevents this. For more information, see https://cffi.readthedocs.io/en/latest/using.html#callbacks

The suggested solution is to refactor the code so that it does not use ffi.callback(). The new callback style seems easy enough (using another decorator @ffi.def_extern()), but a change to the builder script is required, and I don't know what that is...

Would you be able to have a look at that? I'm happy to help in any way I can if you point me in the right direction.

Thanks for your work on this module and on SoundFile! Both have been super helpful.

Interesting! I was not aware of that. Thank you for your analysis!

To tell you the truth, I don't have much time to invest into Open Source these days. So I'd be super grateful if you could take a stab at implementing the fix yourself. I'll help you as much as I can, but I only have a scant few hours a week available for things like this.