45namespace zrythm::utils::midi
48static constexpr uint8_t MIDI_CH1_NOTE_ON = 0x90;
49static constexpr uint8_t MIDI_CH1_NOTE_OFF = 0x80;
51static constexpr uint8_t MIDI_CH1_POLY_AFTERTOUCH = 0xA0;
52static constexpr uint8_t MIDI_CH1_CTRL_CHANGE = 0xB0;
53static constexpr uint8_t MIDI_CH1_PROG_CHANGE = 0xC0;
55static constexpr uint8_t MIDI_CH1_CHAN_AFTERTOUCH = 0xD0;
56static constexpr uint8_t MIDI_CH1_PITCH_WHEEL_RANGE = 0xE0;
57static constexpr uint8_t MIDI_ALL_NOTES_OFF = 0x7B;
58static constexpr uint8_t MIDI_ALL_SOUND_OFF = 0x78;
59static constexpr uint8_t MIDI_SYSTEM_MESSAGE = 0xF0;
60static constexpr uint8_t MIDI_STATUS_MASK = 0xF0;
61static constexpr uint8_t MIDI_CHANNEL_MASK = 0x0F;
62static constexpr uint8_t MIDI_DATA_MASK = 0x7F;
63static constexpr uint8_t MIDI_SONG_POSITION = 0xF2;
64static constexpr uint8_t MIDI_CLOCK_START = 0xFA;
65static constexpr uint8_t MIDI_CLOCK_CONTINUE = 0xFB;
66static constexpr uint8_t MIDI_CLOCK_BEAT = 0xF8;
67static constexpr uint8_t MIDI_CLOCK_STOP = 0xFC;
68static constexpr uint8_t MIDI_META_EVENT = 0xFF;
73 return std::numeric_limits<std::uint16_t>::max ();
97std::optional<std::string>
122midi_note_number_to_frequency (
const uint8_t note)
124 return 440.f * powf (2.f, ((
float) note - 69.f) * (1.f / 12.f));
128midi_frequency_to_note_number (
const float freq)
130 return (uint8_t) round (
131 69.f + (12.f / logf (2.f)) * logf (freq * (1.0f / 440.f)));
141midi_get_chromatic_scale_index (
const uint8_t note)
152midi_get_octave_number (
const uint8_t note)
154 const uint8_t octave_for_middle_c = 3;
155 return note / 12 + (uint8_t) (octave_for_middle_c - 5);
163midi_is_short_message_type (
164 std::span<const midi_byte_t> short_msg,
167 return (short_msg[0] & 0xf0) == type;
171midi_get_note_number (std::span<const midi_byte_t> short_msg)
177midi_get_velocity (std::span<const midi_byte_t> short_msg)
190midi_get_note_name_with_octave (std::span<const midi_byte_t> short_msg);
193midi_is_note_on (std::span<const midi_byte_t> short_msg)
195 return midi_is_short_message_type (short_msg, MIDI_CH1_NOTE_ON)
196 && midi_get_velocity (short_msg) != 0;
200midi_is_note_off (std::span<const midi_byte_t> short_msg)
202 return midi_is_short_message_type (short_msg, MIDI_CH1_NOTE_OFF)
203 || (midi_is_short_message_type (short_msg, MIDI_CH1_NOTE_ON) && midi_get_velocity (short_msg) == 0);
207midi_is_program_change (std::span<const midi_byte_t> short_msg)
209 return midi_is_short_message_type (short_msg, MIDI_CH1_PROG_CHANGE);
213midi_get_program_change_number (std::span<const midi_byte_t> short_msg)
219midi_is_pitch_wheel (std::span<const midi_byte_t> short_msg)
221 return midi_is_short_message_type (short_msg, MIDI_CH1_PITCH_WHEEL_RANGE);
225midi_is_aftertouch (std::span<const midi_byte_t> short_msg)
227 return midi_is_short_message_type (short_msg, MIDI_CH1_POLY_AFTERTOUCH);
231midi_is_channel_pressure (std::span<const midi_byte_t> short_msg)
233 return midi_is_short_message_type (short_msg, MIDI_CH1_CHAN_AFTERTOUCH);
237midi_is_controller (std::span<const midi_byte_t> short_msg)
239 return midi_is_short_message_type (short_msg, MIDI_CH1_CTRL_CHANGE);
250static inline uint32_t
251midi_get_14_bit_value (std::span<const midi_byte_t> short_msg)
253 return short_msg[1] | ((uint32_t) short_msg[2] << 7);
257midi_get_channel_0_to_15 (std::span<const midi_byte_t> short_msg)
259 return short_msg[0] & 0x0f;
263midi_get_channel_1_to_16 (std::span<const midi_byte_t> short_msg)
265 return midi_get_channel_0_to_15 (short_msg) + 1u;
268static inline uint32_t
269midi_get_pitchwheel_value (std::span<const midi_byte_t> short_msg)
271 return midi_get_14_bit_value (short_msg);
275midi_get_aftertouch_value (std::span<const midi_byte_t> short_msg)
281midi_get_channel_pressure_value (std::span<const midi_byte_t> short_msg)
287midi_get_controller_number (std::span<const midi_byte_t> short_msg)
293midi_get_controller_value (std::span<const midi_byte_t> short_msg)
299midi_is_all_notes_off (std::span<const midi_byte_t> short_msg)
301 return midi_is_controller (short_msg)
302 && midi_get_controller_number (short_msg) == 123;
306midi_is_all_sound_off (std::span<const midi_byte_t> short_msg)
308 return midi_is_controller (short_msg)
309 && midi_get_controller_number (short_msg) == 120;
313midi_is_quarter_frame (std::span<const midi_byte_t> short_msg)
315 return short_msg[0] == 0xf1;
319midi_is_clock (std::span<const midi_byte_t> short_msg)
321 return short_msg[0] == 0xf8;
325midi_is_start (std::span<const midi_byte_t> short_msg)
327 return short_msg[0] == 0xfa;
331midi_is_continue (std::span<const midi_byte_t> short_msg)
333 return short_msg[0] == 0xfb;
337midi_is_stop (std::span<const midi_byte_t> short_msg)
339 return short_msg[0] == 0xfc;
343midi_is_active_sense (std::span<const midi_byte_t> short_msg)
345 return short_msg[0] == 0xfe;
349midi_is_song_position_pointer (std::span<const midi_byte_t> short_msg)
351 return short_msg[0] == 0xf2;
354static inline uint32_t
355midi_get_song_position_pointer_value (std::span<const midi_byte_t> short_msg)
357 return midi_get_14_bit_value (short_msg);
361midi_get_hex_str (std::span<const midi_byte_t> msg);
364midi_print_to_str (std::span<const midi_byte_t> msg);
367midi_print (std::span<const midi_byte_t> msg);
370midi_is_short_msg (std::span<const midi_byte_t> msg)
372 assert (!msg.empty ());
377 return msg[0] != MIDI_SYSTEM_MESSAGE && msg[0] != MIDI_META_EVENT;
381midi_is_sysex (std::span<const midi_byte_t> msg)
383 return msg.size () > 1 && msg[0] == MIDI_SYSTEM_MESSAGE;
387midi_is_meta_event (std::span<const midi_byte_t> msg)
389 return msg.size () > 2 && msg[0] == MIDI_META_EVENT;
393midi_is_short_msg_meta_event (std::span<const midi_byte_t> short_msg)
395 return midi_is_meta_event (short_msg);
399midi_is_meta_event_of_type (
400 std::span<const midi_byte_t> msg,
403 return msg.size () > 2 && msg[1] == type && msg[0] == MIDI_META_EVENT;
407midi_get_meta_event_type (std::span<const midi_byte_t> msg)
409 assert (midi_is_meta_event (msg));
428midi_get_meta_event_data (
433 assert (midi_is_meta_event (std::span (msg, msg_sz)));
439 size_t content_len = 0;
440 size_t len_bytes = 0;
441 for (
size_t i = 2; i < msg_sz; i++)
445 content_len = (content_len << 7) | (
byte & 0x7fu);
450 if (len_bytes == 4 || len_bytes + 2 == msg_sz)
454 size_t content_start = len_bytes + 2;
456 if (content_start + content_len > msg_sz)
459 *data = &msg[content_start];
std::optional< std::string > midi_ctrl_change_get_description(std::span< const midi_byte_t > ctrl_change)
Returns a string representation of the given control change event, if control change.
std::string_view midi_get_note_name(midi_byte_t note)
Returns the note name (eg, "C") for a value between 0 and 127.
int midi_get_msg_length(const uint8_t status_byte)
Returns the length of the MIDI message based on the status byte.
std::string_view midi_get_controller_name(midi_byte_t cc)
Return the name of the given cc (0-127).
std::optional< int > midi_ctrl_change_get_channel(std::span< const midi_byte_t > ctrl_change)
Returns the MIDI channel of the given control change event, if control change.
std::uint8_t midi_byte_t
MIDI byte.
void midi_get_bytes_from_combined(uint32_t val, midi_byte_t *lsb, midi_byte_t *msb)
Used for MIDI controls whose values are split between MSB/LSB.