Fixed crash in slowmotion producer
[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 "lock_free_fifo.h"
32 #include "plugin.h"
33 #include "jack_rack.h"
34
35 #ifndef _
36 #define _(x) x
37 #endif
38
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
43
44 jack_nframes_t sample_rate;
45 jack_nframes_t buffer_size;
46
47 static void
48 jack_shutdown_cb (void * data)
49 {
50 process_info_t * procinfo = data;
51
52 procinfo->quit = TRUE;
53 }
54
55 /** process messages for plugins' control ports */
56 void process_control_port_messages (process_info_t * procinfo) {
57 plugin_t * plugin;
58 unsigned long control;
59 unsigned long channel;
60 gint copy;
61
62 if (!procinfo->chain) return;
63
64 for (plugin = procinfo->chain; plugin; plugin = plugin->next)
65 {
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++)
69 {
70 while (lff_read (plugin->holders[copy].ui_control_fifos + control,
71 plugin->holders[copy].control_memory + control) == 0);
72 }
73
74 if (plugin->wet_dry_enabled)
75 for (channel = 0; channel < procinfo->channels; channel++)
76 {
77 while (lff_read (plugin->wet_dry_fifos + channel,
78 plugin->wet_dry_values + channel) == 0);
79 }
80 }
81 }
82
83 int get_jack_buffers (process_info_t * procinfo, jack_nframes_t frames) {
84 unsigned long channel;
85
86 for (channel = 0; channel < procinfo->channels; channel++)
87 {
88 procinfo->jack_input_buffers[channel] = jack_port_get_buffer (procinfo->jack_input_ports[channel], frames);
89 if (!procinfo->jack_input_buffers[channel])
90 {
91 fprintf (stderr, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
92 return 1;
93 }
94
95 procinfo->jack_output_buffers[channel] = jack_port_get_buffer (procinfo->jack_output_ports[channel], frames);
96 if (!procinfo->jack_output_buffers[channel])
97 {
98 fprintf (stderr, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
99 return 1;
100 }
101 }
102
103 return 0;
104 }
105
106 plugin_t *
107 get_first_enabled_plugin (process_info_t * procinfo)
108 {
109 plugin_t * first_enabled;
110
111 if (!procinfo->chain) return NULL;
112
113 for (first_enabled = procinfo->chain;
114 first_enabled;
115 first_enabled = first_enabled->next)
116 {
117 if (first_enabled->enabled) return first_enabled;
118 }
119
120 return NULL;
121 }
122
123 plugin_t *
124 get_last_enabled_plugin (process_info_t * procinfo)
125 {
126 plugin_t * last_enabled;
127
128 if (!procinfo->chain) return NULL;
129
130 for (last_enabled = procinfo->chain_end;
131 last_enabled;
132 last_enabled = last_enabled->prev)
133 {
134 if (last_enabled->enabled) return last_enabled;
135 }
136
137 return NULL;
138 }
139
140 void
141 connect_chain (process_info_t * procinfo, jack_nframes_t frames)
142 {
143 plugin_t * first_enabled, * last_enabled, * plugin;
144 gint copy;
145 unsigned long channel;
146 if (!procinfo->chain) return;
147
148 first_enabled = get_first_enabled_plugin (procinfo);
149 if (!first_enabled) return;
150
151 last_enabled = get_last_enabled_plugin (procinfo);
152
153 /* sort out the aux ports */
154 plugin = first_enabled;
155 do
156 {
157 if (plugin->desc->aux_channels > 0 && plugin->enabled)
158 {
159 if (procinfo->jack_client)
160 {
161 for (copy = 0; copy < plugin->copies; copy++)
162 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
163 plugin->descriptor->
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));
167 }
168 else
169 {
170 for (copy = 0; copy < frames; copy++)
171 procinfo->silent_buffer[copy] = 0.0;
172
173 for (copy = 0; copy < plugin->copies; copy++)
174 for (channel = 0; channel < plugin->desc->aux_channels; channel++)
175 plugin->descriptor->
176 connect_port (plugin->holders[copy].instance,
177 plugin->desc->audio_aux_port_indicies[channel],
178 procinfo->silent_buffer);
179 }
180 }
181 }
182 while ( (plugin != last_enabled) && (plugin = plugin->next) );
183
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)
187 {
188 plugin_connect_input_ports (last_enabled, last_enabled->prev->audio_output_memory);
189 for (plugin = first_enabled->next; plugin; plugin = plugin->next)
190 {
191 if (plugin->enabled)
192 {
193 plugin_connect_input_ports (plugin, plugin->prev->audio_output_memory);
194 plugin_connect_output_ports (plugin);
195 }
196 }
197 }
198
199 /* input buffers for first plugin */
200 plugin_connect_input_ports (first_enabled, procinfo->jack_input_buffers);
201 }
202
203 void
204 process_chain (process_info_t * procinfo, jack_nframes_t frames)
205 {
206 plugin_t * first_enabled;
207 plugin_t * last_enabled = NULL;
208 plugin_t * plugin;
209 unsigned long channel;
210 unsigned long i;
211
212 if (procinfo->jack_client)
213 {
214 LADSPA_Data zero_signal[frames];
215 guint copy;
216
217 /* set the zero signal to zero */
218 for (channel = 0; channel < frames; channel++)
219 zero_signal[channel] = 0.0;
220
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);
230 }
231
232 first_enabled = get_first_enabled_plugin (procinfo);
233
234 /* no chain; just copy input to output */
235 if (!procinfo->chain || !first_enabled)
236 {
237 unsigned long channel;
238 for (channel = 0; channel < procinfo->channels; channel++)
239 {
240 memcpy (procinfo->jack_output_buffers[channel],
241 procinfo->jack_input_buffers[channel],
242 sizeof(LADSPA_Data) * frames);
243 }
244 return;
245 }
246
247 /* all past here is guaranteed to have at least 1 enabled plugin */
248
249 last_enabled = get_last_enabled_plugin (procinfo);
250
251 for (plugin = first_enabled;
252 plugin;
253 plugin = plugin->next)
254 {
255 if (plugin->enabled)
256 {
257 for (i = 0; i < plugin->copies; i++)
258 plugin->descriptor->run (plugin->holders[i].instance, frames);
259
260 if (plugin->wet_dry_enabled)
261 for (channel = 0; channel < procinfo->channels; channel++)
262 for (i = 0; i < frames; i++)
263 {
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]);
266 }
267
268 if (plugin == last_enabled)
269 break;
270 }
271 else
272 {
273
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);
279 }
280 }
281
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);
287
288 }
289
290 int process_ladspa (process_info_t * procinfo, jack_nframes_t frames,
291 LADSPA_Data ** inputs, LADSPA_Data ** outputs) {
292 unsigned long channel;
293
294 if (!procinfo)
295 {
296 fprintf (stderr, "%s: no process_info from jack!\n", __FUNCTION__);
297 return 1;
298 }
299
300 if (procinfo->quit == TRUE)
301 return 1;
302
303 process_control_port_messages (procinfo);
304
305 for (channel = 0; channel < procinfo->channels; channel++)
306 {
307 procinfo->jack_input_buffers[channel] = inputs[channel];
308 if (!procinfo->jack_input_buffers[channel])
309 {
310 fprintf (stderr, "%s: no jack buffer for input port %ld\n", __FUNCTION__, channel);
311 return 1;
312 }
313
314 procinfo->jack_output_buffers[channel] = outputs[channel];
315 if (!procinfo->jack_output_buffers[channel])
316 {
317 fprintf (stderr, "%s: no jack buffer for output port %ld\n", __FUNCTION__, channel);
318 return 1;
319 }
320 }
321
322 connect_chain (procinfo, frames);
323
324 process_chain (procinfo, frames);
325
326 return 0;
327 }
328
329 int process_jack (jack_nframes_t frames, void * data) {
330 int err;
331 process_info_t * procinfo;
332
333 procinfo = (process_info_t *) data;
334
335 if (!procinfo)
336 {
337 fprintf (stderr, "%s: no process_info from jack!\n", __FUNCTION__);
338 return 1;
339 }
340
341 if (procinfo->port_count == 0)
342 return 0;
343
344 if (procinfo->quit == TRUE)
345 return 1;
346
347 process_control_port_messages (procinfo);
348
349 err = get_jack_buffers (procinfo, frames);
350 if (err)
351 {
352 fprintf(stderr, "%s: failed to get jack ports, not processing\n", __FUNCTION__);
353 return 0;
354 }
355
356 connect_chain (procinfo, frames);
357
358 process_chain (procinfo, frames);
359
360 return 0;
361 }
362
363
364
365 /*******************************************
366 ************** non RT stuff ***************
367 *******************************************/
368
369 static int
370 process_info_connect_jack (process_info_t * procinfo)
371 {
372 printf (_("Connecting to JACK server with client name '%s'\n"), procinfo->jack_client_name);
373
374 procinfo->jack_client = jack_client_new (procinfo->jack_client_name);
375
376 if (!procinfo->jack_client)
377 {
378 fprintf (stderr, "%s: could not create jack client; is the jackd server running?\n", __FUNCTION__);
379 return 1;
380 }
381
382 printf (_("Connected to JACK server\n"));
383
384 jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
385 jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
386
387 return 0;
388 }
389
390 static void
391 process_info_connect_port (process_info_t * procinfo,
392 gshort in,
393 unsigned long port_index,
394 const char * port_name)
395 {
396 const char ** jack_ports;
397 unsigned long jack_port_index;
398 int err;
399 char * full_port_name;
400
401 jack_ports = jack_get_ports (procinfo->jack_client, NULL, NULL,
402 JackPortIsPhysical | (in ? JackPortIsOutput : JackPortIsInput));
403
404 if (!jack_ports)
405 return;
406
407 for (jack_port_index = 0;
408 jack_ports[jack_port_index] && jack_port_index <= port_index;
409 jack_port_index++)
410 {
411 if (jack_port_index != port_index)
412 continue;
413
414 full_port_name = g_strdup_printf ("%s:%s", procinfo->jack_client_name, port_name);
415
416 printf (_("Connecting ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
417
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]);
421
422 if (err)
423 fprintf (stderr, "%s: error connecting ports '%s' and '%s'\n",
424 __FUNCTION__, full_port_name, jack_ports[jack_port_index]);
425 else
426 printf (_("Connected ports '%s' and '%s'\n"), full_port_name, jack_ports[jack_port_index]);
427
428 free (full_port_name);
429 }
430
431 free (jack_ports);
432 }
433
434 int
435 process_info_set_port_count (process_info_t * procinfo,
436 unsigned long port_count, gboolean connect_inputs, gboolean connect_outputs)
437 {
438 unsigned long i;
439 char * port_name;
440 jack_port_t ** port_ptr;
441 gshort in;
442
443 if (procinfo->port_count >= port_count)
444 return -1;
445
446 if (procinfo->port_count == 0)
447 {
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);
450
451 procinfo->jack_input_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
452 procinfo->jack_output_buffers = g_malloc (sizeof (LADSPA_Data *) * port_count);
453 }
454 else
455 {
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);
458
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);
461 }
462
463 for (i = procinfo->port_count; i < port_count; i++)
464 {
465 for (in = 0; in < 2; in++)
466 {
467 port_name = g_strdup_printf ("%s_%ld", in ? "in" : "out", i + 1);
468
469 //printf (_("Creating %s port %s\n"), in ? "input" : "output", port_name);
470
471 port_ptr = (in ? &procinfo->jack_input_ports[i]
472 : &procinfo->jack_output_ports[i]);
473
474 *port_ptr = jack_port_register (procinfo->jack_client,
475 port_name,
476 JACK_DEFAULT_AUDIO_TYPE,
477 in ? JackPortIsInput : JackPortIsOutput,
478 0);
479
480 if (!*port_ptr)
481 {
482 fprintf (stderr, "%s: could not register port '%s'; aborting\n",
483 __FUNCTION__, port_name);
484 return 1;
485 }
486
487 //printf (_("Created %s port %s\n"), in ? "input" : "output", port_name);
488
489 if ((in && connect_inputs) || (!in && connect_outputs))
490 process_info_connect_port (procinfo, in, i, port_name);
491
492 g_free (port_name);
493 }
494 }
495
496 procinfo->port_count = port_count;
497
498 return 0;
499 }
500
501 void
502 process_info_set_channels (process_info_t * procinfo,
503 unsigned long channels, gboolean connect_inputs, gboolean connect_outputs)
504 {
505 process_info_set_port_count (procinfo, channels, connect_inputs, connect_outputs);
506 procinfo->channels = channels;
507 }
508
509 process_info_t *
510 process_info_new (const char * client_name, unsigned long rack_channels,
511 gboolean connect_inputs, gboolean connect_outputs)
512 {
513 process_info_t * procinfo;
514 char * jack_client_name;
515 int err;
516
517 procinfo = g_malloc (sizeof (process_info_t));
518
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;
527
528 if ( client_name == NULL )
529 {
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);
535
536 return procinfo;
537 }
538
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++)
542 {
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) */
547 int i;
548 for (i = err; jack_client_name[i] != '\0'; i++)
549 jack_client_name[i] = jack_client_name[i + 1];
550 }
551 else if (isupper (jack_client_name[err]))
552 jack_client_name[err] = tolower (jack_client_name[err]);
553 }
554
555 err = process_info_connect_jack (procinfo);
556 if (err)
557 {
558 /* g_free (procinfo); */
559 return NULL;
560 /* abort (); */
561 }
562
563 sample_rate = jack_get_sample_rate (procinfo->jack_client);
564 buffer_size = jack_get_sample_rate (procinfo->jack_client);
565
566 jack_set_process_callback (procinfo->jack_client, process_jack, procinfo);
567 jack_on_shutdown (procinfo->jack_client, jack_shutdown_cb, procinfo);
568
569 jack_activate (procinfo->jack_client);
570
571 err = process_info_set_port_count (procinfo, rack_channels, connect_inputs, connect_outputs);
572 if (err)
573 return NULL;
574
575 return procinfo;
576 }
577
578 void
579 process_info_destroy (process_info_t * procinfo) {
580 if (procinfo->jack_client)
581 {
582 jack_deactivate (procinfo->jack_client);
583 jack_client_close (procinfo->jack_client);
584 }
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);
590 g_free (procinfo);
591 }
592
593 void process_quit (process_info_t * procinfo) {
594 procinfo->quit = TRUE;
595 }