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 if( plugin->desc->has_input )
210 plugin_connect_input_ports (first_enabled, procinfo->jack_input_buffers);
214 process_chain (process_info_t * procinfo, jack_nframes_t frames)
216 plugin_t * first_enabled;
217 plugin_t * last_enabled = NULL;
219 unsigned long channel;
222 if (procinfo->jack_client)
224 LADSPA_Data zero_signal[frames];
227 /* set the zero signal to zero */
228 for (channel = 0; channel < frames; channel++)
229 zero_signal[channel] = 0.0;
231 /* possibly set aux output channels to zero if they're not enabled */
232 for (plugin = procinfo->chain; plugin; plugin = plugin->next)
233 if (!plugin->enabled &&
234 plugin->desc->aux_channels > 0 &&
235 !plugin->desc->aux_are_input)
236 for (copy = 0; copy < plugin->copies; copy++)
237 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
238 memcpy (jack_port_get_buffer (plugin->holders[copy].aux_ports[channel], frames),
239 zero_signal, sizeof (LADSPA_Data) * frames);
242 first_enabled = get_first_enabled_plugin (procinfo);
244 /* no chain; just copy input to output */
245 if (!procinfo->chain || !first_enabled)
247 unsigned long channel;
248 for (channel = 0; channel < procinfo->channels; channel++)
250 memcpy (procinfo->jack_output_buffers[channel],
251 procinfo->jack_input_buffers[channel],
252 sizeof(LADSPA_Data) * frames);
257 /* all past here is guaranteed to have at least 1 enabled plugin */
259 last_enabled = get_last_enabled_plugin (procinfo);
261 for (plugin = first_enabled;
263 plugin = plugin->next)
267 for (i = 0; i < plugin->copies; i++)
268 plugin->descriptor->run (plugin->holders[i].instance, frames);
270 if (plugin->wet_dry_enabled)
271 for (channel = 0; channel < procinfo->channels; channel++)
272 for (i = 0; i < frames; i++)
274 plugin->audio_output_memory[channel][i] *= plugin->wet_dry_values[channel];
275 plugin->audio_output_memory[channel][i] += plugin->audio_input_memory[channel][i] * (1.0 - plugin->wet_dry_values[channel]);
278 if (plugin == last_enabled)
284 /* copy the data through */
285 for (i = 0; i < procinfo->channels; i++)
286 memcpy (plugin->audio_output_memory[i],
287 plugin->prev->audio_output_memory[i],
288 sizeof(LADSPA_Data) * frames);
292 /* copy the last enabled data to the jack ports */
293 for (i = 0; i < procinfo->channels; i++)
294 memcpy (procinfo->jack_output_buffers[i],
295 last_enabled->audio_output_memory[i],
296 sizeof(LADSPA_Data) * frames);
300 int process_ladspa (process_info_t * procinfo, jack_nframes_t frames,
301 LADSPA_Data ** inputs, LADSPA_Data ** outputs) {
302 unsigned long channel;
306 mlt_log_error( NULL, "%s: no process_info from jack!\n", __FUNCTION__);
310 if (procinfo->quit == TRUE)
313 process_control_port_messages (procinfo);
315 for (channel = 0; channel < procinfo->channels; channel++)
317 if(get_first_enabled_plugin (procinfo)->desc->has_input)
319 procinfo->jack_input_buffers[channel] = inputs[channel];
320 if (!procinfo->jack_input_buffers[channel])
322 mlt_log_verbose( NULL, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
326 procinfo->jack_output_buffers[channel] = outputs[channel];
327 if (!procinfo->jack_output_buffers[channel])
329 mlt_log_verbose( NULL, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
334 connect_chain (procinfo, frames);
336 process_chain (procinfo, frames);
341 int process_jack (jack_nframes_t frames, void * data) {
343 process_info_t * procinfo;
345 procinfo = (process_info_t *) data;
349 mlt_log_error( NULL, "%s: no process_info from jack!\n", __FUNCTION__);
353 if (procinfo->port_count == 0)
356 if (procinfo->quit == TRUE)
359 process_control_port_messages (procinfo);
361 err = get_jack_buffers (procinfo, frames);
364 mlt_log_warning( NULL, "%s: failed to get jack ports, not processing\n", __FUNCTION__);
368 connect_chain (procinfo, frames);
370 process_chain (procinfo, frames);
377 /*******************************************
378 ************** non RT stuff ***************
379 *******************************************/
382 process_info_connect_jack (process_info_t * procinfo)
384 mlt_log_info( NULL, _("Connecting to JACK server with client name '%s'\n"), procinfo->jack_client_name);
386 procinfo->jack_client = jack_client_open (procinfo->jack_client_name, JackNullOption, NULL);
388 if (!procinfo->jack_client)
390 mlt_log_warning( NULL, "%s: could not create jack client; is the jackd server running?\n", __FUNCTION__);
394 mlt_log_verbose( NULL, _("Connected to JACK server\n"));
396 jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
397 jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
403 process_info_connect_port (process_info_t * procinfo,
405 unsigned long port_index,
406 const char * port_name)
408 const char ** jack_ports;
409 unsigned long jack_port_index;
411 char * full_port_name;
413 jack_ports = jack_get_ports (procinfo->jack_client, NULL, NULL,
414 JackPortIsPhysical | (in ? JackPortIsOutput : JackPortIsInput));
419 for (jack_port_index = 0;
420 jack_ports[jack_port_index] && jack_port_index <= port_index;
423 if (jack_port_index != port_index)
426 full_port_name = g_strdup_printf ("%s:%s", procinfo->jack_client_name, port_name);
428 mlt_log_debug( NULL, _("Connecting ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
430 err = jack_connect (procinfo->jack_client,
431 in ? jack_ports[jack_port_index] : full_port_name,
432 in ? full_port_name : jack_ports[jack_port_index]);
435 mlt_log_warning( NULL, "%s: error connecting ports '%s' and '%s'\n",
436 __FUNCTION__, full_port_name, jack_ports[jack_port_index]);
438 mlt_log_info( NULL, _("Connected ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
440 free (full_port_name);
447 process_info_set_port_count (process_info_t * procinfo,
448 unsigned long port_count, gboolean connect_inputs, gboolean connect_outputs)
452 jack_port_t ** port_ptr;
455 if (procinfo->port_count >= port_count)
458 if (procinfo->port_count == 0)
460 procinfo->jack_input_ports = g_malloc (sizeof (jack_port_t *) * port_count);
461 procinfo->jack_output_ports = g_malloc (sizeof (jack_port_t *) * port_count);
463 procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
464 procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
468 procinfo->jack_input_ports = g_realloc (procinfo->jack_input_ports, sizeof (jack_port_t *) * port_count);
469 procinfo->jack_output_ports = g_realloc (procinfo->jack_output_ports, sizeof (jack_port_t *) * port_count);
471 procinfo->jack_input_buffers = g_realloc (procinfo->jack_input_buffers, sizeof (LADSPA_Data *) * port_count);
472 procinfo->jack_output_buffers = g_realloc (procinfo->jack_output_buffers, sizeof (LADSPA_Data *) * port_count);
475 for (i = procinfo->port_count; i < port_count; i++)
477 for (in = 0; in < 2; in++)
479 port_name = g_strdup_printf ("%s_%ld", in ? "in" : "out", i + 1);
481 //mlt_log_debug( NULL, _("Creating %s port %s\n"), in ? "input" : "output", port_name);
483 port_ptr = (in ? &procinfo->jack_input_ports[i]
484 : &procinfo->jack_output_ports[i]);
486 *port_ptr = jack_port_register (procinfo->jack_client,
488 JACK_DEFAULT_AUDIO_TYPE,
489 in ? JackPortIsInput : JackPortIsOutput,
494 mlt_log_error( NULL, "%s: could not register port '%s'; aborting\n",
495 __FUNCTION__, port_name);
499 //mlt_log_debug( NULL, _("Created %s port %s\n"), in ? "input" : "output", port_name);
501 if ((in && connect_inputs) || (!in && connect_outputs))
502 process_info_connect_port (procinfo, in, i, port_name);
508 procinfo->port_count = port_count;
514 process_info_set_channels (process_info_t * procinfo,
515 unsigned long channels, gboolean connect_inputs, gboolean connect_outputs)
517 process_info_set_port_count (procinfo, channels, connect_inputs, connect_outputs);
518 procinfo->channels = channels;
522 process_info_new (const char * client_name, unsigned long rack_channels,
523 gboolean connect_inputs, gboolean connect_outputs)
525 process_info_t * procinfo;
526 char * jack_client_name;
529 procinfo = g_malloc (sizeof (process_info_t));
531 procinfo->chain = NULL;
532 procinfo->chain_end = NULL;
533 procinfo->jack_client = NULL;
534 procinfo->port_count = 0;
535 procinfo->jack_input_ports = NULL;
536 procinfo->jack_output_ports = NULL;
537 procinfo->channels = rack_channels;
538 procinfo->quit = FALSE;
540 if ( client_name == NULL )
542 sample_rate = 48000; // should be set externally before calling process_ladspa
543 buffer_size = MAX_BUFFER_SIZE;
544 procinfo->silent_buffer = g_malloc (sizeof (LADSPA_Data) * buffer_size );
545 procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * rack_channels);
546 procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * rack_channels);
551 /* sort out the client name */
552 procinfo->jack_client_name = jack_client_name = strdup (client_name);
553 for (err = 0; jack_client_name[err] != '\0'; err++)
555 if (jack_client_name[err] == ' ')
556 jack_client_name[err] = '_';
557 else if (!isalnum (jack_client_name[err]))
558 { /* shift all the chars up one (to remove the non-alphanumeric char) */
560 for (i = err; jack_client_name[i] != '\0'; i++)
561 jack_client_name[i] = jack_client_name[i + 1];
563 else if (isupper (jack_client_name[err]))
564 jack_client_name[err] = tolower (jack_client_name[err]);
567 err = process_info_connect_jack (procinfo);
570 /* g_free (procinfo); */
575 sample_rate = jack_get_sample_rate (procinfo->jack_client);
576 buffer_size = jack_get_sample_rate (procinfo->jack_client);
578 jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
579 pthread_mutex_lock( &g_activate_mutex );
580 jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
581 pthread_mutex_unlock( &g_activate_mutex );
583 jack_activate (procinfo->jack_client);
585 err = process_info_set_port_count (procinfo, rack_channels, connect_inputs, connect_outputs);
593 process_info_destroy (process_info_t * procinfo) {
594 if (procinfo->jack_client)
596 jack_deactivate (procinfo->jack_client);
597 jack_client_close (procinfo->jack_client);
599 g_free (procinfo->silent_buffer);
600 g_free (procinfo->jack_input_ports);
601 g_free (procinfo->jack_output_ports);
602 g_free (procinfo->jack_input_buffers);
603 g_free (procinfo->jack_output_buffers);
607 void process_quit (process_info_t * procinfo) {
608 procinfo->quit = TRUE;