-
Notifications
You must be signed in to change notification settings - Fork 0
/
midislide.c
626 lines (534 loc) · 20.5 KB
/
midislide.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
/*
* Copyright (C) 2018 taylor.fish <contact@taylor.fish>
*
* This file is part of Midislide.
*
* Midislide is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Midislide is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Midislide. If not, see <http://www.gnu.org/licenses/>.
*/
#include "midislide.h"
#include <math.h>
#include <string.h>
#include <stdio.h>
#pragma GCC diagnostic ignored "-Wunused-parameter"
#define PLUGIN_URI "https://taylor.fish/plugins/midislide"
/* Forward declarations */
static const LV2_Atom_Event *run_body(
MidiSlide *plugin, uint32_t n_samples,
const LV2_Atom_Event *start_event, uint32_t frames,
uint32_t output_capacity);
static inline void handle_atom_object(
MidiSlide *plugin, const LV2_Atom_Object *object);
static inline MidiAction get_midi_action(const uint8_t *message);
static inline bool handle_midi_message(
MidiSlide *plugin, const uint8_t *message, MidiAction action,
uint32_t output_capacity);
static inline void compact_stack(MidiSlide *plugin);
static inline void move_primary_to_stack_top(
MidiSlide *plugin, uint8_t start_at);
static inline void set_bend(
MidiSlide *plugin, int value, uint32_t frames,
uint32_t output_capacity);
static inline bool set_bend_from_slide(
MidiSlide *plugin, uint8_t key, uint8_t velocity, uint8_t base_key,
uint32_t frames, uint32_t output_capacity);
static inline void set_bend_from_key(
MidiSlide *plugin, uint8_t key, uint32_t frames,
uint32_t output_capacity);
static inline int relative_key_to_bend(
MidiSlide *plugin, double relative_key);
static inline void stop_note(
MidiSlide *plugin, uint32_t frames, uint32_t output_capacity);
static inline void play_note(
MidiSlide *plugin, uint8_t key, uint8_t velocity, uint32_t frames,
uint32_t output_capacity);
static inline void send_midi_message(
MidiSlide *plugin, MidiEvent *event, uint32_t output_capacity);
static inline void handle_note_on(
MidiSlide *plugin, uint8_t key, uint8_t velocity);
static inline void handle_note_off(MidiSlide *plugin, uint8_t key);
static inline void handle_all_notes_off(MidiSlide *plugin);
static inline void add_to_stack(
MidiSlide *plugin, uint8_t key, uint8_t velocity);
static inline void remove_from_stack(MidiSlide *plugin, uint8_t key);
static inline void clear_stack(MidiSlide *plugin);
/* End forward declarations */
static inline void map_uris(LV2_URID_Map *map, MidiSlideURIs *uris) {
uris->midi_Event = map->map(map->handle, LV2_MIDI__MidiEvent);
uris->atom_Float = map->map(map->handle, LV2_ATOM__Float);
uris->time_Position = map->map(map->handle, LV2_TIME__Position);
uris->time_beatsPerMinute = map->map(
map->handle, LV2_TIME__beatsPerMinute
);
uris->atom_Object = map->map(map->handle, LV2_ATOM__Object);
uris->atom_Blank = map->map(map->handle, LV2_ATOM__Blank);
uris->atom_Resource = map->map(map->handle, LV2_ATOM__Resource);
}
static LV2_Handle instantiate(
const LV2_Descriptor *descriptor, double rate, const char *bundle_path,
const LV2_Feature * const *features) {
LV2_URID_Map *map = NULL;
for (size_t i = 0; features[i] != NULL; i++) {
if (strcmp(features[i]->URI, LV2_URID_URI "#map") == 0) {
map = (LV2_URID_Map *)features[i]->data;
}
}
if (map == NULL) {
fprintf(stderr, "Host does not support urid:map.\n");
return NULL;
}
MidiSlide *plugin = calloc(1, sizeof(MidiSlide));
if (plugin == NULL) {
fprintf(stderr, "Not enough memory to allocate plugin.\n");
return NULL;
}
plugin->map = map;
plugin->sample_rate = rate;
map_uris(map, &plugin->uris);
return (LV2_Handle)plugin;
}
static void connect_port(LV2_Handle instance, uint32_t port, void *data) {
MidiSlide *plugin = (MidiSlide *)instance;
switch (port) {
case PORT_INPUT:
plugin->input = data;
break;
case PORT_OUTPUT:
plugin->output = data;
break;
case PORT_BEAT_DIVISOR:
plugin->beat_divisor = data;
break;
case PORT_BEND_SEMITONE_DISTANCE:
plugin->bend_semitone_distance = data;
break;
case PORT_FORCED_VELOCITY:
plugin->forced_velocity = data;
break;
}
}
static void activate(LV2_Handle instance) {
MidiSlide *plugin = (MidiSlide *)instance;
plugin->note_stack_size = 0;
plugin->samples_per_beat = plugin->sample_rate / 2;
plugin->samples_passed = 0;
plugin->samples_since_sent = 0;
plugin->message_interval = plugin->sample_rate / 500;
plugin->is_sliding = false;
}
static inline bool isAtomObject(uint32_t type, MidiSlideURIs *uris) {
return (
type == uris->atom_Object ||
type == uris->atom_Blank ||
type == uris->atom_Resource
);
}
static inline const uint8_t *getMidiMessage(
const LV2_Atom_Event *event, MidiSlideURIs *uris) {
if (event->body.type != uris->midi_Event) return NULL;
return (const uint8_t *)(event + 1);
}
static inline const LV2_Atom_Object *getAtomObject(
const LV2_Atom_Event *event, MidiSlideURIs *uris) {
if (!isAtomObject(event->body.type, uris)) return NULL;
return (const LV2_Atom_Object *)&event->body;
}
static void run(LV2_Handle instance, uint32_t n_samples) {
MidiSlide *plugin = (MidiSlide *)instance;
const uint32_t output_capacity = plugin->output[0].atom.size;
lv2_atom_sequence_clear(plugin->output);
plugin->output->atom.type = plugin->input->atom.type;
const LV2_Atom_Sequence *input = plugin->input;
const LV2_Atom_Event *event = lv2_atom_sequence_begin(&input->body);
uint32_t last_frames = 0;
while (!lv2_atom_sequence_is_end(&input->body, input->atom.size, event)) {
uint32_t frames = event->time.frames;
uint32_t frame_diff = frames - last_frames;
last_frames = frames;
event = run_body(plugin, frame_diff, event, frames, output_capacity);
}
if (last_frames < n_samples) {
run_body(
plugin, n_samples - last_frames, event, last_frames,
output_capacity
);
}
}
static const LV2_Atom_Event *run_body(
MidiSlide *plugin, uint32_t n_samples,
const LV2_Atom_Event *start_event, uint32_t frames,
uint32_t output_capacity) {
const LV2_Atom_Sequence *input = plugin->input;
MidiNote *note_stack = plugin->note_stack;
uint8_t old_stack_size = plugin->note_stack_size;
uint8_t old_slide_base, old_slide_top = 0;
if (old_stack_size >= 2) {
old_slide_base = note_stack[old_stack_size - 2].key;
old_slide_top = note_stack[old_stack_size - 1].key;
}
const LV2_Atom_Event *event;
// Loop through events and handle atom objects and all MIDI messages except
// "note on" messages.
for (event = start_event;
!lv2_atom_sequence_is_end(&input->body, input->atom.size, event) && (
event->time.frames <= frames
); event = lv2_atom_sequence_next(event)) {
const LV2_Atom_Object *object = getAtomObject(event, &plugin->uris);
if (object != NULL) {
handle_atom_object(plugin, object);
continue;
}
const uint8_t *midi_message = getMidiMessage(event, &plugin->uris);
if (midi_message != NULL) {
MidiAction action = get_midi_action(midi_message);
if (action == ACTION_NOTE_ON) continue;
bool handled = handle_midi_message(
plugin, midi_message, action, output_capacity
);
if (!handled) {
// Forward unchanged MIDI event.
lv2_atom_sequence_append_event(
plugin->output, output_capacity, event
);
}
continue;
}
}
compact_stack(plugin);
// Whether or not a "note off" message should be sent.
bool note_stopped = old_stack_size > 0 && plugin->note_stack_size == 0;
bool force_bend_update = note_stopped || (old_stack_size >= 2 && (
plugin->note_stack_size < 2 ||
old_slide_base != note_stack[plugin->note_stack_size - 2].key ||
old_slide_top != note_stack[plugin->note_stack_size - 1].key
));
if (force_bend_update) plugin->is_sliding = false;
old_stack_size = plugin->note_stack_size;
// Loop through events and handle MIDI "note on" messages.
for (event = start_event;
!lv2_atom_sequence_is_end(&input->body, input->atom.size, event) && (
event->time.frames <= frames
); event = lv2_atom_sequence_next(event)) {
const uint8_t *midi_message = getMidiMessage(event, &plugin->uris);
if (midi_message == NULL) continue;
if (get_midi_action(midi_message) != ACTION_NOTE_ON) continue;
handle_midi_message(
plugin, midi_message, ACTION_NOTE_ON, output_capacity
);
}
bool note_started = old_stack_size == 0 && plugin->note_stack_size > 0;
// If multiple notes were added at the same time, pick the one with the
// lowest velocity and move it to the top (end) of the stack.
move_primary_to_stack_top(plugin, old_stack_size);
if (plugin->note_stack_size > old_stack_size) {
// At least one note was added.
plugin->samples_passed = 0;
force_bend_update = true;
if (plugin->note_stack_size >= 2) {
plugin->is_sliding = true;
}
}
plugin->samples_since_sent += n_samples;
while (force_bend_update ||
plugin->samples_since_sent >= plugin->message_interval) {
bool old_force_update = force_bend_update;
force_bend_update = false;
if (!old_force_update) {
plugin->samples_since_sent -= plugin->message_interval;
}
if (old_force_update && note_stopped) {
stop_note(plugin, frames, output_capacity);
}
if (plugin->note_stack_size == 0) continue;
if (old_force_update && note_started) {
MidiNote *note;
if (plugin->note_stack_size >= 2) {
note = ¬e_stack[plugin->note_stack_size - 2];
} else {
note = ¬e_stack[0];
}
uint8_t key = note->key;
uint8_t velocity = note->velocity;
set_bend(plugin, 0, frames, output_capacity);
play_note(plugin, key, velocity, frames, output_capacity);
continue;
}
if (old_force_update && !plugin->is_sliding) {
uint8_t key = note_stack[plugin->note_stack_size - 1].key;
set_bend_from_key(plugin, key, frames, output_capacity);
continue;
}
if (!plugin->is_sliding) continue;
MidiNote *note = ¬e_stack[plugin->note_stack_size - 1];
bool continue_slide = set_bend_from_slide(
plugin, note->key, note->velocity, (note - 1)->key, frames,
output_capacity
);
if (!continue_slide) plugin->is_sliding = false;
if (old_force_update) {
} else {
if (plugin->is_sliding) {
plugin->samples_passed += plugin->message_interval;
}
}
}
return event;
}
static inline void move_primary_to_stack_top(
MidiSlide *plugin, uint8_t start_at) {
uint8_t note_stack_size = plugin->note_stack_size;
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-overflow"
if (note_stack_size <= 1) return;
if (start_at > note_stack_size - 2) return;
#pragma GCC diagnostic pop
MidiNote *note_stack = plugin->note_stack;
uint8_t min_vel_index = note_stack_size - 1;
uint8_t min_vel = note_stack[min_vel_index].velocity;
for (int i = min_vel_index; i >= start_at; i--) {
uint8_t velocity = note_stack[i].velocity;
if (velocity < min_vel) {
min_vel = velocity;
min_vel_index = (uint8_t)i;
}
}
if (min_vel_index < note_stack_size - 1) {
MidiNote temp = note_stack[note_stack_size - 1];
note_stack[note_stack_size - 1] = note_stack[min_vel_index];
note_stack[min_vel_index] = temp;
uint8_t *key_to_pos = plugin->key_to_stack_pos;
key_to_pos[note_stack[note_stack_size - 1].key] = note_stack_size - 1;
key_to_pos[note_stack[min_vel_index].key] = min_vel_index;
}
}
static inline bool set_bend_from_slide(
MidiSlide *plugin, uint8_t key, uint8_t velocity, uint8_t base_key,
uint32_t frames, uint32_t output_capacity) {
float beat_divisor = *plugin->beat_divisor;
uint32_t samples_per_beat = plugin->samples_per_beat;
uint32_t slide_duration = (samples_per_beat * velocity) / beat_divisor;
uint32_t samples_passed = plugin->samples_passed;
if (samples_passed > slide_duration * 2) return false;
if (samples_passed > slide_duration) {
samples_passed = 2 * slide_duration - samples_passed;
}
int key_diff = (int)key - base_key;
int key_offset = (int)base_key - plugin->key_playing;
if (abs(key_offset) > *plugin->bend_semitone_distance) return false;
if (abs(key_diff + key_offset) > *plugin->bend_semitone_distance) {
return false;
}
double relative_key = (
((double)samples_passed / slide_duration) * key_diff + key_offset
);
int bend_value = relative_key_to_bend(plugin, relative_key);
set_bend(plugin, bend_value, frames, output_capacity);
return true;
}
static inline void set_bend_from_key(
MidiSlide *plugin, uint8_t key, uint32_t frames,
uint32_t output_capacity) {
int relative_key = (int)key - plugin->key_playing;
if (abs(relative_key) > *plugin->bend_semitone_distance) return;
int bend_value = relative_key_to_bend(plugin, relative_key);
set_bend(plugin, bend_value, frames, output_capacity);
}
static inline int relative_key_to_bend(
MidiSlide *plugin, double relative_key) {
int bend_multiplier = relative_key < 0 ? 8192 : 8191;
return bend_multiplier * relative_key / *plugin->bend_semitone_distance;
}
static inline void init_midi_event(
MidiSlide *plugin, MidiEvent *event, uint32_t frames) {
event->event.time.frames = frames;
event->event.body.type = plugin->uris.midi_Event;
event->event.body.size = 3;
}
static inline void set_bend(
MidiSlide *plugin, int value, uint32_t frames,
uint32_t output_capacity) {
uint16_t real_bend = value + 8192;
MidiEvent event;
init_midi_event(plugin, &event, frames);
event.message[0] = LV2_MIDI_MSG_BENDER;
event.message[1] = real_bend & 0x7f;
event.message[2] = (real_bend >> 7) & 0x7f;
send_midi_message(plugin, &event, output_capacity);
}
static inline void play_note(
MidiSlide *plugin, uint8_t key, uint8_t velocity, uint32_t frames,
uint32_t output_capacity) {
MidiEvent event;
init_midi_event(plugin, &event, frames);
plugin->key_playing = key;
if (*plugin->forced_velocity > 0) velocity = *plugin->forced_velocity;
event.message[0] = LV2_MIDI_MSG_NOTE_ON;
event.message[1] = key;
event.message[2] = velocity;
send_midi_message(plugin, &event, output_capacity);
}
static inline void stop_note(
MidiSlide *plugin, uint32_t frames, uint32_t output_capacity) {
MidiEvent event;
init_midi_event(plugin, &event, frames);
event.message[0] = LV2_MIDI_MSG_NOTE_OFF;
event.message[1] = plugin->key_playing;
event.message[2] = 0; // For now, zero velocity.
send_midi_message(plugin, &event, output_capacity);
}
static inline void send_midi_message(
MidiSlide *plugin, MidiEvent *event, uint32_t output_capacity) {
LV2_Atom_Event *result = lv2_atom_sequence_append_event(
plugin->output, output_capacity, &event->event
);
if (result == NULL) {
fprintf(stderr, "Error: Could not append atom event.\n");
}
}
static inline bool handle_midi_message(
MidiSlide *plugin, const uint8_t *message, MidiAction action,
uint32_t output_capacity) {
switch (action) {
case ACTION_NOTE_ON:
handle_note_on(plugin, message[1], message[2]);
break;
case ACTION_NOTE_OFF:
handle_note_off(plugin, message[1]);
break;
case ACTION_ALL_NOTES_OFF:
handle_all_notes_off(plugin);
break;
default:
return false;
}
return true;
}
static inline MidiAction get_midi_action(const uint8_t *message) {
uint8_t message_type = message[0] & 0xF0;
switch (message_type) {
case LV2_MIDI_MSG_NOTE_ON:
return ACTION_NOTE_ON;
case LV2_MIDI_MSG_NOTE_OFF:
return ACTION_NOTE_OFF;
case LV2_MIDI_MSG_CONTROLLER: ;
uint8_t controller = message[1];
if (controller >= LV2_MIDI_CTL_ALL_NOTES_OFF &&
controller <= LV2_MIDI_CTL_MONO2) {
// All messages in this range turn off notes.
return ACTION_ALL_NOTES_OFF;
}
break;
}
return ACTION_UNKNOWN;
}
static inline void handle_note_on(
MidiSlide *plugin, uint8_t key, uint8_t velocity) {
add_to_stack(plugin, key, velocity);
}
static inline void handle_note_off(MidiSlide *plugin, uint8_t key) {
remove_from_stack(plugin, key);
}
static inline void handle_all_notes_off(MidiSlide *plugin) {
clear_stack(plugin);
}
static inline void add_to_stack(
MidiSlide *plugin, uint8_t key, uint8_t velocity) {
uint8_t stack_size = plugin->note_stack_size;
if (stack_size >= 128) {
fprintf(stderr, "Error: Note stack is full.\n");
return;
}
uint8_t old_stack_pos = plugin->key_to_stack_pos[key];
if (old_stack_pos < plugin->note_stack_size &&
plugin->note_stack[old_stack_pos].key == key) {
// Note is already in the stack.
fprintf(stderr, "Error: Note is already in stack.\n");
return;
}
plugin->note_stack[stack_size] = (MidiNote){
.active = true,
.key = key,
.velocity = velocity,
};
plugin->key_to_stack_pos[key] = stack_size;
plugin->note_stack_size++;
}
static inline void remove_from_stack(MidiSlide *plugin, uint8_t key) {
uint8_t stack_pos = plugin->key_to_stack_pos[key];
uint8_t stack_size = plugin->note_stack_size;
if (stack_pos >= stack_size) {
fprintf(stderr, "Error: Note is not in stack.\n");
return;
}
if (plugin->note_stack[stack_pos].key != key) {
fprintf(stderr, "Error: Note is not in stack.\n");
return;
}
plugin->note_stack[stack_pos].active = false;
}
static inline void clear_stack(MidiSlide *plugin) {
plugin->note_stack_size = 0;
}
static inline void compact_stack(MidiSlide *plugin) {
MidiNote *stack = plugin->note_stack;
uint8_t *key_to_stack_pos = plugin->key_to_stack_pos;
uint8_t stack_size = plugin->note_stack_size;
uint8_t offset = 0;
for (uint8_t i = 0; i + offset < stack_size; i++) {
while (i + offset < stack_size && !stack[i + offset].active) {
offset++;
}
if (offset > 0 && i + offset < stack_size) {
stack[i] = stack[i + offset];
key_to_stack_pos[stack[i].key] = i;
}
}
plugin->note_stack_size -= offset;
}
static inline void handle_atom_object(
MidiSlide *plugin, const LV2_Atom_Object *object) {
if (object->body.otype != plugin->uris.time_Position) return;
LV2_Atom *bpm = NULL;
lv2_atom_object_get(object, plugin->uris.time_beatsPerMinute, &bpm, NULL);
if (bpm == NULL) return;
float bpm_float = ((LV2_Atom_Float *)bpm)->body;
plugin->samples_per_beat = 60.0 / bpm_float * plugin->sample_rate;
}
static void deactivate(LV2_Handle instance) {
}
static void cleanup(LV2_Handle instance) {
MidiSlide *plugin = (MidiSlide *)instance;
free(plugin);
}
static const void *extension_data(const char *uri) {
return NULL;
}
static const LV2_Descriptor descriptor = {
PLUGIN_URI,
instantiate,
connect_port,
activate,
run,
deactivate,
cleanup,
extension_data
};
LV2_SYMBOL_EXPORT
const LV2_Descriptor *lv2_descriptor(uint32_t index) {
switch (index) {
case 0:
return &descriptor;
default:
return NULL;
}
}