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

Provided rtmidi_python.cpp no longer compatible

Open CTXz opened this issue 2 years ago • 5 comments

The rtmidi_python.cpp file provided in this repository was built with an older version of Cython and will not work correctly with newer Python versions (3.9+). To fix the problem, you need to rebuild the file using the latest version of Cython.

To do this, you should first delete the rtmidi_python.cpp file. Then, run the following command to rebuild the file using Cython:

python3 setup.py install --from-cython

Note that due to the already present rtmidi_python.cpp file in the repository, running this command without deleting the file first will skip the step of building the source file using Cython. I believe this seems to miss the point of the --from-cython option.

CTXz avatar Mar 22 '23 00:03 CTXz

This project is dead you should use https://github.com/SpotlightKid/python-rtmidi instead

dosas avatar Apr 25 '24 15:04 dosas

No @dosas, this project is not "dead".

This project has been made in 2014, and has worked flawlessly since this date, used by thousands of users (in SamplerBox and other projects). The fact there have been no modification since 2014 does not mean it is dead, instead it shows that this is a very strong code that can last 10 year flawlessly without a single modification.

Also, the fact it is very lightweight (~110 lines of code : https://github.com/superquadratic/rtmidi-python/blob/master/rtmidi_python.pyx) is very good, we can easily inspect the code.

I don't know why many people consider a project dead when it is not modified over time. When a project is very simple and works well, why modify it? I have a hammer which is probably 60 year old, it works well, does not require any modification, and I don't consider it as dead, instead it is very active ;)


Now, the solution on how to make this work with Python 3.9:

apt update
apt install libasound2 libasound2-dev
pip3 install cython==0.29.37
git clone https://github.com/superquadratic/rtmidi-python.git
cd rtmidi-python
rm rtmidi_python.cpp
python3 setup.py install --from-cython

Works perfectly :)

There is also a solution with cython 3.0, I'll post it later.

josephernest avatar Apr 25 '24 18:04 josephernest

I don't know why many people consider a project dead when it is not modified over time

Because that is the definition of a dead project. Nobody maintains it and can even make the smallest change. You seem to have mixed up code quality / stability with maintainability.

has worked flawlessly since this date

No it has not it is not even installable with python3.9

apt install libasound2 libasound2-dev
pip3 install cython==0.29.37
git clone https://github.com/superquadratic/rtmidi-python.git
cd rtmidi-python
rm rtmidi_python.cpp
python3 setup.py install --from-cython

This is a dirty hack. If the project was 'alive' the maintainer would fix that and you could do a pip install or even use it as a dependency.

And there is the additional issue of cython 3.0 ...

And there are many other 'surprises' down the line:

        ********************************************************************************
        Please avoid running ``setup.py`` directly.
        Instead, use pypa/build, pypa/installer or other
        standards-based tools.

        See https://blog.ganssle.io/articles/2021/10/setup-py-deprecated.html for details.
        ********************************************************************************

Looks like there are even some python 2 vs 3 issues

In [1]: import rtmidi_python as rtmidi

In [2]: rtmidi.MidiOut()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[2], line 1
----> 1 rtmidi.MidiOut()

File ~/rtmidi-python/rtmidi_python.pyx:111, in rtmidi_python.MidiOut.__cinit__()
    109 
    110     def __cinit__(self, client_name="RtMidi Output Client"):
--> 111         self.thisptr = new RtMidiOut(UNSPECIFIED, string(<char*>client_name))
    112 
    113     def __dealloc__(self):

TypeError: expected bytes, str found

dosas avatar Apr 25 '24 19:04 dosas

No it has not it is not even installable with python3.9

It is installable with Python 3.9:

git clone https://github.com/superquadratic/rtmidi-python.git
cd rtmidi-python
rm rtmidi_python.cpp
python3 setup.py install --from-cython

Having to run these few lines instead of pip install ... is really minor, when you consider the benefit of having a lightweight library of only 120 lines of code that does the job flawlessly since 10 years.

And there are many other 'surprises' down the line: [...]

No it is not, running python3 setup.py install is perfectly fine. Fashions change over time, but we're not obliged to follow them.

josephernest avatar Apr 25 '24 19:04 josephernest

@dosas Good news, now working on Python 3.9+, including Cython 3, and no more byte/str issues:

~/rtmidi-python# python3.9
Python 3.9.19 (main, Apr  6 2024, 17:57:55)
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cython
>>> cython.__version__
'3.0.10'
>>> import rtmidi_python
>>> rtmidi_python.MidiIn()
<rtmidi_python.MidiIn object at 0x7f686261a600>
>>> rtmidi_python.MidiOut()
<rtmidi_python.MidiOut object at 0x7f6862606b30>
>>> rtmidi_python.MidiIn(b"in")
<rtmidi_python.MidiIn object at 0x7f686261a780>

Four lines have been modified in the original .pyx code (added noexcept + and "" => b"").

You can install the new version in one line with:

pip3 install git+https://github.com/SamplerBox/rtmidi-python.git

josephernest avatar Apr 26 '24 09:04 josephernest