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 "control_message.h"
32 #include "lock_free_fifo.h"
34 #include "jack_rack.h"
41 #define USEC_PER_SEC 1000000
42 #define MSEC_PER_SEC 1000
43 #define TIME_RUN_SKIP_COUNT 5
45 jack_nframes_t sample_rate;
46 jack_nframes_t buffer_size;
48 static char * jack_client_name;
50 int process_control_messages (process_info_t * procinfo) {
51 static int quitting = 0;
55 if (quitting) return quitting;
57 while (lff_read (procinfo->ui_to_process, &ctrlmsg) == 0)
62 /* add a link to the end of the plugin chain */
64 process_add_plugin (procinfo, ctrlmsg.data.add.plugin);
65 err = lff_write (procinfo->process_to_ui, &ctrlmsg);
68 /* remove a link (plugin will be sent back) */
70 ctrlmsg.data.remove.plugin =
71 process_remove_plugin (procinfo, ctrlmsg.data.remove.plugin);
72 err = lff_write (procinfo->process_to_ui, &ctrlmsg);
77 err = lff_write (procinfo->process_to_ui, &ctrlmsg);
83 fprintf (stderr, "%s: gui fifo is out of space\n", __FUNCTION__);
92 /** process messages for plugins' control ports */
93 void process_control_port_messages (process_info_t * procinfo) {
95 unsigned long control;
96 unsigned long channel;
99 if (!procinfo->chain) return;
101 for (plugin = procinfo->chain; plugin; plugin = plugin->next)
103 if (plugin->desc->control_port_count > 0)
104 for (control = 0; control < plugin->desc->control_port_count; control++)
105 for (copy = 0; copy < plugin->copies; copy++)
107 while (lff_read (plugin->holders[copy].ui_control_fifos + control,
108 plugin->holders[copy].control_memory + control) == 0);
111 if (plugin->wet_dry_enabled)
112 for (channel = 0; channel < procinfo->channels; channel++)
114 while (lff_read (plugin->wet_dry_fifos + channel,
115 plugin->wet_dry_values + channel) == 0);
120 int get_jack_buffers (process_info_t * procinfo, jack_nframes_t frames) {
121 unsigned long channel;
123 for (channel = 0; channel < procinfo->channels; channel++)
125 procinfo->jack_input_buffers[channel] = jack_port_get_buffer (procinfo->jack_input_ports[channel], frames);
126 if (!procinfo->jack_input_buffers[channel])
128 fprintf (stderr, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
132 procinfo->jack_output_buffers[channel] = jack_port_get_buffer (procinfo->jack_output_ports[channel], frames);
133 if (!procinfo->jack_output_buffers[channel])
135 fprintf (stderr, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
144 get_first_enabled_plugin (process_info_t * procinfo)
146 plugin_t * first_enabled;
148 if (!procinfo->chain) return NULL;
150 for (first_enabled = procinfo->chain;
152 first_enabled = first_enabled->next)
154 if (first_enabled->enabled) return first_enabled;
161 get_last_enabled_plugin (process_info_t * procinfo)
163 plugin_t * last_enabled;
165 if (!procinfo->chain) return NULL;
167 for (last_enabled = procinfo->chain_end;
169 last_enabled = last_enabled->prev)
171 if (last_enabled->enabled) return last_enabled;
178 connect_chain (process_info_t * procinfo, jack_nframes_t frames)
180 plugin_t * first_enabled, * last_enabled, * plugin;
182 unsigned long channel;
183 if (!procinfo->chain) return;
185 first_enabled = get_first_enabled_plugin (procinfo);
186 if (!first_enabled) return;
188 last_enabled = get_last_enabled_plugin (procinfo);
190 /* sort out the aux ports */
191 plugin = first_enabled;
194 if (plugin->desc->aux_channels > 0)
197 for (copy = 0; copy < plugin->copies; copy++)
198 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
200 connect_port (plugin->holders[copy].instance,
201 plugin->desc->audio_aux_port_indicies[channel],
202 jack_port_get_buffer (plugin->holders[copy].aux_ports[channel], frames));
206 while ( (plugin != last_enabled) && (plugin = plugin->next) );
208 /* ensure that all the of the enabled plugins are connected to their memory */
209 plugin_connect_output_ports (first_enabled);
210 if (first_enabled != last_enabled)
212 plugin_connect_input_ports (last_enabled, last_enabled->prev->audio_output_memory);
214 for (plugin = first_enabled->next;
216 plugin = plugin->next)
220 plugin_connect_input_ports (plugin, plugin->prev->audio_output_memory);
221 plugin_connect_output_ports (plugin);
226 /* input buffers for first plugin */
227 plugin_connect_input_ports (first_enabled, procinfo->jack_input_buffers);
231 process_chain (process_info_t * procinfo, jack_nframes_t frames)
233 LADSPA_Data zero_signal[frames];
234 plugin_t * first_enabled;
235 plugin_t * last_enabled = NULL;
237 unsigned long channel;
241 /* set the zero signal to zero */
242 for (channel = 0; channel < frames; channel++)
243 zero_signal[channel] = 0.0;
245 /* possibly set aux output channels to zero if they're not enabled */
246 for (plugin = procinfo->chain; plugin; plugin = plugin->next)
247 if (!plugin->enabled &&
248 plugin->desc->aux_channels > 0 &&
249 !plugin->desc->aux_are_input)
250 for (copy = 0; copy < plugin->copies; copy++)
251 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
252 memcpy (jack_port_get_buffer (plugin->holders[copy].aux_ports[channel], frames),
253 zero_signal, sizeof (LADSPA_Data) * frames);
255 first_enabled = get_first_enabled_plugin (procinfo);
257 /* no chain; just copy input to output */
258 if (!procinfo->chain || !first_enabled)
260 unsigned long channel;
261 for (channel = 0; channel < procinfo->channels; channel++)
263 memcpy (procinfo->jack_output_buffers[channel],
264 procinfo->jack_input_buffers[channel],
265 sizeof(LADSPA_Data) * frames);
270 /* all past here is guaranteed to have at least 1 enabled plugin */
272 last_enabled = get_last_enabled_plugin (procinfo);
274 for (plugin = first_enabled;
276 plugin = plugin->next)
280 for (i = 0; i < plugin->copies; i++)
281 plugin->descriptor->run (plugin->holders[i].instance, frames);
283 if (plugin->wet_dry_enabled)
284 for (channel = 0; channel < procinfo->channels; channel++)
285 for (i = 0; i < frames; i++)
287 plugin->audio_output_memory[channel][i] *= plugin->wet_dry_values[channel];
288 plugin->audio_output_memory[channel][i] += plugin->audio_input_memory[channel][i] * (1.0 - plugin->wet_dry_values[channel]);
291 if (plugin == last_enabled)
297 /* copy the data through */
298 for (i = 0; i < procinfo->channels; i++)
299 memcpy (plugin->audio_output_memory[i],
300 plugin->prev->audio_output_memory[i],
301 sizeof(LADSPA_Data) * frames);
305 /* copy the last enabled data to the jack ports */
306 for (i = 0; i < procinfo->channels; i++)
307 memcpy (procinfo->jack_output_buffers[i],
308 last_enabled->audio_output_memory[i],
309 sizeof(LADSPA_Data) * frames);
313 int process (jack_nframes_t frames, void * data) {
315 process_info_t * procinfo;
317 procinfo = (process_info_t *) data;
321 fprintf (stderr, "%s: no process_info from jack!\n", __FUNCTION__);
325 if (procinfo->port_count == 0)
328 quitting = process_control_messages (procinfo);
333 process_control_port_messages (procinfo);
335 err = get_jack_buffers (procinfo, frames);
338 fprintf(stderr, "%s: failed to get jack ports, not processing\n", __FUNCTION__);
342 connect_chain (procinfo, frames);
344 process_chain (procinfo, frames);
351 /*******************************************
352 ************** non RT stuff ***************
353 *******************************************/
356 process_info_connect_jack (process_info_t * procinfo, ui_t * ui)
358 printf (_("Connecting to JACK server with client name '%s'\n"), jack_client_name);
360 procinfo->jack_client = jack_client_new (jack_client_name);
362 if (!procinfo->jack_client)
364 fprintf (stderr, "%s: could not create jack client; is the jackd server running?\n", __FUNCTION__);
368 printf (_("Connected to JACK server\n"));
370 jack_set_process_callback (procinfo->jack_client, process, procinfo);
371 jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, ui);
377 process_info_connect_port (process_info_t * procinfo,
380 unsigned long port_index,
381 const char * port_name)
383 const char ** jack_ports;
384 unsigned long jack_port_index;
386 char * full_port_name;
388 jack_ports = jack_get_ports (procinfo->jack_client, NULL, NULL,
389 JackPortIsPhysical | (in ? JackPortIsOutput : JackPortIsInput));
394 for (jack_port_index = 0;
395 jack_ports[jack_port_index] && jack_port_index <= port_index;
398 if (jack_port_index != port_index)
401 full_port_name = g_strdup_printf ("%s:%s", jack_client_name, port_name);
403 printf (_("Connecting ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
405 err = jack_connect (procinfo->jack_client,
406 in ? jack_ports[jack_port_index] : full_port_name,
407 in ? full_port_name : jack_ports[jack_port_index]);
410 fprintf (stderr, "%s: error connecting ports '%s' and '%s'\n",
411 __FUNCTION__, full_port_name, jack_ports[jack_port_index]);
413 printf (_("Connected ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
415 free (full_port_name);
422 process_info_set_port_count (process_info_t * procinfo, ui_t * ui,
423 unsigned long port_count, gboolean connect_inputs, gboolean connect_outputs)
427 jack_port_t ** port_ptr;
430 if (procinfo->port_count >= port_count)
433 if (procinfo->port_count == 0)
435 procinfo->jack_input_ports = g_malloc (sizeof (jack_port_t *) * port_count);
436 procinfo->jack_output_ports = g_malloc (sizeof (jack_port_t *) * port_count);
438 procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
439 procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
443 procinfo->jack_input_ports = g_realloc (procinfo->jack_input_ports, sizeof (jack_port_t *) * port_count);
444 procinfo->jack_output_ports = g_realloc (procinfo->jack_output_ports, sizeof (jack_port_t *) * port_count);
446 procinfo->jack_input_buffers = g_realloc (procinfo->jack_input_buffers, sizeof (LADSPA_Data *) * port_count);
447 procinfo->jack_output_buffers = g_realloc (procinfo->jack_output_buffers, sizeof (LADSPA_Data *) * port_count);
450 for (i = procinfo->port_count; i < port_count; i++)
452 for (in = 0; in < 2; in++)
454 port_name = g_strdup_printf ("%s_%ld", in ? "in" : "out", i + 1);
456 //printf (_("Creating %s port %s\n"), in ? "input" : "output", port_name);
458 port_ptr = (in ? &procinfo->jack_input_ports[i]
459 : &procinfo->jack_output_ports[i]);
461 *port_ptr = jack_port_register (procinfo->jack_client,
463 JACK_DEFAULT_AUDIO_TYPE,
464 in ? JackPortIsInput : JackPortIsOutput,
469 fprintf (stderr, "%s: could not register port '%s'; aborting\n",
470 __FUNCTION__, port_name);
474 //printf (_("Created %s port %s\n"), in ? "input" : "output", port_name);
476 if ((in && connect_inputs) || (!in && connect_outputs))
477 process_info_connect_port (procinfo, ui, in, i, port_name);
483 procinfo->port_count = port_count;
489 process_info_set_channels (process_info_t * procinfo, ui_t * ui,
490 unsigned long channels, gboolean connect_inputs, gboolean connect_outputs)
492 process_info_set_port_count (procinfo, ui, channels, connect_inputs, connect_outputs);
493 procinfo->channels = channels;
497 process_info_new (ui_t * ui, const char * client_name, unsigned long rack_channels,
498 gboolean connect_inputs, gboolean connect_outputs)
500 process_info_t * procinfo;
503 procinfo = g_malloc (sizeof (process_info_t));
505 procinfo->chain = NULL;
506 procinfo->chain_end = NULL;
507 procinfo->jack_client = NULL;
508 procinfo->port_count = 0;
509 procinfo->jack_input_ports = NULL;
510 procinfo->jack_output_ports = NULL;
511 procinfo->channels = rack_channels;
513 /* sort out the client name */
514 jack_client_name = strdup (client_name);
515 for (err = 0; jack_client_name[err] != '\0'; err++)
517 if (jack_client_name[err] == ' ')
518 jack_client_name[err] = '_';
519 else if (!isalnum (jack_client_name[err]))
520 { /* shift all the chars up one (to remove the non-alphanumeric char) */
522 for (i = err; jack_client_name[i] != '\0'; i++)
523 jack_client_name[i] = jack_client_name[i + 1];
525 else if (isupper (jack_client_name[err]))
526 jack_client_name[err] = tolower (jack_client_name[err]);
529 err = process_info_connect_jack (procinfo, ui);
532 /* g_free (procinfo); */
537 sample_rate = jack_get_sample_rate (procinfo->jack_client);
538 buffer_size = jack_get_sample_rate (procinfo->jack_client);
540 jack_set_process_callback (procinfo->jack_client, process, procinfo);
541 jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, ui);
543 procinfo->ui_to_process = ui->ui_to_process;
544 procinfo->process_to_ui = ui->process_to_ui;
546 jack_activate (procinfo->jack_client);
548 err = process_info_set_port_count (procinfo, ui, rack_channels, connect_inputs, connect_outputs);
556 process_info_destroy (process_info_t * procinfo) {
557 jack_deactivate (procinfo->jack_client);
558 jack_client_close (procinfo->jack_client);