-
Notifications
You must be signed in to change notification settings - Fork 0
/
vslpt.c
366 lines (333 loc) · 11.4 KB
/
vslpt.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
/*
Vienna Symphonic Library Performance Tool LV2 Plugin
Copyright 2020 Reese Wang <thuwrx10@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "lv2/atom/atom.h"
#include "lv2/atom/util.h"
#include "lv2/core/lv2.h"
#include "lv2/core/lv2_util.h"
#include "lv2/patch/patch.h"
#include "lv2/state/state.h"
#include "lv2/log/log.h"
#include "lv2/log/logger.h"
#include "lv2/midi/midi.h"
#include "lv2/urid/urid.h"
#define VSLPT_URI "https://github.com/ReeseWang/vslpt-lv2"
#define VSL_FIRSTNOTE_KEYSW 0x0D
#define VSL_RELEASE_KEYSW 0x0E
#define VSL_REPEAT_KEYSW 0x0F
#define VSL_INTERVAL_KEYSW_BASE 0x00
typedef struct {
LV2_URID atom_Path;
LV2_URID atom_Resource;
LV2_URID atom_Sequence;
LV2_URID atom_URID;
LV2_URID atom_eventTransfer;
LV2_URID midi_Event;
LV2_URID patch_Set;
LV2_URID patch_property;
LV2_URID patch_value;
} VSLPTURIs;
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
enum {
MIDI_IN = 0,
MIDI_OUT = 1
};
// Struct for a 3 byte MIDI event, used for writing notes
typedef struct {
LV2_Atom_Event event;
uint8_t msg[3];
} MIDIEvent;
#define NOTE_STACK_SIZE 32
#define MIDI_CHANNELS 16
typedef struct {
// Features
LV2_URID_Map* map;
LV2_Log_Logger logger;
// Ports
const LV2_Atom_Sequence* in_port;
LV2_Atom_Sequence* out_port;
// URIs
VSLPTURIs uris;
// Status
uint8_t note_stack[MIDI_CHANNELS][NOTE_STACK_SIZE][2];
uint8_t note_stack_top[MIDI_CHANNELS];
uint8_t active_note[MIDI_CHANNELS];
uint8_t control_note[MIDI_CHANNELS];
uint32_t out_capacity;
MIDIEvent evbuf;
} VSLPT;
static inline void
map_uris(LV2_URID_Map* map, VSLPTURIs* uris)
{
uris->atom_Path = map->map(map->handle, LV2_ATOM__Path);
uris->atom_Resource = map->map(map->handle, LV2_ATOM__Resource);
uris->atom_Sequence = map->map(map->handle, LV2_ATOM__Sequence);
uris->atom_URID = map->map(map->handle, LV2_ATOM__URID);
uris->atom_eventTransfer = map->map(map->handle, LV2_ATOM__eventTransfer);
uris->midi_Event = map->map(map->handle, LV2_MIDI__MidiEvent);
uris->patch_Set = map->map(map->handle, LV2_PATCH__Set);
uris->patch_property = map->map(map->handle, LV2_PATCH__property);
uris->patch_value = map->map(map->handle, LV2_PATCH__value);
}
static void
connect_port(LV2_Handle instance,
uint32_t port,
void* data)
{
VSLPT* self = (VSLPT*)instance;
switch (port) {
case MIDI_IN:
self->in_port = (const LV2_Atom_Sequence*)data;
break;
case MIDI_OUT:
self->out_port = (LV2_Atom_Sequence*)data;
break;
default:
break;
}
}
static LV2_Handle
instantiate(const LV2_Descriptor* descriptor,
double rate,
const char* path,
const LV2_Feature* const* features)
{
// Allocate and initialise instance structure.
VSLPT* self = (VSLPT*)calloc(1, sizeof(VSLPT));
if (!self) {
return NULL;
}
// Scan host features for URID map
const char* missing = lv2_features_query(
features,
LV2_LOG__log, &self->logger.log, false,
LV2_URID__map, &self->map, true,
NULL);
lv2_log_logger_set_map(&self->logger, self->map);
if (missing) {
lv2_log_error(&self->logger, "Missing feature <%s>\n", missing);
free(self);
return NULL;
}
map_uris(self->map, &self->uris);
memset(self->note_stack_top, 0, MIDI_CHANNELS);
memset(self->active_note, 0xFF, MIDI_CHANNELS);
memset(self->control_note, 0xFF, MIDI_CHANNELS);
return (LV2_Handle)self;
}
static void
cleanup(LV2_Handle instance)
{
free(instance);
}
static void
add_to_note_stack(VSLPT *self,
const uint8_t* const msg)
{
uint8_t ch = msg[0] & 0x0F;
if (self->note_stack_top[ch] == 0)
{
self->note_stack_top[ch] = 1;
self->note_stack[ch][0][0] = msg[1];
self->note_stack[ch][0][1] = msg[2];
}
else
{
for (uint8_t i=0; i<self->note_stack_top[ch]; i++)
if (self->note_stack[ch][i][0] == msg[1])
{
// If has dup notes, update velocity and return
self->note_stack[ch][i][1] = msg[2];
return;
}
// If no duplicated notes, push to stack
self->note_stack[ch][self->note_stack_top[ch]][0] = msg[1];
self->note_stack[ch][self->note_stack_top[ch]][1] = msg[2];
self->note_stack_top[ch]++;
}
}
static void
remove_from_note_stack(VSLPT *self,
const uint8_t* const msg)
{
uint8_t ch = msg[0] & 0x0F;
if (self->note_stack_top[ch] != 0)
{
for (uint8_t i=0; i<self->note_stack_top[ch]; i++)
if (self->note_stack[ch][i][0] == msg[1])
{
// Remove from stack
self->note_stack_top[ch]--;
for (uint8_t j=i; j<self->note_stack_top[ch]; j++)
{
self->note_stack[ch][j][0] = self->note_stack[ch][j+1][0];
self->note_stack[ch][j][1] = self->note_stack[ch][j+1][1];
}
}
}
}
static inline void
send_note_on(VSLPT *self, uint8_t ch, uint8_t note, uint8_t vel)
{
self->evbuf.msg[0] = LV2_MIDI_MSG_NOTE_ON | (0x0F & ch);
self->evbuf.msg[1] = note;
self->evbuf.msg[2] = vel;
lv2_atom_sequence_append_event(
self->out_port, self->out_capacity, &(self->evbuf.event));
}
static inline void
send_note_off(VSLPT *self, uint8_t ch, uint8_t note)
{
self->evbuf.msg[0] = LV2_MIDI_MSG_NOTE_OFF | (0x0F & ch);
self->evbuf.msg[1] = note;
self->evbuf.msg[2] = 64;
lv2_atom_sequence_append_event(
self->out_port, self->out_capacity, &(self->evbuf.event));
}
static void
generate_note_events(VSLPT *self,
const uint8_t* const msg)
{
uint8_t ch = msg[0] & 0x0F;
if (self->active_note[ch] == 0xFF && self->note_stack_top[ch] > 0)
{
send_note_on(self, ch, VSL_FIRSTNOTE_KEYSW, 100);
self->control_note[ch] = VSL_FIRSTNOTE_KEYSW;
send_note_on(self,
ch,
self->note_stack[ch][self->note_stack_top[ch] - 1][0] - 24,
self->note_stack[ch][self->note_stack_top[ch] - 1][1]);
self->active_note[ch] = self->note_stack[ch][self->note_stack_top[ch] - 1][0] - 24;
lv2_log_note(&self->logger, "Note on %d", self->note_stack_top[ch]);
}
else if (self->active_note[ch] != 0xFF && self->note_stack_top[ch] == 0)
{
send_note_off(self, ch, self->active_note[ch]);
self->active_note[ch] = 0xFF;
send_note_off(self, ch, self->control_note[ch]);
self->control_note[ch] = 0xFF;
}
else if (self->active_note[ch] != 0xFF && self->note_stack_top[ch] > 0)
{
uint8_t real_last_note;
uint8_t next_note = self->note_stack[ch][self->note_stack_top[ch] - 1][0];
if (self->active_note[ch] > 72)
real_last_note = self->active_note[ch] - 24;
else
real_last_note = self->active_note[ch] + 24;
if (next_note < real_last_note)
{
send_note_off(self, ch, self->active_note[ch]);
send_note_off(self, ch, self->control_note[ch]);
send_note_on(self, ch, real_last_note - next_note, 100);
self->control_note[ch] = real_last_note - next_note;
send_note_on(self,
ch,
self->note_stack[ch][self->note_stack_top[ch] - 1][0] + 24,
self->note_stack[ch][self->note_stack_top[ch] - 1][1]);
self->active_note[ch] = self->note_stack[ch][self->note_stack_top[ch] - 1][0] + 24;
}
else if (next_note > real_last_note)
{
send_note_off(self, ch, self->active_note[ch]);
send_note_off(self, ch, self->control_note[ch]);
send_note_on(self, ch, next_note - real_last_note, 100);
self->control_note[ch] = next_note - real_last_note;
send_note_on(self,
ch,
self->note_stack[ch][self->note_stack_top[ch] - 1][0] - 24,
self->note_stack[ch][self->note_stack_top[ch] - 1][1]);
self->active_note[ch] = self->note_stack[ch][self->note_stack_top[ch] - 1][0] - 24;
}
}
}
static void
run(LV2_Handle instance,
uint32_t sample_count)
{
VSLPT* self = (VSLPT*)instance;
VSLPTURIs* uris = &self->uris;
// Initially self->out_port contains a Chunk with size set to capacity
// Get the capacity
self->out_capacity = self->out_port->atom.size;
// Write an empty Sequence header to the output
lv2_atom_sequence_clear(self->out_port);
self->out_port->atom.type = self->in_port->atom.type;
// Read incoming events
LV2_ATOM_SEQUENCE_FOREACH(self->in_port, ev) {
if (ev->body.type == uris->midi_Event) {
const uint8_t* const msg = (const uint8_t*)(ev + 1);
switch (lv2_midi_message_type(msg)) {
case LV2_MIDI_MSG_NOTE_ON:
if (msg[2] == 0) // vel=0 is a Note Off
goto noteoff;
add_to_note_stack(self, msg);
goto gen_event;
case LV2_MIDI_MSG_NOTE_OFF:
noteoff:
remove_from_note_stack(self, msg);
gen_event:
// Output note events share the same header
self->evbuf.event = *ev;
generate_note_events(self, msg);
break;
case LV2_MIDI_MSG_CHANNEL_PRESSURE:
// Convert channel pressure to modulation CC
self->evbuf.event.time.frames = ev->time.frames;
self->evbuf.event.body.type = ev->body.type;
self->evbuf.event.body.size = 3;
self->evbuf.msg[0] = LV2_MIDI_MSG_CONTROLLER | (msg[0] & 0x0F);
self->evbuf.msg[1] = 0x01;
self->evbuf.msg[2] = msg[1];
lv2_atom_sequence_append_event(
self->out_port, self->out_capacity, &(self->evbuf.event));
break;
default:
// Forward all other MIDI events directly
lv2_atom_sequence_append_event(
self->out_port, self->out_capacity, ev);
break;
}
}
}
}
static const void*
extension_data(const char* uri)
{
return NULL;
}
static const LV2_Descriptor descriptor = {
VSLPT_URI,
instantiate,
connect_port,
NULL, // activate,
run,
NULL, // deactivate,
cleanup,
extension_data
};
LV2_SYMBOL_EXPORT
const LV2_Descriptor* lv2_descriptor(uint32_t index)
{
switch (index) {
case 0:
return &descriptor;
default:
return NULL;
}
}
// vim: set et ts=4 sw=4: