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