31#ifndef __AUDIO_MIDI_H__
32#define __AUDIO_MIDI_H__
34#include "zrythm-config.h"
36#include "utils/logger.h"
37#include "utils/types.h"
45namespace zrythm::utils::midi
49static constexpr uint8_t MIDI_CH1_NOTE_ON = 0x90;
50static constexpr uint8_t MIDI_CH1_NOTE_OFF = 0x80;
52static constexpr uint8_t MIDI_CH1_POLY_AFTERTOUCH = 0xA0;
53static constexpr uint8_t MIDI_CH1_CTRL_CHANGE = 0xB0;
54static constexpr uint8_t MIDI_CH1_PROG_CHANGE = 0xC0;
56static constexpr uint8_t MIDI_CH1_CHAN_AFTERTOUCH = 0xD0;
57static constexpr uint8_t MIDI_CH1_PITCH_WHEEL_RANGE = 0xE0;
58static constexpr uint8_t MIDI_ALL_NOTES_OFF = 0x7B;
59static constexpr uint8_t MIDI_ALL_SOUND_OFF = 0x78;
60static constexpr uint8_t MIDI_SYSTEM_MESSAGE = 0xF0;
61static constexpr uint8_t MIDI_SONG_POSITION = 0xF2;
62static constexpr uint8_t MIDI_CLOCK_START = 0xFA;
63static constexpr uint8_t MIDI_CLOCK_CONTINUE = 0xFB;
64static constexpr uint8_t MIDI_CLOCK_BEAT = 0xF8;
65static constexpr uint8_t MIDI_CLOCK_STOP = 0xFC;
66static constexpr uint8_t MIDI_META_EVENT = 0xFF;
71 return std::numeric_limits<std::uint16_t>::max ();
95std::optional<std::string>
120midi_note_number_to_frequency (
const uint8_t note)
122 return 440.f * powf (2.f, ((
float) note - 69.f) * (1.f / 12.f));
126midi_frequency_to_note_number (
const float freq)
128 return (uint8_t) round (
129 69.f + (12.f / logf (2.f)) * logf (freq * (1.0f / 440.f)));
139midi_get_chromatic_scale_index (
const uint8_t note)
150midi_get_octave_number (
const uint8_t note)
152 const uint8_t octave_for_middle_c = 3;
153 return note / 12 + (uint8_t) (octave_for_middle_c - 5);
161midi_is_short_message_type (
162 std::span<const midi_byte_t> short_msg,
165 return (short_msg[0] & 0xf0) == type;
169midi_get_note_number (std::span<const midi_byte_t> short_msg)
175midi_get_velocity (std::span<const midi_byte_t> short_msg)
188midi_get_note_name_with_octave (std::span<const midi_byte_t> short_msg);
191midi_is_note_on (std::span<const midi_byte_t> short_msg)
193 return midi_is_short_message_type (short_msg, MIDI_CH1_NOTE_ON)
194 && midi_get_velocity (short_msg) != 0;
198midi_is_note_off (std::span<const midi_byte_t> short_msg)
200 return midi_is_short_message_type (short_msg, MIDI_CH1_NOTE_OFF)
201 || (midi_is_short_message_type (short_msg, MIDI_CH1_NOTE_ON) && midi_get_velocity (short_msg) == 0);
205midi_is_program_change (std::span<const midi_byte_t> short_msg)
207 return midi_is_short_message_type (short_msg, MIDI_CH1_PROG_CHANGE);
211midi_get_program_change_number (std::span<const midi_byte_t> short_msg)
217midi_is_pitch_wheel (std::span<const midi_byte_t> short_msg)
219 return midi_is_short_message_type (short_msg, MIDI_CH1_PITCH_WHEEL_RANGE);
223midi_is_aftertouch (std::span<const midi_byte_t> short_msg)
225 return midi_is_short_message_type (short_msg, MIDI_CH1_POLY_AFTERTOUCH);
229midi_is_channel_pressure (std::span<const midi_byte_t> short_msg)
231 return midi_is_short_message_type (short_msg, MIDI_CH1_CHAN_AFTERTOUCH);
235midi_is_controller (std::span<const midi_byte_t> short_msg)
237 return midi_is_short_message_type (short_msg, MIDI_CH1_CTRL_CHANGE);
248static inline uint32_t
249midi_get_14_bit_value (std::span<const midi_byte_t> short_msg)
251 return short_msg[1] | ((uint32_t) short_msg[2] << 7);
255midi_get_channel_0_to_15 (std::span<const midi_byte_t> short_msg)
257 return short_msg[0] & 0x0f;
261midi_get_channel_1_to_16 (std::span<const midi_byte_t> short_msg)
263 return midi_get_channel_0_to_15 (short_msg) + 1u;
266static inline uint32_t
267midi_get_pitchwheel_value (std::span<const midi_byte_t> short_msg)
269 return midi_get_14_bit_value (short_msg);
273midi_get_aftertouch_value (std::span<const midi_byte_t> short_msg)
279midi_get_channel_pressure_value (std::span<const midi_byte_t> short_msg)
285midi_get_controller_number (std::span<const midi_byte_t> short_msg)
291midi_get_controller_value (std::span<const midi_byte_t> short_msg)
297midi_is_all_notes_off (std::span<const midi_byte_t> short_msg)
299 return midi_is_controller (short_msg)
300 && midi_get_controller_number (short_msg) == 123;
304midi_is_all_sound_off (std::span<const midi_byte_t> short_msg)
306 return midi_is_controller (short_msg)
307 && midi_get_controller_number (short_msg) == 120;
311midi_is_quarter_frame (std::span<const midi_byte_t> short_msg)
313 return short_msg[0] == 0xf1;
317midi_is_clock (std::span<const midi_byte_t> short_msg)
319 return short_msg[0] == 0xf8;
323midi_is_start (std::span<const midi_byte_t> short_msg)
325 return short_msg[0] == 0xfa;
329midi_is_continue (std::span<const midi_byte_t> short_msg)
331 return short_msg[0] == 0xfb;
335midi_is_stop (std::span<const midi_byte_t> short_msg)
337 return short_msg[0] == 0xfc;
341midi_is_active_sense (std::span<const midi_byte_t> short_msg)
343 return short_msg[0] == 0xfe;
347midi_is_song_position_pointer (std::span<const midi_byte_t> short_msg)
349 return short_msg[0] == 0xf2;
352static inline uint32_t
353midi_get_song_position_pointer_value (std::span<const midi_byte_t> short_msg)
355 return midi_get_14_bit_value (short_msg);
359midi_get_hex_str (std::span<const midi_byte_t> msg);
362midi_print_to_str (std::span<const midi_byte_t> msg);
365midi_print (std::span<const midi_byte_t> msg);
368midi_is_short_msg (std::span<const midi_byte_t> msg)
370 assert (!msg.empty ());
375 return msg[0] != MIDI_SYSTEM_MESSAGE && msg[0] != MIDI_META_EVENT;
379midi_is_sysex (std::span<const midi_byte_t> msg)
381 return msg.size () > 1 && msg[0] == MIDI_SYSTEM_MESSAGE;
385midi_is_meta_event (std::span<const midi_byte_t> msg)
387 return msg.size () > 2 && msg[0] == MIDI_META_EVENT;
391midi_is_short_msg_meta_event (std::span<const midi_byte_t> short_msg)
393 return midi_is_meta_event (short_msg);
397midi_is_meta_event_of_type (
398 std::span<const midi_byte_t> msg,
401 return msg.size () > 2 && msg[1] == type && msg[0] == MIDI_META_EVENT;
405midi_get_meta_event_type (std::span<const midi_byte_t> msg)
407 assert (midi_is_meta_event (msg));
426midi_get_meta_event_data (
431 assert (midi_is_meta_event (std::span (msg, msg_sz)));
437 size_t content_len = 0;
438 size_t len_bytes = 0;
439 for (
size_t i = 2; i < msg_sz; i++)
443 content_len = (content_len << 7) | (
byte & 0x7fu);
448 if (len_bytes == 4 || len_bytes + 2 == msg_sz)
452 size_t content_start = len_bytes + 2;
454 if (content_start + content_len > msg_sz)
457 *data = &msg[content_start];
uint8_t midi_byte_t
MIDI byte.
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.
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.