31#ifndef __AUDIO_MIDI_H__
32#define __AUDIO_MIDI_H__
34#include "zrythm-config.h"
41#include "gtk_wrapper.h"
50#define MIDI_CH1_NOTE_ON 0x90
51#define MIDI_CH1_NOTE_OFF 0x80
53#define MIDI_CH1_POLY_AFTERTOUCH 0xA0
54#define MIDI_CH1_CTRL_CHANGE 0xB0
55#define MIDI_CH1_PROG_CHANGE 0xC0
57#define MIDI_CH1_CHAN_AFTERTOUCH 0xD0
58#define MIDI_CH1_PITCH_WHEEL_RANGE 0xE0
59#define MIDI_ALL_NOTES_OFF 0x7B
60#define MIDI_ALL_SOUND_OFF 0x78
61#define MIDI_SYSTEM_MESSAGE 0xF0
62#define MIDI_SONG_POSITION 0xF2
63#define MIDI_CLOCK_START 0xFA
64#define MIDI_CLOCK_CONTINUE 0xFB
65#define MIDI_CLOCK_BEAT 0xF8
66#define MIDI_CLOCK_STOP 0xFC
67#define MIDI_META_EVENT 0xFF
110midi_note_number_to_frequency (
const uint8_t note)
112 return 440.f * powf (2.f, ((
float) note - 69.f) * (1.f / 12.f));
116midi_frequency_to_note_number (
const float freq)
118 return (uint8_t) round (
119 69.f + (12.f / logf (2.f)) * logf (freq * (1.0f / 440.f)));
129midi_get_chromatic_scale_index (
const uint8_t note)
140midi_get_octave_number (
const uint8_t note)
142 const uint8_t octave_for_middle_c = 3;
143 return note / 12 + (uint8_t) (octave_for_middle_c - 5);
151midi_is_short_message_type (
155 return (short_msg[0] & 0xf0) == type;
159midi_get_note_number (
const midi_byte_t short_msg[3])
178midi_get_note_name_with_octave (
const midi_byte_t short_msg[3],
char * buf);
183 return midi_is_short_message_type (short_msg, MIDI_CH1_NOTE_ON)
184 && midi_get_velocity (short_msg) != 0;
190 return midi_is_short_message_type (short_msg, MIDI_CH1_NOTE_OFF)
191 || (midi_is_short_message_type (short_msg, MIDI_CH1_NOTE_ON) && midi_get_velocity (short_msg) == 0);
195midi_is_program_change (
const midi_byte_t short_msg[3])
197 return midi_is_short_message_type (short_msg, MIDI_CH1_PROG_CHANGE);
201midi_get_program_change_number (
const midi_byte_t short_msg[3])
207midi_is_pitch_wheel (
const midi_byte_t short_msg[3])
209 return midi_is_short_message_type (short_msg, MIDI_CH1_PITCH_WHEEL_RANGE);
219midi_is_channel_pressure (
const midi_byte_t short_msg[3])
227 return midi_is_short_message_type (short_msg, MIDI_CH1_CTRL_CHANGE);
238static inline uint32_t
239midi_get_14_bit_value (
const midi_byte_t short_msg[3])
241 return short_msg[1] | ((uint32_t) short_msg[2] << 7);
245midi_get_channel_0_to_15 (
const midi_byte_t short_msg[3])
247 return short_msg[0] & 0x0f;
251midi_get_channel_1_to_16 (
const midi_byte_t short_msg[3])
253 return midi_get_channel_0_to_15 (short_msg) + 1u;
256static inline uint32_t
257midi_get_pitchwheel_value (
const midi_byte_t short_msg[3])
259 return midi_get_14_bit_value (short_msg);
263midi_get_aftertouch_value (
const midi_byte_t short_msg[3])
269midi_get_channel_pressure_value (
const midi_byte_t short_msg[3])
275midi_get_controller_number (
const midi_byte_t short_msg[3])
281midi_get_controller_value (
const midi_byte_t short_msg[3])
287midi_is_all_notes_off (
const midi_byte_t short_msg[3])
289 return midi_is_controller (short_msg)
290 && midi_get_controller_number (short_msg) == 123;
294midi_is_all_sound_off (
const midi_byte_t short_msg[3])
296 return midi_is_controller (short_msg)
297 && midi_get_controller_number (short_msg) == 120;
301midi_is_quarter_frame (
const midi_byte_t short_msg[3])
303 return short_msg[0] == 0xf1;
309 return short_msg[0] == 0xf8;
315 return short_msg[0] == 0xfa;
321 return short_msg[0] == 0xfb;
327 return short_msg[0] == 0xfc;
331midi_is_active_sense (
const midi_byte_t short_msg[3])
333 return short_msg[0] == 0xfe;
337midi_is_song_position_pointer (
const midi_byte_t short_msg[3])
339 return short_msg[0] == 0xf2;
342static inline uint32_t
343midi_get_song_position_pointer_value (
const midi_byte_t short_msg[3])
345 return midi_get_14_bit_value (short_msg);
349midi_get_hex_str (
const midi_byte_t * msg,
const size_t msg_sz,
char * buf);
352midi_print_to_str (
const midi_byte_t * msg,
const size_t msg_sz,
char * buf);
355midi_print (
const midi_byte_t * msg,
const size_t msg_sz);
358midi_is_short_msg (
const midi_byte_t * msg,
const size_t msg_sz)
360 g_return_val_if_fail (msg_sz > 0,
false);
365 return msg[0] != MIDI_SYSTEM_MESSAGE && msg[0] != MIDI_META_EVENT;
369midi_is_sysex (
const midi_byte_t * msg,
const size_t msg_sz)
371 return msg_sz > 1 && msg[0] == MIDI_SYSTEM_MESSAGE;
375midi_is_meta_event (
const midi_byte_t * msg,
const size_t msg_sz)
377 return msg_sz > 2 && msg[0] == MIDI_META_EVENT;
381midi_is_short_msg_meta_event (
const midi_byte_t short_msg[3])
383 return midi_is_meta_event (short_msg, 3);
387midi_is_meta_event_of_type (
392 return msg_sz > 2 && msg[1] == type && msg[0] == MIDI_META_EVENT;
396midi_get_meta_event_type (
const midi_byte_t * msg,
const size_t msg_sz)
398 g_return_val_if_fail (midi_is_meta_event (msg, msg_sz), 0);
404midi_get_meta_event_type_name (
char * buf,
const midi_byte_t type);
418midi_get_meta_event_data (
423 g_return_val_if_fail (midi_is_meta_event (msg, msg_sz), 0);
429 size_t content_len = 0;
430 size_t len_bytes = 0;
431 for (
size_t i = 2; i < msg_sz; i++)
435 content_len = (content_len << 7) | (
byte & 0x7fu);
440 if (len_bytes == 4 || len_bytes + 2 == msg_sz)
444 size_t content_start = len_bytes + 2;
446 if (content_start + content_len > msg_sz)
449 *data = &msg[content_start];
void midi_get_bytes_from_combined(const uint32_t val, midi_byte_t *lsb, midi_byte_t *msb)
Used for MIDI controls whose values are split between MSB/LSB.
int midi_get_msg_length(const uint8_t status_byte)
Returns the length of the MIDI message based on the status byte.
CONST const char * midi_get_controller_name(const midi_byte_t cc)
Return the name of the given cc (0-127).
int midi_ctrl_change_get_ch_and_description(midi_byte_t *ctrl_change, char *buf)
Saves a string representation of the given control change event in the given buffer.
CONST const char * midi_get_note_name(const midi_byte_t note)
Returns the note name (eg, "C") for a value between 0 and 127.
#define MIDI_CH1_CHAN_AFTERTOUCH
Also known as Channel Pressure.
#define MIDI_CH1_POLY_AFTERTOUCH
Also known as Polyphonic Key Pressure.
uint8_t midi_byte_t
MIDI byte.