1 // jackspa, a dirt-simple LADSPA host for JACK
2 // Copyright (C) 2007 Nick Thomas
3 // Copyright (C) 2013 GĂ©raud Meyer <graud@gmx.com>
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of version 2 of the GNU General Public
7 // License as published by the Free Software Foundation; the terms of
8 // any later version are NOT APPLICABLE.
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License along
16 // with this program; if not, write to the Free Software Foundation, Inc.,
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 #include "jackspa.hpp"
26 // Finds the plugin descriptor with the given ID in the given
27 // object file, storing it in state->descriptor. Returns 1 on
28 // success. On failure, returns 0 and sets *error to an appropriate
30 int find_plugin(state_t
*state
, char *file
, int id
, const char **error
)
35 LADSPA_Descriptor
*(*descriptor_fun
)(unsigned long index
);
36 LADSPA_Descriptor
*descriptor
;
39 library
= dlopen(file
, RTLD_LAZY
);
45 // Find the ladspa_descriptor() function.
47 descriptor_fun
= (LADSPA_Descriptor
*(*)(unsigned long))
48 dlsym(library
, "ladspa_descriptor");
49 if (!descriptor_fun
) {
55 // Find the appropriate descriptor.
56 for (i
= 0; !err
; i
++) {
57 descriptor
= descriptor_fun(i
);
60 *error
= "no such plugin index in the given file";
62 } else if ((int)descriptor
->UniqueID
== id
) {
63 state
->descriptor
= descriptor
;
71 // The JACK processing callback.
72 int process(jack_nframes_t nframes
, void *arg
)
74 state_t
*state
= (state_t
*)arg
;
76 float *buffer
= state
->control_port_buffer
;
78 // Connect audio ports and copy control port values.
79 for (i
= 0; i
< (int)state
->descriptor
->PortCount
; i
++) {
80 if (LADSPA_IS_PORT_CONTROL(state
->descriptor
->PortDescriptors
[i
]) &&
81 LADSPA_IS_PORT_INPUT(state
->descriptor
->PortDescriptors
[i
])) {
82 for (j
= 0; j
< (int)nframes
; j
++, buffer
++) {
83 *buffer
= state
->control_port_values
[i
];
86 state
->descriptor
->connect_port
88 (float *)jack_port_get_buffer(state
->ports
[i
], nframes
));
93 state
->descriptor
->run(state
->handle
, nframes
);
98 // The JACK buffer size callback.
99 int buffer_size(jack_nframes_t nframes
, void *arg
)
101 state_t
*state
= (state_t
*)arg
;
105 state
->control_port_buffer
= (float *)realloc
106 (state
->control_port_buffer
, state
->num_control_ports
*
107 nframes
* sizeof(float));
109 buffer
= state
->control_port_buffer
;
111 for (i
= 0; i
< (int)state
->descriptor
->PortCount
; i
++) {
112 if (LADSPA_IS_PORT_CONTROL(state
->descriptor
->
113 PortDescriptors
[i
]) &&
114 LADSPA_IS_PORT_INPUT(state
->descriptor
->PortDescriptors
[i
])) {
115 state
->descriptor
->connect_port(state
->handle
, i
, buffer
);
116 buffer
+= jack_get_buffer_size(state
->jack_client
);
123 // Initializes JACK, opening the appropriate ports, instantiating the
124 // LADSPA Plugin, and starting the synthesis thread running.. Returns
125 // 1 on success. On failure, returns 0 and sets *error to an
126 // appropriate error message.
127 int init_jack(state_t
*state
, const char **error
)
129 static char client_name_prefix
[] = "jackspa_";
131 jack_status_t jack_status
;
136 // Allocate memory for the client name.
137 state
->client_name
= (char *)calloc(1, sizeof(client_name_prefix
) +
138 strlen(state
->descriptor
->Label
));
139 if (!state
->client_name
) {
141 *error
= strerror(errno
);
144 // Set the client name.
146 strcat(state
->client_name
, client_name_prefix
);
147 strcat(state
->client_name
, state
->descriptor
->Label
);
152 state
->jack_client
= jack_client_open(state
->client_name
,
153 JackNullOption
, &jack_status
);
155 *error
= "could not connect to JACK";
160 // Allocate memory for the list of ports.
162 state
->ports
= (jack_port_t
**)calloc(state
->descriptor
->PortCount
,
163 sizeof(jack_port_t
*));
165 *error
= strerror(errno
);
170 // Allocate memory for the list of control port values.
172 state
->control_port_values
=
173 (float *)malloc(state
->descriptor
->PortCount
* sizeof(float));
174 if (!state
->control_port_values
) {
175 *error
= strerror(errno
);
181 state
->num_control_ports
= 0;
182 for (i
= 0; !err
&& i
< (int)state
->descriptor
->PortCount
; i
++) {
183 if (LADSPA_IS_PORT_CONTROL(state
->descriptor
->
184 PortDescriptors
[i
]) &&
185 LADSPA_IS_PORT_INPUT(state
->descriptor
->PortDescriptors
[i
])) {
186 state
->num_control_ports
++;
190 if (LADSPA_IS_PORT_INPUT(state
->descriptor
->PortDescriptors
[i
])) {
191 flags
= JackPortIsInput
;
193 flags
= JackPortIsOutput
;
197 jack_port_register(state
->jack_client
,
198 state
->descriptor
->PortNames
[i
],
199 JACK_DEFAULT_AUDIO_TYPE
,
202 if (!state
->ports
[i
]) {
204 *error
= "could not register JACK ports";
208 // Register our processing callback.
210 if (jack_set_process_callback(state
->jack_client
, &process
, state
)) {
212 *error
= "could not register the JACK processing callback";
216 // Register our buffer size callback.
218 if (jack_set_buffer_size_callback(state
->jack_client
, &buffer_size
,
221 *error
= "could not register the JACK processing callback";
225 // Instantiate the LADSPA plugin.
227 state
->handle
= state
->descriptor
->instantiate
228 (state
->descriptor
, jack_get_sample_rate(state
->jack_client
));
229 if (!state
->handle
) {
231 *error
= "could not instantiate the plugin.";
235 // Allocate control port buffers.
237 state
->control_port_buffer
= (float *)malloc
238 (state
->num_control_ports
*
239 jack_get_buffer_size(state
->jack_client
) *
241 if (!state
->control_port_buffer
) {
243 *error
= strerror(errno
);
247 // Connect control ports.
249 buffer
= state
->control_port_buffer
;
251 for (i
= 0; i
< (int)state
->descriptor
->PortCount
; i
++) {
252 if (LADSPA_IS_PORT_CONTROL(state
->descriptor
->
253 PortDescriptors
[i
]) &&
254 LADSPA_IS_PORT_INPUT(state
->descriptor
->PortDescriptors
[i
])) {
255 state
->descriptor
->connect_port(state
->handle
, i
, buffer
);
256 buffer
+= jack_get_buffer_size(state
->jack_client
);
257 state
->control_port_values
[i
] = 0.0;
262 // Activate the LADSPA plugin.
264 if (state
->descriptor
->activate
) {
265 state
->descriptor
->activate(state
->handle
);
269 // Get the bits flowing.
271 if (jack_activate(state
->jack_client
)) {
273 *error
= "could not activate audio processing";
286 // Parses command-line arguments. Returns 1 on success. On failure,
287 // returns 0 and sets *error to an appropriate error message.
288 int parse_args(state_t
*state
, int argc
, char **argv
, char **plugin_file
,
289 int *plugin_id
, const char **error
)
293 arg_parser_state st
= st_want_plugin_file
;
298 for (i
= 0; i
< argc
; i
++) {
300 case st_want_plugin_file
:
301 *plugin_file
= argv
[i
];
302 st
= st_want_plugin_id
;
305 case st_want_plugin_id
:
306 *plugin_id
= atoi(argv
[i
]);
320 *error
= "must supply a plugin file and a plugin ID";
326 void jackspa_init(state_t
*state
, int argc
, char **argv
)
333 state
->jack_client
= 0;
335 err
= !parse_args(state
, argc
, argv
, &plugin_file
, &plugin_id
, &error
);
338 err
= !find_plugin(state
, plugin_file
, plugin_id
, &error
);
342 err
= !init_jack(state
, &error
);
346 fprintf(stderr
, "Error: %s.\n", error
);