Sound Byte Libs 0.5.1-121-g3358a44
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
monitor.hpp
Go to the documentation of this file.
1/**
2 * @file monitor.hpp
3 * @brief MIDI event monitor — tagged output formatter
4 *
5 * Formats MidiEvent structs as tagged output lines per the SBL tagged
6 * output convention (`#TAG` key=value). Gated behind SBL_MIDI_MONITOR.
7 *
8 * Output examples:
9 * `#MIDI` ch=1 type=note_on num=60 vel=100
10 * `#MIDI` ch=1 type=cc num=74 val=127
11 * `#MIDI` ch=1 type=pb val=8192
12 * `#MIDI` type=clock
13 *
14 * Usage:
15 * void midi_callback(const sbl::midi::MidiEvent& event) {
16 * \#ifdef SBL_MIDI_MONITOR
17 * char buf[64];
18 * sbl::midi::format_event(buf, sizeof(buf), event);
19 * log_write(buf);
20 * \#endif
21 * // ... application handling ...
22 * }
23 */
24#ifndef SBL_PROTOCOL_MIDI_MONITOR_HPP_
25#define SBL_PROTOCOL_MIDI_MONITOR_HPP_
26
27#ifdef SBL_MIDI_MONITOR
28
29#include <cstddef>
32
33namespace sbl::midi {
34
35/// Format a MidiEvent as a tagged output line.
36/// Returns number of characters written (excluding null terminator).
37inline int format_event(char* buf, size_t size, const MidiEvent& e) {
38 uint8_t ch = e.channel + 1; // Convention: 1-indexed
39
40 switch (e.type) {
42 return log::format(buf, size, "#MIDI ch=%u type=note_on num=%u vel=%u\r\n",
43 ch, e.note(), e.velocity());
45 return log::format(buf, size, "#MIDI ch=%u type=note_off num=%u vel=%u\r\n",
46 ch, e.note(), e.velocity());
48 return log::format(buf, size, "#MIDI ch=%u type=cc num=%u val=%u\r\n",
49 ch, e.cc_number(), e.cc_value());
51 return log::format(buf, size, "#MIDI ch=%u type=pb val=%u\r\n",
52 ch, e.pitch_bend_raw());
54 return log::format(buf, size, "#MIDI ch=%u type=pc val=%u\r\n",
55 ch, e.data1);
57 return log::format(buf, size, "#MIDI ch=%u type=cp val=%u\r\n",
58 ch, e.data1);
60 return log::format(buf, size, "#MIDI ch=%u type=pp num=%u val=%u\r\n",
61 ch, e.data1, e.data2);
62
63 // System Real-Time (no channel)
65 return log::format(buf, size, "#MIDI type=clock\r\n");
67 return log::format(buf, size, "#MIDI type=start\r\n");
69 return log::format(buf, size, "#MIDI type=continue\r\n");
71 return log::format(buf, size, "#MIDI type=stop\r\n");
73 return log::format(buf, size, "#MIDI type=active_sensing\r\n");
75 return log::format(buf, size, "#MIDI type=system_reset\r\n");
76
77 // System Common
79 return log::format(buf, size, "#MIDI type=song_pos val=%u\r\n",
80 (static_cast<uint16_t>(e.data2) << 7) | e.data1);
82 return log::format(buf, size, "#MIDI type=song_sel val=%u\r\n", e.data1);
84 return log::format(buf, size, "#MIDI type=tune_req\r\n");
85
86 default:
87 return 0;
88 }
89}
90
91} // namespace sbl::midi
92
93#endif // SBL_MIDI_MONITOR
94
95#endif // SBL_PROTOCOL_MIDI_MONITOR_HPP_
Minimal safe string formatting for embedded systems.
int format(char *buf, size_t size, const char *fmt,...)
Format a string into a buffer (snprintf-style)
Definition format.hpp:265
MIDI parser, encoder, SMF and sequencer.
Definition input.hpp:18
MIDI message types.