]> git.sesse.net Git - mlt/blob - src/modules/jackrack/plugin_desc.c
Add ladspa producer
[mlt] / src / modules / jackrack / plugin_desc.c
1 /*
2  * JACK Rack
3  *
4  * Original:
5  * Copyright (C) Robert Ham 2002, 2003 (node@users.sourceforge.net)
6  *
7  * Modification for MLT:
8  * Copyright (C) 2004 Ushodaya Enterprises Limited
9  * Author: Dan Dennedy <dan@dennedy.org>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24  */
25
26 #include <math.h>
27 #include <float.h>
28 #include <string.h>
29
30 #include "plugin_desc.h"
31 #include "plugin.h"
32
33 #define set_string_property(property, value) \
34   \
35   if (property) \
36     g_free (property); \
37   \
38   if (value) \
39     (property) = g_strdup (value); \
40   else \
41     (property) = NULL;
42
43
44 void
45 plugin_desc_set_ports (plugin_desc_t * pd,
46                        unsigned long port_count,
47                        const LADSPA_PortDescriptor * port_descriptors,
48                        const LADSPA_PortRangeHint * port_range_hints,
49                        const char * const * port_names);
50
51
52
53 static void
54 plugin_desc_init (plugin_desc_t * pd)
55 {
56   pd->object_file      = NULL;
57   pd->id               = 0;
58   pd->name             = NULL;
59   pd->maker            = NULL;
60   pd->properties       = 0;
61   pd->channels         = 0;
62   pd->port_count       = 0;
63   pd->port_descriptors = NULL;
64   pd->port_range_hints = NULL;
65   pd->audio_input_port_indicies = NULL;
66   pd->audio_output_port_indicies = NULL;
67   pd->audio_aux_port_indicies = NULL;
68   pd->control_port_count = 0;
69   pd->control_port_indicies = NULL;
70   pd->aux_channels = 0;
71   pd->aux_are_input = TRUE;
72   pd->has_input = TRUE;
73 }
74
75 static void
76 plugin_desc_free_ports (plugin_desc_t * pd)
77 {
78   if (pd->port_count)
79     {
80       g_free (pd->port_descriptors);
81       g_free (pd->port_range_hints);
82       pd->port_descriptors = NULL;
83       pd->port_range_hints = NULL;
84       pd->port_count = 0;
85     }
86 }
87
88 static void
89 plugin_desc_free (plugin_desc_t * pd)
90 {
91   plugin_desc_set_object_file (pd, NULL);
92   plugin_desc_set_name        (pd, NULL);
93   plugin_desc_set_maker       (pd, NULL);
94   plugin_desc_free_ports      (pd);
95 }
96
97 plugin_desc_t *
98 plugin_desc_new ()
99 {
100   plugin_desc_t * pd;
101   pd = g_malloc (sizeof (plugin_desc_t));
102   plugin_desc_init (pd);
103   return pd;
104 }
105
106 plugin_desc_t *
107 plugin_desc_new_with_descriptor (const char * object_file,
108                                  unsigned long index,
109                                  const LADSPA_Descriptor * descriptor)
110 {
111   plugin_desc_t * pd;
112   pd = plugin_desc_new ();
113   
114   plugin_desc_set_object_file (pd, object_file);
115   plugin_desc_set_index       (pd, index);
116   plugin_desc_set_id          (pd, descriptor->UniqueID);
117   plugin_desc_set_name        (pd, descriptor->Name);
118   plugin_desc_set_maker       (pd, descriptor->Maker);
119   plugin_desc_set_properties  (pd, descriptor->Properties);
120   plugin_desc_set_ports       (pd,
121                                descriptor->PortCount,
122                                descriptor->PortDescriptors,
123                                descriptor->PortRangeHints,
124                                descriptor->PortNames);
125   
126   pd->rt = LADSPA_IS_HARD_RT_CAPABLE(pd->properties) ? TRUE : FALSE;
127
128   return pd;
129 }
130
131 void
132 plugin_desc_destroy (plugin_desc_t * pd)
133 {
134   plugin_desc_free (pd);
135   g_free (pd);
136 }
137
138 void
139 plugin_desc_set_object_file (plugin_desc_t * pd, const char * object_file)
140 {
141   set_string_property (pd->object_file, object_file);
142 }
143
144 void
145 plugin_desc_set_index          (plugin_desc_t * pd, unsigned long index)
146 {
147   pd->index = index;
148 }
149
150
151 void
152 plugin_desc_set_id          (plugin_desc_t * pd, unsigned long id)
153 {
154   pd->id = id;
155 }
156
157 void
158 plugin_desc_set_name        (plugin_desc_t * pd, const char * name)
159 {
160   set_string_property (pd->name, name);
161 }
162
163 void
164 plugin_desc_set_maker       (plugin_desc_t * pd, const char * maker)
165 {
166   set_string_property (pd->maker, maker);
167 }
168
169 void
170 plugin_desc_set_properties  (plugin_desc_t * pd, LADSPA_Properties properties)
171 {
172   pd->properties = properties;
173 }
174
175 static void
176 plugin_desc_add_audio_port_index (unsigned long ** indicies,
177                                   unsigned long * current_port_count,
178                                   unsigned long index)
179 {
180   (*current_port_count)++;
181   
182   if (*current_port_count == 0)
183     *indicies = g_malloc (sizeof (unsigned long) * *current_port_count);
184   else
185     *indicies = g_realloc (*indicies, sizeof (unsigned long) * *current_port_count);
186   
187   (*indicies)[*current_port_count - 1] = index;
188 }
189
190 static void
191 plugin_desc_set_port_counts (plugin_desc_t * pd)
192 {
193   unsigned long i;
194   unsigned long icount = 0;
195   unsigned long ocount = 0;
196   
197   for (i = 0; i < pd->port_count; i++)
198     {
199       if (LADSPA_IS_PORT_AUDIO (pd->port_descriptors[i]))
200         {
201           if (LADSPA_IS_PORT_INPUT (pd->port_descriptors[i]))
202             plugin_desc_add_audio_port_index (&pd->audio_input_port_indicies, &icount, i);
203           else
204             plugin_desc_add_audio_port_index (&pd->audio_output_port_indicies, &ocount, i);
205         }
206       else
207         {
208           if (LADSPA_IS_PORT_OUTPUT (pd->port_descriptors[i]))
209             continue;
210             
211           pd->control_port_count++;
212           if (pd->control_port_count == 0)
213             pd->control_port_indicies = g_malloc (sizeof (unsigned long) * pd->control_port_count);
214           else
215             pd->control_port_indicies = g_realloc (pd->control_port_indicies,
216                                                    sizeof (unsigned long) * pd->control_port_count);
217           
218           pd->control_port_indicies[pd->control_port_count - 1] = i;
219         }
220     }
221   
222   if (icount == ocount)
223     pd->channels = icount;
224   else if( icount == 0 )
225     {
226       pd->channels = ocount;
227       pd->has_input = FALSE;
228     }
229   else
230     { /* deal with auxilliary ports */
231       unsigned long ** port_indicies;
232       unsigned long port_count;
233       unsigned long i, j;
234      
235       if (icount > ocount)
236         {
237           pd->channels = ocount;
238           pd->aux_channels = icount - ocount;
239           pd->aux_are_input = TRUE;
240           port_indicies = &pd->audio_input_port_indicies;
241           port_count = icount;
242         }
243       else
244         {
245           pd->channels = icount;
246           pd->aux_channels = ocount - icount;
247           pd->aux_are_input = FALSE;
248           port_indicies = &pd->audio_output_port_indicies;
249           port_count = ocount;
250         }
251       
252       /* allocate indicies */
253       pd->audio_aux_port_indicies = g_malloc (sizeof (unsigned long) * pd->aux_channels);
254       
255       /* copy indicies */
256       for (i = pd->channels, j = 0; i < port_count; i++, j++)
257         pd->audio_aux_port_indicies[j] = (*port_indicies)[i];
258       
259       /* shrink the main indicies to only have channels indicies */
260       *port_indicies = g_realloc (*port_indicies, sizeof (unsigned long) * pd->channels);
261     }
262 }
263
264 void
265 plugin_desc_set_ports (plugin_desc_t * pd,
266                        unsigned long port_count,
267                        const LADSPA_PortDescriptor * port_descriptors,
268                        const LADSPA_PortRangeHint * port_range_hints,
269                        const char * const * port_names)
270 {
271   unsigned long i;
272
273   plugin_desc_free_ports (pd);
274   
275   if (!port_count)
276     return;
277   
278   pd->port_count = port_count;
279   pd->port_descriptors = g_malloc (sizeof (LADSPA_PortDescriptor) * port_count);
280   pd->port_range_hints = g_malloc (sizeof (LADSPA_PortRangeHint) * port_count);
281   pd->port_names       = g_malloc (sizeof (char *) * port_count);
282   
283   memcpy (pd->port_descriptors, port_descriptors, sizeof (LADSPA_PortDescriptor) * port_count);
284   memcpy (pd->port_range_hints, port_range_hints, sizeof (LADSPA_PortRangeHint) * port_count);
285   
286   for (i = 0; i < port_count; i++)
287     pd->port_names[i] = g_strdup (port_names[i]);
288   
289   plugin_desc_set_port_counts (pd);
290 }
291
292
293 LADSPA_Data
294 plugin_desc_get_default_control_value (plugin_desc_t * pd, unsigned long port_index, guint32 sample_rate)
295 {
296   LADSPA_Data upper, lower;
297   LADSPA_PortRangeHintDescriptor hint_descriptor;
298   
299   hint_descriptor = pd->port_range_hints[port_index].HintDescriptor;
300   
301   /* set upper and lower, possibly adjusted to the sample rate */
302   if (LADSPA_IS_HINT_SAMPLE_RATE(hint_descriptor)) {
303     upper = pd->port_range_hints[port_index].UpperBound * (LADSPA_Data) sample_rate;
304     lower = pd->port_range_hints[port_index].LowerBound * (LADSPA_Data) sample_rate;
305   } else {
306     upper = pd->port_range_hints[port_index].UpperBound;
307     lower = pd->port_range_hints[port_index].LowerBound;
308   }
309   
310   if (LADSPA_IS_HINT_LOGARITHMIC(hint_descriptor))
311     {
312       if (lower < FLT_EPSILON)
313         lower = FLT_EPSILON;
314     }
315     
316
317   if (LADSPA_IS_HINT_HAS_DEFAULT(hint_descriptor)) {
318       
319            if (LADSPA_IS_HINT_DEFAULT_MINIMUM(hint_descriptor)) {
320     
321       return lower;
322        
323     } else if (LADSPA_IS_HINT_DEFAULT_LOW(hint_descriptor)) {
324         
325       if (LADSPA_IS_HINT_LOGARITHMIC(hint_descriptor)) {
326         return exp(log(lower) * 0.75 + log(upper) * 0.25);
327       } else {
328         return lower * 0.75 + upper * 0.25;
329       }
330
331     } else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(hint_descriptor)) {
332         
333       if (LADSPA_IS_HINT_LOGARITHMIC(hint_descriptor)) {
334         return exp(log(lower) * 0.5 + log(upper) * 0.5);
335       } else {
336         return lower * 0.5 + upper * 0.5;
337       }
338
339     } else if (LADSPA_IS_HINT_DEFAULT_HIGH(hint_descriptor)) {
340       
341       if (LADSPA_IS_HINT_LOGARITHMIC(hint_descriptor)) {
342         return exp(log(lower) * 0.25 + log(upper) * 0.75);
343       } else {
344         return lower * 0.25 + upper * 0.75;
345       }
346       
347     } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(hint_descriptor)) {
348       
349       return upper;
350     
351     } else if (LADSPA_IS_HINT_DEFAULT_0(hint_descriptor)) {
352       
353       return 0.0;
354       
355     } else if (LADSPA_IS_HINT_DEFAULT_1(hint_descriptor)) {
356       
357       if (LADSPA_IS_HINT_SAMPLE_RATE(hint_descriptor)) {
358         return (LADSPA_Data) sample_rate;
359       } else {
360         return 1.0;
361       }
362       
363     } else if (LADSPA_IS_HINT_DEFAULT_100(hint_descriptor)) {
364       
365       if (LADSPA_IS_HINT_SAMPLE_RATE(hint_descriptor)) {
366         return 100.0 * (LADSPA_Data) sample_rate;
367       } else {
368         return 100.0;
369       }
370       
371     } else if (LADSPA_IS_HINT_DEFAULT_440(hint_descriptor)) {
372       
373       if (LADSPA_IS_HINT_SAMPLE_RATE(hint_descriptor)) {
374         return 440.0 * (LADSPA_Data) sample_rate;
375       } else {
376         return 440.0;
377       }
378       
379     }  
380       
381   } else { /* try and find a reasonable default */
382         
383            if (LADSPA_IS_HINT_BOUNDED_BELOW(hint_descriptor)) {
384       return lower;
385     } else if (LADSPA_IS_HINT_BOUNDED_ABOVE(hint_descriptor)) {
386       return upper;
387     }
388   }
389
390   return 0.0;
391 }
392
393 LADSPA_Data
394 plugin_desc_change_control_value (plugin_desc_t * pd,
395                                   unsigned long control_index,
396                                   LADSPA_Data value,
397                                   guint32 old_sample_rate,
398                                   guint32 new_sample_rate)
399 {
400   
401   if (LADSPA_IS_HINT_SAMPLE_RATE (pd->port_range_hints[control_index].HintDescriptor))
402     {
403       LADSPA_Data old_sr, new_sr;
404   
405       old_sr = (LADSPA_Data) old_sample_rate;
406       new_sr = (LADSPA_Data) new_sample_rate;
407   
408       value /= old_sr;
409       value *= new_sr;
410     }
411   
412   return value;
413 }
414
415 gint
416 plugin_desc_get_copies (plugin_desc_t * pd, unsigned long rack_channels)
417 {
418   gint copies = 1;
419   
420   if (pd->channels > rack_channels)
421     return 0;
422   
423   while (pd->channels * copies < rack_channels)
424     copies++;
425   
426   if (pd->channels * copies > rack_channels)
427     return 0;
428   
429   return copies;
430 }
431
432 /* EOF */