5

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!

falsetru
  • 357,413
  • 63
  • 732
  • 636
Felicia Agatha
  • 359
  • 5
  • 11

1 Answers1

8

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:

  1. you can install the correct library by doing: pip install python-rtmidi
  2. 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.

DavidH
  • 1,420
  • 1
  • 14
  • 25
  • 1
    it's actually even more nuts than that, there's also [pyrtmidi](https://github.com/patrickkidd/pyrtmidi) which has a third, slightly different, syntax. Even worst, now both *pyrtmidi* and *python-rtmidi* use the same module name: `rtmidi`. I'm stuck finding a way for my program to differentiate between the two... – pevinkinel Jan 25 '21 at 17:56