python-soundfile icon indicating copy to clipboard operation
python-soundfile copied to clipboard

ABI mode to API mode transition on cffi

Open ayghri opened this issue 3 years ago • 12 comments
trafficstars

Build error on win32 due to the declaration: """ SNDFILE* sf_wchar_open (const wchar_t *wpath, int mode, SF_INFO *sfinfo); """ Perhaps it should be removed.

Tested on Macbook Pro M1 and works with no issues.

Find wheel here: https://github.com/aghriss/python-soundfile/releases/tag/0.11.1

ayghri avatar Jun 08 '22 21:06 ayghri

I've accidentally removed _SoundFileInfo

ayghri avatar Jun 08 '22 22:06 ayghri

Could you give a summary of what you are doing here?

bastibe avatar Jun 21 '22 07:06 bastibe

  1. In soundfile.py
  • Added library import:
from _soundfile import lib
  • Replaced decorator :
 @_ffi.callback("?")

with:

 @_ffi.def_extern()
  • added the decorated methods as part of "lib" (`lib.decorared_method)
    self._virtual_io = {'get_filelen': lib.vio_get_filelen,
                        'seek': lib.vio_seek,
                        'read': lib.vio_read,
                        'write': lib.vio_write,
                        'tell': lib.vio_tell}
  1. In soundfile_build.py,
  • Replaced:
ffibuilder.set_source("_soundfile", None)

with :

ffibuilder.set_source('_soundfile', '''
#include <sndfile.h>
''', libraries=['sndfile'])
  • Added the extern declarations to ffibuilder.cdef
'''
extern "Python" sf_count_t  vio_get_filelen (void *user_data) ;
extern "Python" sf_count_t  vio_seek        (sf_count_t offset, int whence, void *user_data) ;
extern "Python" sf_count_t  vio_read        (void *ptr, sf_count_t count, void *user_data) ;
extern "Python" sf_count_t  vio_write       (const void *ptr, sf_count_t count, void *user_data) ;
extern "Python" sf_count_t  vio_tell        (void *user_data) ;
'''

Any other changes can be ignored. There was a problem compiling for win32 (see my first comment)

ayghri avatar Jun 26 '22 00:06 ayghri

Thank you for providing a prototype implementation of the ABI mode. It seems that there is some more work to do to get the test suite to run correctly with this change. To be frank, I currently have very little time to invest into this. I'll try to come back to it once 0.11 has been released.

bastibe avatar Jun 28 '22 09:06 bastibe

it seems the issue comes from the sndfile.h import. I'm guessing it's a path issue or sndfile library is not installed.

ayghri avatar Jul 03 '22 18:07 ayghri

Probably. Would you like to try to fix it?

bastibe avatar Jul 04 '22 06:07 bastibe

I'll work on it this weekend, god willing.

ayghri avatar Jul 05 '22 20:07 ayghri

Great! Please don't feel pressured to rush. It's completely acceptable for things to take time.

bastibe avatar Jul 06 '22 11:07 bastibe

Is there a way to solve the "ffi.callback() cause MemoryError on macOS M1" and allow soundfile to continue to use the ABI mode of CFFI? The CPython and PyPy ecosystems are rapidly evolving. The ABI mode allows a soundfile release to work with new CPython and PyPy versions released later. A large community of users depend on soundfile, and the owner of the soundfile project (@bastibe) may not always have time to release new builds quickly.

hiccup7 avatar Jul 12 '22 15:07 hiccup7

I got it to work on Ubuntu and macOS. https://github.com/aghriss/python-soundfile/actions/runs/2770259758 I'm having difficulty editing Windows includes to add sndfile.h

ayghri avatar Jul 31 '22 17:07 ayghri

Thank you for working on this! Compiling on Windows is always a headache.

bastibe avatar Aug 02 '22 06:08 bastibe

The thing I'm stuck with is how to add include paths for a Windows action. I was able to install libsndfile using choco. I guess I'll try to do it on a local machine and see how to tweak things.

ayghri avatar Aug 07 '22 03:08 ayghri