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
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 * @code
10 * `#MIDI` ch=1 type=note_on num=60 vel=100
11 * `#MIDI` ch=1 type=cc num=74 val=127
12 * `#MIDI` ch=1 type=pb val=8192
13 * `#MIDI` type=clock
14 * @endcode
15 *
16 * Usage:
17 * @code
18 * void midi_callback(const sbl::midi::MidiEvent& event) {
19 * #ifdef SBL_MIDI_MONITOR
20 * char buf[64];
21 * sbl::midi::format_event(buf, sizeof(buf), event);
22 * log_write(buf);
23 * #endif
24 * // ... application handling ...
25 * }
26 * @endcode
27 */
28#ifndef SBL_PROTOCOL_MIDI_MONITOR_HPP_
29#define SBL_PROTOCOL_MIDI_MONITOR_HPP_
30
31#ifdef SBL_MIDI_MONITOR
32
33#include <cstddef>
36
37namespace sbl::midi {
38
39/**
40 * Format a MidiEvent as a tagged output line.
41 * Returns number of characters written (excluding null terminator).
42 */
43inline int format_event(char* buf, size_t size, const MidiEvent& e) {
44 uint8_t ch = e.channel + 1; // Convention: 1-indexed
45
46 switch (e.type) {
47 case MessageType::NoteOn:
48 return log::format(buf, size, "#MIDI ch=%u type=note_on num=%u vel=%u\r\n", ch, e.note(), e.velocity());
49 case MessageType::NoteOff:
50 return log::format(buf, size, "#MIDI ch=%u type=note_off num=%u vel=%u\r\n", ch, e.note(), e.velocity());
51 case MessageType::ControlChange:
52 return log::format(buf, size, "#MIDI ch=%u type=cc num=%u val=%u\r\n", ch, e.cc_number(), e.cc_value());
53 case MessageType::PitchBend:
54 return log::format(buf, size, "#MIDI ch=%u type=pb val=%u\r\n", ch, e.pitch_bend_raw());
55 case MessageType::ProgramChange: return log::format(buf, size, "#MIDI ch=%u type=pc val=%u\r\n", ch, e.data1);
56 case MessageType::ChannelPressure: return log::format(buf, size, "#MIDI ch=%u type=cp val=%u\r\n", ch, e.data1);
57 case MessageType::PolyPressure:
58 return log::format(buf, size, "#MIDI ch=%u type=pp num=%u val=%u\r\n", ch, e.data1, e.data2);
59
60 // System Real-Time (no channel)
61 case MessageType::Clock: return log::format(buf, size, "#MIDI type=clock\r\n");
62 case MessageType::Start: return log::format(buf, size, "#MIDI type=start\r\n");
63 case MessageType::Continue: return log::format(buf, size, "#MIDI type=continue\r\n");
64 case MessageType::Stop: return log::format(buf, size, "#MIDI type=stop\r\n");
65 case MessageType::ActiveSensing: return log::format(buf, size, "#MIDI type=active_sensing\r\n");
66 case MessageType::SystemReset:
67 return log::format(buf, size, "#MIDI type=system_reset\r\n");
68
69 // System Common
70 case MessageType::SongPosition:
71 return log::format(buf, size, "#MIDI type=song_pos val=%u\r\n",
72 (static_cast<uint16_t>(e.data2) << 7) | e.data1);
73 case MessageType::SongSelect: return log::format(buf, size, "#MIDI type=song_sel val=%u\r\n", e.data1);
74 case MessageType::TuneRequest: return log::format(buf, size, "#MIDI type=tune_req\r\n");
75
76 default: return 0;
77 }
78}
79
80} // namespace sbl::midi
81
82#endif // SBL_MIDI_MONITOR
83
84#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 drain.hpp:31
MIDI message types.