Felicia Agatha
Felicia Agatha

Reputation: 369

AttributeError in python-rtmidi sample code

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!

Upvotes: 5

Views: 3686

Answers (1)

DavidH
DavidH

Reputation: 1480

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.

Upvotes: 9

Related Questions