1 /* GSequencer - Advanced GTK Sequencer
2  * Copyright (C) 2005-2020 Joël Krähemann
3  *
4  * This file is part of GSequencer.
5  *
6  * GSequencer is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSequencer is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSequencer.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <ags/audio/fx/ags_fx_lv2_channel.h>
21 
22 #include <ags/plugin/ags_lv2_manager.h>
23 #include <ags/plugin/ags_lv2_plugin.h>
24 #include <ags/plugin/ags_base_plugin.h>
25 #include <ags/plugin/ags_plugin_port.h>
26 
27 #include <ags/audio/ags_port_util.h>
28 
29 #include <ags/audio/fx/ags_fx_lv2_audio.h>
30 
31 #include <ags/i18n.h>
32 
33 void ags_fx_lv2_channel_class_init(AgsFxLv2ChannelClass *fx_lv2_channel);
34 void ags_fx_lv2_channel_init(AgsFxLv2Channel *fx_lv2_channel);
35 void ags_fx_lv2_channel_dispose(GObject *gobject);
36 void ags_fx_lv2_channel_finalize(GObject *gobject);
37 
38 void ags_fx_lv2_channel_notify_buffer_size_callback(GObject *gobject,
39 						    GParamSpec *pspec,
40 						    gpointer user_data);
41 void ags_fx_lv2_channel_notify_samplerate_callback(GObject *gobject,
42 						   GParamSpec *pspec,
43 						   gpointer user_data);
44 
45 /**
46  * SECTION:ags_fx_lv2_channel
47  * @short_description: fx lv2 channel
48  * @title: AgsFxLv2Channel
49  * @section_id:
50  * @include: ags/audio/fx/ags_fx_lv2_channel.h
51  *
52  * The #AgsFxLv2Channel class provides ports to the effect processor.
53  */
54 
55 static gpointer ags_fx_lv2_channel_parent_class = NULL;
56 
57 const gchar *ags_fx_lv2_channel_plugin_name = "ags-fx-lv2";
58 
59 GType
ags_fx_lv2_channel_get_type()60 ags_fx_lv2_channel_get_type()
61 {
62   static volatile gsize g_define_type_id__volatile = 0;
63 
64   if(g_once_init_enter (&g_define_type_id__volatile)){
65     GType ags_type_fx_lv2_channel = 0;
66 
67     static const GTypeInfo ags_fx_lv2_channel_info = {
68       sizeof (AgsFxLv2ChannelClass),
69       NULL, /* base_init */
70       NULL, /* base_finalize */
71       (GClassInitFunc) ags_fx_lv2_channel_class_init,
72       NULL, /* class_finalize */
73       NULL, /* class_channel */
74       sizeof (AgsFxLv2Channel),
75       0,    /* n_preallocs */
76       (GInstanceInitFunc) ags_fx_lv2_channel_init,
77     };
78 
79     ags_type_fx_lv2_channel = g_type_register_static(AGS_TYPE_FX_NOTATION_CHANNEL,
80 						     "AgsFxLv2Channel",
81 						     &ags_fx_lv2_channel_info,
82 						     0);
83 
84     g_once_init_leave(&g_define_type_id__volatile, ags_type_fx_lv2_channel);
85   }
86 
87   return g_define_type_id__volatile;
88 }
89 
90 void
ags_fx_lv2_channel_class_init(AgsFxLv2ChannelClass * fx_lv2_channel)91 ags_fx_lv2_channel_class_init(AgsFxLv2ChannelClass *fx_lv2_channel)
92 {
93   GObjectClass *gobject;
94 
95   ags_fx_lv2_channel_parent_class = g_type_class_peek_parent(fx_lv2_channel);
96 
97   /* GObjectClass */
98   gobject = (GObjectClass *) fx_lv2_channel;
99 
100   gobject->dispose = ags_fx_lv2_channel_dispose;
101   gobject->finalize = ags_fx_lv2_channel_finalize;
102 }
103 
104 void
ags_fx_lv2_channel_init(AgsFxLv2Channel * fx_lv2_channel)105 ags_fx_lv2_channel_init(AgsFxLv2Channel *fx_lv2_channel)
106 {
107   guint i;
108 
109   g_signal_connect(fx_lv2_channel, "notify::buffer-size",
110 		   G_CALLBACK(ags_fx_lv2_channel_notify_buffer_size_callback), NULL);
111 
112   g_signal_connect(fx_lv2_channel, "notify::samplerate",
113 		   G_CALLBACK(ags_fx_lv2_channel_notify_samplerate_callback), NULL);
114 
115   AGS_RECALL(fx_lv2_channel)->name = "ags-fx-lv2";
116   AGS_RECALL(fx_lv2_channel)->version = AGS_RECALL_DEFAULT_VERSION;
117   AGS_RECALL(fx_lv2_channel)->build_id = AGS_RECALL_DEFAULT_BUILD_ID;
118   AGS_RECALL(fx_lv2_channel)->xml_type = "ags-fx-lv2-channel";
119 
120   fx_lv2_channel->output_port_count = 0;
121   fx_lv2_channel->output_port = NULL;
122 
123   fx_lv2_channel->input_port_count = 0;
124   fx_lv2_channel->input_port = NULL;
125 
126   /* input data */
127   for(i = 0; i < AGS_SOUND_SCOPE_LAST; i++){
128     fx_lv2_channel->input_data[i] = ags_fx_lv2_channel_input_data_alloc();
129 
130     fx_lv2_channel->input_data[i]->parent = fx_lv2_channel;
131   }
132 
133   fx_lv2_channel->lv2_plugin = NULL;
134 
135   fx_lv2_channel->lv2_port = NULL;
136 }
137 
138 void
ags_fx_lv2_channel_dispose(GObject * gobject)139 ags_fx_lv2_channel_dispose(GObject *gobject)
140 {
141   AgsFxLv2Channel *fx_lv2_channel;
142 
143   fx_lv2_channel = AGS_FX_LV2_CHANNEL(gobject);
144 
145   /* call parent */
146   G_OBJECT_CLASS(ags_fx_lv2_channel_parent_class)->dispose(gobject);
147 }
148 
149 void
ags_fx_lv2_channel_finalize(GObject * gobject)150 ags_fx_lv2_channel_finalize(GObject *gobject)
151 {
152   AgsPort **iter;
153   AgsFxLv2Channel *fx_lv2_channel;
154 
155   guint i;
156 
157   fx_lv2_channel = AGS_FX_LV2_CHANNEL(gobject);
158 
159   g_free(fx_lv2_channel->output_port);
160   g_free(fx_lv2_channel->input_port);
161 
162   for(i = 0; i < AGS_SOUND_SCOPE_LAST; i++){
163     ags_fx_lv2_channel_input_data_free(fx_lv2_channel->input_data[i]);
164   }
165 
166   if(fx_lv2_channel->lv2_plugin != NULL){
167     g_object_unref(fx_lv2_channel->lv2_plugin);
168   }
169 
170   if(fx_lv2_channel->lv2_port != NULL){
171     for(iter = fx_lv2_channel->lv2_port; iter[0] != NULL; iter++){
172       g_object_unref(iter[0]);
173     }
174 
175     g_free(fx_lv2_channel->lv2_port);
176   }
177 
178   /* call parent */
179   G_OBJECT_CLASS(ags_fx_lv2_channel_parent_class)->finalize(gobject);
180 }
181 
182 void
ags_fx_lv2_channel_notify_buffer_size_callback(GObject * gobject,GParamSpec * pspec,gpointer user_data)183 ags_fx_lv2_channel_notify_buffer_size_callback(GObject *gobject,
184 					       GParamSpec *pspec,
185 					       gpointer user_data)
186 {
187   AgsFxLv2Channel *fx_lv2_channel;
188 
189   guint output_port_count, input_port_count;
190   guint buffer_size;
191   guint i;
192 
193   GRecMutex *recall_mutex;
194 
195   fx_lv2_channel = AGS_FX_LV2_CHANNEL(gobject);
196 
197   /* get recall mutex */
198   recall_mutex = AGS_RECALL_GET_OBJ_MUTEX(fx_lv2_channel);
199 
200   /* get buffer size */
201   g_object_get(fx_lv2_channel,
202 	       "buffer-size", &buffer_size,
203 	       NULL);
204 
205   /* reallocate buffer - apply buffer size */
206   g_rec_mutex_lock(recall_mutex);
207 
208   output_port_count = fx_lv2_channel->output_port_count;
209   input_port_count = fx_lv2_channel->input_port_count;
210 
211   for(i = 0; i < AGS_SOUND_SCOPE_LAST; i++){
212     AgsFxLv2ChannelInputData *input_data;
213 
214     input_data = fx_lv2_channel->input_data[i];
215 
216     if(output_port_count > 0 &&
217        buffer_size > 0){
218       if(input_data->output == NULL){
219 	input_data->output = (float *) g_malloc(output_port_count * buffer_size * sizeof(float));
220       }else{
221 	input_data->output = (float *) g_realloc(input_data->output,
222 						 output_port_count * buffer_size * sizeof(float));
223       }
224     }
225 
226     if(input_port_count > 0 &&
227        buffer_size > 0){
228       if(input_data->input == NULL){
229 	input_data->input = (float *) g_malloc(input_port_count * buffer_size * sizeof(float));
230       }else{
231 	input_data->input = (float *) g_realloc(input_data->input,
232 						input_port_count * buffer_size * sizeof(float));
233       }
234     }
235   }
236 
237   g_rec_mutex_unlock(recall_mutex);
238 }
239 
240 void
ags_fx_lv2_channel_notify_samplerate_callback(GObject * gobject,GParamSpec * pspec,gpointer user_data)241 ags_fx_lv2_channel_notify_samplerate_callback(GObject *gobject,
242 					      GParamSpec *pspec,
243 					      gpointer user_data)
244 {
245   AgsFxLv2Channel *fx_lv2_channel;
246 
247   AgsLv2Plugin *lv2_plugin;
248 
249   guint output_port_count;
250   guint buffer_size;
251   guint samplerate;
252   guint i;
253 
254   GRecMutex *recall_mutex;
255   GRecMutex *base_plugin_mutex;
256 
257   void (*deactivate)(LV2_Handle instance);
258   void (*cleanup)(LV2_Handle instance);
259 
260   fx_lv2_channel = AGS_FX_LV2_CHANNEL(gobject);
261 
262   /* get recall mutex */
263   recall_mutex = AGS_RECALL_GET_OBJ_MUTEX(fx_lv2_channel);
264 
265   /* get LV2 plugin */
266   g_rec_mutex_lock(recall_mutex);
267 
268   lv2_plugin = fx_lv2_channel->lv2_plugin;
269 
270   g_rec_mutex_unlock(recall_mutex);
271 
272   if(lv2_plugin == NULL){
273     return;
274   }
275 
276   buffer_size = AGS_SOUNDCARD_DEFAULT_BUFFER_SIZE;
277   samplerate =  AGS_SOUNDCARD_DEFAULT_SAMPLERATE;
278 
279   g_object_get(fx_lv2_channel,
280 	       "buffer-size", &buffer_size,
281 	       "samplerate", &samplerate,
282 	       NULL);
283 
284   /* get deactivate and cleanup */
285   base_plugin_mutex = NULL;
286 
287   deactivate = NULL;
288   cleanup = NULL;
289 
290   if(lv2_plugin != NULL){
291     gpointer plugin_descriptor;
292 
293     base_plugin_mutex = AGS_BASE_PLUGIN_GET_OBJ_MUTEX(lv2_plugin);
294 
295     g_rec_mutex_lock(base_plugin_mutex);
296 
297     plugin_descriptor = AGS_BASE_PLUGIN(lv2_plugin)->plugin_descriptor;
298 
299     deactivate = AGS_LV2_PLUGIN_DESCRIPTOR(plugin_descriptor)->deactivate;
300     cleanup = AGS_LV2_PLUGIN_DESCRIPTOR(plugin_descriptor)->cleanup;
301 
302     g_rec_mutex_unlock(base_plugin_mutex);
303   }
304 
305   /* reallocate buffer - apply samplerate */
306   g_rec_mutex_lock(recall_mutex);
307 
308   for(i = 0; i < AGS_SOUND_SCOPE_LAST; i++){
309     AgsFxLv2ChannelInputData *input_data;
310 
311     input_data = fx_lv2_channel->input_data[i];
312 
313     if(deactivate != NULL &&
314        input_data->lv2_handle != NULL){
315       deactivate(input_data->lv2_handle[0]);
316     }
317 
318     if(cleanup != NULL &&
319        input_data->lv2_handle != NULL){
320       cleanup(input_data->lv2_handle[0]);
321     }
322 
323     input_data->lv2_handle = ags_base_plugin_instantiate((AgsBasePlugin *) lv2_plugin,
324 							 samplerate, buffer_size);
325   }
326 
327   g_rec_mutex_unlock(recall_mutex);
328 }
329 
330 /**
331  * ags_fx_lv2_channel_input_data_alloc:
332  *
333  * Allocate #AgsFxLv2ChannelInputData-struct
334  *
335  * Returns: the new #AgsFxLv2ChannelInputData-struct
336  *
337  * Since: 3.3.0
338  */
339 AgsFxLv2ChannelInputData*
ags_fx_lv2_channel_input_data_alloc()340 ags_fx_lv2_channel_input_data_alloc()
341 {
342   AgsFxLv2ChannelInputData *input_data;
343 
344   input_data = (AgsFxLv2ChannelInputData *) g_malloc(sizeof(AgsFxLv2ChannelInputData));
345 
346   g_rec_mutex_init(&(input_data->strct_mutex));
347 
348   input_data->parent = NULL;
349 
350   input_data->output = NULL;
351   input_data->input = NULL;
352 
353   input_data->lv2_handle = NULL;
354 
355   return(input_data);
356 }
357 
358 /**
359  * ags_fx_lv2_channel_input_data_free:
360  * @input_data: the #AgsFxLv2ChannelInputData-struct
361  *
362  * Free @input_data.
363  *
364  * Since: 3.3.0
365  */
366 void
ags_fx_lv2_channel_input_data_free(AgsFxLv2ChannelInputData * input_data)367 ags_fx_lv2_channel_input_data_free(AgsFxLv2ChannelInputData *input_data)
368 {
369   AgsLv2Plugin *lv2_plugin;
370 
371   if(input_data == NULL){
372     return;
373   }
374 
375   g_free(input_data->output);
376   g_free(input_data->input);
377 
378   if(input_data->lv2_handle != NULL){
379     gpointer plugin_descriptor;
380 
381     GRecMutex *base_plugin_mutex;
382 
383     void (*deactivate)(LV2_Handle instance);
384     void (*cleanup)(LV2_Handle instance);
385 
386     lv2_plugin = NULL;
387 
388     deactivate = NULL;
389     cleanup = NULL;
390 
391     base_plugin_mutex = NULL;
392 
393     if(input_data->parent != NULL){
394       lv2_plugin = AGS_FX_LV2_CHANNEL(input_data->parent)->lv2_plugin;
395     }
396 
397     if(lv2_plugin != NULL){
398       base_plugin_mutex = AGS_BASE_PLUGIN_GET_OBJ_MUTEX(lv2_plugin);
399 
400       g_rec_mutex_lock(base_plugin_mutex);
401 
402       plugin_descriptor = AGS_BASE_PLUGIN(lv2_plugin)->plugin_descriptor;
403 
404       deactivate = AGS_LV2_PLUGIN_DESCRIPTOR(plugin_descriptor)->deactivate;
405       cleanup = AGS_LV2_PLUGIN_DESCRIPTOR(plugin_descriptor)->cleanup;
406 
407       g_rec_mutex_unlock(base_plugin_mutex);
408     }
409 
410     if(deactivate != NULL &&
411        input_data->lv2_handle != NULL){
412       deactivate(input_data->lv2_handle[0]);
413     }
414 
415     if(cleanup != NULL &&
416        input_data->lv2_handle != NULL){
417       cleanup(input_data->lv2_handle[0]);
418     }
419   }
420 
421   g_free(input_data);
422 }
423 
424 /**
425  * ags_fx_lv2_channel_load_plugin:
426  * @fx_lv2_channel: the #AgsFxLv2Channel
427  *
428  * Load plugin of @fx_lv2_channel.
429  *
430  * Since: 3.3.0
431  */
432 void
ags_fx_lv2_channel_load_plugin(AgsFxLv2Channel * fx_lv2_channel)433 ags_fx_lv2_channel_load_plugin(AgsFxLv2Channel *fx_lv2_channel)
434 {
435   AgsLv2Manager *lv2_manager;
436   AgsLv2Plugin *lv2_plugin;
437 
438   gchar *filename, *effect;
439 
440   guint buffer_size;
441   guint samplerate;
442 
443   GRecMutex *recall_mutex;
444 
445   if(!AGS_IS_FX_LV2_CHANNEL(fx_lv2_channel)){
446     return;
447   }
448 
449   lv2_manager = ags_lv2_manager_get_instance();
450 
451   /* get recall mutex */
452   recall_mutex = AGS_RECALL_GET_OBJ_MUTEX(fx_lv2_channel);
453 
454   /* get filename and effect */
455   filename = NULL;
456   effect = NULL;
457 
458   buffer_size = AGS_SOUNDCARD_DEFAULT_BUFFER_SIZE;
459   samplerate = AGS_SOUNDCARD_DEFAULT_SAMPLERATE;
460 
461   g_object_get(fx_lv2_channel,
462 	       "filename", &filename,
463 	       "effect", &effect,
464 	       "buffer-size", &buffer_size,
465 	       "samplerate", &samplerate,
466 	       NULL);
467 
468   /* check if already loaded */
469   g_rec_mutex_lock(recall_mutex);
470 
471   lv2_plugin = fx_lv2_channel->lv2_plugin;
472 
473   if(lv2_plugin == NULL){
474     lv2_plugin =
475       fx_lv2_channel->lv2_plugin = ags_lv2_manager_find_lv2_plugin(lv2_manager,
476 								   filename, effect);
477   }
478 
479   g_rec_mutex_unlock(recall_mutex);
480 
481   if(lv2_plugin != NULL &&
482      !ags_base_plugin_test_flags((AgsBasePlugin *) lv2_plugin, AGS_BASE_PLUGIN_IS_INSTRUMENT)){
483     guint i;
484 
485     /* set lv2 plugin */
486     g_rec_mutex_lock(recall_mutex);
487 
488     for(i = 0; i < AGS_SOUND_SCOPE_LAST; i++){
489       AgsFxLv2ChannelInputData *input_data;
490 
491       input_data = fx_lv2_channel->input_data[i];
492 
493       if(input_data->lv2_handle == NULL){
494 	input_data->lv2_handle = ags_base_plugin_instantiate((AgsBasePlugin *) lv2_plugin,
495 							     samplerate, buffer_size);
496       }
497     }
498 
499     g_rec_mutex_unlock(recall_mutex);
500   }
501 
502   g_free(filename);
503   g_free(effect);
504 }
505 
506 /**
507  * ags_fx_lv2_channel_load_port:
508  * @fx_lv2_channel: the #AgsFxLv2Channel
509  *
510  * Load port of @fx_lv2_channel.
511  *
512  * Since: 3.3.0
513  */
514 void
ags_fx_lv2_channel_load_port(AgsFxLv2Channel * fx_lv2_channel)515 ags_fx_lv2_channel_load_port(AgsFxLv2Channel *fx_lv2_channel)
516 {
517   AgsChannel *input;
518   AgsFxLv2Audio *fx_lv2_audio;
519   AgsPort **lv2_port;
520 
521   AgsLv2Plugin *lv2_plugin;
522 
523   GList *start_plugin_port, *plugin_port;
524 
525   guint *output_port;
526   guint *input_port;
527 
528   guint audio_channel;
529   guint pad;
530   guint output_port_count, input_port_count;
531   guint control_port_count;
532   gboolean has_event_port;
533   guint event_port;
534   gboolean has_atom_port;
535   guint atom_port;
536   guint samplerate;
537   guint buffer_size;
538   guint nth;
539   guint i, j;
540   gboolean is_live_instrument;
541 
542   GRecMutex *fx_lv2_audio_mutex;
543   GRecMutex *fx_lv2_channel_mutex;
544 
545   if(!AGS_IS_FX_LV2_CHANNEL(fx_lv2_channel)){
546     return;
547   }
548 
549   fx_lv2_channel_mutex = AGS_RECALL_GET_OBJ_MUTEX(fx_lv2_channel);
550 
551   g_rec_mutex_lock(fx_lv2_channel_mutex);
552 
553   if(fx_lv2_channel->lv2_port != NULL){
554     g_rec_mutex_unlock(fx_lv2_channel_mutex);
555 
556     return;
557   }
558 
559   g_rec_mutex_unlock(fx_lv2_channel_mutex);
560 
561   fx_lv2_audio = NULL;
562 
563   samplerate = AGS_SOUNDCARD_DEFAULT_SAMPLERATE;
564   buffer_size = AGS_SOUNDCARD_DEFAULT_BUFFER_SIZE;
565 
566   g_object_get(fx_lv2_channel,
567 	       "recall-audio", &fx_lv2_audio,
568 	       "samplerate", &samplerate,
569 	       "buffer-size", &buffer_size,
570 	       NULL);
571 
572   if(fx_lv2_audio == NULL){
573     return;
574   }
575 
576   is_live_instrument = ags_fx_lv2_audio_test_flags(fx_lv2_audio, AGS_FX_LV2_AUDIO_LIVE_INSTRUMENT);
577 
578   if(is_live_instrument){
579     if(fx_lv2_audio != NULL){
580       g_object_unref(fx_lv2_audio);
581     }
582 
583     return;
584   }
585 
586   /* get recall mutex */
587   fx_lv2_audio_mutex = AGS_RECALL_GET_OBJ_MUTEX(fx_lv2_audio);
588 
589   input = NULL;
590 
591   g_object_get(fx_lv2_channel,
592 	       "source", &input,
593 	       NULL);
594 
595   audio_channel = 0;
596   pad = 0;
597 
598   g_object_get(input,
599 	       "audio-channel", &audio_channel,
600 	       "pad", &pad,
601 	       NULL);
602 
603   /* get LV2 plugin */
604   g_rec_mutex_lock(fx_lv2_audio_mutex);
605 
606   lv2_plugin = fx_lv2_audio->lv2_plugin;
607 
608   output_port_count = fx_lv2_audio->output_port_count;
609   output_port = fx_lv2_audio->output_port;
610 
611   input_port_count = fx_lv2_audio->input_port_count;
612   input_port = fx_lv2_audio->input_port;
613 
614   has_event_port = fx_lv2_audio->has_event_port;
615   event_port = fx_lv2_audio->event_port;
616 
617   has_atom_port = fx_lv2_audio->has_atom_port;
618   atom_port = fx_lv2_audio->atom_port;
619 
620   g_rec_mutex_unlock(fx_lv2_audio_mutex);
621 
622   /* get LV2 port */
623   g_rec_mutex_lock(fx_lv2_channel_mutex);
624 
625   lv2_port = fx_lv2_channel->lv2_port;
626 
627   g_rec_mutex_unlock(fx_lv2_channel_mutex);
628 
629   start_plugin_port = NULL;
630 
631   g_object_get(lv2_plugin,
632 	       "plugin-port", &start_plugin_port,
633 	       NULL);
634 
635   if(ags_base_plugin_test_flags((AgsBasePlugin *) lv2_plugin, AGS_BASE_PLUGIN_IS_INSTRUMENT)){
636     /* get control port count */
637     plugin_port = start_plugin_port;
638 
639     control_port_count = 0;
640 
641     while(plugin_port != NULL){
642       if(ags_plugin_port_test_flags(plugin_port->data,
643 				    AGS_PLUGIN_PORT_CONTROL)){
644 	control_port_count++;
645       }
646 
647       plugin_port = plugin_port->next;
648     }
649 
650     /*  */
651     if(control_port_count > 0){
652       lv2_port = (AgsPort **) g_malloc((control_port_count + 1) * sizeof(AgsPort *));
653 
654       plugin_port = start_plugin_port;
655 
656       for(nth = 0; nth < control_port_count && plugin_port != NULL; ){
657 	if(ags_plugin_port_test_flags(plugin_port->data,
658 				      AGS_PLUGIN_PORT_CONTROL)){
659 	  AgsPluginPort *current_plugin_port;
660 
661 	  gchar *plugin_name;
662 	  gchar *specifier;
663 	  gchar *control_port;
664 
665 	  guint port_index;
666 
667 	  GValue default_value = {0,};
668 
669 	  GRecMutex *plugin_port_mutex;
670 
671 	  current_plugin_port = AGS_PLUGIN_PORT(plugin_port->data);
672 
673 	  /* get plugin port mutex */
674 	  plugin_port_mutex = AGS_PLUGIN_PORT_GET_OBJ_MUTEX(current_plugin_port);
675 
676 	  /* plugin name, specifier and control port */
677 	  plugin_name = g_strdup_printf("lv2-<%s>", lv2_plugin->uri);
678 
679 	  specifier = NULL;
680 
681 	  port_index = 0;
682 
683 	  g_object_get(current_plugin_port,
684 		       "port-name", &specifier,
685 		       "port-index", &port_index,
686 		       NULL);
687 
688 	  control_port = g_strdup_printf("%u/%u",
689 					 nth,
690 					 control_port_count);
691 
692 	  /* default value */
693 	  g_value_init(&default_value,
694 		       G_TYPE_FLOAT);
695 
696 	  g_rec_mutex_lock(plugin_port_mutex);
697 
698 	  g_value_copy(current_plugin_port->default_value,
699 		       &default_value);
700 
701 	  g_rec_mutex_unlock(plugin_port_mutex);
702 
703 	  /* lv2 port */
704 	  lv2_port[nth] = g_object_new(AGS_TYPE_PORT,
705 				       "plugin-name", plugin_name,
706 				       "specifier", specifier,
707 				       "control-port", control_port,
708 				       "port-value-is-pointer", FALSE,
709 				       "port-value-type", G_TYPE_FLOAT,
710 				       NULL);
711 
712 	  if(ags_plugin_port_test_flags(current_plugin_port,
713 					AGS_PLUGIN_PORT_OUTPUT)){
714 	    ags_port_set_flags(lv2_port[nth], AGS_PORT_IS_OUTPUT);
715 
716 	    ags_recall_set_flags((AgsRecall *) fx_lv2_channel,
717 				 AGS_RECALL_HAS_OUTPUT_PORT);
718 
719 	  }else{
720 	    if(!ags_plugin_port_test_flags(current_plugin_port,
721 					   AGS_PLUGIN_PORT_INTEGER) &&
722 	       !ags_plugin_port_test_flags(current_plugin_port,
723 					   AGS_PLUGIN_PORT_TOGGLED)){
724 	      ags_port_set_flags(lv2_port[nth], AGS_PORT_INFINITE_RANGE);
725 	    }
726 	  }
727 
728 	  g_object_set(lv2_port[nth],
729 		       "plugin-port", current_plugin_port,
730 		       NULL);
731 
732 	  ags_port_util_load_lv2_conversion(lv2_port[nth],
733 					    current_plugin_port);
734 
735 	  ags_port_safe_write_raw(lv2_port[nth],
736 				  &default_value);
737 
738 	  ags_recall_add_port((AgsRecall *) fx_lv2_channel,
739 			      lv2_port[nth]);
740 
741 	  /* connect port */
742 	  for(i = 0; i < AGS_SOUND_SCOPE_LAST; i++){
743 	    AgsFxLv2AudioScopeData *scope_data;
744 
745 	    scope_data = fx_lv2_audio->scope_data[i];
746 
747 	    if(i == AGS_SOUND_SCOPE_PLAYBACK ||
748 	       i == AGS_SOUND_SCOPE_NOTATION ||
749 	       i == AGS_SOUND_SCOPE_MIDI){
750 	      for(j = 0; j < scope_data->audio_channels; j++){
751 		AgsFxLv2AudioChannelData *channel_data;
752 
753 		channel_data = scope_data->channel_data[j];
754 
755 		if(pad < 128){
756 		  AgsFxLv2AudioInputData *input_data;
757 
758 		  input_data = channel_data->input_data[pad];
759 
760 		  if(input_data->lv2_handle == NULL){
761 		    input_data->lv2_handle = ags_base_plugin_instantiate((AgsBasePlugin *) lv2_plugin,
762 									 samplerate, buffer_size);
763 		  }
764 
765 		  ags_base_plugin_connect_port((AgsBasePlugin *) lv2_plugin,
766 					       (gpointer) input_data->lv2_handle[0],
767 					       port_index,
768 					       (gpointer) &(lv2_port[nth]->port_value.ags_port_float));
769 		}
770 	      }
771 	    }
772 	  }
773 
774 	  g_free(plugin_name);
775 	  g_free(specifier);
776 	  g_free(control_port);
777 
778 	  g_value_unset(&default_value);
779 
780 	  nth++;
781 	}
782 
783 	plugin_port = plugin_port->next;
784       }
785 
786       lv2_port[nth] = NULL;
787     }
788 
789     /* set LV2 output */
790     g_rec_mutex_lock(fx_lv2_channel_mutex);
791 
792     for(i = 0; i < AGS_SOUND_SCOPE_LAST; i++){
793       AgsFxLv2AudioScopeData *scope_data;
794 
795       scope_data = fx_lv2_audio->scope_data[i];
796 
797       if(i == AGS_SOUND_SCOPE_PLAYBACK ||
798 	 i == AGS_SOUND_SCOPE_NOTATION ||
799 	 i == AGS_SOUND_SCOPE_MIDI){
800 	AgsFxLv2AudioChannelData *channel_data;
801 
802 	channel_data = scope_data->channel_data[audio_channel];
803 
804 	if(!is_live_instrument){
805 	  AgsFxLv2AudioInputData *input_data;
806 
807 	  guint nth;
808 
809 	  input_data = channel_data->input_data[pad];
810 
811 	  if(input_data->output == NULL &&
812 	     output_port_count > 0 &&
813 	     buffer_size > 0){
814 	    input_data->output = (float *) g_malloc(output_port_count * buffer_size * sizeof(float));
815 	  }
816 
817 	  if(input_data->input == NULL &&
818 	     input_port_count > 0 &&
819 	     buffer_size > 0){
820 	    input_data->input = (float *) g_malloc(input_port_count * buffer_size * sizeof(float));
821 	  }
822 
823 	  for(nth = 0; nth < output_port_count; nth++){
824 	    ags_base_plugin_connect_port((AgsBasePlugin *) lv2_plugin,
825 					 input_data->lv2_handle[0],
826 					 output_port[nth],
827 					 &(input_data->output[nth]));
828 	  }
829 
830 	  for(nth = 0; nth < input_port_count; nth++){
831 	    ags_base_plugin_connect_port((AgsBasePlugin *) lv2_plugin,
832 					 input_data->lv2_handle[0],
833 					 input_port[nth],
834 					 &(input_data->input[nth]));
835 	  }
836 
837 	  if(has_event_port){
838 	    input_data->event_port = ags_lv2_plugin_event_buffer_alloc(AGS_FX_LV2_AUDIO_DEFAULT_MIDI_LENGHT);
839 
840 	    ags_base_plugin_connect_port((AgsBasePlugin *) lv2_plugin,
841 					 input_data->lv2_handle[0],
842 					 event_port,
843 					 input_data->event_port);
844 	  }
845 
846 	  if(has_atom_port){
847 	    input_data->atom_port = ags_lv2_plugin_alloc_atom_sequence(AGS_FX_LV2_AUDIO_DEFAULT_MIDI_LENGHT);
848 
849 	    ags_base_plugin_connect_port((AgsBasePlugin *) lv2_plugin,
850 					 input_data->lv2_handle[0],
851 					 atom_port,
852 					 input_data->atom_port);
853 	  }
854 
855 	  ags_base_plugin_activate((AgsBasePlugin *) lv2_plugin,
856 				   input_data->lv2_handle[0]);
857 	}
858       }
859     }
860 
861     g_rec_mutex_unlock(fx_lv2_channel_mutex);
862   }else{
863     /* get control port count */
864     plugin_port = start_plugin_port;
865 
866     output_port = NULL;
867     input_port = NULL;
868 
869     output_port_count = 0;
870     input_port_count = 0;
871 
872     control_port_count = 0;
873 
874     while(plugin_port != NULL){
875       if(ags_plugin_port_test_flags(plugin_port->data,
876 				    AGS_PLUGIN_PORT_CONTROL)){
877 	control_port_count++;
878       }else if(ags_plugin_port_test_flags(plugin_port->data,
879 					  AGS_PLUGIN_PORT_AUDIO)){
880 	guint port_index;
881 
882 	g_object_get(plugin_port->data,
883 		     "port-index", &port_index,
884 		     NULL);
885 
886 	if(ags_plugin_port_test_flags(plugin_port->data,
887 				      AGS_PLUGIN_PORT_INPUT)){
888 	  if(input_port == NULL){
889 	    input_port = (guint *) g_malloc(sizeof(guint));
890 	  }else{
891 	    input_port = (guint *) g_realloc(input_port,
892 					     (input_port_count + 1) * sizeof(guint));
893 	  }
894 
895 	  input_port[input_port_count] = port_index;
896 	  input_port_count++;
897 	}else if(ags_plugin_port_test_flags(plugin_port->data,
898 					    AGS_PLUGIN_PORT_OUTPUT)){
899 	  if(output_port == NULL){
900 	    output_port = (guint *) g_malloc(sizeof(guint));
901 	  }else{
902 	    output_port = (guint *) g_realloc(output_port,
903 					      (output_port_count + 1) * sizeof(guint));
904 	  }
905 
906 	  output_port[output_port_count] = port_index;
907 	  output_port_count++;
908 	}
909       }
910 
911       plugin_port = plugin_port->next;
912     }
913 
914     /*  */
915     if(control_port_count > 0){
916       lv2_port = (AgsPort **) g_malloc((control_port_count + 1) * sizeof(AgsPort *));
917 
918       plugin_port = start_plugin_port;
919 
920       for(nth = 0; nth < control_port_count && plugin_port != NULL;){
921 	if(ags_plugin_port_test_flags(plugin_port->data,
922 				      AGS_PLUGIN_PORT_CONTROL)){
923 	  AgsPluginPort *current_plugin_port;
924 
925 	  gchar *plugin_name;
926 	  gchar *specifier;
927 	  gchar *control_port;
928 
929 	  guint port_index;
930 
931 	  GValue default_value = {0,};
932 
933 	  GRecMutex *plugin_port_mutex;
934 
935 	  current_plugin_port = AGS_PLUGIN_PORT(plugin_port->data);
936 
937 	  /* get plugin port mutex */
938 	  plugin_port_mutex = AGS_PLUGIN_PORT_GET_OBJ_MUTEX(current_plugin_port);
939 
940 	  /* plugin name, specifier and control port */
941 	  plugin_name = g_strdup_printf("lv2-<%s>", lv2_plugin->uri);
942 
943 	  specifier = NULL;
944 
945 	  port_index = 0;
946 
947 	  g_object_get(current_plugin_port,
948 		       "port-name", &specifier,
949 		       "port-index", &port_index,
950 		       NULL);
951 
952 	  control_port = g_strdup_printf("%u/%u",
953 					 nth,
954 					 control_port_count);
955 
956 	  /* default value */
957 	  g_value_init(&default_value,
958 		       G_TYPE_FLOAT);
959 
960 	  g_rec_mutex_lock(plugin_port_mutex);
961 
962 	  g_value_copy(current_plugin_port->default_value,
963 		       &default_value);
964 
965 	  g_rec_mutex_unlock(plugin_port_mutex);
966 
967 	  /* lv2 port */
968 	  lv2_port[nth] = g_object_new(AGS_TYPE_PORT,
969 				       "plugin-name", plugin_name,
970 				       "specifier", specifier,
971 				       "control-port", control_port,
972 				       "port-value-is-pointer", FALSE,
973 				       "port-value-type", G_TYPE_FLOAT,
974 				       NULL);
975 
976 	  if(ags_plugin_port_test_flags(current_plugin_port,
977 					AGS_PLUGIN_PORT_OUTPUT)){
978 	    ags_port_set_flags(lv2_port[nth], AGS_PORT_IS_OUTPUT);
979 
980 	    ags_recall_set_flags((AgsRecall *) fx_lv2_channel,
981 				 AGS_RECALL_HAS_OUTPUT_PORT);
982 
983 	  }else{
984 	    if(!ags_plugin_port_test_flags(current_plugin_port,
985 					   AGS_PLUGIN_PORT_INTEGER) &&
986 	       !ags_plugin_port_test_flags(current_plugin_port,
987 					   AGS_PLUGIN_PORT_TOGGLED)){
988 	      ags_port_set_flags(lv2_port[nth], AGS_PORT_INFINITE_RANGE);
989 	    }
990 	  }
991 
992 	  g_object_set(lv2_port[nth],
993 		       "plugin-port", current_plugin_port,
994 		       NULL);
995 
996 	  ags_port_util_load_lv2_conversion(lv2_port[nth],
997 					    current_plugin_port);
998 
999 	  ags_port_safe_write_raw(lv2_port[nth],
1000 				  &default_value);
1001 
1002 	  ags_recall_add_port((AgsRecall *) fx_lv2_channel,
1003 			      lv2_port[nth]);
1004 
1005 	  /* connect port */
1006 	  for(i = 0; i < AGS_SOUND_SCOPE_LAST; i++){
1007 	    AgsFxLv2ChannelInputData *input_data;
1008 
1009 	    input_data = fx_lv2_channel->input_data[i];
1010 
1011 	    if(input_data->lv2_handle == NULL){
1012 	      input_data->lv2_handle = ags_base_plugin_instantiate((AgsBasePlugin *) lv2_plugin,
1013 								   samplerate, buffer_size);
1014 	    }
1015 
1016 	    ags_base_plugin_connect_port((AgsBasePlugin *) lv2_plugin,
1017 					 input_data->lv2_handle[0],
1018 					 port_index,
1019 					 &(lv2_port[nth]->port_value.ags_port_float));
1020 	  }
1021 
1022 	  g_free(plugin_name);
1023 	  g_free(specifier);
1024 	  g_free(control_port);
1025 
1026 	  g_value_unset(&default_value);
1027 
1028 	  nth++;
1029 	}
1030 
1031 	plugin_port = plugin_port->next;
1032       }
1033 
1034       lv2_port[nth] = NULL;
1035     }
1036 
1037     /* set LV2 output */
1038     g_rec_mutex_lock(fx_lv2_channel_mutex);
1039 
1040     for(i = 0; i < AGS_SOUND_SCOPE_LAST; i++){
1041       AgsFxLv2ChannelInputData *input_data;
1042 
1043       input_data = fx_lv2_channel->input_data[i];
1044 
1045       if(input_data->output == NULL &&
1046 	 output_port_count > 0 &&
1047 	 buffer_size > 0){
1048 	input_data->output = (float *) g_malloc(output_port_count * buffer_size * sizeof(float));
1049       }
1050 
1051       if(input_data->input == NULL &&
1052 	 input_port_count > 0 &&
1053 	 buffer_size > 0){
1054 	input_data->input = (float *) g_malloc(input_port_count * buffer_size * sizeof(float));
1055       }
1056 
1057       for(nth = 0; nth < output_port_count; nth++){
1058 	ags_base_plugin_connect_port((AgsBasePlugin *) lv2_plugin,
1059 				     input_data->lv2_handle[0],
1060 				     output_port[nth],
1061 				     &(input_data->output[nth]));
1062       }
1063 
1064       for(nth = 0; nth < input_port_count; nth++){
1065 	ags_base_plugin_connect_port((AgsBasePlugin *) lv2_plugin,
1066 				     input_data->lv2_handle[0],
1067 				     input_port[nth],
1068 				     &(input_data->input[nth]));
1069       }
1070 
1071       ags_base_plugin_activate((AgsBasePlugin *) lv2_plugin,
1072 			       input_data->lv2_handle[0]);
1073     }
1074 
1075     fx_lv2_channel->output_port_count = output_port_count;
1076     fx_lv2_channel->output_port = output_port;
1077 
1078     fx_lv2_channel->input_port_count = input_port_count;
1079     fx_lv2_channel->input_port = input_port;
1080 
1081     g_rec_mutex_unlock(fx_lv2_channel_mutex);
1082   }
1083 
1084   /* set LV2 port */
1085   g_rec_mutex_lock(fx_lv2_channel_mutex);
1086 
1087   fx_lv2_channel->lv2_port = lv2_port;
1088 
1089   g_rec_mutex_unlock(fx_lv2_channel_mutex);
1090 
1091   /* unref */
1092   if(fx_lv2_audio != NULL){
1093     g_object_unref(fx_lv2_audio);
1094   }
1095 
1096   g_list_free_full(start_plugin_port,
1097 		   (GDestroyNotify) g_object_unref);
1098 }
1099 
1100 /**
1101  * ags_fx_lv2_channel_new:
1102  * @channel: the #AgsChannel
1103  *
1104  * Create a new instance of #AgsFxLv2Channel
1105  *
1106  * Returns: the new #AgsFxLv2Channel
1107  *
1108  * Since: 3.3.0
1109  */
1110 AgsFxLv2Channel*
ags_fx_lv2_channel_new(AgsChannel * channel)1111 ags_fx_lv2_channel_new(AgsChannel *channel)
1112 {
1113   AgsFxLv2Channel *fx_lv2_channel;
1114 
1115   fx_lv2_channel = (AgsFxLv2Channel *) g_object_new(AGS_TYPE_FX_LV2_CHANNEL,
1116 						    "source", channel,
1117 						    NULL);
1118 
1119   return(fx_lv2_channel);
1120 }
1121