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