Use linked list for storing sources in MIDI merger.
[calfbox.git] / usbio.c
blob2234d6ee0e5b3e70b8b2d9d0addb06267ed75ea2
1 /*
2 Calf Box, an open source musical instrument.
3 Copyright (C) 2010-2013 Krzysztof Foltman
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
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
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 /*
21 Note: this is a silly experimental driver for a number of USB MIDI devices.
23 It only supports audio output and MIDI input, as those are my immediate
24 needs, to be able to use a machine running CalfBox as a standalone MIDI
25 instrument. Plug-and-play is supported, as long as current user running
26 calfbox has write access to the USB devices involved. This can be done by
27 running calfbox as root, or by setting right permissions in udev scripts
28 - this may be considered a safer method.
30 Devices supported:
31 * Class-compliant audio output devices (tested with Lexicon Omega and some
32 cheap no-brand C-Media USB soundcard dongle)
33 * Alesis Multimix 8 USB 2.0 (audio output only)
34 * Class-compliant MIDI input devices (tested with several devices)
36 Yes, code quality is pretty awful, especially in areas involving clock
37 sync. I'm going to clean it up iteratively later.
41 #include "config.h"
42 #include "config-api.h"
43 #include "errors.h"
44 #include "hwcfg.h"
45 #include "io.h"
46 #include "meter.h"
47 #include "midi.h"
48 #include "recsrc.h"
49 #include "usbio_impl.h"
51 #include <assert.h>
52 #include <errno.h>
53 #include <libusb.h>
54 #include <math.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <syscall.h>
59 #include <unistd.h>
60 #include <sys/mman.h>
62 ///////////////////////////////////////////////////////////////////////////////
64 int cbox_usbio_get_sample_rate(struct cbox_io_impl *impl)
66 struct cbox_usb_io_impl *uii = (struct cbox_usb_io_impl *)impl;
68 return uii->sample_rate;
71 gboolean cbox_usbio_get_status(struct cbox_io_impl *impl, GError **error)
73 // XXXKF: needs a flag that would indicate whether device is present
74 // XXXKF: needs to return that flag with appropriate message
75 return TRUE;
78 static void run_audio_loop(struct cbox_usb_io_impl *uii)
80 while(!uii->stop_engine && !uii->device_removed) {
81 struct cbox_io *io = uii->ioi.pio;
82 struct timeval tv = {
83 .tv_sec = 0,
84 .tv_usec = 1000
86 libusb_handle_events_timeout(uii->usbctx, &tv);
87 for (GSList *p = io->midi_outputs; p; p = p->next)
89 struct cbox_usb_midi_output *umo = p->data;
90 usbio_send_midi_to_output(umo);
95 void usbio_run_idle_loop(struct cbox_usb_io_impl *uii)
97 while(!uii->stop_engine)
99 struct cbox_io *io = uii->ioi.pio;
100 for (int b = 0; b < uii->output_channels; b++)
101 memset(io->output_buffers[b], 0, io->io_env.buffer_size * sizeof(float));
102 io->cb->process(io->cb->user_data, io, io->io_env.buffer_size);
103 for (GList *p = uii->rt_midi_ports; p; p = p->next)
105 struct cbox_usb_midi_interface *umi = p->data;
106 cbox_midi_buffer_clear(&umi->input_port->hdr.buffer);
108 for (GSList *p = io->midi_outputs; p; p = p->next)
110 struct cbox_usb_midi_output *umo = p->data;
111 usbio_send_midi_to_output(umo);
114 struct timeval tv = {
115 .tv_sec = 0,
116 .tv_usec = 1
118 libusb_handle_events_timeout(uii->usbctx, &tv);
119 usleep((int)(io->io_env.buffer_size * 1000000.0 / uii->sample_rate));
123 static void *engine_thread(void *user_data)
125 struct cbox_usb_io_impl *uii = user_data;
127 usbio_start_midi_capture(uii);
129 if (uii->handle_audiodev)
131 uii->no_resubmit = FALSE;
132 struct sched_param p;
133 memset(&p, 0, sizeof(p));
134 p.sched_priority = cbox_config_get_int("io", "rtpriority", 10);
135 pid_t tid = syscall(SYS_gettid);
136 if (0 != sched_setscheduler(tid, SCHED_FIFO, &p))
137 g_warning("Cannot set realtime priority for the processing thread: %s.", strerror(errno));
139 usbio_start_audio_playback(uii);
140 if (!uii->setup_error)
142 run_audio_loop(uii);
144 uii->no_resubmit = TRUE;
145 memset(&p, 0, sizeof(p));
146 p.sched_priority = 0;
147 if (0 != sched_setscheduler(tid, SCHED_OTHER, &p))
148 g_warning("Cannot unset realtime priority for the processing thread: %s.", strerror(errno));
149 usbio_stop_audio_playback(uii);
151 else
153 uii->no_resubmit = TRUE;
154 g_message("No audio device found - running idle loop.");
155 // notify the UI thread that the (fake) audio loop is running
156 uii->playback_counter = uii->playback_buffers;
157 usbio_run_idle_loop(uii);
160 usbio_stop_midi_capture(uii);
161 return NULL;
164 static void cbox_usbio_destroy_midi_out(struct cbox_io_impl *ioi, struct cbox_midi_output *midiout)
166 g_free(midiout->name);
167 free(midiout);
170 static struct cbox_usb_midi_interface *cur_midi_interface = NULL;
172 struct cbox_midi_input *cbox_usbio_create_midi_in(struct cbox_io_impl *impl, const char *name, GError **error)
174 // struct cbox_usb_io_impl *uii = (struct cbox_usb_io_impl *)impl;
175 struct cbox_usb_midi_input *input = calloc(1, sizeof(struct cbox_usb_midi_input));
176 input->hdr.name = g_strdup(name);
177 input->hdr.removing = FALSE;
178 cbox_uuid_generate(&input->hdr.uuid);
179 cbox_midi_buffer_init(&input->hdr.buffer);
180 input->ifptr = cur_midi_interface;
181 cbox_midi_appsink_init(&input->hdr.appsink, NULL);
182 input->hdr.enable_appsink = FALSE;
184 return (struct cbox_midi_input *)input;
187 struct cbox_midi_output *cbox_usbio_create_midi_out(struct cbox_io_impl *impl, const char *name, GError **error)
189 // struct cbox_usb_io_impl *uii = (struct cbox_usb_io_impl *)impl;
190 struct cbox_usb_midi_output *output = calloc(1, sizeof(struct cbox_usb_midi_output));
191 output->hdr.name = g_strdup(name);
192 output->hdr.removing = FALSE;
193 cbox_uuid_generate(&output->hdr.uuid);
194 cbox_midi_buffer_init(&output->hdr.buffer);
195 cbox_midi_merger_init(&output->hdr.merger, &output->hdr.buffer);
196 output->ifptr = cur_midi_interface;
198 return (struct cbox_midi_output *)output;
201 static void create_midi_ports(struct cbox_usb_io_impl *uii)
203 uii->ioi.createmidiinfunc = cbox_usbio_create_midi_in;
204 uii->ioi.createmidioutfunc = cbox_usbio_create_midi_out;
205 for (GList *p = uii->midi_ports; p; p = p->next)
207 struct cbox_usb_midi_interface *umi = p->data;
208 char buf[80];
209 sprintf(buf, "usb:%03d:%03d", umi->devinfo->bus, umi->devinfo->devadr);
210 cur_midi_interface = umi;
211 if (umi->epdesc_in.found)
212 umi->input_port = (struct cbox_usb_midi_input *)cbox_io_create_midi_input(uii->ioi.pio, buf, NULL);
213 else
214 umi->input_port = NULL;
215 if (umi->epdesc_out.found)
216 umi->output_port = (struct cbox_usb_midi_output *)cbox_io_create_midi_output(uii->ioi.pio, buf, NULL);
217 else
218 umi->output_port = NULL;
220 uii->ioi.createmidiinfunc = NULL;
221 uii->ioi.createmidioutfunc = NULL;
222 cur_midi_interface = NULL;
225 gboolean cbox_usbio_start(struct cbox_io_impl *impl, struct cbox_command_target *fb, GError **error)
227 struct cbox_usb_io_impl *uii = (struct cbox_usb_io_impl *)impl;
229 // XXXKF: needs to queue the playback and capture transfers
231 uii->stop_engine = FALSE;
232 uii->setup_error = FALSE;
233 uii->playback_counter = 0;
235 create_midi_ports(uii);
237 struct cbox_io *io = uii->ioi.pio;
238 // XXXKF There is a short period of time when the playback is in 'started' state
239 // but is not really processing the event loop. This is likely harmless, but
240 // should be kept in mind when adding any code between pthread_create
241 // and usbio_update_port_routing.
242 if (io->cb->on_started)
243 io->cb->on_started(io->cb->user_data);
245 if (pthread_create(&uii->thr_engine, NULL, engine_thread, uii))
247 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "cannot create engine thread: %s", strerror(errno));
248 return FALSE;
250 while(!uii->setup_error && uii->playback_counter < uii->playback_buffers)
251 usleep(10000);
252 usbio_update_port_routing(&uii->ioi);
254 return TRUE;
257 gboolean cbox_usbio_stop(struct cbox_io_impl *impl, GError **error)
259 struct cbox_usb_io_impl *uii = (struct cbox_usb_io_impl *)impl;
261 // XXXKF: needs to kill the playback and capture transfers, and
262 // wait for them to be killed
264 uii->stop_engine = TRUE;
265 pthread_join(uii->thr_engine, NULL);
266 return TRUE;
269 void cbox_usbio_poll_ports(struct cbox_io_impl *impl, struct cbox_command_target *fb)
271 struct cbox_usb_io_impl *uii = (struct cbox_usb_io_impl *)impl;
273 // Dry run, just to detect if anything changed
274 if (usbio_scan_devices(uii, TRUE))
276 struct cbox_io_callbacks *cb = uii->ioi.pio->cb;
277 g_debug("Restarting I/O due to device being connected or disconnected");
278 cbox_io_stop(uii->ioi.pio);
279 // Re-scan, this time actually create the MIDI inputs
280 usbio_scan_devices(uii, FALSE);
281 cbox_io_start(uii->ioi.pio, cb, fb);
285 gboolean cbox_usbio_cycle(struct cbox_io_impl *impl, struct cbox_command_target *fb, GError **error)
287 // struct cbox_usb_io_impl *uii = (struct cbox_usb_io_impl *)impl;
288 // XXXKF: this is for restarting the thing; not implemented for now,
289 // the implementation will be something like in case of JACK - close and
290 // reopen.
291 return TRUE;
294 int cbox_usbio_get_midi_data(struct cbox_io_impl *impl, struct cbox_midi_buffer *destination)
296 struct cbox_usb_io_impl *uii = (struct cbox_usb_io_impl *)impl;
298 cbox_midi_merger_render_to(&uii->midi_input_merger, destination);
299 return 0;
302 void cbox_usbio_destroy(struct cbox_io_impl *impl)
304 struct cbox_usb_io_impl *uii = (struct cbox_usb_io_impl *)impl;
306 GList *prev_keys = g_hash_table_get_values(uii->device_table);
307 for (GList *p = prev_keys; p; p = p->next)
309 struct cbox_usb_device_info *udi = p->data;
310 if (udi->status == CBOX_DEVICE_STATUS_OPENED)
311 usbio_forget_device(uii, udi);
313 g_list_free(prev_keys);
314 g_hash_table_destroy(uii->device_table);
316 libusb_exit(uii->usbctx_probe);
317 libusb_exit(uii->usbctx);
318 cbox_midi_merger_close(&uii->midi_input_merger);
319 free(uii);
322 ///////////////////////////////////////////////////////////////////////////////
324 struct usbio_transfer *usbio_transfer_new(struct libusb_context *usbctx, const char *transfer_type, int index, int isopackets, void *user_data)
326 struct usbio_transfer *p = malloc(sizeof(struct usbio_transfer));
327 p->usbctx = usbctx;
328 p->transfer = libusb_alloc_transfer(isopackets);
329 p->index = index;
330 p->cancel_confirm = FALSE;
331 p->pending = FALSE;
332 p->transfer_type = transfer_type;
333 p->user_data = user_data;
334 return p;
337 int usbio_transfer_submit(struct usbio_transfer *xfer)
339 int res = libusb_submit_transfer(xfer->transfer);
340 if (res != 0)
342 g_warning("usbio_transfer_submit: cannot submit transfer '%s:%d', error = %s", xfer->transfer_type, xfer->index, libusb_error_name(res));
343 return res;
345 xfer->pending = TRUE;
346 return 0;
349 void usbio_transfer_shutdown(struct usbio_transfer *xfer)
351 if (xfer->pending)
353 int res = libusb_cancel_transfer(xfer->transfer);
354 if (res != LIBUSB_ERROR_NO_DEVICE)
356 int tries = 100;
357 while(!xfer->cancel_confirm && tries > 0 && xfer->pending)
359 struct timeval tv = {
360 .tv_sec = 0,
361 .tv_usec = 1000
363 libusb_handle_events_timeout(xfer->usbctx, &tv);
364 tries--;
366 if (!tries)
367 g_warning("Timed out waiting for transfer '%s:%d' to complete; status = %d", xfer->transfer_type, xfer->index, xfer->transfer->status);
372 void usbio_transfer_destroy(struct usbio_transfer *xfer)
374 libusb_free_transfer(xfer->transfer);
375 free(xfer);
379 ///////////////////////////////////////////////////////////////////////////////
381 static gboolean cbox_usb_io_process_cmd(struct cbox_command_target *ct, struct cbox_command_target *fb, struct cbox_osc_command *cmd, GError **error)
383 struct cbox_usb_io_impl *uii = (struct cbox_usb_io_impl *)ct->user_data;
384 struct cbox_io *io = uii->ioi.pio;
385 gboolean handled = FALSE;
386 if (!strcmp(cmd->command, "/status") && !strcmp(cmd->arg_types, ""))
388 if (!cbox_check_fb_channel(fb, cmd->command, error))
389 return FALSE;
391 for (GList *p = uii->midi_ports; p; p = g_list_next(p))
393 struct cbox_usb_midi_interface *midi = p->data;
394 struct cbox_usb_device_info *di = midi->devinfo;
395 if (midi->epdesc_in.found && !cbox_execute_on(fb, NULL, "/usb_midi_input", "iiiiu", error, di->bus, di->devadr, di->vid, di->pid, &midi->input_port->hdr.uuid))
396 return FALSE;
397 if (midi->epdesc_out.found && !cbox_execute_on(fb, NULL, "/usb_midi_output", "iiiiu", error, di->bus, di->devadr, di->vid, di->pid, &midi->output_port->hdr.uuid))
398 return FALSE;
401 return cbox_execute_on(fb, NULL, "/output_resolution", "i", error, 8 * uii->output_resolution) &&
402 cbox_io_process_cmd(io, fb, cmd, error, &handled);
404 else
406 gboolean result = cbox_io_process_cmd(io, fb, cmd, error, &handled);
407 if (!handled)
408 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Unknown combination of target path and argument: '%s', '%s'", cmd->command, cmd->arg_types);
409 return result;
413 ///////////////////////////////////////////////////////////////////////////////
415 gboolean cbox_io_init_usb(struct cbox_io *io, struct cbox_open_params *const params, struct cbox_command_target *fb, GError **error)
417 struct cbox_usb_io_impl *uii = malloc(sizeof(struct cbox_usb_io_impl));
418 if (libusb_init(&uii->usbctx))
420 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Cannot initialise libusb.");
421 return FALSE;
423 if (libusb_init(&uii->usbctx_probe))
425 libusb_exit(uii->usbctx);
426 g_set_error(error, CBOX_MODULE_ERROR, CBOX_MODULE_ERROR_FAILED, "Cannot initialise libusb.");
427 return FALSE;
429 libusb_set_debug(uii->usbctx, 3);
430 libusb_set_debug(uii->usbctx_probe, 3);
431 uii->device_table = g_hash_table_new(g_direct_hash, NULL);
433 uii->sample_rate = cbox_config_get_int(cbox_io_section, "sample_rate", 44100);
434 uii->sync_buffers = cbox_config_get_int(cbox_io_section, "sync_buffers", 2);
435 uii->debug_sync = cbox_config_get_int(cbox_io_section, "debug_sync", 0);
436 uii->playback_buffers = cbox_config_get_int(cbox_io_section, "playback_buffers", 2);
437 // shouldn't be more than 4, otherwise it will crackle due to limitations of
438 // the packet length adjustment. It might work better if adjustment
439 // was per-packet and not per-transfer.
440 uii->iso_packets = cbox_config_get_int(cbox_io_section, "iso_packets", 1);
441 // The USB 2.0 device uses a higher packet rate (125us I think), so the
442 // default number of packets per transfer needs to be different, too -
443 // 1ms is a minimum reasonable value
444 uii->iso_packets_multimix = cbox_config_get_int(cbox_io_section, "iso_packets_multimix", 16);
445 uii->output_resolution = cbox_config_get_int(cbox_io_section, "output_resolution", 16) / 8;
446 uii->output_channels = 2;
447 uii->handle_audiodev = NULL;
448 cbox_midi_merger_init(&uii->midi_input_merger, NULL);
450 // fixed processing buffer size, as we have to deal with packetisation anyway
451 io->io_env.srate = uii->sample_rate;
452 io->io_env.buffer_size = 64;
453 io->cb = NULL;
454 // input and output count is hardcoded for simplicity - in future, it may be
455 // necessary to add support for the extra inputs (needs to be figured out)
456 io->io_env.input_count = 2; //cbox_config_get_int("io", "inputs", 0);
457 io->input_buffers = malloc(sizeof(float *) * io->io_env.input_count);
458 for (int i = 0; i < io->io_env.input_count; i++)
459 io->input_buffers[i] = calloc(io->io_env.buffer_size, sizeof(float));
460 io->io_env.output_count = 2; // cbox_config_get_int("io", "outputs", 2);
461 io->output_buffers = malloc(sizeof(float *) * io->io_env.output_count);
462 for (int i = 0; i < io->io_env.output_count; i++)
463 io->output_buffers[i] = calloc(io->io_env.buffer_size, sizeof(float));
464 io->impl = &uii->ioi;
465 cbox_command_target_init(&io->cmd_target, cbox_usb_io_process_cmd, uii);
467 uii->ioi.pio = io;
468 uii->ioi.getsampleratefunc = cbox_usbio_get_sample_rate;
469 uii->ioi.startfunc = cbox_usbio_start;
470 uii->ioi.stopfunc = cbox_usbio_stop;
471 uii->ioi.getstatusfunc = cbox_usbio_get_status;
472 uii->ioi.pollfunc = cbox_usbio_poll_ports;
473 uii->ioi.cyclefunc = cbox_usbio_cycle;
474 uii->ioi.getmidifunc = cbox_usbio_get_midi_data;
475 uii->ioi.destroymidioutfunc = cbox_usbio_destroy_midi_out;
476 uii->ioi.destroyfunc = cbox_usbio_destroy;
477 uii->ioi.controltransportfunc = NULL;
478 uii->ioi.getsynccompletedfunc = NULL;
479 uii->ioi.updatemidiinroutingfunc = usbio_update_port_routing;
480 uii->midi_ports = NULL;
482 usbio_scan_devices(uii, FALSE);
484 if (cbox_config_get_int("io", "lockall", 0))
485 mlockall(MCL_CURRENT|MCL_FUTURE);
487 return TRUE;