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