]> git.sesse.net Git - mlt/blob - src/modules/jackrack/process.c
Add ladspa producer
[mlt] / src / modules / jackrack / process.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 <pthread.h>
27 #include <jack/jack.h>
28 #include <glib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <stdlib.h>
32 #include <sys/time.h>
33 #include <time.h>
34 #include <ctype.h>
35
36 #include "process.h"
37 #include "lock_free_fifo.h"
38 #include "plugin.h"
39 #include "jack_rack.h"
40 #include "framework/mlt_log.h"
41
42 #ifndef _
43 #define _(x) x
44 #endif
45
46 extern pthread_mutex_t g_activate_mutex;
47
48 #define USEC_PER_SEC         1000000
49 #define MSEC_PER_SEC         1000
50 #define TIME_RUN_SKIP_COUNT  5
51 #define MAX_BUFFER_SIZE      4096
52
53 jack_nframes_t sample_rate;
54 jack_nframes_t buffer_size;
55
56 static void
57 jack_shutdown_cb (void * data)
58 {
59   process_info_t * procinfo = data;
60   
61   procinfo->quit = TRUE;
62 }
63
64 /** process messages for plugins' control ports */
65 void process_control_port_messages (process_info_t * procinfo) {
66   plugin_t * plugin;
67   unsigned long control;
68   unsigned long channel;
69   gint copy;
70   
71   if (!procinfo->chain) return;
72   
73   for (plugin = procinfo->chain; plugin; plugin = plugin->next)
74     {
75       if (plugin->desc->control_port_count > 0)
76         for (control = 0; control < plugin->desc->control_port_count; control++)
77           for (copy = 0; copy < plugin->copies; copy++)
78             {
79               while (lff_read (plugin->holders[copy].ui_control_fifos + control,
80                                plugin->holders[copy].control_memory + control) == 0);
81             }
82       
83       if (plugin->wet_dry_enabled)
84         for (channel = 0; channel < procinfo->channels; channel++)
85           {
86             while (lff_read (plugin->wet_dry_fifos + channel,
87                              plugin->wet_dry_values + channel) == 0);
88           }
89     }
90 }
91
92 int get_jack_buffers (process_info_t * procinfo, jack_nframes_t frames) {
93   unsigned long channel;
94   
95   for (channel = 0; channel < procinfo->channels; channel++)
96     {
97       procinfo->jack_input_buffers[channel] = jack_port_get_buffer (procinfo->jack_input_ports[channel], frames);
98       if (!procinfo->jack_input_buffers[channel])
99         {
100           mlt_log_verbose( NULL, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
101           return 1;
102         }
103
104       procinfo->jack_output_buffers[channel] = jack_port_get_buffer (procinfo->jack_output_ports[channel], frames);
105       if (!procinfo->jack_output_buffers[channel])
106         {
107           mlt_log_verbose( NULL, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
108           return 1;
109         }
110     }
111
112   return 0;
113 }
114
115 plugin_t *
116 get_first_enabled_plugin (process_info_t * procinfo)
117 {
118   plugin_t * first_enabled;
119   
120   if (!procinfo->chain) return NULL;
121
122   for (first_enabled = procinfo->chain;
123        first_enabled;
124        first_enabled = first_enabled->next)
125     {
126       if (first_enabled->enabled) return first_enabled;
127     }
128  
129   return NULL;
130 }
131
132 plugin_t *
133 get_last_enabled_plugin (process_info_t * procinfo)
134 {
135   plugin_t * last_enabled;
136   
137   if (!procinfo->chain) return NULL;
138
139   for (last_enabled = procinfo->chain_end;
140        last_enabled;
141        last_enabled = last_enabled->prev)
142     {
143       if (last_enabled->enabled) return last_enabled;
144     }
145   
146   return NULL;
147 }
148
149 void
150 connect_chain (process_info_t * procinfo, jack_nframes_t frames)
151 {
152   plugin_t * first_enabled, * last_enabled, * plugin;
153   gint copy;
154   unsigned long channel;
155   if (!procinfo->chain) return;
156   
157   first_enabled = get_first_enabled_plugin (procinfo);
158   if (!first_enabled) return;
159   
160   last_enabled = get_last_enabled_plugin (procinfo);
161   
162   /* sort out the aux ports */
163   plugin = first_enabled;
164   do
165     {
166       if (plugin->desc->aux_channels > 0 && plugin->enabled)
167         {
168           if (procinfo->jack_client)
169             {
170               for (copy = 0; copy < plugin->copies; copy++)
171                 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
172                   plugin->descriptor->
173                     connect_port (plugin->holders[copy].instance,
174                                   plugin->desc->audio_aux_port_indicies[channel],
175                                   jack_port_get_buffer (plugin->holders[copy].aux_ports[channel], frames));
176             }
177           else
178             {
179               for (copy = 0; copy < frames; copy++)
180                 procinfo->silent_buffer[copy] = 0.0;
181
182               for (copy = 0; copy < plugin->copies; copy++)
183                 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
184                   plugin->descriptor->
185                     connect_port (plugin->holders[copy].instance,
186                                   plugin->desc->audio_aux_port_indicies[channel],
187                                   procinfo->silent_buffer);
188             }
189         }
190     }
191   while ( (plugin != last_enabled) && (plugin = plugin->next) );
192
193   /* ensure that all the of the enabled plugins are connected to their memory */
194   plugin_connect_output_ports (first_enabled);
195   if (first_enabled != last_enabled)
196     {
197       plugin_connect_input_ports (last_enabled, last_enabled->prev->audio_output_memory);
198       for (plugin = first_enabled->next; plugin; plugin = plugin->next)
199         {
200           if (plugin->enabled)
201             {
202               plugin_connect_input_ports (plugin, plugin->prev->audio_output_memory);
203               plugin_connect_output_ports (plugin);
204             }
205         }
206     }
207
208   /* input buffers for first plugin */
209   if( plugin->desc->has_input )
210     plugin_connect_input_ports (first_enabled, procinfo->jack_input_buffers);
211 }
212
213 void
214 process_chain (process_info_t * procinfo, jack_nframes_t frames)
215 {
216   plugin_t * first_enabled;
217   plugin_t * last_enabled = NULL;
218   plugin_t * plugin;
219   unsigned long channel;
220   unsigned long i;
221
222   if (procinfo->jack_client)
223     {
224       LADSPA_Data zero_signal[frames];
225       guint copy;
226
227       /* set the zero signal to zero */
228       for (channel = 0; channel < frames; channel++)
229         zero_signal[channel] = 0.0;
230     
231       /* possibly set aux output channels to zero if they're not enabled */
232       for (plugin = procinfo->chain; plugin; plugin = plugin->next)
233         if (!plugin->enabled &&
234             plugin->desc->aux_channels > 0 &&
235             !plugin->desc->aux_are_input)
236           for (copy = 0; copy < plugin->copies; copy++)
237             for (channel = 0; channel < plugin->desc->aux_channels; channel++)
238               memcpy (jack_port_get_buffer (plugin->holders[copy].aux_ports[channel], frames),
239                       zero_signal, sizeof (LADSPA_Data) * frames);
240     }
241
242   first_enabled = get_first_enabled_plugin (procinfo);
243   
244   /* no chain; just copy input to output */
245   if (!procinfo->chain || !first_enabled)
246     {
247       unsigned long channel;
248       for (channel = 0; channel < procinfo->channels; channel++)
249         {
250           memcpy (procinfo->jack_output_buffers[channel],
251                   procinfo->jack_input_buffers[channel],
252                   sizeof(LADSPA_Data) * frames);
253         }
254       return;
255     }
256   
257   /* all past here is guaranteed to have at least 1 enabled plugin */
258
259   last_enabled = get_last_enabled_plugin (procinfo);
260   
261   for (plugin = first_enabled;
262        plugin;
263        plugin = plugin->next)
264     {
265       if (plugin->enabled)
266         {
267           for (i = 0; i < plugin->copies; i++)
268             plugin->descriptor->run (plugin->holders[i].instance, frames);
269           
270           if (plugin->wet_dry_enabled)
271             for (channel = 0; channel < procinfo->channels; channel++)
272               for (i = 0; i < frames; i++)
273                 {
274                   plugin->audio_output_memory[channel][i] *= plugin->wet_dry_values[channel];
275                   plugin->audio_output_memory[channel][i] += plugin->audio_input_memory[channel][i] * (1.0 - plugin->wet_dry_values[channel]);
276                 }
277           
278           if (plugin == last_enabled)
279             break;
280         }
281       else
282         {
283     
284           /* copy the data through */
285           for (i = 0; i < procinfo->channels; i++)
286             memcpy (plugin->audio_output_memory[i],
287                     plugin->prev->audio_output_memory[i],
288                     sizeof(LADSPA_Data) * frames);
289         }
290     }
291   
292   /* copy the last enabled data to the jack ports */
293   for (i = 0; i < procinfo->channels; i++)
294     memcpy (procinfo->jack_output_buffers[i],
295             last_enabled->audio_output_memory[i],
296             sizeof(LADSPA_Data) * frames);
297   
298 }
299
300 int process_ladspa (process_info_t * procinfo, jack_nframes_t frames,
301                     LADSPA_Data ** inputs, LADSPA_Data ** outputs) {
302   unsigned long channel;
303   
304   if (!procinfo)
305     {
306       mlt_log_error( NULL, "%s: no process_info from jack!\n", __FUNCTION__);
307       return 1;
308     }
309   
310   if (procinfo->quit == TRUE)
311     return 1;
312   
313   process_control_port_messages (procinfo);
314   
315   for (channel = 0; channel < procinfo->channels; channel++)
316     {
317       if(get_first_enabled_plugin (procinfo)->desc->has_input)
318         {
319           procinfo->jack_input_buffers[channel] = inputs[channel];
320           if (!procinfo->jack_input_buffers[channel])
321             {
322               mlt_log_verbose( NULL, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
323               return 1;
324             }
325         }
326       procinfo->jack_output_buffers[channel] = outputs[channel];
327       if (!procinfo->jack_output_buffers[channel])
328         {
329           mlt_log_verbose( NULL, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
330           return 1;
331         }
332     }
333   
334   connect_chain (procinfo, frames);
335   
336   process_chain (procinfo, frames);
337   
338   return 0;
339 }
340
341 int process_jack (jack_nframes_t frames, void * data) {
342   int err;
343   process_info_t * procinfo;
344   
345   procinfo = (process_info_t *) data;
346   
347   if (!procinfo)
348     {
349       mlt_log_error( NULL, "%s: no process_info from jack!\n", __FUNCTION__);
350       return 1;
351     }
352   
353   if (procinfo->port_count == 0)
354     return 0;
355   
356   if (procinfo->quit == TRUE)
357     return 1;
358   
359   process_control_port_messages (procinfo);
360   
361   err = get_jack_buffers (procinfo, frames);
362   if (err)
363     {
364       mlt_log_warning( NULL, "%s: failed to get jack ports, not processing\n", __FUNCTION__);
365       return 0;
366     }
367   
368   connect_chain (procinfo, frames);
369   
370   process_chain (procinfo, frames);
371   
372   return 0;
373 }
374
375
376
377 /*******************************************
378  ************** non RT stuff ***************
379  *******************************************/
380  
381 static int
382 process_info_connect_jack (process_info_t * procinfo)
383 {
384   mlt_log_info( NULL, _("Connecting to JACK server with client name '%s'\n"), procinfo->jack_client_name);
385
386   procinfo->jack_client = jack_client_open (procinfo->jack_client_name, JackNullOption, NULL);
387
388   if (!procinfo->jack_client)
389     {
390       mlt_log_warning( NULL, "%s: could not create jack client; is the jackd server running?\n", __FUNCTION__);
391       return 1;
392     }
393
394   mlt_log_verbose( NULL, _("Connected to JACK server\n"));
395
396   jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
397   jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
398                                             
399   return 0;
400 }
401
402 static void
403 process_info_connect_port (process_info_t * procinfo,
404                            gshort in,
405                            unsigned long port_index,
406                            const char * port_name)
407 {
408   const char ** jack_ports;
409   unsigned long jack_port_index;
410   int err;
411   char * full_port_name;
412   
413   jack_ports = jack_get_ports (procinfo->jack_client, NULL, NULL,
414                                JackPortIsPhysical | (in ? JackPortIsOutput : JackPortIsInput));
415   
416   if (!jack_ports)
417     return;
418   
419   for (jack_port_index = 0;
420        jack_ports[jack_port_index] && jack_port_index <= port_index;
421        jack_port_index++)
422     {
423       if (jack_port_index != port_index)
424         continue;
425         
426       full_port_name = g_strdup_printf ("%s:%s", procinfo->jack_client_name, port_name);
427
428       mlt_log_debug( NULL, _("Connecting ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
429
430       err = jack_connect (procinfo->jack_client,
431                           in ? jack_ports[jack_port_index] : full_port_name,
432                           in ? full_port_name : jack_ports[jack_port_index]);
433
434       if (err)
435         mlt_log_warning( NULL, "%s: error connecting ports '%s' and '%s'\n",
436                  __FUNCTION__, full_port_name, jack_ports[jack_port_index]);
437       else
438         mlt_log_info( NULL, _("Connected ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
439       
440       free (full_port_name);
441     }
442   
443   free (jack_ports);
444 }
445
446 int
447 process_info_set_port_count (process_info_t * procinfo,
448         unsigned long port_count, gboolean connect_inputs, gboolean connect_outputs)
449 {
450   unsigned long i;
451   char * port_name;
452   jack_port_t ** port_ptr;
453   gshort in;
454   
455   if (procinfo->port_count >= port_count)
456       return -1;
457   
458   if (procinfo->port_count == 0)
459     {
460       procinfo->jack_input_ports = g_malloc (sizeof (jack_port_t *) * port_count);
461       procinfo->jack_output_ports = g_malloc (sizeof (jack_port_t *) * port_count);
462       
463       procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
464       procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
465     }
466   else
467     {
468       procinfo->jack_input_ports = g_realloc (procinfo->jack_input_ports, sizeof (jack_port_t *) * port_count);
469       procinfo->jack_output_ports = g_realloc (procinfo->jack_output_ports, sizeof (jack_port_t *) * port_count);
470
471       procinfo->jack_input_buffers = g_realloc (procinfo->jack_input_buffers, sizeof (LADSPA_Data *) * port_count);
472       procinfo->jack_output_buffers = g_realloc (procinfo->jack_output_buffers, sizeof (LADSPA_Data *) * port_count);
473     }
474   
475   for (i = procinfo->port_count; i < port_count; i++)
476     {
477     for (in = 0; in < 2; in++)
478       {
479         port_name = g_strdup_printf ("%s_%ld", in ? "in" : "out", i + 1);
480        
481         //mlt_log_debug( NULL, _("Creating %s port %s\n"), in ? "input" : "output", port_name);
482         
483         port_ptr = (in ? &procinfo->jack_input_ports[i]
484                        : &procinfo->jack_output_ports[i]);
485         
486         *port_ptr =  jack_port_register (procinfo->jack_client,
487                                          port_name,
488                                          JACK_DEFAULT_AUDIO_TYPE,
489                                          in ? JackPortIsInput : JackPortIsOutput,
490                                          0);
491         
492         if (!*port_ptr)
493           {
494             mlt_log_error( NULL, "%s: could not register port '%s'; aborting\n",
495                      __FUNCTION__, port_name);
496             return 1;
497           }
498
499         //mlt_log_debug( NULL, _("Created %s port %s\n"), in ? "input" : "output", port_name);
500         
501         if ((in && connect_inputs) || (!in && connect_outputs))
502           process_info_connect_port (procinfo, in, i, port_name);
503         
504         g_free (port_name);
505       }
506     }
507   
508   procinfo->port_count = port_count;
509
510   return 0;
511 }
512
513 void
514 process_info_set_channels (process_info_t * procinfo,
515         unsigned long channels, gboolean connect_inputs, gboolean connect_outputs)
516 {
517   process_info_set_port_count (procinfo, channels, connect_inputs, connect_outputs);
518   procinfo->channels = channels; 
519 }
520
521 process_info_t *
522 process_info_new (const char * client_name, unsigned long rack_channels, 
523         gboolean connect_inputs, gboolean connect_outputs)
524 {
525   process_info_t * procinfo;
526   char * jack_client_name;
527   int err;
528
529   procinfo = g_malloc (sizeof (process_info_t));
530   
531   procinfo->chain = NULL;
532   procinfo->chain_end = NULL;
533   procinfo->jack_client = NULL;
534   procinfo->port_count = 0;
535   procinfo->jack_input_ports = NULL;
536   procinfo->jack_output_ports = NULL;
537   procinfo->channels = rack_channels;
538   procinfo->quit = FALSE;
539         
540   if ( client_name == NULL )
541     {
542       sample_rate = 48000; // should be set externally before calling process_ladspa
543       buffer_size = MAX_BUFFER_SIZE;
544       procinfo->silent_buffer = g_malloc (sizeof (LADSPA_Data) * buffer_size );
545       procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * rack_channels);
546       procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * rack_channels);
547
548       return procinfo;
549     }
550
551   /* sort out the client name */
552   procinfo->jack_client_name = jack_client_name = strdup (client_name);
553   for (err = 0; jack_client_name[err] != '\0'; err++)
554     {
555       if (jack_client_name[err] == ' ')
556         jack_client_name[err] = '_';
557       else if (!isalnum (jack_client_name[err]))
558         { /* shift all the chars up one (to remove the non-alphanumeric char) */
559           int i;
560           for (i = err; jack_client_name[i] != '\0'; i++)
561             jack_client_name[i] = jack_client_name[i + 1];
562         }
563       else if (isupper (jack_client_name[err]))
564         jack_client_name[err] = tolower (jack_client_name[err]);
565     }
566   
567   err = process_info_connect_jack (procinfo);
568   if (err)
569     {
570 /*      g_free (procinfo); */
571       return NULL;
572 /*      abort (); */
573     }
574   
575   sample_rate = jack_get_sample_rate (procinfo->jack_client);
576   buffer_size = jack_get_sample_rate (procinfo->jack_client);
577   
578   jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
579   pthread_mutex_lock( &g_activate_mutex );
580   jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
581   pthread_mutex_unlock( &g_activate_mutex );
582   
583   jack_activate (procinfo->jack_client);
584
585   err = process_info_set_port_count (procinfo, rack_channels, connect_inputs, connect_outputs);
586   if (err)
587     return NULL;
588
589   return procinfo;
590 }
591
592 void
593 process_info_destroy (process_info_t * procinfo) {
594   if (procinfo->jack_client)
595     {
596       jack_deactivate (procinfo->jack_client);
597       jack_client_close (procinfo->jack_client);
598     }
599   g_free (procinfo->silent_buffer);
600   g_free (procinfo->jack_input_ports);
601   g_free (procinfo->jack_output_ports);
602   g_free (procinfo->jack_input_buffers);
603   g_free (procinfo->jack_output_buffers);
604   g_free (procinfo);
605 }
606
607 void process_quit (process_info_t * procinfo) {
608   procinfo->quit = TRUE;
609 }