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-2014 Meltytech, LLC
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24 
25 #include <pthread.h>
26 #include <jack/jack.h>
27 #include <glib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <sys/time.h>
32 #include <time.h>
33 #include <ctype.h>
34 
35 #include "process.h"
36 #include "lock_free_fifo.h"
37 #include "plugin.h"
38 #include "jack_rack.h"
39 #include "framework/mlt_log.h"
40 
41 #ifndef _
42 #define _(x) x
43 #endif
44 
45 extern pthread_mutex_t g_activate_mutex;
46 
47 #define USEC_PER_SEC         1000000
48 #define MSEC_PER_SEC         1000
49 #define TIME_RUN_SKIP_COUNT  5
50 #define MAX_BUFFER_SIZE      4096
51 
52 jack_nframes_t sample_rate;
53 jack_nframes_t buffer_size;
54 
55 static void
jack_shutdown_cb(void * data)56 jack_shutdown_cb (void * data)
57 {
58   process_info_t * procinfo = data;
59 
60   procinfo->quit = TRUE;
61 }
62 
63 /** process messages for plugins' control ports */
process_control_port_messages(process_info_t * procinfo)64 void process_control_port_messages (process_info_t * procinfo) {
65   plugin_t * plugin;
66   unsigned long control;
67   unsigned long channel;
68   gint copy;
69 
70   if (!procinfo->chain) return;
71 
72   for (plugin = procinfo->chain; plugin; plugin = plugin->next)
73     {
74       if (plugin->desc->control_port_count > 0)
75         for (control = 0; control < plugin->desc->control_port_count; control++)
76           for (copy = 0; copy < plugin->copies; copy++)
77             {
78               while (lff_read (plugin->holders[copy].ui_control_fifos + control,
79                                plugin->holders[copy].control_memory + control) == 0);
80             }
81 
82       if (plugin->wet_dry_enabled)
83         for (channel = 0; channel < procinfo->channels; channel++)
84           {
85             while (lff_read (plugin->wet_dry_fifos + channel,
86                              plugin->wet_dry_values + channel) == 0);
87           }
88     }
89 }
90 
get_jack_buffers(process_info_t * procinfo,jack_nframes_t frames)91 int get_jack_buffers (process_info_t * procinfo, jack_nframes_t frames) {
92   unsigned long channel;
93 
94   for (channel = 0; channel < procinfo->channels; channel++)
95     {
96       procinfo->jack_input_buffers[channel] = jack_port_get_buffer (procinfo->jack_input_ports[channel], frames);
97       if (!procinfo->jack_input_buffers[channel])
98         {
99           mlt_log_verbose( NULL, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
100           return 1;
101         }
102 
103       procinfo->jack_output_buffers[channel] = jack_port_get_buffer (procinfo->jack_output_ports[channel], frames);
104       if (!procinfo->jack_output_buffers[channel])
105         {
106           mlt_log_verbose( NULL, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
107           return 1;
108         }
109     }
110 
111   return 0;
112 }
113 
114 plugin_t *
get_first_enabled_plugin(process_info_t * procinfo)115 get_first_enabled_plugin (process_info_t * procinfo)
116 {
117   plugin_t * first_enabled;
118 
119   if (!procinfo->chain) return NULL;
120 
121   for (first_enabled = procinfo->chain;
122        first_enabled;
123        first_enabled = first_enabled->next)
124     {
125       if (first_enabled->enabled) return first_enabled;
126     }
127 
128   return NULL;
129 }
130 
131 plugin_t *
get_last_enabled_plugin(process_info_t * procinfo)132 get_last_enabled_plugin (process_info_t * procinfo)
133 {
134   plugin_t * last_enabled;
135 
136   if (!procinfo->chain) return NULL;
137 
138   for (last_enabled = procinfo->chain_end;
139        last_enabled;
140        last_enabled = last_enabled->prev)
141     {
142       if (last_enabled->enabled) return last_enabled;
143     }
144 
145   return NULL;
146 }
147 
148 void
connect_chain(process_info_t * procinfo,jack_nframes_t frames)149 connect_chain (process_info_t * procinfo, jack_nframes_t frames)
150 {
151   plugin_t * first_enabled, * last_enabled, * plugin;
152   gint copy;
153   unsigned long channel;
154   if (!procinfo->chain) return;
155 
156   first_enabled = get_first_enabled_plugin (procinfo);
157   if (!first_enabled) return;
158 
159   last_enabled = get_last_enabled_plugin (procinfo);
160 
161   /* sort out the aux ports */
162   plugin = first_enabled;
163   do
164     {
165       if (plugin->desc->aux_channels > 0 && plugin->enabled)
166         {
167           if (procinfo->jack_client)
168             {
169               for (copy = 0; copy < plugin->copies; copy++)
170                 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
171                   plugin->descriptor->
172                     connect_port (plugin->holders[copy].instance,
173                                   plugin->desc->audio_aux_port_indicies[channel],
174                                   jack_port_get_buffer (plugin->holders[copy].aux_ports[channel], frames));
175             }
176           else
177             {
178               for (copy = 0; copy < frames; copy++)
179                 procinfo->silent_buffer[copy] = 0.0;
180 
181               for (copy = 0; copy < plugin->copies; copy++)
182                 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
183                   plugin->descriptor->
184                     connect_port (plugin->holders[copy].instance,
185                                   plugin->desc->audio_aux_port_indicies[channel],
186                                   procinfo->silent_buffer);
187             }
188         }
189     }
190   while ( (plugin != last_enabled) && (plugin = plugin->next) );
191 
192   /* ensure that all the of the enabled plugins are connected to their memory */
193   plugin_connect_output_ports (first_enabled);
194   if (first_enabled != last_enabled)
195     {
196       plugin_connect_input_ports (last_enabled, last_enabled->prev->audio_output_memory);
197       for (plugin = first_enabled->next; plugin; plugin = plugin->next)
198         {
199           if (plugin->enabled)
200             {
201               plugin_connect_input_ports (plugin, plugin->prev->audio_output_memory);
202               plugin_connect_output_ports (plugin);
203             }
204         }
205     }
206 
207   /* input buffers for first plugin */
208   if( plugin->desc->has_input )
209     plugin_connect_input_ports (first_enabled, procinfo->jack_input_buffers);
210 }
211 
212 void
process_chain(process_info_t * procinfo,jack_nframes_t frames)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 
process_ladspa(process_info_t * procinfo,jack_nframes_t frames,LADSPA_Data ** inputs,LADSPA_Data ** outputs)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       if(get_first_enabled_plugin (procinfo)->desc->has_input)
317         {
318           procinfo->jack_input_buffers[channel] = inputs[channel];
319           if (!procinfo->jack_input_buffers[channel])
320             {
321               mlt_log_verbose( NULL, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
322               return 1;
323             }
324         }
325       procinfo->jack_output_buffers[channel] = outputs[channel];
326       if (!procinfo->jack_output_buffers[channel])
327         {
328           mlt_log_verbose( NULL, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
329           return 1;
330         }
331     }
332 
333   connect_chain (procinfo, frames);
334 
335   process_chain (procinfo, frames);
336 
337   return 0;
338 }
339 
process_jack(jack_nframes_t frames,void * data)340 int process_jack (jack_nframes_t frames, void * data) {
341   int err;
342   process_info_t * procinfo;
343 
344   procinfo = (process_info_t *) data;
345 
346   if (!procinfo)
347     {
348       mlt_log_error( NULL, "%s: no process_info from jack!\n", __FUNCTION__);
349       return 1;
350     }
351 
352   if (procinfo->port_count == 0)
353     return 0;
354 
355   if (procinfo->quit == TRUE)
356     return 1;
357 
358   process_control_port_messages (procinfo);
359 
360   err = get_jack_buffers (procinfo, frames);
361   if (err)
362     {
363       mlt_log_warning( NULL, "%s: failed to get jack ports, not processing\n", __FUNCTION__);
364       return 0;
365     }
366 
367   connect_chain (procinfo, frames);
368 
369   process_chain (procinfo, frames);
370 
371   return 0;
372 }
373 
374 
375 
376 /*******************************************
377  ************** non RT stuff ***************
378  *******************************************/
379 
380 static int
process_info_connect_jack(process_info_t * procinfo)381 process_info_connect_jack (process_info_t * procinfo)
382 {
383   mlt_log_info( NULL, _("Connecting to JACK server with client name '%s'\n"), procinfo->jack_client_name);
384 
385   procinfo->jack_client = jack_client_open (procinfo->jack_client_name, JackNullOption, NULL);
386 
387   if (!procinfo->jack_client)
388     {
389       mlt_log_warning( NULL, "%s: could not create jack client; is the jackd server running?\n", __FUNCTION__);
390       return 1;
391     }
392 
393   mlt_log_verbose( NULL, _("Connected to JACK server\n"));
394 
395   jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
396   jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
397 
398   return 0;
399 }
400 
401 static void
process_info_connect_port(process_info_t * procinfo,gshort in,unsigned long port_index,const char * port_name)402 process_info_connect_port (process_info_t * procinfo,
403                            gshort in,
404                            unsigned long port_index,
405                            const char * port_name)
406 {
407   const char ** jack_ports;
408   unsigned long jack_port_index;
409   int err;
410   char * full_port_name;
411 
412   jack_ports = jack_get_ports (procinfo->jack_client, NULL, NULL,
413                                JackPortIsPhysical | (in ? JackPortIsOutput : JackPortIsInput));
414 
415   if (!jack_ports)
416     return;
417 
418   for (jack_port_index = 0;
419        jack_ports[jack_port_index] && jack_port_index <= port_index;
420        jack_port_index++)
421     {
422       if (jack_port_index != port_index)
423         continue;
424 
425       full_port_name = g_strdup_printf ("%s:%s", procinfo->jack_client_name, port_name);
426 
427       mlt_log_debug( NULL, _("Connecting ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
428 
429       err = jack_connect (procinfo->jack_client,
430                           in ? jack_ports[jack_port_index] : full_port_name,
431                           in ? full_port_name : jack_ports[jack_port_index]);
432 
433       if (err)
434         mlt_log_warning( NULL, "%s: error connecting ports '%s' and '%s'\n",
435                  __FUNCTION__, full_port_name, jack_ports[jack_port_index]);
436       else
437         mlt_log_info( NULL, _("Connected ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
438 
439       free (full_port_name);
440     }
441 
442   free (jack_ports);
443 }
444 
445 int
process_info_set_port_count(process_info_t * procinfo,unsigned long port_count,gboolean connect_inputs,gboolean connect_outputs)446 process_info_set_port_count (process_info_t * procinfo,
447 	unsigned long port_count, gboolean connect_inputs, gboolean connect_outputs)
448 {
449   unsigned long i;
450   char * port_name;
451   jack_port_t ** port_ptr;
452   gshort in;
453 
454   if (procinfo->port_count >= port_count)
455       return -1;
456 
457   if (procinfo->port_count == 0)
458     {
459       procinfo->jack_input_ports = g_malloc (sizeof (jack_port_t *) * port_count);
460       procinfo->jack_output_ports = g_malloc (sizeof (jack_port_t *) * port_count);
461 
462       procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
463       procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
464     }
465   else
466     {
467       procinfo->jack_input_ports = g_realloc (procinfo->jack_input_ports, sizeof (jack_port_t *) * port_count);
468       procinfo->jack_output_ports = g_realloc (procinfo->jack_output_ports, sizeof (jack_port_t *) * port_count);
469 
470       procinfo->jack_input_buffers = g_realloc (procinfo->jack_input_buffers, sizeof (LADSPA_Data *) * port_count);
471       procinfo->jack_output_buffers = g_realloc (procinfo->jack_output_buffers, sizeof (LADSPA_Data *) * port_count);
472     }
473 
474   for (i = procinfo->port_count; i < port_count; i++)
475     {
476     for (in = 0; in < 2; in++)
477       {
478         port_name = g_strdup_printf ("%s_%ld", in ? "in" : "out", i + 1);
479 
480         //mlt_log_debug( NULL, _("Creating %s port %s\n"), in ? "input" : "output", port_name);
481 
482         port_ptr = (in ? &procinfo->jack_input_ports[i]
483                        : &procinfo->jack_output_ports[i]);
484 
485         *port_ptr =  jack_port_register (procinfo->jack_client,
486                                          port_name,
487                                          JACK_DEFAULT_AUDIO_TYPE,
488                                          in ? JackPortIsInput : JackPortIsOutput,
489                                          0);
490 
491         if (!*port_ptr)
492           {
493             mlt_log_error( NULL, "%s: could not register port '%s'; aborting\n",
494                      __FUNCTION__, port_name);
495             return 1;
496           }
497 
498         //mlt_log_debug( NULL, _("Created %s port %s\n"), in ? "input" : "output", port_name);
499 
500         if ((in && connect_inputs) || (!in && connect_outputs))
501           process_info_connect_port (procinfo, in, i, port_name);
502 
503         g_free (port_name);
504       }
505     }
506 
507   procinfo->port_count = port_count;
508 
509   return 0;
510 }
511 
512 void
process_info_set_channels(process_info_t * procinfo,unsigned long channels,gboolean connect_inputs,gboolean connect_outputs)513 process_info_set_channels (process_info_t * procinfo,
514 	unsigned long channels, gboolean connect_inputs, gboolean connect_outputs)
515 {
516   process_info_set_port_count (procinfo, channels, connect_inputs, connect_outputs);
517   procinfo->channels = channels;
518 }
519 
520 process_info_t *
process_info_new(const char * client_name,unsigned long rack_channels,gboolean connect_inputs,gboolean connect_outputs)521 process_info_new (const char * client_name, unsigned long rack_channels,
522 	gboolean connect_inputs, gboolean connect_outputs)
523 {
524   process_info_t * procinfo;
525   char * jack_client_name;
526   int err;
527 
528   procinfo = g_malloc (sizeof (process_info_t));
529 
530   procinfo->chain = NULL;
531   procinfo->chain_end = NULL;
532   procinfo->jack_client = NULL;
533   procinfo->port_count = 0;
534   procinfo->jack_input_ports = NULL;
535   procinfo->jack_output_ports = NULL;
536   procinfo->channels = rack_channels;
537   procinfo->quit = FALSE;
538 
539   if ( client_name == NULL )
540     {
541       sample_rate = 48000; // should be set externally before calling process_ladspa
542       buffer_size = MAX_BUFFER_SIZE;
543       procinfo->silent_buffer = g_malloc (sizeof (LADSPA_Data) * buffer_size );
544       procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * rack_channels);
545       procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * rack_channels);
546 
547       return procinfo;
548     }
549 
550   /* sort out the client name */
551   procinfo->jack_client_name = jack_client_name = strdup (client_name);
552   for (err = 0; jack_client_name[err] != '\0'; err++)
553     {
554       if (jack_client_name[err] == ' ')
555         jack_client_name[err] = '_';
556       else if (!isalnum (jack_client_name[err]))
557         { /* shift all the chars up one (to remove the non-alphanumeric char) */
558           int i;
559           for (i = err; jack_client_name[i] != '\0'; i++)
560             jack_client_name[i] = jack_client_name[i + 1];
561         }
562       else if (isupper (jack_client_name[err]))
563         jack_client_name[err] = tolower (jack_client_name[err]);
564     }
565 
566   err = process_info_connect_jack (procinfo);
567   if (err)
568     {
569 /*      g_free (procinfo); */
570       return NULL;
571 /*      abort (); */
572     }
573 
574   sample_rate = jack_get_sample_rate (procinfo->jack_client);
575   buffer_size = jack_get_sample_rate (procinfo->jack_client);
576 
577   jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
578   pthread_mutex_lock( &g_activate_mutex );
579   jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
580   pthread_mutex_unlock( &g_activate_mutex );
581 
582   jack_activate (procinfo->jack_client);
583 
584   err = process_info_set_port_count (procinfo, rack_channels, connect_inputs, connect_outputs);
585   if (err)
586     return NULL;
587 
588   return procinfo;
589 }
590 
591 void
process_info_destroy(process_info_t * procinfo)592 process_info_destroy (process_info_t * procinfo) {
593   if (procinfo->jack_client)
594     {
595       jack_deactivate (procinfo->jack_client);
596       jack_client_close (procinfo->jack_client);
597     }
598   g_free (procinfo->silent_buffer);
599   g_free (procinfo->jack_input_ports);
600   g_free (procinfo->jack_output_ports);
601   g_free (procinfo->jack_input_buffers);
602   g_free (procinfo->jack_output_buffers);
603   g_free (procinfo);
604 }
605 
process_quit(process_info_t * procinfo)606 void process_quit (process_info_t * procinfo) {
607   procinfo->quit = TRUE;
608 }
609