def get_ports(port_name: str, *, clock_source: bool = False) -> Tuple[MidiIn, MidiOut]: midi_in = MidiIn() midi_out = MidiOut() midi_in_ports = midi_in.get_ports() midi_out_ports = midi_out.get_ports() try: midi_in.open_port(midi_in_ports.index(port_name)) except ValueError: raise ValueError(port_name) from None if clock_source: midi_in.ignore_types(timing=False) try: midi_out.open_port(midi_out_ports.index(port_name)) except ValueError: raise ValueError(port_name) from None return midi_in, midi_out
def read_midi(): alsa_midi = MidiIn(API_LINUX_ALSA) print(alsa_midi.get_ports()) selected_port = int(input("Select port: ")) with alsa_midi.open_port(selected_port) as open_port: timer = time.time() try: while True: message = open_port.get_message() if message: message, deltatime = message timer += deltatime print(" @%0.6f %r" % (timer, message)) print(message) except KeyboardInterrupt: print('') finally: print("Exit.")