Sound Byte Libs 29c5ff3
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#pragma once
25
26#ifdef SBL_MIDI_MONITOR
27
28#include <cstddef>
29#include <sbl/midi/types.hpp>
30#include <sbl/log/format.hpp>
31
32namespace sbl::midi {
33
34/// Format a MidiEvent as a tagged output line.
35/// Returns number of characters written (excluding null terminator).
36inline int format_event(char* buf, size_t size, const MidiEvent& e) {
37 uint8_t ch = e.channel + 1; // Convention: 1-indexed
38
39 switch (e.type) {
41 return log::format(buf, size, "#MIDI ch=%u type=note_on num=%u vel=%u\r\n",
42 ch, e.note(), e.velocity());
44 return log::format(buf, size, "#MIDI ch=%u type=note_off num=%u vel=%u\r\n",
45 ch, e.note(), e.velocity());
47 return log::format(buf, size, "#MIDI ch=%u type=cc num=%u val=%u\r\n",
48 ch, e.cc_number(), e.cc_value());
50 return log::format(buf, size, "#MIDI ch=%u type=pb val=%u\r\n",
51 ch, e.pitch_bend_raw());
53 return log::format(buf, size, "#MIDI ch=%u type=pc val=%u\r\n",
54 ch, e.data1);
56 return log::format(buf, size, "#MIDI ch=%u type=cp val=%u\r\n",
57 ch, e.data1);
59 return log::format(buf, size, "#MIDI ch=%u type=pp num=%u val=%u\r\n",
60 ch, e.data1, e.data2);
61
62 // System Real-Time (no channel)
64 return log::format(buf, size, "#MIDI type=clock\r\n");
66 return log::format(buf, size, "#MIDI type=start\r\n");
68 return log::format(buf, size, "#MIDI type=continue\r\n");
70 return log::format(buf, size, "#MIDI type=stop\r\n");
72 return log::format(buf, size, "#MIDI type=active_sensing\r\n");
74 return log::format(buf, size, "#MIDI type=system_reset\r\n");
75
76 // System Common
78 return log::format(buf, size, "#MIDI type=song_pos val=%u\r\n",
79 (static_cast<uint16_t>(e.data2) << 7) | e.data1);
81 return log::format(buf, size, "#MIDI type=song_sel val=%u\r\n", e.data1);
83 return log::format(buf, size, "#MIDI type=tune_req\r\n");
84
85 default:
86 return 0;
87 }
88}
89
90} // namespace sbl::midi
91
92#endif // SBL_MIDI_MONITOR
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 protocol support.
Definition input.hpp:17