lv2 tweaks, add set dft transition, zoom fix, fs test_filter fix
[goodguy/history.git] / cinelerra-5.1 / cinelerra / pluginlv2.C
1 #ifdef HAVE_LV2
2
3 #include "bctrace.h"
4 #include "bcwindowbase.h"
5 #include "pluginlv2.h"
6 #include "samples.h"
7
8 #include <sys/shm.h>
9 #include <sys/mman.h>
10
11 PluginLV2::PluginLV2()
12 {
13         shm_bfr = 0;
14         use_shm = 1;
15         shmid = -1;
16         in_buffers = 0;   iport = 0;
17         out_buffers = 0;  oport = 0;
18         nb_inputs = 0;
19         nb_outputs = 0;
20         max_bufsz = 0;
21         ui_features = 0;
22
23         samplerate = 44100;
24         refreshrate = 30.;
25         block_length = 4096;
26         midi_buf_size = 8192;
27
28         world = 0;
29         lilv = 0;
30         lilv_uis = 0;
31         inst = 0;
32         sinst = 0;
33         ui_host = 0;
34
35         lv2_InputPort = 0;
36         lv2_OutputPort = 0;
37         lv2_AudioPort = 0;
38         lv2_ControlPort = 0;
39         lv2_CVPort = 0;
40         lv2_Optional = 0;
41         atom_AtomPort = 0;
42         atom_Sequence = 0;
43         powerOf2BlockLength = 0;
44         fixedBlockLength = 0;
45         boundedBlockLength = 0;
46         seq_out = 0;
47
48         worker_thread = 0;
49         memset(&schedule, 0, sizeof(schedule));
50         schedule.handle = (LV2_Worker_Schedule_Handle)this;
51         schedule.schedule_work = lv2_worker_schedule;
52         worker_iface = 0;  worker_done = -1;
53         pthread_mutex_init(&worker_lock, 0);
54         pthread_cond_init(&worker_ready, 0);
55         work_avail = 0;   work_input = 0;
56         work_output = 0;  work_tail = &work_output;
57 }
58
59 PluginLV2::~PluginLV2()
60 {
61         reset_lv2();
62         if( world )     lilv_world_free(world);
63         pthread_mutex_destroy(&worker_lock);
64         pthread_cond_destroy(&worker_ready);
65 }
66
67 void PluginLV2::reset_lv2()
68 {
69         worker_stop();
70         if( inst ) lilv_instance_deactivate(inst);
71         lilv_instance_free(inst);             inst = 0;
72         lilv_uis_free(lilv_uis);              lilv_uis = 0;
73
74         lilv_node_free(lv2_InputPort);        lv2_InputPort = 0;
75         lilv_node_free(lv2_OutputPort);       lv2_OutputPort = 0;
76         lilv_node_free(lv2_AudioPort);        lv2_AudioPort = 0;
77         lilv_node_free(lv2_ControlPort);      lv2_ControlPort = 0;
78         lilv_node_free(lv2_CVPort);           lv2_CVPort = 0;
79
80         lilv_node_free(lv2_Optional);         lv2_Optional = 0;
81         lilv_node_free(atom_AtomPort);        atom_AtomPort = 0;
82         lilv_node_free(atom_Sequence);        atom_Sequence = 0;
83         lilv_node_free(boundedBlockLength);   boundedBlockLength = 0;
84         lilv_node_free(fixedBlockLength);     fixedBlockLength = 0;
85         lilv_node_free(powerOf2BlockLength);  powerOf2BlockLength = 0;
86
87         delete [] (char *)seq_out;            seq_out = 0;
88         uri_table.remove_all_objects();
89         features.remove_all_objects();        ui_features = 0;
90         del_buffer();
91 }
92
93
94 int PluginLV2::load_lv2(const char *path, char *title)
95 {
96         if( !world ) {
97                 world = lilv_world_new();
98                 if( !world ) {
99                         printf("lv2: lilv_world_new failed\n");
100                         return 1;
101                 }
102                 lilv_world_load_all(world);
103         }
104
105         LilvNode *uri = lilv_new_uri(world, path);
106         if( !uri ) {
107                 printf("lv2: lilv_new_uri(%s) failed\n", path);
108                 return 1;
109         }
110
111         const LilvPlugins *all_plugins = lilv_world_get_all_plugins(world);
112         lilv = lilv_plugins_get_by_uri(all_plugins, uri);
113         lilv_node_free(uri);
114         if( !lilv ) {
115                 printf("lv2: lilv_plugins_get_by_uri (%s) failed\n", path);
116                 return 1;
117         }
118
119         if( title ) {
120                 LilvNode *name = lilv_plugin_get_name(lilv);
121                 const char *nm = lilv_node_as_string(name);
122                 sprintf(title, "L2_%s", nm);
123                 lilv_node_free(name);
124         }
125         return 0;
126 }
127
128 int PluginLV2::init_lv2(PluginLV2ClientConfig &conf, int sample_rate, int bfrsz)
129 {
130         reset_lv2();
131
132         lv2_AudioPort       = lilv_new_uri(world, LV2_CORE__AudioPort);
133         lv2_ControlPort     = lilv_new_uri(world, LV2_CORE__ControlPort);
134         lv2_CVPort          = lilv_new_uri(world, LV2_CORE__CVPort);
135         lv2_InputPort       = lilv_new_uri(world, LV2_CORE__InputPort);
136         lv2_OutputPort      = lilv_new_uri(world, LV2_CORE__OutputPort);
137         lv2_Optional        = lilv_new_uri(world, LV2_CORE__connectionOptional);
138         atom_AtomPort       = lilv_new_uri(world, LV2_ATOM__AtomPort);
139         atom_Sequence       = lilv_new_uri(world, LV2_ATOM__Sequence);
140         powerOf2BlockLength = lilv_new_uri(world, LV2_BUF_SIZE__powerOf2BlockLength);
141         fixedBlockLength    = lilv_new_uri(world, LV2_BUF_SIZE__fixedBlockLength);
142         boundedBlockLength  = lilv_new_uri(world, LV2_BUF_SIZE__boundedBlockLength);
143         seq_out = (LV2_Atom_Sequence *) new char[sizeof(LV2_Atom_Sequence) + LV2_SEQ_SIZE];
144
145         conf.init_lv2(lilv, this);
146         nb_inputs = nb_outputs = 0;
147
148         for( int i=0; i<conf.nb_ports; ++i ) {
149                 const LilvPort *lp = lilv_plugin_get_port_by_index(lilv, i);
150                 if( !lp ) continue;
151                 int is_input = lilv_port_is_a(lilv, lp, lv2_InputPort);
152                 if( !is_input && !lilv_port_is_a(lilv, lp, lv2_OutputPort) &&
153                     !lilv_port_has_property(lilv, lp, lv2_Optional) ) {
154                         printf("lv2: not input, not output, and not optional: %s\n", conf.names[i]);
155                         continue;
156                 }
157                 if( is_input && lilv_port_is_a(lilv, lp, lv2_ControlPort) ) {
158                         conf.append(new PluginLV2Client_Opt(&conf, i));
159                         continue;
160                 }
161                 if( lilv_port_is_a(lilv, lp, lv2_AudioPort) ||
162                     lilv_port_is_a(lilv, lp, lv2_CVPort ) ) {
163                         if( is_input ) ++nb_inputs; else ++nb_outputs;
164                         continue;
165                 }
166         }
167
168
169         uri_map.callback_data = (LV2_URI_Map_Callback_Data)this;
170         uri_map.uri_to_id = uri_to_id;
171         features.append(new Lv2Feature(NS_EXT "uri-map", &uri_map));
172         map.handle = (void*)&uri_table;
173         map.map = uri_table_map;
174         features.append(new Lv2Feature(LV2_URID_MAP_URI, &map));
175         unmap.handle = (void*)&uri_table;
176         unmap.unmap  = uri_table_unmap;
177         features.append(new Lv2Feature(LV2_URID_UNMAP_URI, &unmap));
178         features.append(new Lv2Feature(LV2_BUF_SIZE__powerOf2BlockLength, 0));
179         features.append(new Lv2Feature(LV2_BUF_SIZE__fixedBlockLength,    0));
180         features.append(new Lv2Feature(LV2_BUF_SIZE__boundedBlockLength,  0));
181         features.append(new Lv2Feature(LV2_WORKER__schedule, &schedule));
182
183         if( sample_rate < 64 ) sample_rate = samplerate;
184
185         atom_int   = uri_table.map(LV2_ATOM__Int);
186         atom_float = uri_table.map(LV2_ATOM__Float);
187         param_sampleRate = uri_table.map(LV2_PARAMETERS__sampleRate);
188         bufsz_minBlockLength =  uri_table.map(LV2_BUF_SIZE__minBlockLength);
189         bufsz_maxBlockLength = uri_table.map(LV2_BUF_SIZE__maxBlockLength);
190         bufsz_sequenceSize =  uri_table.map(LV2_BUF_SIZE__sequenceSize);
191         ui_updateRate = uri_table.map(LV2_UI__updateRate);
192
193         samplerate = sample_rate;
194         block_length = bfrsz;
195         options.add(param_sampleRate, sizeof(float), atom_float, &samplerate);
196         options.add(bufsz_minBlockLength, sizeof(int), atom_int, &block_length);
197         options.add(bufsz_maxBlockLength, sizeof(int), atom_int, &block_length);
198         options.add(bufsz_sequenceSize, sizeof(int), atom_int, &midi_buf_size);
199         options.add(ui_updateRate, sizeof(float),  atom_float, &refreshrate);
200         options.add(0, 0, 0, 0);
201
202         features.append(new Lv2Feature(LV2_OPTIONS__options,  &options[0]));
203         features.append(0);
204
205         inst = lilv_plugin_instantiate(lilv, sample_rate, features);
206         if( !inst ) {
207                 printf("lv2: lilv_plugin_instantiate failed\n");
208                 return 1;
209         }
210
211         const LV2_Descriptor *lilv_desc = inst->lv2_descriptor;
212         worker_iface = !lilv_desc->extension_data ? 0 :
213                 (LV2_Worker_Interface*)lilv_desc->extension_data(LV2_WORKER__interface);
214         if( worker_iface )
215                 worker_start();
216
217         lilv_instance_activate(inst);
218 // not sure what to do with these
219         max_bufsz = nb_inputs &&
220                 (lilv_plugin_has_feature(lilv, powerOf2BlockLength) ||
221                  lilv_plugin_has_feature(lilv, fixedBlockLength) ||
222                  lilv_plugin_has_feature(lilv, boundedBlockLength)) ? 4096 : 0;
223         return 0;
224 }
225
226 LV2_URID PluginLV2::uri_table_map(LV2_URID_Map_Handle handle, const char *uri)
227 {
228         return ((PluginLV2UriTable *)handle)->map(uri);
229 }
230
231 const char *PluginLV2::uri_table_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
232 {
233         return ((PluginLV2UriTable *)handle)->unmap(urid);
234 }
235
236 uint32_t PluginLV2::uri_to_id(LV2_URI_Map_Callback_Data callback_data,
237         const char *map, const char *uri)
238 {
239         PluginLV2 *the = (PluginLV2 *)callback_data;
240         return the->map.map(the->uri_table, uri);
241 }
242
243 void PluginLV2::connect_ports(PluginLV2ClientConfig &conf, int ports)
244 {
245         int ich = 0, och = 0;
246         for( int i=0; i<conf.nb_ports; ++i ) {
247                 const LilvPort *lp = lilv_plugin_get_port_by_index(lilv, i);
248                 if( !lp ) continue;
249                 int port = conf.ports[i];
250                 if( !(port & ports) ) continue;
251                 if( (port & PORTS_CONTROL) ) {
252                         lilv_instance_connect_port(inst, i, &conf.ctls[i]);
253                         continue;
254                 }
255                 if( (port & PORTS_AUDIO) ) {
256                         if( (port & PORTS_INPUT) ) {
257                                 lilv_instance_connect_port(inst, i, in_buffers[ich]);
258                                 iport[ich++] = i;
259                         }
260                         else if( (port & PORTS_OUTPUT) ) {
261                                 lilv_instance_connect_port(inst, i, out_buffers[och]);
262                                 oport[och++] = i;
263                         }
264                         continue;
265                 }
266                 if( (port & PORTS_ATOM) ) {
267                         if( (port & PORTS_INPUT) )
268                                 lilv_instance_connect_port(inst, i, &seq_in);
269                         else
270                                 lilv_instance_connect_port(inst, i, seq_out);
271                         continue;
272                 }
273         }
274
275         seq_in[0].atom.size = sizeof(LV2_Atom_Sequence_Body);
276         seq_in[0].atom.type = uri_table.map(LV2_ATOM__Sequence);
277         seq_in[1].atom.size = 0;
278         seq_in[1].atom.type = 0;
279         seq_out->atom.size  = LV2_SEQ_SIZE;
280         seq_out->atom.type  = uri_table.map(LV2_ATOM__Chunk);
281 }
282
283 void PluginLV2::del_buffer()
284 {
285         if( shmid >= 0 )
286                 shm_buffer(-1);
287
288         delete [] in_buffers;  in_buffers = 0;
289         delete [] out_buffers;  out_buffers = 0;
290 }
291
292 void PluginLV2::new_buffer(int64_t sz)
293 {
294         uint8_t *bp = 0;
295         if( use_shm ) {  // currently, always uses shm
296                 shmid = shmget(IPC_PRIVATE, sz, IPC_CREAT | 0777);
297                 if( shmid >= 0 ) {
298                         bp = (unsigned char*)shmat(shmid, NULL, 0);
299                         if( bp == (void *) -1 ) { perror("shmat"); bp = 0; }
300                         shmctl(shmid, IPC_RMID, 0); // delete when last ref gone
301                 }
302                 else {
303                         perror("PluginLV2::allocate_buffer: shmget failed\n");
304                         BC_Trace::dump_shm_stats(stdout);
305                 }
306         }
307
308         shm_bfr = (shm_bfr_t *) bp;
309         if( shm_bfr ) shm_bfr->sz = sz;
310 }
311
312 shm_bfr_t *PluginLV2::shm_buffer(int shmid)
313 {
314         if( this->shmid != shmid ) {
315                 if( this->shmid >= 0 ) {
316                         shmdt(shm_bfr);  this->shmid = -1;
317                         shm_bfr = 0;
318                 }
319                 if( shmid >= 0 ) {
320                         shm_bfr = (shm_bfr_t *)shmat(shmid, NULL, 0);
321                         if( shm_bfr == (void *)-1 ) { perror("shmat");  shm_bfr = 0; }
322                         this->shmid = shm_bfr ? shmid : -1;
323                 }
324         }
325         return shm_bfr;
326 }
327
328 void PluginLV2::init_buffer(int samples)
329 {
330         int64_t sz = sizeof(shm_bfr_t) +
331                 sizeof(iport[0])*nb_inputs + sizeof(oport[0])*nb_outputs +
332                 sizeof(*in_buffers[0]) *samples * nb_inputs +
333                 sizeof(*out_buffers[0])*samples * nb_outputs;
334
335         if( shm_bfr ) {
336                 if( shm_bfr->sz < sz ||
337                     shm_bfr->nb_inputs != nb_inputs ||
338                     shm_bfr->nb_outputs != nb_outputs )
339                         del_buffer();
340         }
341
342         if( !shm_bfr )
343                 new_buffer(sz);
344
345         shm_bfr->samples = samples;
346         shm_bfr->done = 0;
347         shm_bfr->nb_inputs = nb_inputs;
348         shm_bfr->nb_outputs = nb_outputs;
349
350         map_buffer();
351 }
352
353 // shm_bfr layout:
354 // struct shm_bfr {
355 //   int64_t sz;
356 //   int samples, done;
357 //   int nb_inputs, nb_outputs;
358 //   int iport[nb_inputs], 
359 //   float in_buffers[samples][nb_inputs];
360 //   int oport[nb_outputs];
361 //   float out_buffers[samples][nb_outputs];
362 // };
363
364 void PluginLV2::map_buffer()
365 {
366         uint8_t *bp = (uint8_t *)(shm_bfr + 1);
367
368         nb_inputs = shm_bfr->nb_inputs;
369         iport = (int *)bp;
370         bp += sizeof(iport[0])*nb_inputs;
371         in_buffers = new float*[nb_inputs];
372         int samples = shm_bfr->samples;
373         for(int i=0; i<nb_inputs; ++i ) {
374                 in_buffers[i] = (float *)bp;
375                 bp += sizeof(*in_buffers[0])*samples;
376         }
377
378         nb_outputs = shm_bfr->nb_outputs;
379         oport = (int *)bp;
380         bp += sizeof(oport[0])*nb_outputs;
381         out_buffers = new float*[nb_outputs];
382         for( int i=0; i<nb_outputs; ++i ) {
383                 out_buffers[i] = (float *)bp;
384                 bp += sizeof(*out_buffers[0])*samples;
385         }
386 }
387
388
389 // LV2 Worker
390
391 PluginLV2Work::PluginLV2Work()
392 {
393         next = 0;
394         alloc = used = 0;
395         data = 0;
396 }
397
398 PluginLV2Work::~PluginLV2Work()
399 {
400         delete [] data;
401 }
402
403 void PluginLV2Work::load(const void *vp, unsigned size)
404 {
405         if( alloc < size ) {
406                 delete [] data;
407                 data = new char[alloc=size];
408         }
409         memcpy(data, vp, used=size);
410 }
411
412 PluginLV2Work *PluginLV2::get_work()
413 {
414 // must hold worker_lock
415         if( !work_avail ) return new PluginLV2Work();
416         PluginLV2Work *wp = work_avail;
417         work_avail = wp->next;  wp->next = 0;
418         return wp;
419 }
420
421 void *PluginLV2::worker_func()
422 {
423         pthread_mutex_lock(&worker_lock);
424         for(;;) {
425                 while( !worker_done && !work_input )
426                         pthread_cond_wait(&worker_ready, &worker_lock);
427                 if( worker_done ) break;
428                 PluginLV2Work *wp = work_input;  work_input = wp->next;
429
430                 pthread_mutex_unlock(&worker_lock);
431                 worker_iface->work(inst, lv2_worker_respond, this, wp->used, wp->data);
432                 pthread_mutex_lock(&worker_lock);
433                 wp->next = work_avail;  work_avail = wp;
434         }
435         pthread_mutex_unlock(&worker_lock);
436         return NULL;
437 }
438 void *PluginLV2::worker_func(void* vp)
439 {
440         PluginLV2 *the = (PluginLV2 *)vp;
441         return the->worker_func();
442 }
443
444 void PluginLV2::worker_start()
445 {
446         pthread_create(&worker_thread, 0, worker_func, this);
447 }
448
449 void PluginLV2::worker_stop()
450 {
451         if( !worker_done ) {
452                 worker_done = 1;
453                 pthread_mutex_lock(&worker_lock);
454                 pthread_cond_signal(&worker_ready);
455                 pthread_mutex_unlock(&worker_lock);
456                 pthread_join(worker_thread, 0);
457                 worker_thread = 0;
458         }
459         work_stop(work_avail);
460         work_stop(work_input);
461         work_stop(work_output);
462         work_tail = &work_output;
463 }
464
465 void PluginLV2::work_stop(PluginLV2Work *&work)
466 {
467         while( work ) {
468                 PluginLV2Work *wp = work;
469                 work = wp->next;
470                 delete wp;
471         }
472 }
473
474 LV2_Worker_Status PluginLV2::worker_schedule(uint32_t inp_size, const void *inp_data)
475 {
476         if( is_forked() ) {
477                 if( !pthread_mutex_trylock(&worker_lock) ) {
478                         PluginLV2Work *wp = get_work();
479                         wp->load(inp_data, inp_size);
480                         wp->next = work_input;  work_input = wp;
481                         pthread_cond_signal(&worker_ready);
482                         pthread_mutex_unlock(&worker_lock);
483                 }
484         }
485         else if( worker_iface )
486                 worker_iface->work(inst, lv2_worker_respond, this, inp_size, inp_data);
487         return LV2_WORKER_SUCCESS;
488 }
489 LV2_Worker_Status PluginLV2::lv2_worker_schedule(LV2_Worker_Schedule_Handle vp,
490                     uint32_t inp_size, const void *inp_data)
491 {
492         PluginLV2 *the = (PluginLV2 *)vp;
493         return the->worker_schedule(inp_size, inp_data);
494 }
495
496 LV2_Worker_Status PluginLV2::worker_respond(uint32_t out_size, const void *out_data)
497 {
498         pthread_mutex_lock(&worker_lock);
499         PluginLV2Work *wp = get_work();
500         wp->load(out_data, out_size);
501         *work_tail = wp;  work_tail = &wp->next;
502         pthread_mutex_unlock(&worker_lock);
503         return LV2_WORKER_SUCCESS;
504 }
505 LV2_Worker_Status PluginLV2::lv2_worker_respond(LV2_Worker_Respond_Handle vp,
506                 uint32_t out_size, const void *out_data)
507 {
508         PluginLV2 *the = (PluginLV2 *)vp;
509         return the->worker_respond(out_size, out_data);
510 }
511
512 void PluginLV2::worker_responses()
513 {
514         pthread_mutex_lock(&worker_lock);
515         while( work_output ) {
516                 PluginLV2Work *rp = work_output;
517                 if( !(work_output=rp->next) ) work_tail = &work_output;
518                 pthread_mutex_unlock(&worker_lock);
519                 worker_iface->work_response(inst, rp->used, rp->data);
520                 pthread_mutex_lock(&worker_lock);
521                 rp->next = work_avail;  work_avail = rp;
522         }
523         pthread_mutex_unlock(&worker_lock);
524 }
525
526 #include "file.h"
527 #include "pluginlv2ui.h"
528
529 PluginLV2ChildUI::PluginLV2ChildUI()
530 {
531         ac = 0;
532         av = 0;
533         gui = 0;
534         hidden = 1;
535 }
536
537 PluginLV2ChildUI::~PluginLV2ChildUI()
538 {
539         delete gui;
540 }
541
542 void PluginLV2ChildUI::run()
543 {
544         ArrayList<char *> av;
545         av.set_array_delete();
546         char arg[BCTEXTLEN];
547         const char *exec_path = File::get_cinlib_path();
548         snprintf(arg, sizeof(arg), "%s/%s", exec_path, "lv2ui");
549         av.append(cstrdup(arg));
550         sprintf(arg, "%d", child_fd);
551         av.append(cstrdup(arg));
552         sprintf(arg, "%d", parent_fd);
553         av.append(cstrdup(arg));
554         sprintf(arg, "%d", ppid);
555         av.append(cstrdup(arg));
556         av.append(0);
557         execv(av[0], &av.values[0]);
558         fprintf(stderr, "execv failed: %s\n %m\n", av.values[0]);
559         av.remove_all_objects();
560         _exit(1);
561 }
562
563 #define LV2_EXTERNAL_UI_URI__KX__Widget "http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget"
564
565 PluginLV2UI::PluginLV2UI()
566 {
567         lilv_ui = 0;
568         lilv_type = 0;
569
570         done = -1;
571         running = 0;
572         updates = 0;
573         hidden = 1;
574         title[0] = 0;
575
576         memset(&uri_map, 0, sizeof(uri_map));
577         memset(&extui_host, 0, sizeof(extui_host));
578         wgt_type = LV2_EXTERNAL_UI_URI__KX__Widget;
579         gtk_type = LV2_UI__GtkUI;
580         ui_type = 0;
581 }
582
583 PluginLV2UI::~PluginLV2UI ()
584 {
585 }
586
587 #endif