:mod:`usb_midi` =============== .. py:module:: usb_midi .. autoapi-nested-parse:: MIDI over USB The `usb_midi` module contains classes to transmit and receive MIDI messages over USB. .. data:: ports :annotation: :Tuple[Union[PortIn, PortOut], ...] Tuple of all MIDI ports. Each item is ether `PortIn` or `PortOut`. .. py:class:: 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. .. method:: 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. :return: Data read :rtype: bytes or None .. method:: readinto(self, buf: 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. :return: number of bytes read and stored into ``buf`` :rtype: bytes or None .. py:class:: 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. .. method:: write(self, buf: ReadableBuffer) -> Optional[int] Write the buffer of bytes to the bus. :return: the number of bytes written :rtype: int or None