]> git.sesse.net Git - mlt/blob - src/modules/jackrack/process.c
88c00e8f55915a64150118848f0e5b117e4c0c88
[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   plugin_connect_input_ports (first_enabled, procinfo->jack_input_buffers);
210 }
211
212 void
213 process_chain (process_info_t * procinfo, jack_nframes_t frames)
214 {
215   plugin_t * first_enabled;
216   plugin_t * last_enabled = NULL;
217   plugin_t * plugin;
218   unsigned long channel;
219   unsigned long i;
220
221   if (procinfo->jack_client)
222     {
223       LADSPA_Data zero_signal[frames];
224       guint copy;
225
226       /* set the zero signal to zero */
227       for (channel = 0; channel < frames; channel++)
228         zero_signal[channel] = 0.0;
229     
230       /* possibly set aux output channels to zero if they're not enabled */
231       for (plugin = procinfo->chain; plugin; plugin = plugin->next)
232         if (!plugin->enabled &&
233             plugin->desc->aux_channels > 0 &&
234             !plugin->desc->aux_are_input)
235           for (copy = 0; copy < plugin->copies; copy++)
236             for (channel = 0; channel < plugin->desc->aux_channels; channel++)
237               memcpy (jack_port_get_buffer (plugin->holders[copy].aux_ports[channel], frames),
238                       zero_signal, sizeof (LADSPA_Data) * frames);
239     }
240
241   first_enabled = get_first_enabled_plugin (procinfo);
242   
243   /* no chain; just copy input to output */
244   if (!procinfo->chain || !first_enabled)
245     {
246       unsigned long channel;
247       for (channel = 0; channel < procinfo->channels; channel++)
248         {
249           memcpy (procinfo->jack_output_buffers[channel],
250                   procinfo->jack_input_buffers[channel],
251                   sizeof(LADSPA_Data) * frames);
252         }
253       return;
254     }
255   
256   /* all past here is guaranteed to have at least 1 enabled plugin */
257
258   last_enabled = get_last_enabled_plugin (procinfo);
259   
260   for (plugin = first_enabled;
261        plugin;
262        plugin = plugin->next)
263     {
264       if (plugin->enabled)
265         {
266           for (i = 0; i < plugin->copies; i++)
267             plugin->descriptor->run (plugin->holders[i].instance, frames);
268           
269           if (plugin->wet_dry_enabled)
270             for (channel = 0; channel < procinfo->channels; channel++)
271               for (i = 0; i < frames; i++)
272                 {
273                   plugin->audio_output_memory[channel][i] *= plugin->wet_dry_values[channel];
274                   plugin->audio_output_memory[channel][i] += plugin->audio_input_memory[channel][i] * (1.0 - plugin->wet_dry_values[channel]);
275                 }
276           
277           if (plugin == last_enabled)
278             break;
279         }
280       else
281         {
282     
283           /* copy the data through */
284           for (i = 0; i < procinfo->channels; i++)
285             memcpy (plugin->audio_output_memory[i],
286                     plugin->prev->audio_output_memory[i],
287                     sizeof(LADSPA_Data) * frames);
288         }
289     }
290   
291   /* copy the last enabled data to the jack ports */
292   for (i = 0; i < procinfo->channels; i++)
293     memcpy (procinfo->jack_output_buffers[i],
294             last_enabled->audio_output_memory[i],
295             sizeof(LADSPA_Data) * frames);
296   
297 }
298
299 int process_ladspa (process_info_t * procinfo, jack_nframes_t frames,
300                     LADSPA_Data ** inputs, LADSPA_Data ** outputs) {
301   unsigned long channel;
302   
303   if (!procinfo)
304     {
305       mlt_log_error( NULL, "%s: no process_info from jack!\n", __FUNCTION__);
306       return 1;
307     }
308   
309   if (procinfo->quit == TRUE)
310     return 1;
311   
312   process_control_port_messages (procinfo);
313   
314   for (channel = 0; channel < procinfo->channels; channel++)
315     {
316       procinfo->jack_input_buffers[channel] = inputs[channel];
317       if (!procinfo->jack_input_buffers[channel])
318         {
319           mlt_log_verbose( NULL, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
320           return 1;
321         }
322
323       procinfo->jack_output_buffers[channel] = outputs[channel];
324       if (!procinfo->jack_output_buffers[channel])
325         {
326           mlt_log_verbose( NULL, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
327           return 1;
328         }
329     }
330   
331   connect_chain (procinfo, frames);
332   
333   process_chain (procinfo, frames);
334   
335   return 0;
336 }
337
338 int process_jack (jack_nframes_t frames, void * data) {
339   int err;
340   process_info_t * procinfo;
341   
342   procinfo = (process_info_t *) data;
343   
344   if (!procinfo)
345     {
346       mlt_log_error( NULL, "%s: no process_info from jack!\n", __FUNCTION__);
347       return 1;
348     }
349   
350   if (procinfo->port_count == 0)
351     return 0;
352   
353   if (procinfo->quit == TRUE)
354     return 1;
355   
356   process_control_port_messages (procinfo);
357   
358   err = get_jack_buffers (procinfo, frames);
359   if (err)
360     {
361       mlt_log_warning( NULL, "%s: failed to get jack ports, not processing\n", __FUNCTION__);
362       return 0;
363     }
364   
365   connect_chain (procinfo, frames);
366   
367   process_chain (procinfo, frames);
368   
369   return 0;
370 }
371
372
373
374 /*******************************************
375  ************** non RT stuff ***************
376  *******************************************/
377  
378 static int
379 process_info_connect_jack (process_info_t * procinfo)
380 {
381   mlt_log_info( NULL, _("Connecting to JACK server with client name '%s'\n"), procinfo->jack_client_name);
382
383   procinfo->jack_client = jack_client_open (procinfo->jack_client_name, JackNullOption, NULL);
384
385   if (!procinfo->jack_client)
386     {
387       mlt_log_warning( NULL, "%s: could not create jack client; is the jackd server running?\n", __FUNCTION__);
388       return 1;
389     }
390
391   mlt_log_verbose( NULL, _("Connected to JACK server\n"));
392
393   jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
394   jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
395                                             
396   return 0;
397 }
398
399 static void
400 process_info_connect_port (process_info_t * procinfo,
401                            gshort in,
402                            unsigned long port_index,
403                            const char * port_name)
404 {
405   const char ** jack_ports;
406   unsigned long jack_port_index;
407   int err;
408   char * full_port_name;
409   
410   jack_ports = jack_get_ports (procinfo->jack_client, NULL, NULL,
411                                JackPortIsPhysical | (in ? JackPortIsOutput : JackPortIsInput));
412   
413   if (!jack_ports)
414     return;
415   
416   for (jack_port_index = 0;
417        jack_ports[jack_port_index] && jack_port_index <= port_index;
418        jack_port_index++)
419     {
420       if (jack_port_index != port_index)
421         continue;
422         
423       full_port_name = g_strdup_printf ("%s:%s", procinfo->jack_client_name, port_name);
424
425       mlt_log_debug( NULL, _("Connecting ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
426
427       err = jack_connect (procinfo->jack_client,
428                           in ? jack_ports[jack_port_index] : full_port_name,
429                           in ? full_port_name : jack_ports[jack_port_index]);
430
431       if (err)
432         mlt_log_warning( NULL, "%s: error connecting ports '%s' and '%s'\n",
433                  __FUNCTION__, full_port_name, jack_ports[jack_port_index]);
434       else
435         mlt_log_info( NULL, _("Connected ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
436       
437       free (full_port_name);
438     }
439   
440   free (jack_ports);
441 }
442
443 int
444 process_info_set_port_count (process_info_t * procinfo,
445         unsigned long port_count, gboolean connect_inputs, gboolean connect_outputs)
446 {
447   unsigned long i;
448   char * port_name;
449   jack_port_t ** port_ptr;
450   gshort in;
451   
452   if (procinfo->port_count >= port_count)
453       return -1;
454   
455   if (procinfo->port_count == 0)
456     {
457       procinfo->jack_input_ports = g_malloc (sizeof (jack_port_t *) * port_count);
458       procinfo->jack_output_ports = g_malloc (sizeof (jack_port_t *) * port_count);
459       
460       procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
461       procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
462     }
463   else
464     {
465       procinfo->jack_input_ports = g_realloc (procinfo->jack_input_ports, sizeof (jack_port_t *) * port_count);
466       procinfo->jack_output_ports = g_realloc (procinfo->jack_output_ports, sizeof (jack_port_t *) * port_count);
467
468       procinfo->jack_input_buffers = g_realloc (procinfo->jack_input_buffers, sizeof (LADSPA_Data *) * port_count);
469       procinfo->jack_output_buffers = g_realloc (procinfo->jack_output_buffers, sizeof (LADSPA_Data *) * port_count);
470     }
471   
472   for (i = procinfo->port_count; i < port_count; i++)
473     {
474     for (in = 0; in < 2; in++)
475       {
476         port_name = g_strdup_printf ("%s_%ld", in ? "in" : "out", i + 1);
477        
478         //mlt_log_debug( NULL, _("Creating %s port %s\n"), in ? "input" : "output", port_name);
479         
480         port_ptr = (in ? &procinfo->jack_input_ports[i]
481                        : &procinfo->jack_output_ports[i]);
482         
483         *port_ptr =  jack_port_register (procinfo->jack_client,
484                                          port_name,
485                                          JACK_DEFAULT_AUDIO_TYPE,
486                                          in ? JackPortIsInput : JackPortIsOutput,
487                                          0);
488         
489         if (!*port_ptr)
490           {
491             mlt_log_error( NULL, "%s: could not register port '%s'; aborting\n",
492                      __FUNCTION__, port_name);
493             return 1;
494           }
495
496         //mlt_log_debug( NULL, _("Created %s port %s\n"), in ? "input" : "output", port_name);
497         
498         if ((in && connect_inputs) || (!in && connect_outputs))
499           process_info_connect_port (procinfo, in, i, port_name);
500         
501         g_free (port_name);
502       }
503     }
504   
505   procinfo->port_count = port_count;
506
507   return 0;
508 }
509
510 void
511 process_info_set_channels (process_info_t * procinfo,
512         unsigned long channels, gboolean connect_inputs, gboolean connect_outputs)
513 {
514   process_info_set_port_count (procinfo, channels, connect_inputs, connect_outputs);
515   procinfo->channels = channels; 
516 }
517
518 process_info_t *
519 process_info_new (const char * client_name, unsigned long rack_channels, 
520         gboolean connect_inputs, gboolean connect_outputs)
521 {
522   process_info_t * procinfo;
523   char * jack_client_name;
524   int err;
525
526   procinfo = g_malloc (sizeof (process_info_t));
527   
528   procinfo->chain = NULL;
529   procinfo->chain_end = NULL;
530   procinfo->jack_client = NULL;
531   procinfo->port_count = 0;
532   procinfo->jack_input_ports = NULL;
533   procinfo->jack_output_ports = NULL;
534   procinfo->channels = rack_channels;
535   procinfo->quit = FALSE;
536         
537   if ( client_name == NULL )
538     {
539       sample_rate = 48000; // should be set externally before calling process_ladspa
540       buffer_size = MAX_BUFFER_SIZE;
541       procinfo->silent_buffer = g_malloc (sizeof (LADSPA_Data) * buffer_size );
542       procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * rack_channels);
543       procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * rack_channels);
544
545       return procinfo;
546     }
547
548   /* sort out the client name */
549   procinfo->jack_client_name = jack_client_name = strdup (client_name);
550   for (err = 0; jack_client_name[err] != '\0'; err++)
551     {
552       if (jack_client_name[err] == ' ')
553         jack_client_name[err] = '_';
554       else if (!isalnum (jack_client_name[err]))
555         { /* shift all the chars up one (to remove the non-alphanumeric char) */
556           int i;
557           for (i = err; jack_client_name[i] != '\0'; i++)
558             jack_client_name[i] = jack_client_name[i + 1];
559         }
560       else if (isupper (jack_client_name[err]))
561         jack_client_name[err] = tolower (jack_client_name[err]);
562     }
563   
564   err = process_info_connect_jack (procinfo);
565   if (err)
566     {
567 /*      g_free (procinfo); */
568       return NULL;
569 /*      abort (); */
570     }
571   
572   sample_rate = jack_get_sample_rate (procinfo->jack_client);
573   buffer_size = jack_get_sample_rate (procinfo->jack_client);
574   
575   jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
576   pthread_mutex_lock( &g_activate_mutex );
577   jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
578   pthread_mutex_unlock( &g_activate_mutex );
579   
580   jack_activate (procinfo->jack_client);
581
582   err = process_info_set_port_count (procinfo, rack_channels, connect_inputs, connect_outputs);
583   if (err)
584     return NULL;
585
586   return procinfo;
587 }
588
589 void
590 process_info_destroy (process_info_t * procinfo) {
591   if (procinfo->jack_client)
592     {
593       jack_deactivate (procinfo->jack_client);
594       jack_client_close (procinfo->jack_client);
595     }
596   g_free (procinfo->silent_buffer);
597   g_free (procinfo->jack_input_ports);
598   g_free (procinfo->jack_output_ports);
599   g_free (procinfo->jack_input_buffers);
600   g_free (procinfo->jack_output_buffers);
601   g_free (procinfo);
602 }
603
604 void process_quit (process_info_t * procinfo) {
605   procinfo->quit = TRUE;
606 }