upgrade libvpx+lv2, fix dbl tap play bug, add multi nest/unnest clips, add del top...
[goodguy/cinelerra.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         double bps = 2. * sample_rate / bfrsz;
132         if( bps > refreshrate ) refreshrate = bps;
133
134         lv2_AudioPort       = lilv_new_uri(world, LV2_CORE__AudioPort);
135         lv2_ControlPort     = lilv_new_uri(world, LV2_CORE__ControlPort);
136         lv2_CVPort          = lilv_new_uri(world, LV2_CORE__CVPort);
137         lv2_InputPort       = lilv_new_uri(world, LV2_CORE__InputPort);
138         lv2_OutputPort      = lilv_new_uri(world, LV2_CORE__OutputPort);
139         lv2_Optional        = lilv_new_uri(world, LV2_CORE__connectionOptional);
140         atom_AtomPort       = lilv_new_uri(world, LV2_ATOM__AtomPort);
141         atom_Sequence       = lilv_new_uri(world, LV2_ATOM__Sequence);
142         powerOf2BlockLength = lilv_new_uri(world, LV2_BUF_SIZE__powerOf2BlockLength);
143         fixedBlockLength    = lilv_new_uri(world, LV2_BUF_SIZE__fixedBlockLength);
144         boundedBlockLength  = lilv_new_uri(world, LV2_BUF_SIZE__boundedBlockLength);
145         seq_out = (LV2_Atom_Sequence *) new char[sizeof(LV2_Atom_Sequence) + LV2_SEQ_SIZE];
146
147         conf.init_lv2(lilv, this);
148         nb_inputs = nb_outputs = 0;
149
150         for( int i=0; i<conf.nb_ports; ++i ) {
151                 const LilvPort *lp = lilv_plugin_get_port_by_index(lilv, i);
152                 if( !lp ) continue;
153                 int is_input = lilv_port_is_a(lilv, lp, lv2_InputPort);
154                 if( !is_input && !lilv_port_is_a(lilv, lp, lv2_OutputPort) &&
155                     !lilv_port_has_property(lilv, lp, lv2_Optional) ) {
156                         printf("lv2: not input, not output, and not optional: %s\n", conf.names[i]);
157                         continue;
158                 }
159                 if( is_input && lilv_port_is_a(lilv, lp, lv2_ControlPort) ) {
160                         conf.append(new PluginLV2Client_Opt(&conf, i));
161                         continue;
162                 }
163                 if( lilv_port_is_a(lilv, lp, lv2_AudioPort) ||
164                     lilv_port_is_a(lilv, lp, lv2_CVPort ) ) {
165                         if( is_input ) ++nb_inputs; else ++nb_outputs;
166                         continue;
167                 }
168         }
169
170         uri_map.handle = (LV2_URID_Map_Handle)this;
171         uri_map.map = map_uri;
172         features.append(new Lv2Feature(LV2_URID__map, &uri_map));
173         uri_unmap.handle = (LV2_URID_Unmap_Handle)this;
174         uri_unmap.unmap = unmap_uri;
175         features.append(new Lv2Feature(LV2_URID__unmap, &uri_unmap));
176         features.append(new Lv2Feature(LV2_BUF_SIZE__powerOf2BlockLength, 0));
177         features.append(new Lv2Feature(LV2_BUF_SIZE__fixedBlockLength,    0));
178         features.append(new Lv2Feature(LV2_BUF_SIZE__boundedBlockLength,  0));
179         features.append(new Lv2Feature(LV2_WORKER__schedule, &schedule));
180
181         if( sample_rate < 64 ) sample_rate = samplerate;
182
183         atom_int   = uri_table.map(LV2_ATOM__Int);
184         atom_float = uri_table.map(LV2_ATOM__Float);
185         param_sampleRate = uri_table.map(LV2_PARAMETERS__sampleRate);
186         bufsz_minBlockLength =  uri_table.map(LV2_BUF_SIZE__minBlockLength);
187         bufsz_maxBlockLength = uri_table.map(LV2_BUF_SIZE__maxBlockLength);
188         bufsz_sequenceSize =  uri_table.map(LV2_BUF_SIZE__sequenceSize);
189         ui_updateRate = uri_table.map(LV2_UI__updateRate);
190
191         samplerate = sample_rate;
192         block_length = bfrsz;
193         options.add(param_sampleRate, sizeof(float), atom_float, &samplerate);
194         options.add(bufsz_minBlockLength, sizeof(int), atom_int, &block_length);
195         options.add(bufsz_maxBlockLength, sizeof(int), atom_int, &block_length);
196         options.add(bufsz_sequenceSize, sizeof(int), atom_int, &midi_buf_size);
197         options.add(ui_updateRate, sizeof(float),  atom_float, &refreshrate);
198         options.add(0, 0, 0, 0);
199
200         features.append(new Lv2Feature(LV2_OPTIONS__options,  &options[0]));
201         features.append(0);
202
203         inst = lilv_plugin_instantiate(lilv, sample_rate, features);
204         if( !inst ) {
205                 printf("lv2: lilv_plugin_instantiate failed\n");
206                 return 1;
207         }
208
209         const LV2_Descriptor *lilv_desc = inst->lv2_descriptor;
210         worker_iface = !lilv_desc->extension_data ? 0 :
211                 (LV2_Worker_Interface*)lilv_desc->extension_data(LV2_WORKER__interface);
212         if( worker_iface )
213                 worker_start();
214
215         lilv_instance_activate(inst);
216 // not sure what to do with these
217         max_bufsz = nb_inputs &&
218                 (lilv_plugin_has_feature(lilv, powerOf2BlockLength) ||
219                  lilv_plugin_has_feature(lilv, fixedBlockLength) ||
220                  lilv_plugin_has_feature(lilv, boundedBlockLength)) ? 4096 : 0;
221         return 0;
222 }
223
224 uint32_t PluginLV2::map_uri(LV2_URID_Map_Handle handle, const char *uri)
225 {
226         PluginLV2 *the = (PluginLV2 *)handle;
227         return the->uri_table.map(uri);
228 }
229
230 const char *PluginLV2::unmap_uri(LV2_URID_Unmap_Handle handle, LV2_URID urid)
231 {
232         PluginLV2 *the = (PluginLV2 *)handle;
233         return the->uri_table.unmap(urid);
234 }
235
236 void PluginLV2::connect_ports(PluginLV2ClientConfig &conf, int ports)
237 {
238         int ich = 0, och = 0;
239         for( int i=0; i<conf.nb_ports; ++i ) {
240                 const LilvPort *lp = lilv_plugin_get_port_by_index(lilv, i);
241                 if( !lp ) continue;
242                 int port = conf.ports[i];
243                 if( !(port & ports) ) continue;
244                 if( (port & PORTS_CONTROL) ) {
245                         lilv_instance_connect_port(inst, i, &conf.ctls[i]);
246                         continue;
247                 }
248                 if( (port & PORTS_AUDIO) ) {
249                         if( (port & PORTS_INPUT) ) {
250                                 lilv_instance_connect_port(inst, i, in_buffers[ich]);
251                                 iport[ich++] = i;
252                         }
253                         else if( (port & PORTS_OUTPUT) ) {
254                                 lilv_instance_connect_port(inst, i, out_buffers[och]);
255                                 oport[och++] = i;
256                         }
257                         continue;
258                 }
259                 if( (port & PORTS_ATOM) ) {
260                         if( (port & PORTS_INPUT) )
261                                 lilv_instance_connect_port(inst, i, &seq_in);
262                         else
263                                 lilv_instance_connect_port(inst, i, seq_out);
264                         continue;
265                 }
266         }
267
268         seq_in[0].atom.size = sizeof(LV2_Atom_Sequence_Body);
269         seq_in[0].atom.type = uri_table.map(LV2_ATOM__Sequence);
270         seq_in[1].atom.size = 0;
271         seq_in[1].atom.type = 0;
272         seq_out->atom.size  = LV2_SEQ_SIZE;
273         seq_out->atom.type  = uri_table.map(LV2_ATOM__Chunk);
274 }
275
276 void PluginLV2::del_buffer()
277 {
278         if( shmid >= 0 )
279                 shm_buffer(-1);
280
281         delete [] in_buffers;  in_buffers = 0;
282         delete [] out_buffers;  out_buffers = 0;
283 }
284
285 void PluginLV2::new_buffer(int64_t sz)
286 {
287         uint8_t *bp = 0;
288         if( use_shm ) {  // currently, always uses shm
289                 shmid = shmget(IPC_PRIVATE, sz, IPC_CREAT | 0777);
290                 if( shmid >= 0 ) {
291                         bp = (unsigned char*)shmat(shmid, NULL, 0);
292                         if( bp == (void *) -1 ) { perror("shmat"); bp = 0; }
293                         shmctl(shmid, IPC_RMID, 0); // delete when last ref gone
294                 }
295                 else {
296                         perror("PluginLV2::allocate_buffer: shmget failed\n");
297                         BC_Trace::dump_shm_stats(stdout);
298                 }
299         }
300
301         shm_bfr = (shm_bfr_t *) bp;
302         if( shm_bfr ) shm_bfr->sz = sz;
303 }
304
305 shm_bfr_t *PluginLV2::shm_buffer(int shmid)
306 {
307         if( this->shmid != shmid ) {
308                 if( this->shmid >= 0 ) {
309                         shmdt(shm_bfr);  this->shmid = -1;
310                         shm_bfr = 0;
311                 }
312                 if( shmid >= 0 ) {
313                         shm_bfr = (shm_bfr_t *)shmat(shmid, NULL, 0);
314                         if( shm_bfr == (void *)-1 ) { perror("shmat");  shm_bfr = 0; }
315                         this->shmid = shm_bfr ? shmid : -1;
316                 }
317         }
318         return shm_bfr;
319 }
320
321 void PluginLV2::init_buffer(int samples)
322 {
323         int64_t sz = sizeof(shm_bfr_t) +
324                 sizeof(iport[0])*nb_inputs + sizeof(oport[0])*nb_outputs +
325                 sizeof(*in_buffers[0]) *samples * nb_inputs +
326                 sizeof(*out_buffers[0])*samples * nb_outputs;
327
328         if( shm_bfr ) {
329                 if( shm_bfr->sz < sz ||
330                     shm_bfr->nb_inputs != nb_inputs ||
331                     shm_bfr->nb_outputs != nb_outputs )
332                         del_buffer();
333         }
334
335         if( !shm_bfr )
336                 new_buffer(sz);
337
338         shm_bfr->samples = samples;
339         shm_bfr->done = 0;
340         shm_bfr->nb_inputs = nb_inputs;
341         shm_bfr->nb_outputs = nb_outputs;
342
343         map_buffer();
344 }
345
346 // shm_bfr layout:
347 // struct shm_bfr {
348 //   int64_t sz;
349 //   int samples, done;
350 //   int nb_inputs, nb_outputs;
351 //   int iport[nb_inputs], 
352 //   float in_buffers[samples][nb_inputs];
353 //   int oport[nb_outputs];
354 //   float out_buffers[samples][nb_outputs];
355 // };
356
357 void PluginLV2::map_buffer()
358 {
359         uint8_t *bp = (uint8_t *)(shm_bfr + 1);
360
361         nb_inputs = shm_bfr->nb_inputs;
362         iport = (int *)bp;
363         bp += sizeof(iport[0])*nb_inputs;
364         in_buffers = new float*[nb_inputs];
365         int samples = shm_bfr->samples;
366         for(int i=0; i<nb_inputs; ++i ) {
367                 in_buffers[i] = (float *)bp;
368                 bp += sizeof(*in_buffers[0])*samples;
369         }
370
371         nb_outputs = shm_bfr->nb_outputs;
372         oport = (int *)bp;
373         bp += sizeof(oport[0])*nb_outputs;
374         out_buffers = new float*[nb_outputs];
375         for( int i=0; i<nb_outputs; ++i ) {
376                 out_buffers[i] = (float *)bp;
377                 bp += sizeof(*out_buffers[0])*samples;
378         }
379 }
380
381
382 // LV2 Worker
383
384 PluginLV2Work::PluginLV2Work()
385 {
386         next = 0;
387         alloc = used = 0;
388         data = 0;
389 }
390
391 PluginLV2Work::~PluginLV2Work()
392 {
393         delete [] data;
394 }
395
396 void PluginLV2Work::load(const void *vp, unsigned size)
397 {
398         if( alloc < size ) {
399                 delete [] data;
400                 data = new char[alloc=size];
401         }
402         memcpy(data, vp, used=size);
403 }
404
405 PluginLV2Work *PluginLV2::get_work()
406 {
407 // must hold worker_lock
408         if( !work_avail ) return new PluginLV2Work();
409         PluginLV2Work *wp = work_avail;
410         work_avail = wp->next;  wp->next = 0;
411         return wp;
412 }
413
414 void *PluginLV2::worker_func()
415 {
416         pthread_mutex_lock(&worker_lock);
417         for(;;) {
418                 while( !worker_done && !work_input )
419                         pthread_cond_wait(&worker_ready, &worker_lock);
420                 if( worker_done ) break;
421                 PluginLV2Work *wp = work_input;  work_input = wp->next;
422
423                 pthread_mutex_unlock(&worker_lock);
424                 worker_iface->work(inst, lv2_worker_respond, this, wp->used, wp->data);
425                 pthread_mutex_lock(&worker_lock);
426                 wp->next = work_avail;  work_avail = wp;
427         }
428         pthread_mutex_unlock(&worker_lock);
429         return NULL;
430 }
431 void *PluginLV2::worker_func(void* vp)
432 {
433         PluginLV2 *the = (PluginLV2 *)vp;
434         return the->worker_func();
435 }
436
437 void PluginLV2::worker_start()
438 {
439         pthread_create(&worker_thread, 0, worker_func, this);
440 }
441
442 void PluginLV2::worker_stop()
443 {
444         if( !worker_done ) {
445                 worker_done = 1;
446                 pthread_mutex_lock(&worker_lock);
447                 pthread_cond_signal(&worker_ready);
448                 pthread_mutex_unlock(&worker_lock);
449                 pthread_join(worker_thread, 0);
450                 worker_thread = 0;
451         }
452         work_stop(work_avail);
453         work_stop(work_input);
454         work_stop(work_output);
455         work_tail = &work_output;
456 }
457
458 void PluginLV2::work_stop(PluginLV2Work *&work)
459 {
460         while( work ) {
461                 PluginLV2Work *wp = work;
462                 work = wp->next;
463                 delete wp;
464         }
465 }
466
467 LV2_Worker_Status PluginLV2::worker_schedule(uint32_t inp_size, const void *inp_data)
468 {
469         if( is_forked() ) {
470                 if( !pthread_mutex_trylock(&worker_lock) ) {
471                         PluginLV2Work *wp = get_work();
472                         wp->load(inp_data, inp_size);
473                         wp->next = work_input;  work_input = wp;
474                         pthread_cond_signal(&worker_ready);
475                         pthread_mutex_unlock(&worker_lock);
476                 }
477         }
478         else if( worker_iface )
479                 worker_iface->work(inst, lv2_worker_respond, this, inp_size, inp_data);
480         return LV2_WORKER_SUCCESS;
481 }
482 LV2_Worker_Status PluginLV2::lv2_worker_schedule(LV2_Worker_Schedule_Handle vp,
483                     uint32_t inp_size, const void *inp_data)
484 {
485         PluginLV2 *the = (PluginLV2 *)vp;
486         return the->worker_schedule(inp_size, inp_data);
487 }
488
489 LV2_Worker_Status PluginLV2::worker_respond(uint32_t out_size, const void *out_data)
490 {
491         pthread_mutex_lock(&worker_lock);
492         PluginLV2Work *wp = get_work();
493         wp->load(out_data, out_size);
494         *work_tail = wp;  work_tail = &wp->next;
495         pthread_mutex_unlock(&worker_lock);
496         return LV2_WORKER_SUCCESS;
497 }
498 LV2_Worker_Status PluginLV2::lv2_worker_respond(LV2_Worker_Respond_Handle vp,
499                 uint32_t out_size, const void *out_data)
500 {
501         PluginLV2 *the = (PluginLV2 *)vp;
502         return the->worker_respond(out_size, out_data);
503 }
504
505 void PluginLV2::worker_responses()
506 {
507         pthread_mutex_lock(&worker_lock);
508         while( work_output ) {
509                 PluginLV2Work *rp = work_output;
510                 if( !(work_output=rp->next) ) work_tail = &work_output;
511                 pthread_mutex_unlock(&worker_lock);
512                 worker_iface->work_response(inst, rp->used, rp->data);
513                 pthread_mutex_lock(&worker_lock);
514                 rp->next = work_avail;  work_avail = rp;
515         }
516         pthread_mutex_unlock(&worker_lock);
517 }
518
519 #include "file.h"
520 #include "pluginlv2ui.h"
521
522 PluginLV2ChildUI::PluginLV2ChildUI()
523 {
524         ac = 0;
525         av = 0;
526         gui = 0;
527         hidden = 1;
528 }
529
530 PluginLV2ChildUI::~PluginLV2ChildUI()
531 {
532         delete gui;
533 }
534
535 void PluginLV2ChildUI::run()
536 {
537         ArrayList<char *> av;
538         av.set_array_delete();
539         char arg[BCTEXTLEN];
540         const char *exec_path = File::get_cinlib_path();
541         snprintf(arg, sizeof(arg), "%s/%s", exec_path, "lv2ui");
542         av.append(cstrdup(arg));
543         sprintf(arg, "%d", child_fd);
544         av.append(cstrdup(arg));
545         sprintf(arg, "%d", parent_fd);
546         av.append(cstrdup(arg));
547         sprintf(arg, "%d", ppid);
548         av.append(cstrdup(arg));
549         av.append(0);
550         execv(av[0], &av.values[0]);
551         fprintf(stderr, "execv failed: %s\n %m\n", av.values[0]);
552         av.remove_all_objects();
553         _exit(1);
554 }
555
556 #define LV2_EXTERNAL_UI_URI__KX__Widget "http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget"
557
558 PluginLV2UI::PluginLV2UI()
559 {
560         lilv_ui = 0;
561         lilv_type = 0;
562
563         done = -1;
564         running = 0;
565         updates = 0;
566         hidden = 1;
567         title[0] = 0;
568
569         memset(&uri_map, 0, sizeof(uri_map));
570         memset(&uri_unmap, 0, sizeof(uri_unmap));
571         memset(&extui_host, 0, sizeof(extui_host));
572         wgt_type = LV2_EXTERNAL_UI_URI__KX__Widget;
573         gtk_type = LV2_UI__GtkUI;
574         ui_type = 0;
575 }
576
577 PluginLV2UI::~PluginLV2UI ()
578 {
579 }
580
581 #endif