4 * Copyright (C) Robert Ham 2002, 2003 (node@users.sourceforge.net)
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <jack/jack.h>
31 #include "lock_free_fifo.h"
33 #include "jack_rack.h"
39 #define USEC_PER_SEC 1000000
40 #define MSEC_PER_SEC 1000
41 #define TIME_RUN_SKIP_COUNT 5
42 #define MAX_BUFFER_SIZE 4096
44 jack_nframes_t sample_rate;
45 jack_nframes_t buffer_size;
48 jack_shutdown_cb (void * data)
50 process_info_t * procinfo = data;
52 procinfo->quit = TRUE;
55 /** process messages for plugins' control ports */
56 void process_control_port_messages (process_info_t * procinfo) {
58 unsigned long control;
59 unsigned long channel;
62 if (!procinfo->chain) return;
64 for (plugin = procinfo->chain; plugin; plugin = plugin->next)
66 if (plugin->desc->control_port_count > 0)
67 for (control = 0; control < plugin->desc->control_port_count; control++)
68 for (copy = 0; copy < plugin->copies; copy++)
70 while (lff_read (plugin->holders[copy].ui_control_fifos + control,
71 plugin->holders[copy].control_memory + control) == 0);
74 if (plugin->wet_dry_enabled)
75 for (channel = 0; channel < procinfo->channels; channel++)
77 while (lff_read (plugin->wet_dry_fifos + channel,
78 plugin->wet_dry_values + channel) == 0);
83 int get_jack_buffers (process_info_t * procinfo, jack_nframes_t frames) {
84 unsigned long channel;
86 for (channel = 0; channel < procinfo->channels; channel++)
88 procinfo->jack_input_buffers[channel] = jack_port_get_buffer (procinfo->jack_input_ports[channel], frames);
89 if (!procinfo->jack_input_buffers[channel])
91 fprintf (stderr, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
95 procinfo->jack_output_buffers[channel] = jack_port_get_buffer (procinfo->jack_output_ports[channel], frames);
96 if (!procinfo->jack_output_buffers[channel])
98 fprintf (stderr, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
107 get_first_enabled_plugin (process_info_t * procinfo)
109 plugin_t * first_enabled;
111 if (!procinfo->chain) return NULL;
113 for (first_enabled = procinfo->chain;
115 first_enabled = first_enabled->next)
117 if (first_enabled->enabled) return first_enabled;
124 get_last_enabled_plugin (process_info_t * procinfo)
126 plugin_t * last_enabled;
128 if (!procinfo->chain) return NULL;
130 for (last_enabled = procinfo->chain_end;
132 last_enabled = last_enabled->prev)
134 if (last_enabled->enabled) return last_enabled;
141 connect_chain (process_info_t * procinfo, jack_nframes_t frames)
143 plugin_t * first_enabled, * last_enabled, * plugin;
145 unsigned long channel;
146 if (!procinfo->chain) return;
148 first_enabled = get_first_enabled_plugin (procinfo);
149 if (!first_enabled) return;
151 last_enabled = get_last_enabled_plugin (procinfo);
153 /* sort out the aux ports */
154 plugin = first_enabled;
157 if (plugin->desc->aux_channels > 0 && plugin->enabled)
159 if (procinfo->jack_client)
161 for (copy = 0; copy < plugin->copies; copy++)
162 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
164 connect_port (plugin->holders[copy].instance,
165 plugin->desc->audio_aux_port_indicies[channel],
166 jack_port_get_buffer (plugin->holders[copy].aux_ports[channel], frames));
170 for (copy = 0; copy < frames; copy++)
171 procinfo->silent_buffer[copy] = 0.0;
173 for (copy = 0; copy < plugin->copies; copy++)
174 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
176 connect_port (plugin->holders[copy].instance,
177 plugin->desc->audio_aux_port_indicies[channel],
178 procinfo->silent_buffer);
182 while ( (plugin != last_enabled) && (plugin = plugin->next) );
184 /* ensure that all the of the enabled plugins are connected to their memory */
185 plugin_connect_output_ports (first_enabled);
186 if (first_enabled != last_enabled)
188 plugin_connect_input_ports (last_enabled, last_enabled->prev->audio_output_memory);
189 for (plugin = first_enabled->next; plugin; plugin = plugin->next)
193 plugin_connect_input_ports (plugin, plugin->prev->audio_output_memory);
194 plugin_connect_output_ports (plugin);
199 /* input buffers for first plugin */
200 plugin_connect_input_ports (first_enabled, procinfo->jack_input_buffers);
204 process_chain (process_info_t * procinfo, jack_nframes_t frames)
206 plugin_t * first_enabled;
207 plugin_t * last_enabled = NULL;
209 unsigned long channel;
212 if (procinfo->jack_client)
214 LADSPA_Data zero_signal[frames];
217 /* set the zero signal to zero */
218 for (channel = 0; channel < frames; channel++)
219 zero_signal[channel] = 0.0;
221 /* possibly set aux output channels to zero if they're not enabled */
222 for (plugin = procinfo->chain; plugin; plugin = plugin->next)
223 if (!plugin->enabled &&
224 plugin->desc->aux_channels > 0 &&
225 !plugin->desc->aux_are_input)
226 for (copy = 0; copy < plugin->copies; copy++)
227 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
228 memcpy (jack_port_get_buffer (plugin->holders[copy].aux_ports[channel], frames),
229 zero_signal, sizeof (LADSPA_Data) * frames);
232 first_enabled = get_first_enabled_plugin (procinfo);
234 /* no chain; just copy input to output */
235 if (!procinfo->chain || !first_enabled)
237 unsigned long channel;
238 for (channel = 0; channel < procinfo->channels; channel++)
240 memcpy (procinfo->jack_output_buffers[channel],
241 procinfo->jack_input_buffers[channel],
242 sizeof(LADSPA_Data) * frames);
247 /* all past here is guaranteed to have at least 1 enabled plugin */
249 last_enabled = get_last_enabled_plugin (procinfo);
251 for (plugin = first_enabled;
253 plugin = plugin->next)
257 for (i = 0; i < plugin->copies; i++)
258 plugin->descriptor->run (plugin->holders[i].instance, frames);
260 if (plugin->wet_dry_enabled)
261 for (channel = 0; channel < procinfo->channels; channel++)
262 for (i = 0; i < frames; i++)
264 plugin->audio_output_memory[channel][i] *= plugin->wet_dry_values[channel];
265 plugin->audio_output_memory[channel][i] += plugin->audio_input_memory[channel][i] * (1.0 - plugin->wet_dry_values[channel]);
268 if (plugin == last_enabled)
274 /* copy the data through */
275 for (i = 0; i < procinfo->channels; i++)
276 memcpy (plugin->audio_output_memory[i],
277 plugin->prev->audio_output_memory[i],
278 sizeof(LADSPA_Data) * frames);
282 /* copy the last enabled data to the jack ports */
283 for (i = 0; i < procinfo->channels; i++)
284 memcpy (procinfo->jack_output_buffers[i],
285 last_enabled->audio_output_memory[i],
286 sizeof(LADSPA_Data) * frames);
290 int process_ladspa (process_info_t * procinfo, jack_nframes_t frames,
291 LADSPA_Data ** inputs, LADSPA_Data ** outputs) {
292 unsigned long channel;
296 fprintf (stderr, "%s: no process_info from jack!\n", __FUNCTION__);
300 if (procinfo->quit == TRUE)
303 process_control_port_messages (procinfo);
305 for (channel = 0; channel < procinfo->channels; channel++)
307 procinfo->jack_input_buffers[channel] = inputs[channel];
308 if (!procinfo->jack_input_buffers[channel])
310 fprintf (stderr, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
314 procinfo->jack_output_buffers[channel] = outputs[channel];
315 if (!procinfo->jack_output_buffers[channel])
317 fprintf (stderr, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
322 connect_chain (procinfo, frames);
324 process_chain (procinfo, frames);
329 int process_jack (jack_nframes_t frames, void * data) {
331 process_info_t * procinfo;
333 procinfo = (process_info_t *) data;
337 fprintf (stderr, "%s: no process_info from jack!\n", __FUNCTION__);
341 if (procinfo->port_count == 0)
344 if (procinfo->quit == TRUE)
347 process_control_port_messages (procinfo);
349 err = get_jack_buffers (procinfo, frames);
352 fprintf(stderr, "%s: failed to get jack ports, not processing\n", __FUNCTION__);
356 connect_chain (procinfo, frames);
358 process_chain (procinfo, frames);
365 /*******************************************
366 ************** non RT stuff ***************
367 *******************************************/
370 process_info_connect_jack (process_info_t * procinfo)
372 printf (_("Connecting to JACK server with client name '%s'\n"), procinfo->jack_client_name);
374 procinfo->jack_client = jack_client_new (procinfo->jack_client_name);
376 if (!procinfo->jack_client)
378 fprintf (stderr, "%s: could not create jack client; is the jackd server running?\n", __FUNCTION__);
382 printf (_("Connected to JACK server\n"));
384 jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
385 jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
391 process_info_connect_port (process_info_t * procinfo,
393 unsigned long port_index,
394 const char * port_name)
396 const char ** jack_ports;
397 unsigned long jack_port_index;
399 char * full_port_name;
401 jack_ports = jack_get_ports (procinfo->jack_client, NULL, NULL,
402 JackPortIsPhysical | (in ? JackPortIsOutput : JackPortIsInput));
407 for (jack_port_index = 0;
408 jack_ports[jack_port_index] && jack_port_index <= port_index;
411 if (jack_port_index != port_index)
414 full_port_name = g_strdup_printf ("%s:%s", procinfo->jack_client_name, port_name);
416 printf (_("Connecting ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
418 err = jack_connect (procinfo->jack_client,
419 in ? jack_ports[jack_port_index] : full_port_name,
420 in ? full_port_name : jack_ports[jack_port_index]);
423 fprintf (stderr, "%s: error connecting ports '%s' and '%s'\n",
424 __FUNCTION__, full_port_name, jack_ports[jack_port_index]);
426 printf (_("Connected ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
428 free (full_port_name);
435 process_info_set_port_count (process_info_t * procinfo,
436 unsigned long port_count, gboolean connect_inputs, gboolean connect_outputs)
440 jack_port_t ** port_ptr;
443 if (procinfo->port_count >= port_count)
446 if (procinfo->port_count == 0)
448 procinfo->jack_input_ports = g_malloc (sizeof (jack_port_t *) * port_count);
449 procinfo->jack_output_ports = g_malloc (sizeof (jack_port_t *) * port_count);
451 procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
452 procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
456 procinfo->jack_input_ports = g_realloc (procinfo->jack_input_ports, sizeof (jack_port_t *) * port_count);
457 procinfo->jack_output_ports = g_realloc (procinfo->jack_output_ports, sizeof (jack_port_t *) * port_count);
459 procinfo->jack_input_buffers = g_realloc (procinfo->jack_input_buffers, sizeof (LADSPA_Data *) * port_count);
460 procinfo->jack_output_buffers = g_realloc (procinfo->jack_output_buffers, sizeof (LADSPA_Data *) * port_count);
463 for (i = procinfo->port_count; i < port_count; i++)
465 for (in = 0; in < 2; in++)
467 port_name = g_strdup_printf ("%s_%ld", in ? "in" : "out", i + 1);
469 //printf (_("Creating %s port %s\n"), in ? "input" : "output", port_name);
471 port_ptr = (in ? &procinfo->jack_input_ports[i]
472 : &procinfo->jack_output_ports[i]);
474 *port_ptr = jack_port_register (procinfo->jack_client,
476 JACK_DEFAULT_AUDIO_TYPE,
477 in ? JackPortIsInput : JackPortIsOutput,
482 fprintf (stderr, "%s: could not register port '%s'; aborting\n",
483 __FUNCTION__, port_name);
487 //printf (_("Created %s port %s\n"), in ? "input" : "output", port_name);
489 if ((in && connect_inputs) || (!in && connect_outputs))
490 process_info_connect_port (procinfo, in, i, port_name);
496 procinfo->port_count = port_count;
502 process_info_set_channels (process_info_t * procinfo,
503 unsigned long channels, gboolean connect_inputs, gboolean connect_outputs)
505 process_info_set_port_count (procinfo, channels, connect_inputs, connect_outputs);
506 procinfo->channels = channels;
510 process_info_new (const char * client_name, unsigned long rack_channels,
511 gboolean connect_inputs, gboolean connect_outputs)
513 process_info_t * procinfo;
514 char * jack_client_name;
517 procinfo = g_malloc (sizeof (process_info_t));
519 procinfo->chain = NULL;
520 procinfo->chain_end = NULL;
521 procinfo->jack_client = NULL;
522 procinfo->port_count = 0;
523 procinfo->jack_input_ports = NULL;
524 procinfo->jack_output_ports = NULL;
525 procinfo->channels = rack_channels;
526 procinfo->quit = FALSE;
528 if ( client_name == NULL )
530 sample_rate = 48000; // should be set externally before calling process_ladspa
531 buffer_size = MAX_BUFFER_SIZE;
532 procinfo->silent_buffer = g_malloc (sizeof (LADSPA_Data) * buffer_size );
533 procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * rack_channels);
534 procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * rack_channels);
539 /* sort out the client name */
540 procinfo->jack_client_name = jack_client_name = strdup (client_name);
541 for (err = 0; jack_client_name[err] != '\0'; err++)
543 if (jack_client_name[err] == ' ')
544 jack_client_name[err] = '_';
545 else if (!isalnum (jack_client_name[err]))
546 { /* shift all the chars up one (to remove the non-alphanumeric char) */
548 for (i = err; jack_client_name[i] != '\0'; i++)
549 jack_client_name[i] = jack_client_name[i + 1];
551 else if (isupper (jack_client_name[err]))
552 jack_client_name[err] = tolower (jack_client_name[err]);
555 err = process_info_connect_jack (procinfo);
558 /* g_free (procinfo); */
563 sample_rate = jack_get_sample_rate (procinfo->jack_client);
564 buffer_size = jack_get_sample_rate (procinfo->jack_client);
566 jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
567 jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
569 jack_activate (procinfo->jack_client);
571 err = process_info_set_port_count (procinfo, rack_channels, connect_inputs, connect_outputs);
579 process_info_destroy (process_info_t * procinfo) {
580 if (procinfo->jack_client)
582 jack_deactivate (procinfo->jack_client);
583 jack_client_close (procinfo->jack_client);
585 g_free (procinfo->silent_buffer);
586 g_free (procinfo->jack_input_ports);
587 g_free (procinfo->jack_output_ports);
588 g_free (procinfo->jack_input_buffers);
589 g_free (procinfo->jack_output_buffers);
593 void process_quit (process_info_t * procinfo) {
594 procinfo->quit = TRUE;