4 #include "bcwindowbase.h"
11 PluginLV2::PluginLV2()
16 in_buffers = 0; iport = 0;
17 out_buffers = 0; oport = 0;
43 powerOf2BlockLength = 0;
45 boundedBlockLength = 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;
59 PluginLV2::~PluginLV2()
62 if( world ) lilv_world_free(world);
63 pthread_mutex_destroy(&worker_lock);
64 pthread_cond_destroy(&worker_ready);
67 void PluginLV2::reset_lv2()
70 if( inst ) lilv_instance_deactivate(inst);
71 lilv_instance_free(inst); inst = 0;
72 lilv_uis_free(lilv_uis); lilv_uis = 0;
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;
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;
87 delete [] (char *)seq_out; seq_out = 0;
88 uri_table.remove_all_objects();
89 features.remove_all_objects(); ui_features = 0;
94 int PluginLV2::load_lv2(const char *path, char *title)
97 world = lilv_world_new();
99 printf("lv2: lilv_world_new failed\n");
102 lilv_world_load_all(world);
105 LilvNode *uri = lilv_new_uri(world, path);
107 printf("lv2: lilv_new_uri(%s) failed\n", path);
111 const LilvPlugins *all_plugins = lilv_world_get_all_plugins(world);
112 lilv = lilv_plugins_get_by_uri(all_plugins, uri);
115 printf("lv2: lilv_plugins_get_by_uri (%s) failed\n", path);
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);
128 int PluginLV2::init_lv2(PluginLV2ClientConfig &conf, int sample_rate, int bfrsz)
131 double bps = 2. * sample_rate / bfrsz;
132 if( bps > refreshrate ) refreshrate = bps;
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];
147 conf.init_lv2(lilv, this);
148 nb_inputs = nb_outputs = 0;
150 for( int i=0; i<conf.nb_ports; ++i ) {
151 const LilvPort *lp = lilv_plugin_get_port_by_index(lilv, i);
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]);
159 if( is_input && lilv_port_is_a(lilv, lp, lv2_ControlPort) ) {
160 conf.append(new PluginLV2Client_Opt(&conf, i));
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;
171 uri_map.callback_data = (LV2_URI_Map_Callback_Data)this;
172 uri_map.uri_to_id = uri_to_id;
173 features.append(new Lv2Feature(NS_EXT "uri-map", &uri_map));
174 map.handle = (void*)&uri_table;
175 map.map = uri_table_map;
176 features.append(new Lv2Feature(LV2_URID_MAP_URI, &map));
177 unmap.handle = (void*)&uri_table;
178 unmap.unmap = uri_table_unmap;
179 features.append(new Lv2Feature(LV2_URID_UNMAP_URI, &unmap));
180 features.append(new Lv2Feature(LV2_BUF_SIZE__powerOf2BlockLength, 0));
181 features.append(new Lv2Feature(LV2_BUF_SIZE__fixedBlockLength, 0));
182 features.append(new Lv2Feature(LV2_BUF_SIZE__boundedBlockLength, 0));
183 features.append(new Lv2Feature(LV2_WORKER__schedule, &schedule));
185 if( sample_rate < 64 ) sample_rate = samplerate;
187 atom_int = uri_table.map(LV2_ATOM__Int);
188 atom_float = uri_table.map(LV2_ATOM__Float);
189 param_sampleRate = uri_table.map(LV2_PARAMETERS__sampleRate);
190 bufsz_minBlockLength = uri_table.map(LV2_BUF_SIZE__minBlockLength);
191 bufsz_maxBlockLength = uri_table.map(LV2_BUF_SIZE__maxBlockLength);
192 bufsz_sequenceSize = uri_table.map(LV2_BUF_SIZE__sequenceSize);
193 ui_updateRate = uri_table.map(LV2_UI__updateRate);
195 samplerate = sample_rate;
196 block_length = bfrsz;
197 options.add(param_sampleRate, sizeof(float), atom_float, &samplerate);
198 options.add(bufsz_minBlockLength, sizeof(int), atom_int, &block_length);
199 options.add(bufsz_maxBlockLength, sizeof(int), atom_int, &block_length);
200 options.add(bufsz_sequenceSize, sizeof(int), atom_int, &midi_buf_size);
201 options.add(ui_updateRate, sizeof(float), atom_float, &refreshrate);
202 options.add(0, 0, 0, 0);
204 features.append(new Lv2Feature(LV2_OPTIONS__options, &options[0]));
207 inst = lilv_plugin_instantiate(lilv, sample_rate, features);
209 printf("lv2: lilv_plugin_instantiate failed\n");
213 const LV2_Descriptor *lilv_desc = inst->lv2_descriptor;
214 worker_iface = !lilv_desc->extension_data ? 0 :
215 (LV2_Worker_Interface*)lilv_desc->extension_data(LV2_WORKER__interface);
219 lilv_instance_activate(inst);
220 // not sure what to do with these
221 max_bufsz = nb_inputs &&
222 (lilv_plugin_has_feature(lilv, powerOf2BlockLength) ||
223 lilv_plugin_has_feature(lilv, fixedBlockLength) ||
224 lilv_plugin_has_feature(lilv, boundedBlockLength)) ? 4096 : 0;
228 LV2_URID PluginLV2::uri_table_map(LV2_URID_Map_Handle handle, const char *uri)
230 return ((PluginLV2UriTable *)handle)->map(uri);
233 const char *PluginLV2::uri_table_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
235 return ((PluginLV2UriTable *)handle)->unmap(urid);
238 uint32_t PluginLV2::uri_to_id(LV2_URI_Map_Callback_Data callback_data,
239 const char *map, const char *uri)
241 PluginLV2 *the = (PluginLV2 *)callback_data;
242 return the->map.map(the->uri_table, uri);
245 void PluginLV2::connect_ports(PluginLV2ClientConfig &conf, int ports)
247 int ich = 0, och = 0;
248 for( int i=0; i<conf.nb_ports; ++i ) {
249 const LilvPort *lp = lilv_plugin_get_port_by_index(lilv, i);
251 int port = conf.ports[i];
252 if( !(port & ports) ) continue;
253 if( (port & PORTS_CONTROL) ) {
254 lilv_instance_connect_port(inst, i, &conf.ctls[i]);
257 if( (port & PORTS_AUDIO) ) {
258 if( (port & PORTS_INPUT) ) {
259 lilv_instance_connect_port(inst, i, in_buffers[ich]);
262 else if( (port & PORTS_OUTPUT) ) {
263 lilv_instance_connect_port(inst, i, out_buffers[och]);
268 if( (port & PORTS_ATOM) ) {
269 if( (port & PORTS_INPUT) )
270 lilv_instance_connect_port(inst, i, &seq_in);
272 lilv_instance_connect_port(inst, i, seq_out);
277 seq_in[0].atom.size = sizeof(LV2_Atom_Sequence_Body);
278 seq_in[0].atom.type = uri_table.map(LV2_ATOM__Sequence);
279 seq_in[1].atom.size = 0;
280 seq_in[1].atom.type = 0;
281 seq_out->atom.size = LV2_SEQ_SIZE;
282 seq_out->atom.type = uri_table.map(LV2_ATOM__Chunk);
285 void PluginLV2::del_buffer()
290 delete [] in_buffers; in_buffers = 0;
291 delete [] out_buffers; out_buffers = 0;
294 void PluginLV2::new_buffer(int64_t sz)
297 if( use_shm ) { // currently, always uses shm
298 shmid = shmget(IPC_PRIVATE, sz, IPC_CREAT | 0777);
300 bp = (unsigned char*)shmat(shmid, NULL, 0);
301 if( bp == (void *) -1 ) { perror("shmat"); bp = 0; }
302 shmctl(shmid, IPC_RMID, 0); // delete when last ref gone
305 perror("PluginLV2::allocate_buffer: shmget failed\n");
306 BC_Trace::dump_shm_stats(stdout);
310 shm_bfr = (shm_bfr_t *) bp;
311 if( shm_bfr ) shm_bfr->sz = sz;
314 shm_bfr_t *PluginLV2::shm_buffer(int shmid)
316 if( this->shmid != shmid ) {
317 if( this->shmid >= 0 ) {
318 shmdt(shm_bfr); this->shmid = -1;
322 shm_bfr = (shm_bfr_t *)shmat(shmid, NULL, 0);
323 if( shm_bfr == (void *)-1 ) { perror("shmat"); shm_bfr = 0; }
324 this->shmid = shm_bfr ? shmid : -1;
330 void PluginLV2::init_buffer(int samples)
332 int64_t sz = sizeof(shm_bfr_t) +
333 sizeof(iport[0])*nb_inputs + sizeof(oport[0])*nb_outputs +
334 sizeof(*in_buffers[0]) *samples * nb_inputs +
335 sizeof(*out_buffers[0])*samples * nb_outputs;
338 if( shm_bfr->sz < sz ||
339 shm_bfr->nb_inputs != nb_inputs ||
340 shm_bfr->nb_outputs != nb_outputs )
347 shm_bfr->samples = samples;
349 shm_bfr->nb_inputs = nb_inputs;
350 shm_bfr->nb_outputs = nb_outputs;
358 // int samples, done;
359 // int nb_inputs, nb_outputs;
360 // int iport[nb_inputs],
361 // float in_buffers[samples][nb_inputs];
362 // int oport[nb_outputs];
363 // float out_buffers[samples][nb_outputs];
366 void PluginLV2::map_buffer()
368 uint8_t *bp = (uint8_t *)(shm_bfr + 1);
370 nb_inputs = shm_bfr->nb_inputs;
372 bp += sizeof(iport[0])*nb_inputs;
373 in_buffers = new float*[nb_inputs];
374 int samples = shm_bfr->samples;
375 for(int i=0; i<nb_inputs; ++i ) {
376 in_buffers[i] = (float *)bp;
377 bp += sizeof(*in_buffers[0])*samples;
380 nb_outputs = shm_bfr->nb_outputs;
382 bp += sizeof(oport[0])*nb_outputs;
383 out_buffers = new float*[nb_outputs];
384 for( int i=0; i<nb_outputs; ++i ) {
385 out_buffers[i] = (float *)bp;
386 bp += sizeof(*out_buffers[0])*samples;
393 PluginLV2Work::PluginLV2Work()
400 PluginLV2Work::~PluginLV2Work()
405 void PluginLV2Work::load(const void *vp, unsigned size)
409 data = new char[alloc=size];
411 memcpy(data, vp, used=size);
414 PluginLV2Work *PluginLV2::get_work()
416 // must hold worker_lock
417 if( !work_avail ) return new PluginLV2Work();
418 PluginLV2Work *wp = work_avail;
419 work_avail = wp->next; wp->next = 0;
423 void *PluginLV2::worker_func()
425 pthread_mutex_lock(&worker_lock);
427 while( !worker_done && !work_input )
428 pthread_cond_wait(&worker_ready, &worker_lock);
429 if( worker_done ) break;
430 PluginLV2Work *wp = work_input; work_input = wp->next;
432 pthread_mutex_unlock(&worker_lock);
433 worker_iface->work(inst, lv2_worker_respond, this, wp->used, wp->data);
434 pthread_mutex_lock(&worker_lock);
435 wp->next = work_avail; work_avail = wp;
437 pthread_mutex_unlock(&worker_lock);
440 void *PluginLV2::worker_func(void* vp)
442 PluginLV2 *the = (PluginLV2 *)vp;
443 return the->worker_func();
446 void PluginLV2::worker_start()
448 pthread_create(&worker_thread, 0, worker_func, this);
451 void PluginLV2::worker_stop()
455 pthread_mutex_lock(&worker_lock);
456 pthread_cond_signal(&worker_ready);
457 pthread_mutex_unlock(&worker_lock);
458 pthread_join(worker_thread, 0);
461 work_stop(work_avail);
462 work_stop(work_input);
463 work_stop(work_output);
464 work_tail = &work_output;
467 void PluginLV2::work_stop(PluginLV2Work *&work)
470 PluginLV2Work *wp = work;
476 LV2_Worker_Status PluginLV2::worker_schedule(uint32_t inp_size, const void *inp_data)
479 if( !pthread_mutex_trylock(&worker_lock) ) {
480 PluginLV2Work *wp = get_work();
481 wp->load(inp_data, inp_size);
482 wp->next = work_input; work_input = wp;
483 pthread_cond_signal(&worker_ready);
484 pthread_mutex_unlock(&worker_lock);
487 else if( worker_iface )
488 worker_iface->work(inst, lv2_worker_respond, this, inp_size, inp_data);
489 return LV2_WORKER_SUCCESS;
491 LV2_Worker_Status PluginLV2::lv2_worker_schedule(LV2_Worker_Schedule_Handle vp,
492 uint32_t inp_size, const void *inp_data)
494 PluginLV2 *the = (PluginLV2 *)vp;
495 return the->worker_schedule(inp_size, inp_data);
498 LV2_Worker_Status PluginLV2::worker_respond(uint32_t out_size, const void *out_data)
500 pthread_mutex_lock(&worker_lock);
501 PluginLV2Work *wp = get_work();
502 wp->load(out_data, out_size);
503 *work_tail = wp; work_tail = &wp->next;
504 pthread_mutex_unlock(&worker_lock);
505 return LV2_WORKER_SUCCESS;
507 LV2_Worker_Status PluginLV2::lv2_worker_respond(LV2_Worker_Respond_Handle vp,
508 uint32_t out_size, const void *out_data)
510 PluginLV2 *the = (PluginLV2 *)vp;
511 return the->worker_respond(out_size, out_data);
514 void PluginLV2::worker_responses()
516 pthread_mutex_lock(&worker_lock);
517 while( work_output ) {
518 PluginLV2Work *rp = work_output;
519 if( !(work_output=rp->next) ) work_tail = &work_output;
520 pthread_mutex_unlock(&worker_lock);
521 worker_iface->work_response(inst, rp->used, rp->data);
522 pthread_mutex_lock(&worker_lock);
523 rp->next = work_avail; work_avail = rp;
525 pthread_mutex_unlock(&worker_lock);
529 #include "pluginlv2ui.h"
531 PluginLV2ChildUI::PluginLV2ChildUI()
539 PluginLV2ChildUI::~PluginLV2ChildUI()
544 void PluginLV2ChildUI::run()
546 ArrayList<char *> av;
547 av.set_array_delete();
549 const char *exec_path = File::get_cinlib_path();
550 snprintf(arg, sizeof(arg), "%s/%s", exec_path, "lv2ui");
551 av.append(cstrdup(arg));
552 sprintf(arg, "%d", child_fd);
553 av.append(cstrdup(arg));
554 sprintf(arg, "%d", parent_fd);
555 av.append(cstrdup(arg));
556 sprintf(arg, "%d", ppid);
557 av.append(cstrdup(arg));
559 execv(av[0], &av.values[0]);
560 fprintf(stderr, "execv failed: %s\n %m\n", av.values[0]);
561 av.remove_all_objects();
565 #define LV2_EXTERNAL_UI_URI__KX__Widget "http://kxstudio.sf.net/ns/lv2ext/external-ui#Widget"
567 PluginLV2UI::PluginLV2UI()
578 memset(&uri_map, 0, sizeof(uri_map));
579 memset(&extui_host, 0, sizeof(extui_host));
580 wgt_type = LV2_EXTERNAL_UI_URI__KX__Widget;
581 gtk_type = LV2_UI__GtkUI;
585 PluginLV2UI::~PluginLV2UI ()