Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 995
Felicia Agatha Avatar asked Jul 03 '16 02:07

Felicia Agatha


1 Answers

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.

like image 77
DavidH Avatar answered Sep 28 '22 08:09

DavidH