usb_midi – MIDI over USB

The usb_midi module contains classes to transmit and receive MIDI messages over USB.

usb_midi.ports :Tuple[Union[PortIn, PortOut], ...]

Tuple of all MIDI ports. Each item is ether PortIn or PortOut.

usb_midi.disable()None

Disable presenting a USB MIDI device to the host. The device is normally enabled by default, but on some boards with limited endpoints including ESP32-S2 and certain STM boards, it is disabled by default. Can be called in boot.py, before USB is connected.

usb_midi.enable()None

Enable presenting a USB MIDI device to the host. The device is enabled by default, so you do not normally need to call this function. Can be called in boot.py, before USB is connected.

If you enable too many devices at once, you will run out of USB endpoints. The number of available endpoints varies by microcontroller. CircuitPython will go into safe mode after running boot.py to inform you if not enough endpoints are available.

class usb_midi.PortIn

Receives midi commands over USB

You cannot create an instance of usb_midi.PortIn.

PortIn objects are constructed for every corresponding entry in the USB descriptor and added to the usb_midi.ports tuple.

read(self, nbytes: Optional[int] = None)Optional[bytes]

Read characters. If nbytes is specified then read at most that many bytes. Otherwise, read everything that arrives until the connection times out. Providing the number of bytes expected is highly recommended because it will be faster.

Returns

Data read

Return type

bytes or None

readinto(self, buf: _typing.WriteableBuffer, nbytes: Optional[int] = None)Optional[bytes]

Read bytes into the buf. If nbytes is specified then read at most that many bytes. Otherwise, read at most len(buf) bytes.

Returns

number of bytes read and stored into buf

Return type

bytes or None

class usb_midi.PortOut

Sends midi messages to a computer over USB

You cannot create an instance of usb_midi.PortOut.

PortOut objects are constructed for every corresponding entry in the USB descriptor and added to the usb_midi.ports tuple.

write(self, buf: _typing.ReadableBuffer)Optional[int]

Write the buffer of bytes to the bus.

Returns

the number of bytes written

Return type

int or None