MIDIEventLoop class

class MIDIEventLoop(port)

The event loop that watches for Chords or Sequences and other children of NoteList and calls the event handlers. Uses callbacks if the backend supports it. Otherwise an internal loop will need to be started with start() and stop().

Parameters:

port (str or mido port) – mido port. Default is “default”, which gets the 1st port from mido.get_input_names(). Also accepts strings as returned form mido.get_input_names().

Raises:
  • RunTimeError – When using the default port and but mido.get_input_names() doesn’t return any ports.
  • TypeError – When something besides a mido port or str is passed to the port parameter.
down_notes

set of notes that are currently down. Used to determine when a Chord is being pressed.

recent_notes

deque of the last n notes, n is the Sequence.maxlen() class attribute.

chord_handlers

dict of Chords mapped to a list of of handler functions.

sequence_handlers

dict of Sequences mapped to a list of handler functions.

port

mido port being used

running_handler_threads

A list of the current running handlers threads.

on_notes(notes_obj)

Decorator function similar to add_handler(). Function is spawned in a new thread.

Parameters:notes_objNoteList or child class. If a string is passed, will try to resolve to a Chord similar to the output of Chord.identify()
add_handler(func, notes_obj)

Create a new event handler that runs the function when a notes_obj is pressed

Parameters:
  • func (function) – Function to call when the chord is detected. Will be spawned in a new daemon thread.
  • notes_objNoteList or child class. If a string is passed, will try to resolve to a Chord similar to the output of Chord.identify()
clear_handlers(notes_obj=None)

Clear chord_handlers and/or sequence_handlers for a given notes_obj. Default is to clear all handlers if notes_obj is not specified.

Parameters:notes_obj – Default None. NoteList or child class. If a string is passed, will try to resolve to a Chord similar to the output of Chord.identify()
start(blocking=False)

Only required when backend doesn’t support callbacks. Start the main loop, used to process MIDI and trigger event handlers. Loop can be stopped with stop(). Warns when called while using a backend that supports callbacks.

Parameters:blocking (bool) – Default false. If true, will block until a handler calls stop() on the MIDIEventLoop object.
stop()

Only required when backend doesn’t support callbacks. Stop the main loop. Loop can be restarted with start() after being stopped. Warns when called while using a backend that supports callbacks.

static _resolve_notes_obj(notes_obj)

Typically called when another function needs to make sure a notes_obj is legal, e.g. when using it as a parameter.

Parameters:notes_obj – Default None. NoteList or child class. If a string is passed, will try to resolve to a Chord similar to the output of Chord.identify()
Returns:notes_obj
Return type:Chord or Sequence
Raises:AssertionError – When something besides a Chord or Sequence or str is passed to it.