1 /* jackspa.c - LADSPA plugin instance with JACK audio ports
2 * Copyright © 2007 Nick Thomas
3 * Copyright © 2013 Géraud Meyer <graud@gmx.com>
5 * This file is part of ng-jackspa.
7 * ng-jackpsa is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License version 2 as published by the
9 * Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along
17 * with ng-jackspa. If not, see <http://www.gnu.org/licenses/>.
26 #include "interface.h"
31 /* Initial config (command line) */
32 static gboolean jackexactname
= FALSE
;
33 static gboolean jacknostartserver
= FALSE
;
34 static gchar
*jackclientname
= NULL
;
35 static gchar
**portnames
= NULL
;
36 gboolean
parse_names(const gchar
*opt
, const gchar
*arg
,
37 gpointer data
, GError
**error
)
39 g_strfreev(portnames
); /* discard a possible previous option */
40 portnames
= g_strsplit(arg
, ":", -1);
43 static gboolean controlvout
= FALSE
;
44 static gint controlvin
= 0;
45 gboolean
parse_cvio(const gchar
*opt
, const gchar
*arg
,
46 gpointer data
, GError
**error
)
51 const GOptionEntry jackspa_entries
[] =
53 /* long, short, flags, arg_type, arg_data, description, arg_description */
54 { "jack-exactname", 'N', 0, G_OPTION_ARG_NONE
, &jackexactname
,
55 "Do not allow JACK to generate an unused client name", NULL
},
56 { "jack-nostartserver", 'S', 0, G_OPTION_ARG_NONE
, &jacknostartserver
,
57 "Do not start a JACK server if none is running", NULL
},
58 { "jack-client", 'j', 0, G_OPTION_ARG_STRING
, &jackclientname
,
59 "Use the given name instead of the concatenation of a fixed prefix and the plugin label", "client_name" },
60 { "names", 'n', 0, G_OPTION_ARG_CALLBACK
, parse_names
,
61 "Colon separated list of overriding names for the LADPSA audio and control ports", "port_names" },
62 { "controlv-out", 'O', 0, G_OPTION_ARG_NONE
, &controlvout
,
63 "Export the control outputs as control voltages on JACK audio ports", NULL
},
64 { "controlv-in", 'I', 0, G_OPTION_ARG_NONE
, &controlvin
,
65 "Export the control inputs as control voltages on JACK input audio ports", NULL
},
66 { "controlv-inasout", 'T', G_OPTION_FLAG_NO_ARG
, G_OPTION_ARG_CALLBACK
, parse_cvio
,
67 "Export the control inputs as control voltages on JACK output audio ports not on input ones", NULL
},
70 static char *ladspa_library
= NULL
;
73 union { unsigned long uid
; char *label
; } id
;
75 static plugin_id_t ladspa_id
= { 0 };
78 /* Finds the plugin descriptor with the given ID in the given object file,
79 * storing it in state->descriptor. Returns 1 on success. On failure, returns 0
80 * and sets *error to an appropriate error message.
82 int find_plugin(state_t
*state
, const char *file
, const plugin_id_t plugin_id
,
86 unsigned long i
; /* port index */
87 LADSPA_Descriptor
*(*descriptor_fun
)(unsigned long index
);
88 LADSPA_Descriptor
*descriptor
;
90 /* Open the library. */
91 state
->library
= ladspa_dlopen(file
, RTLD_LAZY
);
93 err
= 1, *error
= dlerror();
95 /* Find the ladspa_descriptor() function. */
97 descriptor_fun
= (LADSPA_Descriptor
*(*)(unsigned long))
98 dlsym(state
->library
, "ladspa_descriptor");
100 err
= 1, *error
= dlerror();
103 /* Find the appropriate descriptor. */
104 for (i
= 0; !err
; i
++) {
105 descriptor
= descriptor_fun(i
);
108 err
= 1, *error
= plugin_id
.is_label
?
109 "no such plugin label in the given library" :
110 "no such plugin UID in the given library";
111 else if ( (!plugin_id
.is_label
&& descriptor
->UniqueID
== plugin_id
.id
.uid
) ||
112 (plugin_id
.is_label
&& !strcmp(descriptor
->Label
, plugin_id
.id
.label
)) )
114 state
->descriptor
= descriptor
;
122 int close_plugin(state_t
*state
, const char **error
)
124 state
->descriptor
= NULL
;
126 if (dlclose(state
->library
))
127 return (*error
= dlerror(), 0);
128 state
->library
= NULL
;
133 /* The JACK processing callback. */
134 int process(jack_nframes_t nframes
, void *arg
)
136 state_t
*state
= (state_t
*)arg
;
137 unsigned long p
; /* loop variable for ports */
138 const LADSPA_PortDescriptor
*port
; /* loop variable for port descriptors */
139 void *buffer
; /* JACK buffer */
140 LADSPA_Data buf
; /* buffer for the control outputs */
142 /* Connect audio ports and unused control ports */
143 for ( p
= 0, port
= state
->descriptor
->PortDescriptors
;
144 p
< state
->descriptor
->PortCount
; p
++, port
++ )
146 if (LADSPA_IS_PORT_CONTROL(*port
)) {
147 if (LADSPA_IS_PORT_INPUT(*port
) && !controlvin
)
149 if (LADSPA_IS_PORT_OUTPUT(*port
) && !controlvout
) {
150 state
->descriptor
->connect_port(state
->handle
, p
, &buf
);
151 /* forget the control output ports values */
156 buffer
= jack_port_get_buffer(state
->jack_ports
[p
], nframes
);
157 if (LADSPA_IS_PORT_CONTROL(*port
)) {
158 if (LADSPA_IS_PORT_OUTPUT(*port
))
159 memset(buffer
, 0, (size_t)nframes
); /* might be needless */
160 /* only the first sample will be set */
161 else if (controlvin
== 1 && !jack_port_connected(state
->jack_ports
[p
]))
162 *(LADSPA_Data
*)buffer
= state
->control_port_values
[p
];
163 /* use the previous value (or the value set in the UI) */
164 else if (controlvin
== 1)
165 state
->control_port_values
[p
] = *(LADSPA_Data
*)buffer
;
166 /* record the new control value */
168 memset(buffer
, 0, (size_t)nframes
); /* might be needless */
169 *(LADSPA_Data
*)buffer
= state
->control_port_values
[p
];
172 state
->descriptor
->connect_port
173 (state
->handle
, p
, (LADSPA_Data
*)buffer
);
176 /* Run the plugin. */
177 state
->descriptor
->run(state
->handle
, nframes
);
182 /* Initializes JACK, opening the appropriate ports, instantiating the LADSPA
183 * Plugin, and starting the synthesis thread running.. Returns 1 on success. On
184 * failure, returns 0 and sets *error to an appropriate error message.
186 int init_jack(state_t
*state
, const char **error
)
188 static char client_name_prefix
[] = "jackspa_";
190 jack_options_t oflags
;
191 jack_status_t jack_status
;
192 unsigned long p
; /* loop variable for ports */
193 const LADSPA_PortDescriptor
*port
; /* loop variable for port descriptors */
194 enum JackPortFlags pflags
;
196 /* Allocate memory for the client name. */
197 if (!jackclientname
) {
198 state
->client_name
= (char *)calloc
199 ( sizeof(client_name_prefix
) + strlen(state
->descriptor
->Label
),
201 if (!state
->client_name
)
202 err
= 1, *error
= strerror(errno
);
205 /* Set the client name. */
208 state
->client_name
= jackclientname
;
210 /* state->client_name[0] = '\0'; */ /* done by calloc */
211 strcat(state
->client_name
, client_name_prefix
);
212 strcat(state
->client_name
, state
->descriptor
->Label
);
218 oflags
= JackNullOption
;
219 if (jackexactname
) oflags
|= JackUseExactName
;
220 if (jacknostartserver
) oflags
|= JackNoStartServer
;
222 jack_client_open(state
->client_name
, oflags
, &jack_status
);
223 if (!state
->jack_client
)
224 err
= 1, *error
= "could not connect to JACK";
227 /* Free memory for the client name */
228 if (!jackclientname
) free(state
->client_name
);
229 if (!err
) state
->client_name
= jack_get_client_name(state
->jack_client
);
231 /* Allocate memory for the list of ports. */
233 state
->jack_ports
= (jack_port_t
**)calloc
234 (state
->descriptor
->PortCount
, sizeof(jack_port_t
*));
235 if (!state
->jack_ports
)
236 err
= 1, *error
= strerror(errno
);
239 /* Allocate memory for the list of control port values. */
241 state
->control_port_values
= (LADSPA_Data
*)calloc
242 ((size_t)state
->descriptor
->PortCount
, sizeof(LADSPA_Data
));
243 if (!state
->control_port_values
)
244 err
= 1, *error
= strerror(errno
);
247 /* Register ports. */
248 state
->num_control_ports
= 0; state
->num_meter_ports
= 0;
249 for ( p
= 0, port
= state
->descriptor
->PortDescriptors
;
250 !err
&& p
< state
->descriptor
->PortCount
; p
++, port
++ )
252 /* Get the actual port name */
253 state
->port_names
[p
] = glib_strv_index(p
, portnames
);
254 if (!state
->port_names
[p
] || *state
->port_names
[p
] == '\0')
255 state
->port_names
[p
] = state
->descriptor
->PortNames
[p
];
257 /* Count control ports and skip non exported ports */
258 if (LADSPA_IS_PORT_CONTROL(*port
)) {
259 if (LADSPA_IS_PORT_INPUT(*port
)) {
260 state
->num_control_ports
++;
261 if (!controlvin
) continue;
264 state
->num_meter_ports
++;
265 if (!controlvout
) continue;
269 if (LADSPA_IS_PORT_INPUT(*port
)) {
270 if (controlvin
!= 1 && LADSPA_IS_PORT_CONTROL(*port
))
271 pflags
= JackPortIsOutput
;
273 pflags
= JackPortIsInput
;
276 pflags
= JackPortIsOutput
;
278 state
->jack_ports
[p
] = jack_port_register
279 ( state
->jack_client
, state
->port_names
[p
],
280 JACK_DEFAULT_AUDIO_TYPE
, pflags
, 0 );
282 if (!state
->jack_ports
[p
])
283 err
= 1, *error
= "could not register JACK ports";
286 /* Register our processing callback. */
288 if (jack_set_process_callback(state
->jack_client
, &process
, state
))
289 err
= 1, *error
= "could not register the JACK processing callback";
291 /* Instantiate the LADSPA plugin. */
293 state
->handle
= state
->descriptor
->instantiate
294 (state
->descriptor
, jack_get_sample_rate(state
->jack_client
));
296 err
= 1, *error
= "could not instantiate the plugin.";
299 /* Connect input control ports. */
301 for (p
= 0; p
< state
->descriptor
->PortCount
; p
++)
302 if ( LADSPA_IS_PORT_CONTROL(state
->descriptor
->PortDescriptors
[p
]) &&
303 LADSPA_IS_PORT_INPUT(state
->descriptor
->PortDescriptors
[p
]))
305 state
->descriptor
->connect_port
306 (state
->handle
, p
, &state
->control_port_values
[p
]);
307 /* state->control_port_values[p] = 0.0; */ /* done by calloc() */
310 /* Activate the LADSPA plugin. */
312 if (state
->descriptor
->activate
)
313 state
->descriptor
->activate(state
->handle
);
315 /* Get the bits flowing. */
317 if (jack_activate(state
->jack_client
))
318 err
= 1, *error
= "could not activate audio processing";
329 /* Parses command-line arguments. Returns 1 on success. On failure, returns 0
330 * and sets *error to an appropriate error message.
332 int parse_args(state_t
*state
, int argc
, char **argv
, char **plugin_file
,
333 plugin_id_t
*plugin_id
, const char **error
)
336 int i
; /* arg index */
337 arg_parser_state st
= st_want_plugin_file
;
340 argc
--, argv
++; /* skip the program name */
342 for (i
= 0; i
< argc
; i
++)
344 case st_want_plugin_file
:
345 *plugin_file
= argv
[i
];
346 st
= st_want_plugin_id
;
349 case st_want_plugin_id
:
350 plugin_id
->is_label
= 0, plugin_id
->id
.uid
= strtol(argv
[i
], &e
, 0);
351 if (e
== argv
[i
] || *e
!= '\0')
352 plugin_id
->is_label
= 1, plugin_id
->id
.label
= argv
[i
];
358 err
= 1, *error
= "extra arguments given";
363 err
= 1, *error
= "must supply a plugin file and a plugin ID";
368 int jackspa_init(state_t
*state
, int argc
, char **argv
)
373 state
->jack_client
= 0;
375 err
= !parse_args(state
, argc
, argv
, &ladspa_library
, &ladspa_id
, &error
);
378 err
= !find_plugin(state
, ladspa_library
, ladspa_id
, &error
);
382 (const char **)calloc(state
->descriptor
->PortCount
, sizeof(char *));
383 if (!state
->port_names
)
384 err
= 1, error
= "memory allocation error";
388 err
= !init_jack(state
, &error
);
391 fprintf(stderr
, "jackspa error: %s\n", error
);
396 int jackspa_fini(state_t
*state
)
401 if ((err
= jack_client_close(state
->jack_client
)))
403 state
->jack_client
= 0;
405 if (state
->descriptor
->deactivate
)
406 state
->descriptor
->deactivate(state
->handle
);
407 state
->descriptor
->cleanup(state
->handle
);
410 free(state
->control_port_values
), state
->control_port_values
= NULL
;
411 free(state
->jack_ports
), state
->jack_ports
= NULL
;
412 state
->num_control_ports
= 0; state
->num_meter_ports
= 0;
414 free(state
->port_names
);
416 err
= !close_plugin(state
, &error
);