1 /*
2 * Copyright (C) 2018-2020 Alexandros Theodotou <alex at zrythm dot org>
3 *
4 * This file is part of Zrythm
5 *
6 * Zrythm is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Zrythm is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with Zrythm. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20 /**
21 * \file
22 *
23 * MIDI events.
24 */
25
26 #ifndef __AUDIO_MIDI_EVENT_H__
27 #define __AUDIO_MIDI_EVENT_H__
28
29 #include "zrythm-config.h"
30
31 #include <stdint.h>
32 #include <string.h>
33
34 #include "utils/types.h"
35 #include "zix/sem.h"
36
37 #ifdef HAVE_JACK
38 #include "weak_libjack.h"
39 #endif
40
41 #include <gtk/gtk.h>
42
43 typedef struct ChordDescriptor ChordDescriptor;
44
45 /**
46 * @addtogroup audio
47 *
48 * @{
49 */
50
51 /** Max events to hold in queues. */
52 #define MAX_MIDI_EVENTS 2560
53
54 /**
55 * Type of MIDI event.
56 *
57 * These are in order of precedence.
58 */
59 typedef enum MidiEventType
60 {
61 MIDI_EVENT_TYPE_PITCHBEND,
62 MIDI_EVENT_TYPE_CONTROLLER,
63 MIDI_EVENT_TYPE_NOTE_OFF,
64 MIDI_EVENT_TYPE_NOTE_ON,
65 MIDI_EVENT_TYPE_ALL_NOTES_OFF,
66
67 /** Unknown type. */
68 MIDI_EVENT_TYPE_RAW,
69 } MidiEventType;
70
71 /**
72 * Backend-agnostic MIDI event descriptor.
73 */
74 typedef struct MidiEvent
75 {
76 /** The values below will be filled in
77 * depending on what event this is. */
78 MidiEventType type;
79
80 /** -8192 to 8191. */
81 int pitchbend;
82
83 /** The controller, for control events. */
84 midi_byte_t controller;
85
86 /** Control value (also used for modulation
87 * wheel (0 ~ 127). */
88 midi_byte_t control;
89
90 /** MIDI channel, starting from 1. */
91 midi_byte_t channel;
92
93 /** Note value (0 ~ 127). */
94 midi_byte_t note_pitch;
95
96 /** Velocity (0 ~ 127). */
97 midi_byte_t velocity;
98
99 /** Time of the MIDI event, in frames from the
100 * start of the current cycle. */
101 midi_time_t time;
102
103 /** Time using g_get_monotonic_time (). */
104 gint64 systime;
105
106 /** Raw MIDI data. */
107 midi_byte_t raw_buffer[3];
108
109 size_t raw_buffer_sz;
110
111 } MidiEvent;
112
113 typedef struct MidiEvents MidiEvents;
114 typedef struct Port Port;
115
116 /**
117 * Container for passing midi events through ports.
118 * This should be passed in the data field of MIDI Ports
119 */
120 typedef struct MidiEvents
121 {
122 /** Event count. */
123 volatile int num_events;
124
125 /** Events to use in this cycle. */
126 MidiEvent events[MAX_MIDI_EVENTS];
127
128 /**
129 * For queueing events from the GUI or from ALSA
130 * at random times, since they run in different
131 * threads.
132 *
133 * Engine will copy them to the
134 * original MIDI events when ready to be processed
135 *
136 * Also has other uses.
137 */
138 MidiEvent queued_events[MAX_MIDI_EVENTS];
139 volatile int num_queued_events;
140
141 /** Semaphore for exclusive read/write. */
142 ZixSem access_sem;
143
144 } MidiEvents;
145
146 /**
147 * Used by Windows MME and RtMidi when adding events
148 * to the ring.
149 */
150 typedef struct MidiEventHeader
151 {
152 uint64_t time;
153 size_t size;
154 } MidiEventHeader;
155
156 /**
157 * Inits the MidiEvents struct.
158 */
159 void
160 midi_events_init (
161 MidiEvents * self);
162
163 /**
164 * Allocates and inits a MidiEvents struct.
165 */
166 MidiEvents *
167 midi_events_new (void);
168
169 /**
170 * Copies the members from one MidiEvent to another.
171 */
172 static inline void
midi_event_copy(MidiEvent * dest,MidiEvent * src)173 midi_event_copy (
174 MidiEvent * dest,
175 MidiEvent * src)
176 {
177 memcpy (dest, src, sizeof (MidiEvent));
178 }
179
180 NONNULL
181 void
182 midi_event_set_velocity (
183 MidiEvent * ev,
184 midi_byte_t vel);
185
186 void
187 midi_event_print (
188 const MidiEvent * ev);
189
190 int
191 midi_events_are_equal (
192 const MidiEvent * src,
193 const MidiEvent * dest);
194
195 /**
196 * Sorts the MIDI events by time ascendingly.
197 */
198 void
199 midi_events_sort_by_time (
200 MidiEvents * self);
201
202 void
203 midi_events_print (
204 MidiEvents * self,
205 const int queued);
206
207 /**
208 * Appends the events from src to dest
209 *
210 * @param queued Append queued events instead of
211 * main events.
212 * @param start_frame The start frame offset from 0
213 * in this cycle.
214 * @param nframes Number of frames to process.
215 */
216 void
217 midi_events_append (
218 /* FIXME reverse dest/src */
219 MidiEvents * src,
220 MidiEvents * dest,
221 const nframes_t start_frame,
222 const nframes_t nframes,
223 bool queued);
224
225 /**
226 * Appends the events from src to dest
227 *
228 * @param queued Append queued events instead of
229 * main events.
230 * @param channels Allowed channels (array of 16
231 * booleans).
232 * @param start_frame The start frame offset from 0
233 * in this cycle.
234 * @param nframes Number of frames to process.
235 */
236 void
237 midi_events_append_w_filter (
238 MidiEvents * src,
239 MidiEvents * dest,
240 int * channels,
241 const nframes_t start_frame,
242 const nframes_t nframes,
243 bool queued);
244
245 /**
246 * Adds a note on event to the given MidiEvents.
247 *
248 * @param channel MIDI channel starting from 1.
249 * @param queued Add to queued events instead.
250 */
251 void
252 midi_events_add_note_on (
253 MidiEvents * self,
254 midi_byte_t channel,
255 midi_byte_t note_pitch,
256 midi_byte_t velocity,
257 midi_time_t time,
258 int queued);
259
260 /**
261 * Adds a note on for each note in the chord.
262 */
263 void
264 midi_events_add_note_ons_from_chord_descr (
265 MidiEvents * self,
266 ChordDescriptor * descr,
267 midi_byte_t channel,
268 midi_byte_t velocity,
269 midi_time_t time,
270 bool queued);
271
272 /**
273 * Adds a note off for each note in the chord.
274 */
275 void
276 midi_events_add_note_offs_from_chord_descr (
277 MidiEvents * self,
278 ChordDescriptor * descr,
279 midi_byte_t channel,
280 midi_time_t time,
281 bool queued);
282
283 /**
284 * Add CC volume event.
285 *
286 * TODO
287 */
288 void
289 midi_events_add_cc_volume (
290 MidiEvents * self,
291 midi_byte_t channel,
292 midi_byte_t volume,
293 midi_time_t time,
294 bool queued);
295
296 /**
297 * Returrns if the MidiEvents have any note on
298 * events.
299 *
300 * @param check_main Check the main events.
301 * @param check_queued Check the queued events.
302 */
303 int
304 midi_events_has_note_on (
305 MidiEvents * self,
306 int check_main,
307 int check_queued);
308
309 /**
310 * Parses a MidiEvent from a raw MIDI buffer.
311 *
312 * This must be a full 3-byte message. If in
313 * 'running status' mode, the caller is responsible
314 * for prepending the status byte.
315 */
316 void
317 midi_events_add_event_from_buf (
318 MidiEvents * self,
319 midi_time_t time,
320 midi_byte_t * buf,
321 int buf_size,
322 int queued);
323
324 /**
325 * Adds a note off event to the given MidiEvents.
326 *
327 * @param channel MIDI channel starting from 1.
328 * @param queued Add to queued events instead.
329 */
330 void
331 midi_events_add_note_off (
332 MidiEvents * self,
333 midi_byte_t channel,
334 midi_byte_t note_pitch,
335 midi_time_t time,
336 int queued);
337
338 /**
339 * Adds a control event to the given MidiEvents.
340 *
341 * @param channel MIDI channel starting from 1.
342 * @param queued Add to queued events instead.
343 */
344 void
345 midi_events_add_control_change (
346 MidiEvents * self,
347 midi_byte_t channel,
348 midi_byte_t controller,
349 midi_byte_t control,
350 midi_time_t time,
351 int queued);
352
353 void
354 midi_events_add_raw (
355 MidiEvents * self,
356 uint8_t * buf,
357 size_t buf_sz,
358 midi_time_t time,
359 bool queued);
360
361 /**
362 * Adds a control event to the given MidiEvents.
363 *
364 * @param channel MIDI channel starting from 1.
365 * @param pitchbend -8192 to 8191
366 * @param queued Add to queued events instead.
367 */
368 void
369 midi_events_add_pitchbend (
370 MidiEvents * self,
371 midi_byte_t channel,
372 int pitchbend,
373 midi_time_t time,
374 int queued);
375
376 /**
377 * Queues MIDI note off to event queue.
378 */
379 void
380 midi_events_add_all_notes_off (
381 MidiEvents * self,
382 midi_byte_t channel,
383 midi_time_t time,
384 bool queued);
385
386 void
387 midi_events_panic (
388 MidiEvents * self,
389 bool queued);
390
391 /**
392 * Clears midi events.
393 *
394 * @param queued Clear queued events instead.
395 */
396 void
397 midi_events_clear (
398 MidiEvents * midi_events,
399 int queued);
400
401 /**
402 * Clears duplicates.
403 *
404 * @param queued Clear duplicates from queued events
405 * instead.
406 */
407 void
408 midi_events_clear_duplicates (
409 MidiEvents * midi_events,
410 const int queued);
411
412 /**
413 * Copies the queue contents to the original struct
414 */
415 void
416 midi_events_dequeue (
417 MidiEvents * midi_events);
418
419 /**
420 * Returns if a note on event for the given note
421 * exists in the given events.
422 */
423 int
424 midi_events_check_for_note_on (
425 MidiEvents * midi_events,
426 int note,
427 int queued);
428
429 /**
430 * Deletes the midi event with a note on signal
431 * from the queue, and returns if it deleted or not.
432 */
433 int
434 midi_events_delete_note_on (
435 MidiEvents * midi_events,
436 int note,
437 int queued);
438
439 #ifdef HAVE_JACK
440 /**
441 * Writes the events to the given JACK buffer.
442 */
443 void
444 midi_events_copy_to_jack (
445 MidiEvents * self,
446 void * buff);
447 #endif
448
449 /**
450 * Sorts the MidiEvents by time.
451 */
452 void
453 midi_events_sort (
454 MidiEvents * self,
455 const bool queued);
456
457 /**
458 * Sets the given MIDI channel on all applicable
459 * MIDI events.
460 */
461 void
462 midi_events_set_channel (
463 MidiEvents * self,
464 const int queued,
465 const midi_byte_t channel);
466
467 void
468 midi_events_delete_event (
469 MidiEvents * events,
470 const MidiEvent * ev,
471 const bool queued);
472
473 /**
474 * Frees the MIDI events.
475 */
476 void
477 midi_events_free (
478 MidiEvents * self);
479
480 /**
481 * @}
482 */
483
484 #endif
485