Skip to content

🎼 midi

midi

The midi module provides a way to communicate with external synthesizers.

Cookbook

with midi.use_chan(1):
  midi.note_on(60)
  sleep(1)
  midi.note_off(60)

Reference

use_chan(chan)

Context manager to set the MIDI channel to use.

use_chan can be used to send MIDI events to a specific channel. It can be used as a context manager to set the MIDI channel to use, or as a function from within a loop.

Examples:

@loop(beats=4, track=1)
def my_loop:
    # This block will send MIDI events to channel 1.
    with midi.use_chan(1):
        midi.note_on(60)
        sleep(1)
        midi.note_off(60)

   # From this point on, MIDI events will be sent to channel 3.
   midi.use_chan(3)
   midi.note_on(60)

note(note, duration, velocity=127, chan=None)

Send the MIDI note on and off to the external synthesizer using the track id of the loop as MIDI channel.

Parameters:

Name Type Description Default
note int

The MIDI note to send.

required
duration float

The duration of the note in beats.

required
velocity int

The velocity. Defaults to 127.

127
chan int | None

The MIDI chan to send the note to.

None

Raises:

Type Description
NotInLoopException

If called from outside a loop.

note_off(note, velocity=127, chan=None)

Send the MIDI note off to the external synthesizer using the track id of the loop as MIDI channel.

Parameters:

Name Type Description Default
note int

The MIDI note to stop.

required
velocity int

The velocity. Defaults to 127.

127
chan int | None

The MIDI chan to stop the note on. Uses the value from use_chan() if not provided.

None

Raises:

Type Description
NotInLoopException

If called from outside a loop.

note_on(note, velocity=127, chan=None)

Send the MIDI note to the external synthesizer using the track id of the loop as MIDI channel. Args: note: The MIDI note to send. velocity: The velocity. Defaults to 127. chan: The MIDI chan to send the note to. Uses the value from use_chan() if not provided.

Raises:

Type Description
NotInLoopException

If called from outside a loop.