]> git.sesse.net Git - mlt/blob - src/modules/jackrack/plugin_desc.c
Merge ../mlt++
[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->properties       = 0;
60   pd->channels         = 0;
61   pd->port_count       = 0;
62   pd->port_descriptors = NULL;
63   pd->port_range_hints = NULL;
64   pd->audio_input_port_indicies = NULL;
65   pd->audio_output_port_indicies = NULL;
66   pd->audio_aux_port_indicies = NULL;
67   pd->control_port_count = 0;
68   pd->control_port_indicies = NULL;
69   pd->aux_channels = 0;
70   pd->aux_are_input = TRUE;
71 }
72
73 static void
74 plugin_desc_free_ports (plugin_desc_t * pd)
75 {
76   if (pd->port_count)
77     {
78       g_free (pd->port_descriptors);
79       g_free (pd->port_range_hints);
80       pd->port_descriptors = NULL;
81       pd->port_range_hints = NULL;
82       pd->port_count = 0;
83     }
84 }
85
86 static void
87 plugin_desc_free (plugin_desc_t * pd)
88 {
89   plugin_desc_set_object_file (pd, NULL);
90   plugin_desc_set_name        (pd, NULL);
91   plugin_desc_free_ports      (pd);
92 }
93
94 plugin_desc_t *
95 plugin_desc_new ()
96 {
97   plugin_desc_t * pd;
98   pd = g_malloc (sizeof (plugin_desc_t));
99   plugin_desc_init (pd);
100   return pd;
101 }
102
103 plugin_desc_t *
104 plugin_desc_new_with_descriptor (const char * object_file,
105                                  unsigned long index,
106                                  const LADSPA_Descriptor * descriptor)
107 {
108   plugin_desc_t * pd;
109   pd = plugin_desc_new ();
110   
111   plugin_desc_set_object_file (pd, object_file);
112   plugin_desc_set_index       (pd, index);
113   plugin_desc_set_id          (pd, descriptor->UniqueID);
114   plugin_desc_set_name        (pd, descriptor->Name);
115   plugin_desc_set_properties  (pd, descriptor->Properties);
116   plugin_desc_set_ports       (pd,
117                                descriptor->PortCount,
118                                descriptor->PortDescriptors,
119                                descriptor->PortRangeHints,
120                                descriptor->PortNames);
121   
122   pd->rt = LADSPA_IS_HARD_RT_CAPABLE(pd->properties) ? TRUE : FALSE;
123
124   return pd;
125 }
126
127 void
128 plugin_desc_destroy (plugin_desc_t * pd)
129 {
130   plugin_desc_free (pd);
131   g_free (pd);
132 }
133
134 void
135 plugin_desc_set_object_file (plugin_desc_t * pd, const char * object_file)
136 {
137   set_string_property (pd->object_file, object_file);
138 }
139
140 void
141 plugin_desc_set_index          (plugin_desc_t * pd, unsigned long index)
142 {
143   pd->index = index;
144 }
145
146
147 void
148 plugin_desc_set_id          (plugin_desc_t * pd, unsigned long id)
149 {
150   pd->id = id;
151 }
152
153 void
154 plugin_desc_set_name        (plugin_desc_t * pd, const char * name)
155 {
156   set_string_property (pd->name, name);
157 }
158
159 void
160 plugin_desc_set_properties  (plugin_desc_t * pd, LADSPA_Properties properties)
161 {
162   pd->properties = properties;
163 }
164
165 static void
166 plugin_desc_add_audio_port_index (unsigned long ** indicies,
167                                   unsigned long * current_port_count,
168                                   unsigned long index)
169 {
170   (*current_port_count)++;
171   
172   if (*current_port_count == 0)
173     *indicies = g_malloc (sizeof (unsigned long) * *current_port_count);
174   else
175     *indicies = g_realloc (*indicies, sizeof (unsigned long) * *current_port_count);
176   
177   (*indicies)[*current_port_count - 1] = index;
178 }
179
180 static void
181 plugin_desc_set_port_counts (plugin_desc_t * pd)
182 {
183   unsigned long i;
184   unsigned long icount = 0;
185   unsigned long ocount = 0;
186   
187   for (i = 0; i < pd->port_count; i++)
188     {
189       if (LADSPA_IS_PORT_AUDIO (pd->port_descriptors[i]))
190         {
191           if (LADSPA_IS_PORT_INPUT (pd->port_descriptors[i]))
192             plugin_desc_add_audio_port_index (&pd->audio_input_port_indicies, &icount, i);
193           else
194             plugin_desc_add_audio_port_index (&pd->audio_output_port_indicies, &ocount, i);
195         }
196       else
197         {
198           if (LADSPA_IS_PORT_OUTPUT (pd->port_descriptors[i]))
199             continue;
200             
201           pd->control_port_count++;
202           if (pd->control_port_count == 0)
203             pd->control_port_indicies = g_malloc (sizeof (unsigned long) * pd->control_port_count);
204           else
205             pd->control_port_indicies = g_realloc (pd->control_port_indicies,
206                                                    sizeof (unsigned long) * pd->control_port_count);
207           
208           pd->control_port_indicies[pd->control_port_count - 1] = i;
209         }
210     }
211   
212   if (icount == ocount)
213     pd->channels = icount;
214   else
215     { /* deal with auxilliary ports */
216       unsigned long ** port_indicies;
217       unsigned long port_count;
218       unsigned long i, j;
219      
220       if (icount > ocount)
221         {
222           pd->channels = ocount;
223           pd->aux_channels = icount - ocount;
224           pd->aux_are_input = TRUE;
225           port_indicies = &pd->audio_input_port_indicies;
226           port_count = icount;
227         }
228       else
229         {
230           pd->channels = icount;
231           pd->aux_channels = ocount - icount;
232           pd->aux_are_input = FALSE;
233           port_indicies = &pd->audio_output_port_indicies;
234           port_count = ocount;
235         }
236       
237       /* allocate indicies */
238       pd->audio_aux_port_indicies = g_malloc (sizeof (unsigned long) * pd->aux_channels);
239       
240       /* copy indicies */
241       for (i = pd->channels, j = 0; i < port_count; i++, j++)
242         pd->audio_aux_port_indicies[j] = (*port_indicies)[i];
243       
244       /* shrink the main indicies to only have channels indicies */
245       *port_indicies = g_realloc (*port_indicies, sizeof (unsigned long) * pd->channels);
246     }
247 }
248
249 void
250 plugin_desc_set_ports (plugin_desc_t * pd,
251                        unsigned long port_count,
252                        const LADSPA_PortDescriptor * port_descriptors,
253                        const LADSPA_PortRangeHint * port_range_hints,
254                        const char * const * port_names)
255 {
256   unsigned long i;
257
258   plugin_desc_free_ports (pd);
259   
260   if (!port_count)
261     return;
262   
263   pd->port_count = port_count;
264   pd->port_descriptors = g_malloc (sizeof (LADSPA_PortDescriptor) * port_count);
265   pd->port_range_hints = g_malloc (sizeof (LADSPA_PortRangeHint) * port_count);
266   pd->port_names       = g_malloc (sizeof (char *) * port_count);
267   
268   memcpy (pd->port_descriptors, port_descriptors, sizeof (LADSPA_PortDescriptor) * port_count);
269   memcpy (pd->port_range_hints, port_range_hints, sizeof (LADSPA_PortRangeHint) * port_count);
270   
271   for (i = 0; i < port_count; i++)
272     pd->port_names[i] = g_strdup (port_names[i]);
273   
274   plugin_desc_set_port_counts (pd);
275 }
276
277
278 LADSPA_Data
279 plugin_desc_get_default_control_value (plugin_desc_t * pd, unsigned long port_index, guint32 sample_rate)
280 {
281   LADSPA_Data upper, lower;
282   LADSPA_PortRangeHintDescriptor hint_descriptor;
283   
284   hint_descriptor = pd->port_range_hints[port_index].HintDescriptor;
285   
286   /* set upper and lower, possibly adjusted to the sample rate */
287   if (LADSPA_IS_HINT_SAMPLE_RATE(hint_descriptor)) {
288     upper = pd->port_range_hints[port_index].UpperBound * (LADSPA_Data) sample_rate;
289     lower = pd->port_range_hints[port_index].LowerBound * (LADSPA_Data) sample_rate;
290   } else {
291     upper = pd->port_range_hints[port_index].UpperBound;
292     lower = pd->port_range_hints[port_index].LowerBound;
293   }
294   
295   if (LADSPA_IS_HINT_LOGARITHMIC(hint_descriptor))
296     {
297       if (lower < FLT_EPSILON)
298         lower = FLT_EPSILON;
299     }
300     
301
302   if (LADSPA_IS_HINT_HAS_DEFAULT(hint_descriptor)) {
303       
304            if (LADSPA_IS_HINT_DEFAULT_MINIMUM(hint_descriptor)) {
305     
306       return lower;
307        
308     } else if (LADSPA_IS_HINT_DEFAULT_LOW(hint_descriptor)) {
309         
310       if (LADSPA_IS_HINT_LOGARITHMIC(hint_descriptor)) {
311         return exp(log(lower) * 0.75 + log(upper) * 0.25);
312       } else {
313         return lower * 0.75 + upper * 0.25;
314       }
315
316     } else if (LADSPA_IS_HINT_DEFAULT_MIDDLE(hint_descriptor)) {
317         
318       if (LADSPA_IS_HINT_LOGARITHMIC(hint_descriptor)) {
319         return exp(log(lower) * 0.5 + log(upper) * 0.5);
320       } else {
321         return lower * 0.5 + upper * 0.5;
322       }
323
324     } else if (LADSPA_IS_HINT_DEFAULT_HIGH(hint_descriptor)) {
325       
326       if (LADSPA_IS_HINT_LOGARITHMIC(hint_descriptor)) {
327         return exp(log(lower) * 0.25 + log(upper) * 0.75);
328       } else {
329         return lower * 0.25 + upper * 0.75;
330       }
331       
332     } else if (LADSPA_IS_HINT_DEFAULT_MAXIMUM(hint_descriptor)) {
333       
334       return upper;
335     
336     } else if (LADSPA_IS_HINT_DEFAULT_0(hint_descriptor)) {
337       
338       return 0.0;
339       
340     } else if (LADSPA_IS_HINT_DEFAULT_1(hint_descriptor)) {
341       
342       if (LADSPA_IS_HINT_SAMPLE_RATE(hint_descriptor)) {
343         return (LADSPA_Data) sample_rate;
344       } else {
345         return 1.0;
346       }
347       
348     } else if (LADSPA_IS_HINT_DEFAULT_100(hint_descriptor)) {
349       
350       if (LADSPA_IS_HINT_SAMPLE_RATE(hint_descriptor)) {
351         return 100.0 * (LADSPA_Data) sample_rate;
352       } else {
353         return 100.0;
354       }
355       
356     } else if (LADSPA_IS_HINT_DEFAULT_440(hint_descriptor)) {
357       
358       if (LADSPA_IS_HINT_SAMPLE_RATE(hint_descriptor)) {
359         return 440.0 * (LADSPA_Data) sample_rate;
360       } else {
361         return 440.0;
362       }
363       
364     }  
365       
366   } else { /* try and find a reasonable default */
367         
368            if (LADSPA_IS_HINT_BOUNDED_BELOW(hint_descriptor)) {
369       return lower;
370     } else if (LADSPA_IS_HINT_BOUNDED_ABOVE(hint_descriptor)) {
371       return upper;
372     }
373   }
374
375   return 0.0;
376 }
377
378 LADSPA_Data
379 plugin_desc_change_control_value (plugin_desc_t * pd,
380                                   unsigned long control_index,
381                                   LADSPA_Data value,
382                                   guint32 old_sample_rate,
383                                   guint32 new_sample_rate)
384 {
385   
386   if (LADSPA_IS_HINT_SAMPLE_RATE (pd->port_range_hints[control_index].HintDescriptor))
387     {
388       LADSPA_Data old_sr, new_sr;
389   
390       old_sr = (LADSPA_Data) old_sample_rate;
391       new_sr = (LADSPA_Data) new_sample_rate;
392   
393       value /= old_sr;
394       value *= new_sr;
395     }
396   
397   return value;
398 }
399
400 gint
401 plugin_desc_get_copies (plugin_desc_t * pd, unsigned long rack_channels)
402 {
403   gint copies = 1;
404   
405   if (pd->channels > rack_channels)
406     return 0;
407   
408   while (pd->channels * copies < rack_channels)
409     copies++;
410   
411   if (pd->channels * copies > rack_channels)
412     return 0;
413   
414   return copies;
415 }
416
417 /* EOF */