Sound Byte Libs 29c5ff3
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#pragma once
2/**
3 * @file midi.hpp
4 * @brief USB MIDI device interface
5 *
6 * Provides bidirectional USB MIDI 1.0 via TinyUSB's MIDI device class.
7 * Operates on cable 0 (single virtual cable).
8 *
9 * RX path: tud_midi_stream_read() → raw MIDI bytes → feed to sbl::midi::Parser
10 * TX path: MidiEvent → raw MIDI bytes → tud_midi_stream_write()
11 *
12 * Requires CFG_TUD_MIDI=1 in tusb_config.h and the composite CDC+MIDI
13 * descriptor in usb_descriptors.c.
14 *
15 * Usage:
16 * // RX: poll in main loop (or use sbl::midi::poll_usb())
17 * uint8_t buf[64];
18 * uint32_t n = sbl::usb::MidiPort::read(buf, sizeof(buf));
19 * parser.push(buf, n);
20 *
21 * // TX: send events
22 * sbl::usb::MidiPort::send(event);
23 */
24
25#include <cstdint>
26#include <sbl/midi/types.hpp>
27
28namespace sbl::usb {
29
30/**
31 * @brief USB MIDI device port (static class)
32 *
33 * All methods are safe to call from main loop context.
34 * Not ISR-safe — TinyUSB device stack is not reentrant.
35 */
36class MidiPort {
37public:
38 /**
39 * @brief Check if USB MIDI interface is mounted by host
40 * @return true if host has enumerated and configured the MIDI interface
41 */
42 static bool connected();
43
44 /**
45 * @brief Read decoded MIDI bytes from USB (RX)
46 *
47 * TinyUSB unpacks 4-byte USB MIDI Event Packets into standard MIDI
48 * byte stream. Feed the output directly to sbl::midi::Parser::push().
49 *
50 * @param buf Buffer to receive MIDI bytes
51 * @param max_len Maximum bytes to read
52 * @return Number of bytes read (0 if nothing available)
53 */
54 static uint32_t read(uint8_t* buf, uint32_t max_len);
55
56 /**
57 * @brief Send a MidiEvent over USB MIDI (TX)
58 *
59 * Converts the event to standard MIDI bytes and writes them via
60 * tud_midi_stream_write(), which handles USB packet framing.
61 *
62 * @param event The MIDI event to send
63 * @return true if all bytes were written, false if buffer full
64 */
65 static bool send(const sbl::midi::MidiEvent& event);
66
67 /**
68 * @brief Write raw MIDI bytes to USB MIDI (TX)
69 *
70 * TinyUSB packs bytes into 4-byte USB MIDI Event Packets.
71 *
72 * @param data Raw MIDI bytes to send
73 * @param len Number of bytes
74 * @return Number of bytes written
75 */
76 static uint32_t write(const uint8_t* data, uint32_t len);
77};
78
79} // namespace sbl::usb
USB MIDI device port (static class)
Definition midi.hpp:36
static bool connected()
Check if USB MIDI interface is mounted by host.
Definition midi.cpp:21
static bool send(const sbl::midi::MidiEvent &event)
Send a MidiEvent over USB MIDI (TX)
Definition midi.cpp:30
static uint32_t read(uint8_t *buf, uint32_t max_len)
Read decoded MIDI bytes from USB (RX)
Definition midi.cpp:25
static uint32_t write(const uint8_t *data, uint32_t len)
Write raw MIDI bytes to USB MIDI (TX)
Definition midi.cpp:40
USB device support.
Definition cdc.cpp:24