forked from nicklan/drmr
/
drmr.c
509 lines (458 loc) · 15 KB
/
drmr.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
/* drmr.c
* LV2 DrMr plugin
* Copyright 2012 Nick Lanham <nick@afternight.org>
*
* Public License v3. source code is available at
* <http://github.com/nicklan/drmr>
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "drmr.h"
#include "drmr_hydrogen.h"
#define REQ_BUF_SIZE 10
#define VELOCITY_MAX 127
static int current_kit_changed = 0;
static void* load_thread(void* arg) {
DrMr* drmr = (DrMr*)arg;
drmr_sample *loaded_samples,*old_samples;
int loaded_count, old_scount;
char *request;
for(;;) {
pthread_mutex_lock(&drmr->load_mutex);
pthread_cond_wait(&drmr->load_cond,
&drmr->load_mutex);
pthread_mutex_unlock(&drmr->load_mutex);
old_samples = drmr->samples;
old_scount = drmr->num_samples;
request = drmr->request_buf[drmr->curReq];
loaded_samples = load_hydrogen_kit(request,drmr->rate,&loaded_count);
if (!loaded_samples) {
fprintf(stderr,"Failed to load kit at: %s\n",request);
pthread_mutex_lock(&drmr->load_mutex);
drmr->num_samples = 0;
drmr->samples = NULL;
pthread_mutex_unlock(&drmr->load_mutex);
}
else {
// just lock for the critical moment when we swap in the new kit
printf("loaded kit at: %s\n",request);
pthread_mutex_lock(&drmr->load_mutex);
drmr->samples = loaded_samples;
drmr->num_samples = loaded_count;
pthread_mutex_unlock(&drmr->load_mutex);
}
if (old_scount > 0) free_samples(old_samples,old_scount);
drmr->current_path = request;
current_kit_changed = 1;
}
return 0;
}
static LV2_Handle
instantiate(const LV2_Descriptor* descriptor,
double rate,
const char* bundle_path,
const LV2_Feature* const* features) {
int i;
DrMr* drmr = malloc(sizeof(DrMr));
drmr->map = NULL;
drmr->samples = NULL;
drmr->num_samples = 0;
drmr->current_path = NULL;
drmr->curReq = -1;
drmr->rate = rate;
drmr->ignore_velocity = false;
drmr->ignore_note_off = false;
if (pthread_mutex_init(&drmr->load_mutex, 0)) {
fprintf(stderr, "Could not initialize load_mutex.\n");
free(drmr);
return 0;
}
if (pthread_cond_init(&drmr->load_cond, 0)) {
fprintf(stderr, "Could not initialize load_cond.\n");
free(drmr);
return 0;
}
while(*features) {
if (!strcmp((*features)->URI, LV2_URID_URI "#map"))
drmr->map = (LV2_URID_Map *)((*features)->data);
features++;
}
if (!drmr->map) {
fprintf(stderr, "LV2 host does not support urid#map.\n");
free(drmr);
return 0;
}
map_drmr_uris(drmr->map,&(drmr->uris));
lv2_atom_forge_init(&drmr->forge, drmr->map);
if (pthread_create(&drmr->load_thread, 0, load_thread, drmr)) {
fprintf(stderr, "Could not initialize loading thread.\n");
free(drmr);
return 0;
}
drmr->request_buf = malloc(REQ_BUF_SIZE*sizeof(char*));
memset(drmr->request_buf,0,REQ_BUF_SIZE*sizeof(char*));
drmr->gains = malloc(32*sizeof(float*));
drmr->pans = malloc(32*sizeof(float*));
for(i = 0;i<32;i++) {
drmr->gains[i] = NULL;
drmr->pans[i] = NULL;
}
return (LV2_Handle)drmr;
}
static void
connect_port(LV2_Handle instance,
uint32_t port,
void* data) {
DrMr* drmr = (DrMr*)instance;
DrMrPortIndex port_index = (DrMrPortIndex)port;
switch (port_index) {
case DRMR_CONTROL:
drmr->control_port = (LV2_Atom_Sequence*)data;
break;
case DRMR_CORE_EVENT:
drmr->core_event_port = (LV2_Atom_Sequence*)data;
break;
case DRMR_LEFT:
drmr->left = (float*)data;
break;
case DRMR_RIGHT:
drmr->right = (float*)data;
break;
case DRMR_BASENOTE:
if (data) drmr->baseNote = (float*)data;
default:
break;
}
if (port_index >= DRMR_GAIN_ONE && port_index <= DRMR_GAIN_THIRTYTWO) {
int goff = port_index - DRMR_GAIN_ONE;
drmr->gains[goff] = (float*)data;
}
if (port_index >= DRMR_PAN_ONE && port_index <= DRMR_PAN_THIRTYTWO) {
int poff = port_index - DRMR_PAN_ONE;
drmr->pans[poff] = (float*)data;
}
}
static inline LV2_Atom *build_update_message(DrMr *drmr) {
LV2_Atom_Forge_Frame set_frame;
LV2_Atom* msg = (LV2_Atom*)lv2_atom_forge_resource
(&drmr->forge, &set_frame, 1, drmr->uris.ui_msg);
if (drmr->current_path) {
lv2_atom_forge_property_head(&drmr->forge, drmr->uris.kit_path,0);
lv2_atom_forge_string(&drmr->forge, drmr->current_path, strlen(drmr->current_path));
}
lv2_atom_forge_pop(&drmr->forge,&set_frame);
return msg;
}
static inline LV2_Atom *build_state_message(DrMr *drmr) {
LV2_Atom_Forge_Frame set_frame;
LV2_Atom* msg = (LV2_Atom*)lv2_atom_forge_resource
(&drmr->forge, &set_frame, 1, drmr->uris.get_state);
if (drmr->current_path) {
lv2_atom_forge_property_head(&drmr->forge, drmr->uris.kit_path,0);
lv2_atom_forge_string(&drmr->forge, drmr->current_path, strlen(drmr->current_path));
}
lv2_atom_forge_pop(&drmr->forge,&set_frame);
return msg;
}
static inline LV2_Atom *build_midi_info_message(DrMr *drmr, uint8_t *data) {
LV2_Atom_Forge_Frame set_frame;
LV2_Atom* msg = (LV2_Atom*)lv2_atom_forge_resource
(&drmr->forge, &set_frame, 1, drmr->uris.midi_info);
lv2_atom_forge_property_head(&drmr->forge, drmr->uris.midi_event,0);
lv2_atom_forge_write(&drmr->forge, data, 3);
lv2_atom_forge_pop(&drmr->forge,&set_frame);
return msg;
}
static inline void layer_to_sample(drmr_sample *sample, float gain) {
int i;
float mapped_gain = (1-(gain/GAIN_MIN));
if (mapped_gain > 1.0f) mapped_gain = 1.0f;
for(i = 0;i < sample->layer_count;i++) {
if (sample->layers[i].min <= mapped_gain &&
(sample->layers[i].max > mapped_gain ||
(sample->layers[i].max == 1 && mapped_gain == 1))) {
sample->limit = sample->layers[i].limit;
sample->info = sample->layers[i].info;
sample->data = sample->layers[i].data;
return;
}
}
fprintf(stderr,"Couldn't find layer for gain %f in sample\n\n",gain);
/* to avoid not playing something, and to deal with kits like the
k-27_trash_kit, let's just use the first layer */
sample->limit = sample->layers[0].limit;
sample->info = sample->layers[0].info;
sample->data = sample->layers[0].data;
}
static inline void trigger_sample(DrMr *drmr, int nn, uint8_t* const data) {
// need to mutex this to avoid getting the samples array
// changed after the check that the midi-note is valid
pthread_mutex_lock(&drmr->load_mutex);
if (nn >= 0 && nn < drmr->num_samples) {
if (drmr->samples[nn].layer_count > 0) {
layer_to_sample(drmr->samples+nn,*(drmr->gains[nn]));
if (drmr->samples[nn].limit == 0)
fprintf(stderr,"Failed to find layer at: %i for %f\n",nn,*drmr->gains[nn]);
}
if (data) {
lv2_atom_forge_frame_time(&drmr->forge, 0);
build_midi_info_message(drmr,data);
}
drmr->samples[nn].active = 1;
drmr->samples[nn].offset = 0;
drmr->samples[nn].velocity = drmr->ignore_velocity?1.0f:((float)data[2])/VELOCITY_MAX;
}
pthread_mutex_unlock(&drmr->load_mutex);
}
static inline void untrigger_sample(DrMr *drmr, int nn) {
pthread_mutex_lock(&drmr->load_mutex);
if (nn >= 0 && nn < drmr->num_samples) {
if (drmr->samples[nn].layer_count > 0) {
layer_to_sample(drmr->samples+nn,*(drmr->gains[nn]));
if (drmr->samples[nn].limit == 0)
fprintf(stderr,"Failed to find layer at: %i for %f\n",nn,*drmr->gains[nn]);
}
drmr->samples[nn].active = 0;
drmr->samples[nn].offset = 0;
}
pthread_mutex_unlock(&drmr->load_mutex);
}
#define DB3SCALE -0.8317830986718104f
#define DB3SCALEPO 1.8317830986718104f
// taken from lv2 example amp plugin
#define DB_CO(g) ((g) > GAIN_MIN ? powf(10.0f, (g) * 0.05f) : 0.0f)
static void run(LV2_Handle instance, uint32_t n_samples) {
int i,baseNote;
DrMr* drmr = (DrMr*)instance;
baseNote = (int)floorf(*(drmr->baseNote));
const uint32_t event_capacity = drmr->core_event_port->atom.size;
lv2_atom_forge_set_buffer(&drmr->forge,
(uint8_t*)drmr->core_event_port,
event_capacity);
LV2_Atom_Forge_Frame seq_frame;
lv2_atom_forge_sequence_head(&drmr->forge, &seq_frame, 0);
LV2_SEQUENCE_FOREACH(drmr->control_port, i) {
LV2_Atom_Event* const ev = lv2_sequence_iter_get(i);
if (ev->body.type == drmr->uris.midi_event) {
uint8_t nn;
uint8_t* const data = (uint8_t* const)(ev + 1);
//int channel = *data & 15;
switch ((*data) >> 4) {
case 8:
if (!drmr->ignore_note_off) {
nn = data[1];
nn-=baseNote;
untrigger_sample(drmr,nn);
}
break;
case 9: {
nn = data[1];
nn-=baseNote;
trigger_sample(drmr,nn,data);
break;
}
default:
printf("Unhandeled status: %i\n",(*data)>>4);
}
}
else if (ev->body.type == drmr->uris.atom_resource) {
const LV2_Atom_Object *obj = (LV2_Atom_Object*)&ev->body;
if (obj->body.otype == drmr->uris.ui_msg) {
const LV2_Atom* path = NULL;
const LV2_Atom* trigger = NULL;
const LV2_Atom* ignvel = NULL;
const LV2_Atom* ignno = NULL;
lv2_object_get(obj,
drmr->uris.kit_path, &path,
drmr->uris.sample_trigger, &trigger,
drmr->uris.velocity_toggle, &ignvel,
drmr->uris.note_off_toggle, &ignno,
0);
if (path) {
int reqPos = (drmr->curReq+1)%REQ_BUF_SIZE;
char *tmp = NULL;
if (reqPos >= 0 &&
drmr->request_buf[reqPos])
tmp = drmr->request_buf[reqPos];
drmr->request_buf[reqPos] = strdup(LV2_ATOM_BODY(path));
drmr->curReq = reqPos;
if (tmp) free(tmp);
}
if (trigger) {
int32_t si = ((const LV2_Atom_Int*)trigger)->body;
uint8_t mdata[3];
mdata[0] = 0x90; // note on
mdata[1] = si+baseNote;
mdata[2] = 0x7f;
trigger_sample(drmr,si,mdata);
}
if (ignvel)
drmr->ignore_velocity = ((const LV2_Atom_Bool*)ignvel)->body;
if (ignno)
drmr->ignore_note_off = ((const LV2_Atom_Bool*)ignno)->body;
} else if (obj->body.otype == drmr->uris.get_state) {
lv2_atom_forge_frame_time(&drmr->forge, 0);
build_state_message(drmr);
}
}
else printf("unrecognized event\n");
}
if ((drmr->curReq >= 0) &&
drmr->request_buf[drmr->curReq] &&
(!drmr->current_path ||
strcmp(drmr->current_path,
drmr->request_buf[drmr->curReq])))
pthread_cond_signal(&drmr->load_cond);
if (current_kit_changed) {
current_kit_changed = 0;
lv2_atom_forge_frame_time(&drmr->forge, 0);
build_update_message(drmr);
}
lv2_atom_forge_pop(&drmr->forge, &seq_frame);
for(i = 0;i<n_samples;i++) {
drmr->left[i] = 0.0f;
drmr->right[i] = 0.0f;
}
pthread_mutex_lock(&drmr->load_mutex);
for (i = 0;i < drmr->num_samples;i++) {
int pos,lim;
drmr_sample* cs = drmr->samples+i;
if (cs->active && (cs->limit > 0)) {
float coef_right, coef_left;
if (i < 32) {
float gain = DB_CO(*(drmr->gains[i]));
float pan_right = ((*drmr->pans[i])+1)/2.0f;
float pan_left = 1-pan_right;
coef_right = (pan_right * (DB3SCALE * pan_right + DB3SCALEPO))*gain*cs->velocity;
coef_left = (pan_left * (DB3SCALE * pan_left + DB3SCALEPO))*gain*cs->velocity;
}
else {
coef_right = coef_left = 1.0f;
}
if (cs->info->channels == 1) { // play mono sample
lim = (n_samples < (cs->limit - cs->offset)?n_samples:(cs->limit-cs->offset));
for(pos = 0;pos < lim;pos++) {
drmr->left[pos] += cs->data[cs->offset]*coef_left;
drmr->right[pos] += cs->data[cs->offset]*coef_right;
cs->offset++;
}
} else { // play stereo sample
lim = (cs->limit-cs->offset)/cs->info->channels;
if (lim > n_samples) lim = n_samples;
for (pos=0;pos<lim;pos++) {
drmr->left[pos] += cs->data[cs->offset++]*coef_left;
drmr->right[pos] += cs->data[cs->offset++]*coef_right;
}
}
if (cs->offset >= cs->limit) cs->active = 0;
}
}
pthread_mutex_unlock(&drmr->load_mutex);
}
static void cleanup(LV2_Handle instance) {
DrMr* drmr = (DrMr*)instance;
pthread_cancel(drmr->load_thread);
pthread_join(drmr->load_thread, 0);
if (drmr->num_samples > 0)
free_samples(drmr->samples,drmr->num_samples);
free(drmr->gains);
free(instance);
}
void save_state(LV2_Handle instance,
LV2_State_Store_Function store,
void* handle,
uint32_t flags,
const LV2_Feature *const * features) {
DrMr *drmr = (DrMr*)instance;
LV2_State_Map_Path* map_path = NULL;
while(*features) {
if (!strcmp((*features)->URI, LV2_STATE__mapPath))
map_path = (LV2_State_Map_Path*)((*features)->data);
features++;
}
if (map_path == NULL) {
fprintf(stderr,"Host does not support map_path, cannot save state\n");
return;
}
char* mapped_path = map_path->abstract_path(map_path->handle,
drmr->current_path);
if (store(handle,
drmr->uris.kit_path,
mapped_path,
strlen(mapped_path) + 1,
drmr->uris.string_urid,
LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE)) {
fprintf(stderr,"Store failed\n");
}
}
void restore_state(LV2_Handle instance,
LV2_State_Retrieve_Function retrieve,
void* handle,
uint32_t flags,
const LV2_Feature *const * features) {
DrMr* drmr = (DrMr*)instance;
size_t size;
uint32_t type;
uint32_t fgs;
LV2_State_Map_Path* map_path = NULL;
while(*features) {
if (!strcmp((*features)->URI, LV2_STATE__mapPath))
map_path = (LV2_State_Map_Path*)((*features)->data);
features++;
}
if (map_path == NULL) {
fprintf(stderr,"Host does not support map_path, cannot restore state\n");
return;
}
const char* abstract_path =
retrieve(handle, drmr->uris.kit_path, &size, &type, &fgs);
if (!abstract_path) {
fprintf(stderr,"Found no path in state, not restoring\n");
return;
}
char *kit_path = map_path->absolute_path(map_path->handle,abstract_path);
if (kit_path) { // safe as we're in "Instantiation" threading class
int reqPos = (drmr->curReq+1)%REQ_BUF_SIZE;
char *tmp = NULL;
if (reqPos >= 0 && drmr->request_buf[reqPos])
tmp = drmr->request_buf[reqPos];
drmr->request_buf[reqPos] = strdup(kit_path);
drmr->curReq = reqPos;
if (tmp) free(tmp);
}
}
static const void* extension_data(const char* uri) {
static const LV2_State_Interface state_iface = { save_state, restore_state };
if (!strcmp(uri, LV2_STATE_URI "#Interface")) return &state_iface;
return NULL;
}
static const LV2_Descriptor descriptor = {
DRMR_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;
}
}