5 * Copyright (C) Robert Ham 2002, 2003 (node@users.sourceforge.net)
7 * Modification for MLT:
8 * Copyright (C) 2004 Ushodaya Enterprises Limited
9 * Author: Dan Dennedy <dan@dennedy.org>
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.
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.
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.
26 #include <jack/jack.h>
36 #include "lock_free_fifo.h"
38 #include "jack_rack.h"
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
49 jack_nframes_t sample_rate;
50 jack_nframes_t buffer_size;
53 jack_shutdown_cb (void * data)
55 process_info_t * procinfo = data;
57 procinfo->quit = TRUE;
60 /** process messages for plugins' control ports */
61 void process_control_port_messages (process_info_t * procinfo) {
63 unsigned long control;
64 unsigned long channel;
67 if (!procinfo->chain) return;
69 for (plugin = procinfo->chain; plugin; plugin = plugin->next)
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++)
75 while (lff_read (plugin->holders[copy].ui_control_fifos + control,
76 plugin->holders[copy].control_memory + control) == 0);
79 if (plugin->wet_dry_enabled)
80 for (channel = 0; channel < procinfo->channels; channel++)
82 while (lff_read (plugin->wet_dry_fifos + channel,
83 plugin->wet_dry_values + channel) == 0);
88 int get_jack_buffers (process_info_t * procinfo, jack_nframes_t frames) {
89 unsigned long channel;
91 for (channel = 0; channel < procinfo->channels; channel++)
93 procinfo->jack_input_buffers[channel] = jack_port_get_buffer (procinfo->jack_input_ports[channel], frames);
94 if (!procinfo->jack_input_buffers[channel])
96 fprintf (stderr, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
100 procinfo->jack_output_buffers[channel] = jack_port_get_buffer (procinfo->jack_output_ports[channel], frames);
101 if (!procinfo->jack_output_buffers[channel])
103 fprintf (stderr, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
112 get_first_enabled_plugin (process_info_t * procinfo)
114 plugin_t * first_enabled;
116 if (!procinfo->chain) return NULL;
118 for (first_enabled = procinfo->chain;
120 first_enabled = first_enabled->next)
122 if (first_enabled->enabled) return first_enabled;
129 get_last_enabled_plugin (process_info_t * procinfo)
131 plugin_t * last_enabled;
133 if (!procinfo->chain) return NULL;
135 for (last_enabled = procinfo->chain_end;
137 last_enabled = last_enabled->prev)
139 if (last_enabled->enabled) return last_enabled;
146 connect_chain (process_info_t * procinfo, jack_nframes_t frames)
148 plugin_t * first_enabled, * last_enabled, * plugin;
150 unsigned long channel;
151 if (!procinfo->chain) return;
153 first_enabled = get_first_enabled_plugin (procinfo);
154 if (!first_enabled) return;
156 last_enabled = get_last_enabled_plugin (procinfo);
158 /* sort out the aux ports */
159 plugin = first_enabled;
162 if (plugin->desc->aux_channels > 0 && plugin->enabled)
164 if (procinfo->jack_client)
166 for (copy = 0; copy < plugin->copies; copy++)
167 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
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));
175 for (copy = 0; copy < frames; copy++)
176 procinfo->silent_buffer[copy] = 0.0;
178 for (copy = 0; copy < plugin->copies; copy++)
179 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
181 connect_port (plugin->holders[copy].instance,
182 plugin->desc->audio_aux_port_indicies[channel],
183 procinfo->silent_buffer);
187 while ( (plugin != last_enabled) && (plugin = plugin->next) );
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)
193 plugin_connect_input_ports (last_enabled, last_enabled->prev->audio_output_memory);
194 for (plugin = first_enabled->next; plugin; plugin = plugin->next)
198 plugin_connect_input_ports (plugin, plugin->prev->audio_output_memory);
199 plugin_connect_output_ports (plugin);
204 /* input buffers for first plugin */
205 plugin_connect_input_ports (first_enabled, procinfo->jack_input_buffers);
209 process_chain (process_info_t * procinfo, jack_nframes_t frames)
211 plugin_t * first_enabled;
212 plugin_t * last_enabled = NULL;
214 unsigned long channel;
217 if (procinfo->jack_client)
219 LADSPA_Data zero_signal[frames];
222 /* set the zero signal to zero */
223 for (channel = 0; channel < frames; channel++)
224 zero_signal[channel] = 0.0;
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);
237 first_enabled = get_first_enabled_plugin (procinfo);
239 /* no chain; just copy input to output */
240 if (!procinfo->chain || !first_enabled)
242 unsigned long channel;
243 for (channel = 0; channel < procinfo->channels; channel++)
245 memcpy (procinfo->jack_output_buffers[channel],
246 procinfo->jack_input_buffers[channel],
247 sizeof(LADSPA_Data) * frames);
252 /* all past here is guaranteed to have at least 1 enabled plugin */
254 last_enabled = get_last_enabled_plugin (procinfo);
256 for (plugin = first_enabled;
258 plugin = plugin->next)
262 for (i = 0; i < plugin->copies; i++)
263 plugin->descriptor->run (plugin->holders[i].instance, frames);
265 if (plugin->wet_dry_enabled)
266 for (channel = 0; channel < procinfo->channels; channel++)
267 for (i = 0; i < frames; i++)
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]);
273 if (plugin == last_enabled)
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);
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);
295 int process_ladspa (process_info_t * procinfo, jack_nframes_t frames,
296 LADSPA_Data ** inputs, LADSPA_Data ** outputs) {
297 unsigned long channel;
301 fprintf (stderr, "%s: no process_info from jack!\n", __FUNCTION__);
305 if (procinfo->quit == TRUE)
308 process_control_port_messages (procinfo);
310 for (channel = 0; channel < procinfo->channels; channel++)
312 procinfo->jack_input_buffers[channel] = inputs[channel];
313 if (!procinfo->jack_input_buffers[channel])
315 fprintf (stderr, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
319 procinfo->jack_output_buffers[channel] = outputs[channel];
320 if (!procinfo->jack_output_buffers[channel])
322 fprintf (stderr, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
327 connect_chain (procinfo, frames);
329 process_chain (procinfo, frames);
334 int process_jack (jack_nframes_t frames, void * data) {
336 process_info_t * procinfo;
338 procinfo = (process_info_t *) data;
342 fprintf (stderr, "%s: no process_info from jack!\n", __FUNCTION__);
346 if (procinfo->port_count == 0)
349 if (procinfo->quit == TRUE)
352 process_control_port_messages (procinfo);
354 err = get_jack_buffers (procinfo, frames);
357 fprintf(stderr, "%s: failed to get jack ports, not processing\n", __FUNCTION__);
361 connect_chain (procinfo, frames);
363 process_chain (procinfo, frames);
370 /*******************************************
371 ************** non RT stuff ***************
372 *******************************************/
375 process_info_connect_jack (process_info_t * procinfo)
377 printf (_("Connecting to JACK server with client name '%s'\n"), procinfo->jack_client_name);
379 procinfo->jack_client = jack_client_new (procinfo->jack_client_name);
381 if (!procinfo->jack_client)
383 fprintf (stderr, "%s: could not create jack client; is the jackd server running?\n", __FUNCTION__);
387 printf (_("Connected to JACK server\n"));
389 jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
390 jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
396 process_info_connect_port (process_info_t * procinfo,
398 unsigned long port_index,
399 const char * port_name)
401 const char ** jack_ports;
402 unsigned long jack_port_index;
404 char * full_port_name;
406 jack_ports = jack_get_ports (procinfo->jack_client, NULL, NULL,
407 JackPortIsPhysical | (in ? JackPortIsOutput : JackPortIsInput));
412 for (jack_port_index = 0;
413 jack_ports[jack_port_index] && jack_port_index <= port_index;
416 if (jack_port_index != port_index)
419 full_port_name = g_strdup_printf ("%s:%s", procinfo->jack_client_name, port_name);
421 printf (_("Connecting ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
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]);
428 fprintf (stderr, "%s: error connecting ports '%s' and '%s'\n",
429 __FUNCTION__, full_port_name, jack_ports[jack_port_index]);
431 printf (_("Connected ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
433 free (full_port_name);
440 process_info_set_port_count (process_info_t * procinfo,
441 unsigned long port_count, gboolean connect_inputs, gboolean connect_outputs)
445 jack_port_t ** port_ptr;
448 if (procinfo->port_count >= port_count)
451 if (procinfo->port_count == 0)
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);
456 procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
457 procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
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);
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);
468 for (i = procinfo->port_count; i < port_count; i++)
470 for (in = 0; in < 2; in++)
472 port_name = g_strdup_printf ("%s_%ld", in ? "in" : "out", i + 1);
474 //printf (_("Creating %s port %s\n"), in ? "input" : "output", port_name);
476 port_ptr = (in ? &procinfo->jack_input_ports[i]
477 : &procinfo->jack_output_ports[i]);
479 *port_ptr = jack_port_register (procinfo->jack_client,
481 JACK_DEFAULT_AUDIO_TYPE,
482 in ? JackPortIsInput : JackPortIsOutput,
487 fprintf (stderr, "%s: could not register port '%s'; aborting\n",
488 __FUNCTION__, port_name);
492 //printf (_("Created %s port %s\n"), in ? "input" : "output", port_name);
494 if ((in && connect_inputs) || (!in && connect_outputs))
495 process_info_connect_port (procinfo, in, i, port_name);
501 procinfo->port_count = port_count;
507 process_info_set_channels (process_info_t * procinfo,
508 unsigned long channels, gboolean connect_inputs, gboolean connect_outputs)
510 process_info_set_port_count (procinfo, channels, connect_inputs, connect_outputs);
511 procinfo->channels = channels;
515 process_info_new (const char * client_name, unsigned long rack_channels,
516 gboolean connect_inputs, gboolean connect_outputs)
518 process_info_t * procinfo;
519 char * jack_client_name;
522 procinfo = g_malloc (sizeof (process_info_t));
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;
533 if ( client_name == NULL )
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);
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++)
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) */
553 for (i = err; jack_client_name[i] != '\0'; i++)
554 jack_client_name[i] = jack_client_name[i + 1];
556 else if (isupper (jack_client_name[err]))
557 jack_client_name[err] = tolower (jack_client_name[err]);
560 err = process_info_connect_jack (procinfo);
563 /* g_free (procinfo); */
568 sample_rate = jack_get_sample_rate (procinfo->jack_client);
569 buffer_size = jack_get_sample_rate (procinfo->jack_client);
571 jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
572 jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
574 jack_activate (procinfo->jack_client);
576 err = process_info_set_port_count (procinfo, rack_channels, connect_inputs, connect_outputs);
584 process_info_destroy (process_info_t * procinfo) {
585 if (procinfo->jack_client)
587 jack_deactivate (procinfo->jack_client);
588 jack_client_close (procinfo->jack_client);
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);
598 void process_quit (process_info_t * procinfo) {
599 procinfo->quit = TRUE;