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.
27 #include <jack/jack.h>
37 #include "lock_free_fifo.h"
39 #include "jack_rack.h"
40 #include "framework/mlt_log.h"
46 extern pthread_mutex_t g_activate_mutex;
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
53 jack_nframes_t sample_rate;
54 jack_nframes_t buffer_size;
57 jack_shutdown_cb (void * data)
59 process_info_t * procinfo = data;
61 procinfo->quit = TRUE;
64 /** process messages for plugins' control ports */
65 void process_control_port_messages (process_info_t * procinfo) {
67 unsigned long control;
68 unsigned long channel;
71 if (!procinfo->chain) return;
73 for (plugin = procinfo->chain; plugin; plugin = plugin->next)
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++)
79 while (lff_read (plugin->holders[copy].ui_control_fifos + control,
80 plugin->holders[copy].control_memory + control) == 0);
83 if (plugin->wet_dry_enabled)
84 for (channel = 0; channel < procinfo->channels; channel++)
86 while (lff_read (plugin->wet_dry_fifos + channel,
87 plugin->wet_dry_values + channel) == 0);
92 int get_jack_buffers (process_info_t * procinfo, jack_nframes_t frames) {
93 unsigned long channel;
95 for (channel = 0; channel < procinfo->channels; channel++)
97 procinfo->jack_input_buffers[channel] = jack_port_get_buffer (procinfo->jack_input_ports[channel], frames);
98 if (!procinfo->jack_input_buffers[channel])
100 mlt_log_verbose( NULL, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
104 procinfo->jack_output_buffers[channel] = jack_port_get_buffer (procinfo->jack_output_ports[channel], frames);
105 if (!procinfo->jack_output_buffers[channel])
107 mlt_log_verbose( NULL, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
116 get_first_enabled_plugin (process_info_t * procinfo)
118 plugin_t * first_enabled;
120 if (!procinfo->chain) return NULL;
122 for (first_enabled = procinfo->chain;
124 first_enabled = first_enabled->next)
126 if (first_enabled->enabled) return first_enabled;
133 get_last_enabled_plugin (process_info_t * procinfo)
135 plugin_t * last_enabled;
137 if (!procinfo->chain) return NULL;
139 for (last_enabled = procinfo->chain_end;
141 last_enabled = last_enabled->prev)
143 if (last_enabled->enabled) return last_enabled;
150 connect_chain (process_info_t * procinfo, jack_nframes_t frames)
152 plugin_t * first_enabled, * last_enabled, * plugin;
154 unsigned long channel;
155 if (!procinfo->chain) return;
157 first_enabled = get_first_enabled_plugin (procinfo);
158 if (!first_enabled) return;
160 last_enabled = get_last_enabled_plugin (procinfo);
162 /* sort out the aux ports */
163 plugin = first_enabled;
166 if (plugin->desc->aux_channels > 0 && plugin->enabled)
168 if (procinfo->jack_client)
170 for (copy = 0; copy < plugin->copies; copy++)
171 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
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));
179 for (copy = 0; copy < frames; copy++)
180 procinfo->silent_buffer[copy] = 0.0;
182 for (copy = 0; copy < plugin->copies; copy++)
183 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
185 connect_port (plugin->holders[copy].instance,
186 plugin->desc->audio_aux_port_indicies[channel],
187 procinfo->silent_buffer);
191 while ( (plugin != last_enabled) && (plugin = plugin->next) );
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)
197 plugin_connect_input_ports (last_enabled, last_enabled->prev->audio_output_memory);
198 for (plugin = first_enabled->next; plugin; plugin = plugin->next)
202 plugin_connect_input_ports (plugin, plugin->prev->audio_output_memory);
203 plugin_connect_output_ports (plugin);
208 /* input buffers for first plugin */
209 plugin_connect_input_ports (first_enabled, procinfo->jack_input_buffers);
213 process_chain (process_info_t * procinfo, jack_nframes_t frames)
215 plugin_t * first_enabled;
216 plugin_t * last_enabled = NULL;
218 unsigned long channel;
221 if (procinfo->jack_client)
223 LADSPA_Data zero_signal[frames];
226 /* set the zero signal to zero */
227 for (channel = 0; channel < frames; channel++)
228 zero_signal[channel] = 0.0;
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);
241 first_enabled = get_first_enabled_plugin (procinfo);
243 /* no chain; just copy input to output */
244 if (!procinfo->chain || !first_enabled)
246 unsigned long channel;
247 for (channel = 0; channel < procinfo->channels; channel++)
249 memcpy (procinfo->jack_output_buffers[channel],
250 procinfo->jack_input_buffers[channel],
251 sizeof(LADSPA_Data) * frames);
256 /* all past here is guaranteed to have at least 1 enabled plugin */
258 last_enabled = get_last_enabled_plugin (procinfo);
260 for (plugin = first_enabled;
262 plugin = plugin->next)
266 for (i = 0; i < plugin->copies; i++)
267 plugin->descriptor->run (plugin->holders[i].instance, frames);
269 if (plugin->wet_dry_enabled)
270 for (channel = 0; channel < procinfo->channels; channel++)
271 for (i = 0; i < frames; i++)
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]);
277 if (plugin == last_enabled)
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);
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);
299 int process_ladspa (process_info_t * procinfo, jack_nframes_t frames,
300 LADSPA_Data ** inputs, LADSPA_Data ** outputs) {
301 unsigned long channel;
305 mlt_log_error( NULL, "%s: no process_info from jack!\n", __FUNCTION__);
309 if (procinfo->quit == TRUE)
312 process_control_port_messages (procinfo);
314 for (channel = 0; channel < procinfo->channels; channel++)
316 procinfo->jack_input_buffers[channel] = inputs[channel];
317 if (!procinfo->jack_input_buffers[channel])
319 mlt_log_verbose( NULL, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
323 procinfo->jack_output_buffers[channel] = outputs[channel];
324 if (!procinfo->jack_output_buffers[channel])
326 mlt_log_verbose( NULL, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
331 connect_chain (procinfo, frames);
333 process_chain (procinfo, frames);
338 int process_jack (jack_nframes_t frames, void * data) {
340 process_info_t * procinfo;
342 procinfo = (process_info_t *) data;
346 mlt_log_error( NULL, "%s: no process_info from jack!\n", __FUNCTION__);
350 if (procinfo->port_count == 0)
353 if (procinfo->quit == TRUE)
356 process_control_port_messages (procinfo);
358 err = get_jack_buffers (procinfo, frames);
361 mlt_log_warning( NULL, "%s: failed to get jack ports, not processing\n", __FUNCTION__);
365 connect_chain (procinfo, frames);
367 process_chain (procinfo, frames);
374 /*******************************************
375 ************** non RT stuff ***************
376 *******************************************/
379 process_info_connect_jack (process_info_t * procinfo)
381 mlt_log_info( NULL, _("Connecting to JACK server with client name '%s'\n"), procinfo->jack_client_name);
383 procinfo->jack_client = jack_client_open (procinfo->jack_client_name, JackNullOption, NULL);
385 if (!procinfo->jack_client)
387 mlt_log_warning( NULL, "%s: could not create jack client; is the jackd server running?\n", __FUNCTION__);
391 mlt_log_verbose( NULL, _("Connected to JACK server\n"));
393 jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
394 jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
400 process_info_connect_port (process_info_t * procinfo,
402 unsigned long port_index,
403 const char * port_name)
405 const char ** jack_ports;
406 unsigned long jack_port_index;
408 char * full_port_name;
410 jack_ports = jack_get_ports (procinfo->jack_client, NULL, NULL,
411 JackPortIsPhysical | (in ? JackPortIsOutput : JackPortIsInput));
416 for (jack_port_index = 0;
417 jack_ports[jack_port_index] && jack_port_index <= port_index;
420 if (jack_port_index != port_index)
423 full_port_name = g_strdup_printf ("%s:%s", procinfo->jack_client_name, port_name);
425 mlt_log_debug( NULL, _("Connecting ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
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]);
432 mlt_log_warning( NULL, "%s: error connecting ports '%s' and '%s'\n",
433 __FUNCTION__, full_port_name, jack_ports[jack_port_index]);
435 mlt_log_info( NULL, _("Connected ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
437 free (full_port_name);
444 process_info_set_port_count (process_info_t * procinfo,
445 unsigned long port_count, gboolean connect_inputs, gboolean connect_outputs)
449 jack_port_t ** port_ptr;
452 if (procinfo->port_count >= port_count)
455 if (procinfo->port_count == 0)
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);
460 procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
461 procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
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);
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);
472 for (i = procinfo->port_count; i < port_count; i++)
474 for (in = 0; in < 2; in++)
476 port_name = g_strdup_printf ("%s_%ld", in ? "in" : "out", i + 1);
478 //mlt_log_debug( NULL, _("Creating %s port %s\n"), in ? "input" : "output", port_name);
480 port_ptr = (in ? &procinfo->jack_input_ports[i]
481 : &procinfo->jack_output_ports[i]);
483 *port_ptr = jack_port_register (procinfo->jack_client,
485 JACK_DEFAULT_AUDIO_TYPE,
486 in ? JackPortIsInput : JackPortIsOutput,
491 mlt_log_error( NULL, "%s: could not register port '%s'; aborting\n",
492 __FUNCTION__, port_name);
496 //mlt_log_debug( NULL, _("Created %s port %s\n"), in ? "input" : "output", port_name);
498 if ((in && connect_inputs) || (!in && connect_outputs))
499 process_info_connect_port (procinfo, in, i, port_name);
505 procinfo->port_count = port_count;
511 process_info_set_channels (process_info_t * procinfo,
512 unsigned long channels, gboolean connect_inputs, gboolean connect_outputs)
514 process_info_set_port_count (procinfo, channels, connect_inputs, connect_outputs);
515 procinfo->channels = channels;
519 process_info_new (const char * client_name, unsigned long rack_channels,
520 gboolean connect_inputs, gboolean connect_outputs)
522 process_info_t * procinfo;
523 char * jack_client_name;
526 procinfo = g_malloc (sizeof (process_info_t));
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;
537 if ( client_name == NULL )
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);
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++)
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) */
557 for (i = err; jack_client_name[i] != '\0'; i++)
558 jack_client_name[i] = jack_client_name[i + 1];
560 else if (isupper (jack_client_name[err]))
561 jack_client_name[err] = tolower (jack_client_name[err]);
564 err = process_info_connect_jack (procinfo);
567 /* g_free (procinfo); */
572 sample_rate = jack_get_sample_rate (procinfo->jack_client);
573 buffer_size = jack_get_sample_rate (procinfo->jack_client);
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 );
580 jack_activate (procinfo->jack_client);
582 err = process_info_set_port_count (procinfo, rack_channels, connect_inputs, connect_outputs);
590 process_info_destroy (process_info_t * procinfo) {
591 if (procinfo->jack_client)
593 jack_deactivate (procinfo->jack_client);
594 jack_client_close (procinfo->jack_client);
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);
604 void process_quit (process_info_t * procinfo) {
605 procinfo->quit = TRUE;