Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
midi.hpp
Go to the documentation of this file.
1#ifndef SBL_USB_MIDI_HPP_
2#define SBL_USB_MIDI_HPP_
3
4/**
5 * @file midi.hpp
6 * @brief USB MIDI device interface
7 *
8 * Provides bidirectional USB MIDI 1.0 via TinyUSB's MIDI device class.
9 * Operates on cable 0 (single virtual cable).
10 *
11 * RX path: tud_midi_stream_read() -> raw MIDI bytes -> feed to sbl::midi::Parser
12 * TX path: MidiEvent -> raw MIDI bytes -> tud_midi_stream_write()
13 *
14 * Output ports. A USB device has one cable, so hardware targets have one
15 * output port and `port` is always 0. A target that offers more says so
16 * through out_port_count(): the workbench gives an app several, which is what
17 * a multi-voice sequencer sends into.
18 *
19 * Requires CFG_TUD_MIDI=1 in tusb_config.h and the composite CDC+MIDI
20 * descriptor in usb_descriptors.c.
21 *
22 * Usage:
23 * @code
24 * // RX: poll in main loop (or sbl::midi::drain<sbl::usb::MidiPort>(parser))
25 * uint8_t buf[64];
26 * uint32_t n = sbl::usb::MidiPort::read(buf, sizeof(buf));
27 * parser.push(buf, n);
28 *
29 * // TX: send events
30 * sbl::usb::MidiPort::send(event);
31 * @endcode
32 */
33
34#include <cstdint>
36
37namespace sbl::usb {
38
39/**
40 * @brief USB MIDI device port (static class)
41 *
42 * @context Main loop only; not ISR-safe: the TinyUSB device stack is not
43 * reentrant.
44 */
45class MidiPort {
46public:
47 /**
48 * @brief Check if USB MIDI interface is mounted by host
49 * @return true if host has enumerated and configured the MIDI interface
50 */
51 static bool connected();
52
53 /**
54 * @brief Read decoded MIDI bytes from USB (RX)
55 *
56 * TinyUSB unpacks 4-byte USB MIDI Event Packets into standard MIDI
57 * byte stream. Feed the output directly to sbl::midi::Parser::push().
58 *
59 * @param buf Buffer to receive MIDI bytes
60 * @param max_len Maximum bytes to read
61 * @return Number of bytes read (0 if nothing available)
62 */
63 static uint32_t read(uint8_t* buf, uint32_t max_len);
64
65 /**
66 * @brief Send a MidiEvent over USB MIDI (TX)
67 *
68 * Converts the event to standard MIDI bytes and writes them via
69 * tud_midi_stream_write(), which handles USB packet framing.
70 *
71 * @param event The MIDI event to send
72 * @param port Output port, 0 to out_port_count() - 1
73 * @return true if all bytes were written, false if the buffer is full or
74 * the port does not exist
75 */
76 static bool send(const sbl::midi::MidiEvent& event, uint8_t port = 0);
77
78 /**
79 * @brief Write raw MIDI bytes to USB MIDI (TX)
80 *
81 * TinyUSB packs bytes into 4-byte USB MIDI Event Packets.
82 *
83 * @param data Raw MIDI bytes to send
84 * @param len Number of bytes
85 * @param port Output port, 0 to out_port_count() - 1
86 * @return Number of bytes written (0 if the port does not exist)
87 */
88 static uint32_t write(const uint8_t* data, uint32_t len, uint8_t port = 0);
89
90 /**
91 * @brief How many output ports this target has
92 *
93 * One on a USB device. A port existing does not mean anything is
94 * listening to it.
95 */
96 static uint8_t out_port_count();
97};
98
99} // namespace sbl::usb
100
101#endif // SBL_USB_MIDI_HPP_
USB MIDI device port (static class)
Definition midi.hpp:45
static bool connected()
Check if USB MIDI interface is mounted by host.
Definition midi.cpp:24
static uint32_t write(const uint8_t *data, uint32_t len, uint8_t port=0)
Write raw MIDI bytes to USB MIDI (TX)
Definition midi.cpp:47
static uint8_t out_port_count()
How many output ports this target has.
Definition midi.cpp:54
static uint32_t read(uint8_t *buf, uint32_t max_len)
Read decoded MIDI bytes from USB (RX)
Definition midi.cpp:26
static bool send(const sbl::midi::MidiEvent &event, uint8_t port=0)
Send a MidiEvent over USB MIDI (TX)
Definition midi.cpp:33
USB device classes.
Definition cdc.cpp:25
MIDI message types.