]> git.sesse.net Git - mlt/blob - src/modules/jackrack/jack_rack.c
On Qt 5, use QThread, QOpenGLContext, and QOffscreenSurface.
[mlt] / src / modules / jackrack / jack_rack.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 <stdio.h>
27 #include <stdlib.h>
28 #include <strings.h>
29 #include <string.h>
30 #include <ctype.h>
31
32 #include <ladspa.h>
33 #include <libxml/tree.h>
34
35 #include "jack_rack.h"
36 #include "lock_free_fifo.h"
37 #include "plugin_settings.h"
38 #include "framework/mlt_log.h"
39
40 #ifndef _
41 #define _(x) x
42 #endif
43 #define _x (const xmlChar*)
44 #define _s (const char*)
45
46 extern plugin_mgr_t *g_jackrack_plugin_mgr;
47
48 jack_rack_t *
49 jack_rack_new (const char * client_name, unsigned long channels)
50 {
51   jack_rack_t *rack;
52
53   rack = g_malloc (sizeof (jack_rack_t));
54   rack->saved_plugins  = NULL;
55   rack->channels       = channels;
56   rack->procinfo = process_info_new (client_name, channels, FALSE, FALSE);
57   if (!rack->procinfo) {
58     g_free (rack);
59     return NULL;
60   }
61   rack->plugin_mgr = g_jackrack_plugin_mgr;
62   plugin_mgr_set_plugins (rack->plugin_mgr, channels);
63
64   return rack;
65 }
66
67
68 void
69 jack_rack_destroy (jack_rack_t * jack_rack)
70 {
71   process_quit (jack_rack->procinfo);
72   // plugin_mgr is shared and global now, so we do not destroy it with each instance
73 //  plugin_mgr_destroy (jack_rack->plugin_mgr);
74   process_info_destroy (jack_rack->procinfo);
75   g_slist_free (jack_rack->saved_plugins);
76   g_free (jack_rack);
77 }
78
79 plugin_t *
80 jack_rack_instantiate_plugin (jack_rack_t * jack_rack, plugin_desc_t * desc)
81 {
82   plugin_t * plugin;
83   
84   /* check whether or not the plugin is RT capable and confirm with the user if it isn't */
85   if (!LADSPA_IS_HARD_RT_CAPABLE(desc->properties)) {
86     mlt_log_info( NULL, "Plugin not RT capable. The plugin '%s' does not describe itself as being capable of real-time operation. You may experience drop outs or jack may even kick us out if you use it.\n",
87                desc->name);
88   }
89
90   /* create the plugin */
91   plugin = plugin_new (desc, jack_rack);
92
93   if (!plugin) {
94    mlt_log_error( NULL, "Error loading file plugin '%s' from file '%s'\n",
95                desc->name, desc->object_file);
96   }
97   
98   return plugin;
99 }
100
101
102 void
103 jack_rack_add_saved_plugin (jack_rack_t * jack_rack, saved_plugin_t * saved_plugin)
104 {
105   plugin_t * plugin = jack_rack_instantiate_plugin (jack_rack, saved_plugin->settings->desc);
106   if (!plugin)
107     return;
108   jack_rack->saved_plugins = g_slist_append (jack_rack->saved_plugins, saved_plugin);
109   process_add_plugin (jack_rack->procinfo, plugin);
110   jack_rack_add_plugin (jack_rack, plugin);
111 }
112
113
114 void
115 jack_rack_add_plugin (jack_rack_t * jack_rack, plugin_t * plugin)
116 {
117   saved_plugin_t * saved_plugin = NULL;
118   GSList * list;
119   unsigned long control, channel;
120   LADSPA_Data value;
121   guint copy;
122   
123   /* see if there's any saved settings that match the plugin id */
124   for (list = jack_rack->saved_plugins; list; list = g_slist_next (list))
125     {
126       saved_plugin = list->data;
127       
128       if (saved_plugin->settings->desc->id == plugin->desc->id)
129         {
130           /* process the settings! */
131           jack_rack->saved_plugins = g_slist_remove (jack_rack->saved_plugins, saved_plugin);
132           break;
133         }
134       saved_plugin = NULL;
135     }
136
137    if ( !saved_plugin )
138         return;
139
140   /* initialize plugin parameters */
141   plugin->enabled = settings_get_enabled (saved_plugin->settings);
142   plugin->wet_dry_enabled = settings_get_wet_dry_enabled (saved_plugin->settings);
143         
144   for (control = 0; control < saved_plugin->settings->desc->control_port_count; control++)
145     for (copy = 0; copy < plugin->copies; copy++)
146       {
147         value = settings_get_control_value (saved_plugin->settings, copy, control);
148         plugin->holders[copy].control_memory[control] = value;
149 //mlt_log_debug( NULL, "setting control value %s (%d) = %f\n", saved_plugin->settings->desc->port_names[control], copy, value);
150 //        lff_write (plugin->holders[copy].ui_control_fifos + control, &value);
151       }
152   if (plugin->wet_dry_enabled)
153     for (channel = 0; channel < jack_rack->channels; channel++)
154       {
155         value = settings_get_wet_dry_value (saved_plugin->settings, channel);
156         plugin->wet_dry_values[channel] = value;
157 //mlt_log_debug( NULL, "setting wet/dry value %d = %f\n", channel, value);
158 //        lff_write (plugin->wet_dry_fifos + channel, &value);
159       }
160 }
161
162
163 static void
164 saved_rack_parse_plugin (jack_rack_t * jack_rack, saved_rack_t * saved_rack, saved_plugin_t * saved_plugin,
165                          const char * filename, xmlNodePtr plugin)
166 {
167   plugin_desc_t * desc;
168   settings_t * settings = NULL;
169   xmlNodePtr node;
170   xmlNodePtr sub_node;
171   xmlChar *content;
172   unsigned long num;
173   unsigned long control = 0;
174 #ifdef WIN32
175   xmlFreeFunc xmlFree = NULL;
176   xmlMemGet( &xmlFree, NULL, NULL, NULL);
177 #endif
178
179   for (node = plugin->children; node; node = node->next)
180     {
181       if (xmlStrcmp (node->name, _x("id")) == 0)
182         {
183           content = xmlNodeGetContent (node);
184           num = strtoul (_s(content), NULL, 10);
185           xmlFree (content);
186
187           desc = plugin_mgr_get_any_desc (jack_rack->plugin_mgr, num);
188           if (!desc)
189             {
190               mlt_log_verbose( NULL, _("The file '%s' contains an unknown plugin with ID '%ld'; skipping\n"), filename, num);
191               return;
192             }
193           
194           settings = settings_new (desc, saved_rack->channels, saved_rack->sample_rate);
195         }
196       else if (xmlStrcmp (node->name, _x("enabled")) == 0)
197         {
198           content = xmlNodeGetContent (node);
199           settings_set_enabled (settings, xmlStrcmp (content, _x("true")) == 0 ? TRUE : FALSE);
200           xmlFree (content);
201         }
202       else if (xmlStrcmp (node->name, _x("wet_dry_enabled")) == 0)
203         {
204           content = xmlNodeGetContent (node);
205           settings_set_wet_dry_enabled (settings, xmlStrcmp (content, _x("true")) == 0 ? TRUE : FALSE);
206           xmlFree (content);
207         }
208       else if (xmlStrcmp (node->name, _x("wet_dry_locked")) == 0)
209         {
210           content = xmlNodeGetContent (node);
211           settings_set_wet_dry_locked (settings, xmlStrcmp (content, _x("true")) == 0 ? TRUE : FALSE);
212           xmlFree (content);
213         }
214       else if (xmlStrcmp (node->name, _x("wet_dry_values")) == 0)
215         {
216           unsigned long channel = 0;
217           
218           for (sub_node = node->children; sub_node; sub_node = sub_node->next)
219             {
220               if (xmlStrcmp (sub_node->name, _x("value")) == 0)
221                 {
222                   content = xmlNodeGetContent (sub_node);
223                   settings_set_wet_dry_value (settings, channel, strtod (_s(content), NULL));
224                   xmlFree (content);
225                   
226                   channel++;
227                 }
228             }
229         }
230       else if (xmlStrcmp (node->name, _x("lockall")) == 0)
231         {
232           content = xmlNodeGetContent (node);
233           settings_set_lock_all (settings, xmlStrcmp (content, _x("true")) == 0 ? TRUE : FALSE);
234           xmlFree (content);
235         }
236       else if (xmlStrcmp (node->name, _x("controlrow")) == 0)
237         {
238           gint copy = 0;
239
240           for (sub_node = node->children; sub_node; sub_node = sub_node->next)
241             {
242               if (xmlStrcmp (sub_node->name, _x("lock")) == 0)
243                 {
244                   content = xmlNodeGetContent (sub_node);
245                   settings_set_lock (settings, control, xmlStrcmp (content, _x("true")) == 0 ? TRUE : FALSE);
246                   xmlFree (content);
247                 }
248               else if (xmlStrcmp (sub_node->name, _x("value")) == 0)
249                 {
250                   content = xmlNodeGetContent (sub_node);
251                   settings_set_control_value (settings, copy, control, strtod (_s(content), NULL));
252                   xmlFree (content);
253                   copy++;
254                 }
255             }
256           
257           control++;
258         }
259     }
260   
261   if (settings)
262     saved_plugin->settings = settings;
263 }
264
265 static void
266 saved_rack_parse_jackrack (jack_rack_t * jack_rack, saved_rack_t * saved_rack, const char * filename, xmlNodePtr jackrack)
267 {
268   xmlNodePtr node;
269   xmlChar *content;
270   saved_plugin_t * saved_plugin;
271 #ifdef WIN32
272   xmlFreeFunc xmlFree = NULL;
273   xmlMemGet( &xmlFree, NULL, NULL, NULL);
274 #endif
275
276   for (node = jackrack->children; node; node = node->next)
277     {
278       if (xmlStrcmp (node->name, _x("channels")) == 0)
279         {
280           content = xmlNodeGetContent (node);
281           saved_rack->channels = strtoul (_s(content), NULL, 10);
282           xmlFree (content);
283         }
284       else if (xmlStrcmp (node->name, _x("samplerate")) == 0)
285         {
286           content = xmlNodeGetContent (node);
287           saved_rack->sample_rate = strtoul (_s(content), NULL, 10);
288           xmlFree (content);
289         }
290       else if (xmlStrcmp (node->name, _x("plugin")) == 0)
291         {
292           saved_plugin = g_malloc0 (sizeof (saved_plugin_t));
293           saved_rack->plugins = g_slist_append (saved_rack->plugins, saved_plugin);
294           saved_rack_parse_plugin (jack_rack, saved_rack, saved_plugin, filename, node);
295         }
296     }
297 }
298
299 static saved_rack_t *
300 saved_rack_new (jack_rack_t * jack_rack, const char * filename, xmlDocPtr doc)
301 {
302   xmlNodePtr node;
303   saved_rack_t *saved_rack;
304   
305   /* create the saved rack */
306   saved_rack = g_malloc (sizeof (saved_rack_t));
307   saved_rack->plugins = NULL;
308   saved_rack->sample_rate = 48000;
309   saved_rack->channels = 2;
310   
311   for (node = doc->children; node; node = node->next)
312     {
313       if (xmlStrcmp (node->name, _x("jackrack")) == 0)
314         saved_rack_parse_jackrack (jack_rack, saved_rack, filename, node);
315     }
316   
317   return saved_rack;
318 }
319
320 static void
321 saved_rack_destroy (saved_rack_t * saved_rack)
322 {
323   GSList * list;
324   
325   for (list = saved_rack->plugins; list; list = g_slist_next (list))
326     settings_destroy (((saved_plugin_t *) list->data)->settings);
327   g_slist_free (saved_rack->plugins);
328   g_free (saved_rack);
329 }
330
331
332 int
333 jack_rack_open_file (jack_rack_t * jack_rack, const char * filename)
334 {
335   xmlDocPtr doc;
336   saved_rack_t * saved_rack;
337   GSList * list;
338   saved_plugin_t * saved_plugin;
339
340   doc = xmlParseFile (filename);
341   if (!doc)
342     {
343       mlt_log_error( NULL, _("Could not parse file '%s'\n"), filename);
344       return 1;
345     }
346   
347   if (xmlStrcmp ( ((xmlDtdPtr)doc->children)->name, _x("jackrack")) != 0)
348     {
349       mlt_log_error( NULL, _("The file '%s' is not a JACK Rack settings file\n"), filename);
350       return 1;
351     }
352   
353   saved_rack = saved_rack_new (jack_rack, filename, doc);
354   xmlFreeDoc (doc);
355   
356   if (!saved_rack)
357     return 1;
358
359   for (list = saved_rack->plugins; list; list = g_slist_next (list))
360     {
361       saved_plugin = list->data;
362       
363       settings_set_sample_rate (saved_plugin->settings, sample_rate);
364       
365       jack_rack_add_saved_plugin (jack_rack, saved_plugin);
366     }
367   
368   saved_rack_destroy (saved_rack);
369   
370   return 0;
371 }
372
373
374 /* EOF */