Zrythm v2.0.0-DEV
a highly automated and intuitive digital audio workstation
Loading...
Searching...
No Matches
midi.h
Go to the documentation of this file.
1// clang-format off
2// SPDX-FileCopyrightText: © 2018-2024 Alexandros Theodotou <alex@zrythm.org>
3// SPDX-License-Identifier: LicenseRef-ZrythmLicense
4/*
5 * This file incorporates work covered by the following copyright and
6 * permission notice:
7 *
8 * ---
9 *
10 * CHOC is (C)2021 Tracktion Corporation, and is offered under the terms of the ISC license:
11 *
12 * Permission to use, copy, modify, and/or distribute this software for any purpose with or
13 * without fee is hereby granted, provided that the above copyright notice and this permission
14 * notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
15 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
16 * AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
17 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
18 * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
19 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 *
21 * ---
22 */
23// clang-format on
24
30
31#pragma once
32
33#include <cassert>
34#include <cmath>
35#include <cstddef>
36#include <cstdint>
37#include <limits>
38#include <optional>
39#include <span>
40#include <string>
41
43using midi_byte_t = std::uint8_t;
44
45namespace zrythm::utils::midi
46{
47/* see http://www.onicos.com/staff/iz/formats/midi-event.html */
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_SONG_POSITION = 0xF2;
61static constexpr uint8_t MIDI_CLOCK_START = 0xFA;
62static constexpr uint8_t MIDI_CLOCK_CONTINUE = 0xFB;
63static constexpr uint8_t MIDI_CLOCK_BEAT = 0xF8;
64static constexpr uint8_t MIDI_CLOCK_STOP = 0xFC;
65static constexpr uint8_t MIDI_META_EVENT = 0xFF;
66
67constexpr size_t
68midi_max_sysex_size ()
69{
70 return std::numeric_limits<std::uint16_t>::max ();
71}
72
76[[gnu::const]]
77std::string_view
79
87void
89
94std::optional<std::string>
95midi_ctrl_change_get_description (std::span<const midi_byte_t> ctrl_change);
96
104std::optional<int>
105midi_ctrl_change_get_channel (std::span<const midi_byte_t> ctrl_change);
106
111int
112midi_get_msg_length (const uint8_t status_byte);
113
118static inline float
119midi_note_number_to_frequency (const uint8_t note)
120{
121 return 440.f * powf (2.f, ((float) note - 69.f) * (1.f / 12.f));
122}
123
124static inline uint8_t
125midi_frequency_to_note_number (const float freq)
126{
127 return (uint8_t) round (
128 69.f + (12.f / logf (2.f)) * logf (freq * (1.0f / 440.f)));
129}
130
137static inline uint8_t
138midi_get_chromatic_scale_index (const uint8_t note)
139{
140 return note % 12;
141}
142
148static inline uint8_t
149midi_get_octave_number (const uint8_t note)
150{
151 const uint8_t octave_for_middle_c = 3;
152 return note / 12 + (uint8_t) (octave_for_middle_c - 5);
153}
154
159static inline bool
160midi_is_short_message_type (
161 std::span<const midi_byte_t> short_msg,
162 const midi_byte_t type)
163{
164 return (short_msg[0] & 0xf0) == type;
165}
166
167static inline midi_byte_t
168midi_get_note_number (std::span<const midi_byte_t> short_msg)
169{
170 return short_msg[1];
171}
172
173static inline midi_byte_t
174midi_get_velocity (std::span<const midi_byte_t> short_msg)
175{
176 return short_msg[2];
177}
178
182[[gnu::const]]
183std::string_view
185
186std::string
187midi_get_note_name_with_octave (std::span<const midi_byte_t> short_msg);
188
189static inline bool
190midi_is_note_on (std::span<const midi_byte_t> short_msg)
191{
192 return midi_is_short_message_type (short_msg, MIDI_CH1_NOTE_ON)
193 && midi_get_velocity (short_msg) != 0;
194}
195
196static inline bool
197midi_is_note_off (std::span<const midi_byte_t> short_msg)
198{
199 return midi_is_short_message_type (short_msg, MIDI_CH1_NOTE_OFF)
200 || (midi_is_short_message_type (short_msg, MIDI_CH1_NOTE_ON) && midi_get_velocity (short_msg) == 0);
201}
202
203static inline bool
204midi_is_program_change (std::span<const midi_byte_t> short_msg)
205{
206 return midi_is_short_message_type (short_msg, MIDI_CH1_PROG_CHANGE);
207}
208
209static inline midi_byte_t
210midi_get_program_change_number (std::span<const midi_byte_t> short_msg)
211{
212 return short_msg[1];
213}
214
215static inline bool
216midi_is_pitch_wheel (std::span<const midi_byte_t> short_msg)
217{
218 return midi_is_short_message_type (short_msg, MIDI_CH1_PITCH_WHEEL_RANGE);
219}
220
221static inline bool
222midi_is_aftertouch (std::span<const midi_byte_t> short_msg)
223{
224 return midi_is_short_message_type (short_msg, MIDI_CH1_POLY_AFTERTOUCH);
225}
226
227static inline bool
228midi_is_channel_pressure (std::span<const midi_byte_t> short_msg)
229{
230 return midi_is_short_message_type (short_msg, MIDI_CH1_CHAN_AFTERTOUCH);
231}
232
233static inline bool
234midi_is_controller (std::span<const midi_byte_t> short_msg)
235{
236 return midi_is_short_message_type (short_msg, MIDI_CH1_CTRL_CHANGE);
237}
238
247static inline uint32_t
248midi_get_14_bit_value (std::span<const midi_byte_t> short_msg)
249{
250 return short_msg[1] | ((uint32_t) short_msg[2] << 7);
251}
252
253static inline midi_byte_t
254midi_get_channel_0_to_15 (std::span<const midi_byte_t> short_msg)
255{
256 return short_msg[0] & 0x0f;
257}
258
259static inline midi_byte_t
260midi_get_channel_1_to_16 (std::span<const midi_byte_t> short_msg)
261{
262 return midi_get_channel_0_to_15 (short_msg) + 1u;
263}
264
265static inline uint32_t
266midi_get_pitchwheel_value (std::span<const midi_byte_t> short_msg)
267{
268 return midi_get_14_bit_value (short_msg);
269}
270
271static inline midi_byte_t
272midi_get_aftertouch_value (std::span<const midi_byte_t> short_msg)
273{
274 return short_msg[2];
275}
276
277static inline midi_byte_t
278midi_get_channel_pressure_value (std::span<const midi_byte_t> short_msg)
279{
280 return short_msg[1];
281}
282
283static inline midi_byte_t
284midi_get_controller_number (std::span<const midi_byte_t> short_msg)
285{
286 return short_msg[1];
287}
288
289static inline midi_byte_t
290midi_get_controller_value (std::span<const midi_byte_t> short_msg)
291{
292 return short_msg[2];
293}
294
295static inline bool
296midi_is_all_notes_off (std::span<const midi_byte_t> short_msg)
297{
298 return midi_is_controller (short_msg)
299 && midi_get_controller_number (short_msg) == 123;
300}
301
302static inline bool
303midi_is_all_sound_off (std::span<const midi_byte_t> short_msg)
304{
305 return midi_is_controller (short_msg)
306 && midi_get_controller_number (short_msg) == 120;
307}
308
309static inline bool
310midi_is_quarter_frame (std::span<const midi_byte_t> short_msg)
311{
312 return short_msg[0] == 0xf1;
313}
314
315static inline bool
316midi_is_clock (std::span<const midi_byte_t> short_msg)
317{
318 return short_msg[0] == 0xf8;
319}
320
321static inline bool
322midi_is_start (std::span<const midi_byte_t> short_msg)
323{
324 return short_msg[0] == 0xfa;
325}
326
327static inline bool
328midi_is_continue (std::span<const midi_byte_t> short_msg)
329{
330 return short_msg[0] == 0xfb;
331}
332
333static inline bool
334midi_is_stop (std::span<const midi_byte_t> short_msg)
335{
336 return short_msg[0] == 0xfc;
337}
338
339static inline bool
340midi_is_active_sense (std::span<const midi_byte_t> short_msg)
341{
342 return short_msg[0] == 0xfe;
343}
344
345static inline bool
346midi_is_song_position_pointer (std::span<const midi_byte_t> short_msg)
347{
348 return short_msg[0] == 0xf2;
349}
350
351static inline uint32_t
352midi_get_song_position_pointer_value (std::span<const midi_byte_t> short_msg)
353{
354 return midi_get_14_bit_value (short_msg);
355}
356
357std::string
358midi_get_hex_str (std::span<const midi_byte_t> msg);
359
360std::string
361midi_print_to_str (std::span<const midi_byte_t> msg);
362
363void
364midi_print (std::span<const midi_byte_t> msg);
365
366static inline bool
367midi_is_short_msg (std::span<const midi_byte_t> msg)
368{
369 assert (!msg.empty ());
370
371 if (msg.size () > 3)
372 return false;
373
374 return msg[0] != MIDI_SYSTEM_MESSAGE && msg[0] != MIDI_META_EVENT;
375}
376
377static inline bool
378midi_is_sysex (std::span<const midi_byte_t> msg)
379{
380 return msg.size () > 1 && msg[0] == MIDI_SYSTEM_MESSAGE;
381}
382
383static inline bool
384midi_is_meta_event (std::span<const midi_byte_t> msg)
385{
386 return msg.size () > 2 && msg[0] == MIDI_META_EVENT;
387}
388
389static inline bool
390midi_is_short_msg_meta_event (std::span<const midi_byte_t> short_msg)
391{
392 return midi_is_meta_event (short_msg);
393}
394
395static inline bool
396midi_is_meta_event_of_type (
397 std::span<const midi_byte_t> msg,
398 const midi_byte_t type)
399{
400 return msg.size () > 2 && msg[1] == type && msg[0] == MIDI_META_EVENT;
401}
402
403static inline midi_byte_t
404midi_get_meta_event_type (std::span<const midi_byte_t> msg)
405{
406 assert (midi_is_meta_event (msg));
407 return msg[1];
408}
409
410std::string
411midi_get_meta_event_type_name (midi_byte_t type);
412
424static inline size_t
425midi_get_meta_event_data (
426 const midi_byte_t ** data,
427 const midi_byte_t * msg,
428 const size_t msg_sz)
429{
430 assert (midi_is_meta_event (std::span (msg, msg_sz)));
431
432 /* malformed data */
433 if (msg_sz < 4)
434 return 0;
435
436 size_t content_len = 0;
437 size_t len_bytes = 0;
438 for (size_t i = 2; i < msg_sz; i++)
439 {
440 const midi_byte_t byte = msg[i];
441 len_bytes++;
442 content_len = (content_len << 7) | (byte & 0x7fu);
443
444 if (byte < 0x80)
445 break;
446
447 if (len_bytes == 4 || len_bytes + 2 == msg_sz)
448 return 0; /* malformed data */
449 }
450
451 size_t content_start = len_bytes + 2;
452
453 if (content_start + content_len > msg_sz)
454 return 0; /* malformed data */
455
456 *data = &msg[content_start];
457 return content_len;
458}
459
460} // namespace zrythm::utils::midi
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.
Definition midi.h:43
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.