added jackrack filter
[melted] / src / modules / jackrack / process.c
1 /*
2 * jack-ladspa-host
3 *
4 * Copyright (C) Robert Ham 2002, 2003 (node@users.sourceforge.net)
5 *
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.
10 *
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.
15 *
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.
19 */
20
21 #include <jack/jack.h>
22 #include <glib.h>
23 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <sys/time.h>
27 #include <time.h>
28 #include <ctype.h>
29
30 #include "process.h"
31 #include "control_message.h"
32 #include "lock_free_fifo.h"
33 #include "plugin.h"
34 #include "jack_rack.h"
35 #include "ui.h"
36
37 #ifndef _
38 #define _(x) x
39 #endif
40
41 #define USEC_PER_SEC 1000000
42 #define MSEC_PER_SEC 1000
43 #define TIME_RUN_SKIP_COUNT 5
44
45 jack_nframes_t sample_rate;
46 jack_nframes_t buffer_size;
47
48 static char * jack_client_name;
49
50 int process_control_messages (process_info_t * procinfo) {
51 static int quitting = 0;
52 ctrlmsg_t ctrlmsg;
53 int err = 0;
54
55 if (quitting) return quitting;
56
57 while (lff_read (procinfo->ui_to_process, &ctrlmsg) == 0)
58 {
59 switch (ctrlmsg.type)
60 {
61
62 /* add a link to the end of the plugin chain */
63 case CTRLMSG_ADD:
64 process_add_plugin (procinfo, ctrlmsg.data.add.plugin);
65 err = lff_write (procinfo->process_to_ui, &ctrlmsg);
66 break;
67
68 /* remove a link (plugin will be sent back) */
69 case CTRLMSG_REMOVE:
70 ctrlmsg.data.remove.plugin =
71 process_remove_plugin (procinfo, ctrlmsg.data.remove.plugin);
72 err = lff_write (procinfo->process_to_ui, &ctrlmsg);
73 break;
74
75 case CTRLMSG_QUIT:
76 quitting = 1;
77 err = lff_write (procinfo->process_to_ui, &ctrlmsg);
78 return 1;
79 }
80
81 if (err)
82 {
83 fprintf (stderr, "%s: gui fifo is out of space\n", __FUNCTION__);
84 return err;
85 }
86
87 }
88
89 return 0;
90 }
91
92 /** process messages for plugins' control ports */
93 void process_control_port_messages (process_info_t * procinfo) {
94 plugin_t * plugin;
95 unsigned long control;
96 unsigned long channel;
97 gint copy;
98
99 if (!procinfo->chain) return;
100
101 for (plugin = procinfo->chain; plugin; plugin = plugin->next)
102 {
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++)
106 {
107 while (lff_read (plugin->holders[copy].ui_control_fifos + control,
108 plugin->holders[copy].control_memory + control) == 0);
109 }
110
111 if (plugin->wet_dry_enabled)
112 for (channel = 0; channel < procinfo->channels; channel++)
113 {
114 while (lff_read (plugin->wet_dry_fifos + channel,
115 plugin->wet_dry_values + channel) == 0);
116 }
117 }
118 }
119
120 int get_jack_buffers (process_info_t * procinfo, jack_nframes_t frames) {
121 unsigned long channel;
122
123 for (channel = 0; channel < procinfo->channels; channel++)
124 {
125 procinfo->jack_input_buffers[channel] = jack_port_get_buffer (procinfo->jack_input_ports[channel], frames);
126 if (!procinfo->jack_input_buffers[channel])
127 {
128 fprintf (stderr, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
129 return 1;
130 }
131
132 procinfo->jack_output_buffers[channel] = jack_port_get_buffer (procinfo->jack_output_ports[channel], frames);
133 if (!procinfo->jack_output_buffers[channel])
134 {
135 fprintf (stderr, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
136 return 1;
137 }
138 }
139
140 return 0;
141 }
142
143 plugin_t *
144 get_first_enabled_plugin (process_info_t * procinfo)
145 {
146 plugin_t * first_enabled;
147
148 if (!procinfo->chain) return NULL;
149
150 for (first_enabled = procinfo->chain;
151 first_enabled;
152 first_enabled = first_enabled->next)
153 {
154 if (first_enabled->enabled) return first_enabled;
155 }
156
157 return NULL;
158 }
159
160 plugin_t *
161 get_last_enabled_plugin (process_info_t * procinfo)
162 {
163 plugin_t * last_enabled;
164
165 if (!procinfo->chain) return NULL;
166
167 for (last_enabled = procinfo->chain_end;
168 last_enabled;
169 last_enabled = last_enabled->prev)
170 {
171 if (last_enabled->enabled) return last_enabled;
172 }
173
174 return NULL;
175 }
176
177 void
178 connect_chain (process_info_t * procinfo, jack_nframes_t frames)
179 {
180 plugin_t * first_enabled, * last_enabled, * plugin;
181 gint copy;
182 unsigned long channel;
183 if (!procinfo->chain) return;
184
185 first_enabled = get_first_enabled_plugin (procinfo);
186 if (!first_enabled) return;
187
188 last_enabled = get_last_enabled_plugin (procinfo);
189
190 /* sort out the aux ports */
191 plugin = first_enabled;
192 do
193 {
194 if (plugin->desc->aux_channels > 0)
195 {
196 if (plugin->enabled)
197 for (copy = 0; copy < plugin->copies; copy++)
198 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
199 plugin->descriptor->
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));
203 }
204
205 }
206 while ( (plugin != last_enabled) && (plugin = plugin->next) );
207
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)
211 {
212 plugin_connect_input_ports (last_enabled, last_enabled->prev->audio_output_memory);
213
214 for (plugin = first_enabled->next;
215 plugin;
216 plugin = plugin->next)
217 {
218 if (plugin->enabled)
219 {
220 plugin_connect_input_ports (plugin, plugin->prev->audio_output_memory);
221 plugin_connect_output_ports (plugin);
222 }
223 }
224 }
225
226 /* input buffers for first plugin */
227 plugin_connect_input_ports (first_enabled, procinfo->jack_input_buffers);
228 }
229
230 void
231 process_chain (process_info_t * procinfo, jack_nframes_t frames)
232 {
233 LADSPA_Data zero_signal[frames];
234 plugin_t * first_enabled;
235 plugin_t * last_enabled = NULL;
236 plugin_t * plugin;
237 unsigned long channel;
238 unsigned long i;
239 guint copy;
240
241 /* set the zero signal to zero */
242 for (channel = 0; channel < frames; channel++)
243 zero_signal[channel] = 0.0;
244
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);
254
255 first_enabled = get_first_enabled_plugin (procinfo);
256
257 /* no chain; just copy input to output */
258 if (!procinfo->chain || !first_enabled)
259 {
260 unsigned long channel;
261 for (channel = 0; channel < procinfo->channels; channel++)
262 {
263 memcpy (procinfo->jack_output_buffers[channel],
264 procinfo->jack_input_buffers[channel],
265 sizeof(LADSPA_Data) * frames);
266 }
267 return;
268 }
269
270 /* all past here is guaranteed to have at least 1 enabled plugin */
271
272 last_enabled = get_last_enabled_plugin (procinfo);
273
274 for (plugin = first_enabled;
275 plugin;
276 plugin = plugin->next)
277 {
278 if (plugin->enabled)
279 {
280 for (i = 0; i < plugin->copies; i++)
281 plugin->descriptor->run (plugin->holders[i].instance, frames);
282
283 if (plugin->wet_dry_enabled)
284 for (channel = 0; channel < procinfo->channels; channel++)
285 for (i = 0; i < frames; i++)
286 {
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]);
289 }
290
291 if (plugin == last_enabled)
292 break;
293 }
294 else
295 {
296
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);
302 }
303 }
304
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);
310
311 }
312
313 int process (jack_nframes_t frames, void * data) {
314 int err, quitting;
315 process_info_t * procinfo;
316
317 procinfo = (process_info_t *) data;
318
319 if (!procinfo)
320 {
321 fprintf (stderr, "%s: no process_info from jack!\n", __FUNCTION__);
322 return 1;
323 }
324
325 if (procinfo->port_count == 0)
326 return 0;
327
328 quitting = process_control_messages (procinfo);
329
330 if (quitting)
331 return 1;
332
333 process_control_port_messages (procinfo);
334
335 err = get_jack_buffers (procinfo, frames);
336 if (err)
337 {
338 fprintf(stderr, "%s: failed to get jack ports, not processing\n", __FUNCTION__);
339 return 0;
340 }
341
342 connect_chain (procinfo, frames);
343
344 process_chain (procinfo, frames);
345
346 return 0;
347 }
348
349
350
351 /*******************************************
352 ************** non RT stuff ***************
353 *******************************************/
354
355 static int
356 process_info_connect_jack (process_info_t * procinfo, ui_t * ui)
357 {
358 printf (_("Connecting to JACK server with client name '%s'\n"), jack_client_name);
359
360 procinfo->jack_client = jack_client_new (jack_client_name);
361
362 if (!procinfo->jack_client)
363 {
364 fprintf (stderr, "%s: could not create jack client; is the jackd server running?\n", __FUNCTION__);
365 return 1;
366 }
367
368 printf (_("Connected to JACK server\n"));
369
370 jack_set_process_callback (procinfo->jack_client, process, procinfo);
371 jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, ui);
372
373 return 0;
374 }
375
376 static void
377 process_info_connect_port (process_info_t * procinfo,
378 ui_t * ui,
379 gshort in,
380 unsigned long port_index,
381 const char * port_name)
382 {
383 const char ** jack_ports;
384 unsigned long jack_port_index;
385 int err;
386 char * full_port_name;
387
388 jack_ports = jack_get_ports (procinfo->jack_client, NULL, NULL,
389 JackPortIsPhysical | (in ? JackPortIsOutput : JackPortIsInput));
390
391 if (!jack_ports)
392 return;
393
394 for (jack_port_index = 0;
395 jack_ports[jack_port_index] && jack_port_index <= port_index;
396 jack_port_index++)
397 {
398 if (jack_port_index != port_index)
399 continue;
400
401 full_port_name = g_strdup_printf ("%s:%s", jack_client_name, port_name);
402
403 printf (_("Connecting ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
404
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]);
408
409 if (err)
410 fprintf (stderr, "%s: error connecting ports '%s' and '%s'\n",
411 __FUNCTION__, full_port_name, jack_ports[jack_port_index]);
412 else
413 printf (_("Connected ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
414
415 free (full_port_name);
416 }
417
418 free (jack_ports);
419 }
420
421 int
422 process_info_set_port_count (process_info_t * procinfo, ui_t * ui,
423 unsigned long port_count, gboolean connect_inputs, gboolean connect_outputs)
424 {
425 unsigned long i;
426 char * port_name;
427 jack_port_t ** port_ptr;
428 gshort in;
429
430 if (procinfo->port_count >= port_count)
431 return;
432
433 if (procinfo->port_count == 0)
434 {
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);
437
438 procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
439 procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
440 }
441 else
442 {
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);
445
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);
448 }
449
450 for (i = procinfo->port_count; i < port_count; i++)
451 {
452 for (in = 0; in < 2; in++)
453 {
454 port_name = g_strdup_printf ("%s_%ld", in ? "in" : "out", i + 1);
455
456 //printf (_("Creating %s port %s\n"), in ? "input" : "output", port_name);
457
458 port_ptr = (in ? &procinfo->jack_input_ports[i]
459 : &procinfo->jack_output_ports[i]);
460
461 *port_ptr = jack_port_register (procinfo->jack_client,
462 port_name,
463 JACK_DEFAULT_AUDIO_TYPE,
464 in ? JackPortIsInput : JackPortIsOutput,
465 0);
466
467 if (!*port_ptr)
468 {
469 fprintf (stderr, "%s: could not register port '%s'; aborting\n",
470 __FUNCTION__, port_name);
471 return 1;
472 }
473
474 //printf (_("Created %s port %s\n"), in ? "input" : "output", port_name);
475
476 if ((in && connect_inputs) || (!in && connect_outputs))
477 process_info_connect_port (procinfo, ui, in, i, port_name);
478
479 g_free (port_name);
480 }
481 }
482
483 procinfo->port_count = port_count;
484
485 return 0;
486 }
487
488 void
489 process_info_set_channels (process_info_t * procinfo, ui_t * ui,
490 unsigned long channels, gboolean connect_inputs, gboolean connect_outputs)
491 {
492 process_info_set_port_count (procinfo, ui, channels, connect_inputs, connect_outputs);
493 procinfo->channels = channels;
494 }
495
496 process_info_t *
497 process_info_new (ui_t * ui, const char * client_name, unsigned long rack_channels,
498 gboolean connect_inputs, gboolean connect_outputs)
499 {
500 process_info_t * procinfo;
501 int err;
502
503 procinfo = g_malloc (sizeof (process_info_t));
504
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;
512
513 /* sort out the client name */
514 jack_client_name = strdup (client_name);
515 for (err = 0; jack_client_name[err] != '\0'; err++)
516 {
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) */
521 int i;
522 for (i = err; jack_client_name[i] != '\0'; i++)
523 jack_client_name[i] = jack_client_name[i + 1];
524 }
525 else if (isupper (jack_client_name[err]))
526 jack_client_name[err] = tolower (jack_client_name[err]);
527 }
528
529 err = process_info_connect_jack (procinfo, ui);
530 if (err)
531 {
532 /* g_free (procinfo); */
533 return NULL;
534 /* abort (); */
535 }
536
537 sample_rate = jack_get_sample_rate (procinfo->jack_client);
538 buffer_size = jack_get_sample_rate (procinfo->jack_client);
539
540 jack_set_process_callback (procinfo->jack_client, process, procinfo);
541 jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, ui);
542
543 procinfo->ui_to_process = ui->ui_to_process;
544 procinfo->process_to_ui = ui->process_to_ui;
545
546 jack_activate (procinfo->jack_client);
547
548 err = process_info_set_port_count (procinfo, ui, rack_channels, connect_inputs, connect_outputs);
549 if (err)
550 return NULL;
551
552 return procinfo;
553 }
554
555 void
556 process_info_destroy (process_info_t * procinfo) {
557 jack_deactivate (procinfo->jack_client);
558 jack_client_close (procinfo->jack_client);
559 g_free (procinfo);
560 }