I installed rtmidi for python and was able to import it. But when I tried to run the whole usage example given here: https://pypi.python.org/pypi/python-rtmidi, I got this error:
AttributeError: 'rtmidi_python.MidiOut' object has no attribute 'get_ports'
Here's the full code:
import time
import rtmidi_python as rtmidi
midiout = rtmidi.MidiOut()
available_ports = midiout.get_ports()
if available_ports:
midiout.open_port(0)
else:
midiout.open_virtual_port("My virtual output")
note_on = [0x90, 60, 112] # channel 1, middle C, velocity 112
note_off = [0x80, 60, 0]
midiout.send_message(note_on)
time.sleep(0.5)
midiout.send_message(note_off)
del midiout
I modified the code a little bit in the import part, because somehow it doesn't work when I put import rtmidi
but works when I put import rtmidi_python
.
I'm using Python 3.5. Any help will be appreciated, thanks!
The reason you're having trouble is that you are running sample code for python-rtmidi, but you installed rtmidi-python. I kid you not, these are two separate libraries that do the same thing with almost the same interface. It's nuts! You have two options:
pip install python-rtmidi
you can modify your code so that it works with rtmidi-python
as follows:
import time
import rtmidi_python as rtmidi
midiout = rtmidi.MidiOut()
available_ports = midiout.ports
if available_ports:
midiout.open_port(0)
else:
midiout.open_virtual_port("My virtual output")
note_on = [0x90, 60, 112] # channel 1, middle C, velocity 112
note_off = [0x80, 60, 0]
midiout.send_message(note_on)
time.sleep(0.5)
midiout.send_message(note_off)
del midiout
You see: instead of doing get_ports()
, you simply references the ports
attribute.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With