diff options
Diffstat (limited to 'src/jalv.c')
-rw-r--r-- | src/jalv.c | 536 |
1 files changed, 22 insertions, 514 deletions
@@ -40,15 +40,6 @@ #include "jalv_config.h" #include "jalv_internal.h" -#include <jack/jack.h> -#include <jack/midiport.h> -#ifdef JALV_JACK_SESSION -# include <jack/session.h> -#endif -#ifdef HAVE_JACK_METADATA -# include <jack/metadata.h> -#endif - #include "lv2/lv2plug.in/ns/ext/atom/atom.h" #include "lv2/lv2plug.in/ns/ext/buf-size/buf-size.h" #include "lv2/lv2plug.in/ns/ext/data-access/data-access.h" @@ -84,12 +75,6 @@ # define MAX(a, b) (((a) > (b)) ? (a) : (b)) #endif -#ifdef __clang__ -# define REALTIME __attribute__((annotate("realtime"))) -#else -# define REALTIME -#endif - /* Size factor for UI ring buffers. The ring size is a few times the size of an event output to give the UI a chance to keep up. Experiments with Ingen, which can highly saturate its event output, led me to this value. It @@ -208,7 +193,7 @@ create_port(Jalv* jalv, struct Port* const port = &jalv->ports[port_index]; port->lilv_port = lilv_plugin_get_port_by_index(jalv->plugin, port_index); - port->jack_port = NULL; + port->sys_port = NULL; port->evbuf = NULL; port->buf_size = 0; port->index = port_index; @@ -301,7 +286,7 @@ jalv_create_ports(Jalv* jalv) /** Allocate port buffers (only necessary for MIDI). */ -static void +void jalv_allocate_port_buffers(Jalv* jalv) { for (uint32_t i = 0; i < jalv->num_ports; ++i) { @@ -368,81 +353,6 @@ print_control_value(Jalv* jalv, const struct Port* port, float value) printf("%-*s = %f\n", jalv->longest_sym, lilv_node_as_string(sym), value); } -/** - Expose a port to Jack (if applicable) and connect it to its buffer. -*/ -static void -activate_port(Jalv* jalv, - uint32_t port_index) -{ - struct Port* const port = &jalv->ports[port_index]; - - const LilvNode* sym = lilv_port_get_symbol(jalv->plugin, port->lilv_port); - - /* Connect unsupported ports to NULL (known to be optional by this point) */ - if (port->flow == FLOW_UNKNOWN || port->type == TYPE_UNKNOWN) { - lilv_instance_connect_port(jalv->instance, port_index, NULL); - return; - } - - /* Build Jack flags for port */ - enum JackPortFlags jack_flags = (port->flow == FLOW_INPUT) - ? JackPortIsInput - : JackPortIsOutput; - - /* Connect the port based on its type */ - switch (port->type) { - case TYPE_CONTROL: - lilv_instance_connect_port(jalv->instance, port_index, &port->control); - break; - case TYPE_AUDIO: - port->jack_port = jack_port_register( - jalv->jack_client, lilv_node_as_string(sym), - JACK_DEFAULT_AUDIO_TYPE, jack_flags, 0); - break; -#ifdef HAVE_JACK_METADATA - case TYPE_CV: - port->jack_port = jack_port_register( - jalv->jack_client, lilv_node_as_string(sym), - JACK_DEFAULT_AUDIO_TYPE, jack_flags, 0); - if (port->jack_port) { - jack_set_property(jalv->jack_client, jack_port_uuid(port->jack_port), - "http://jackaudio.org/metadata/signal-type", "CV", - "text/plain"); - } - break; -#endif - case TYPE_EVENT: - if (lilv_port_supports_event( - jalv->plugin, port->lilv_port, jalv->nodes.midi_MidiEvent)) { - port->jack_port = jack_port_register( - jalv->jack_client, lilv_node_as_string(sym), - JACK_DEFAULT_MIDI_TYPE, jack_flags, 0); - } - break; - default: - break; - } - -#ifdef HAVE_JACK_METADATA - if (port->jack_port) { - // Set port order to index - char index_str[16]; - snprintf(index_str, sizeof(index_str), "%d", port_index); - jack_set_property(jalv->jack_client, jack_port_uuid(port->jack_port), - "http://jackaudio.org/metadata/order", index_str, - "http://www.w3.org/2001/XMLSchema#integer"); - - // Set port pretty name to label - LilvNode* name = lilv_port_get_name(jalv->plugin, port->lilv_port); - jack_set_property(jalv->jack_client, jack_port_uuid(port->jack_port), - JACK_METADATA_PRETTY_NAME, lilv_node_as_string(name), - "text/plain"); - lilv_node_free(name); - } -#endif -} - void jalv_create_controls(Jalv* jalv, bool writable) { @@ -532,350 +442,6 @@ jalv_set_control(const ControlID* control, } } - -/** Jack buffer size callback. */ -static int -jack_buffer_size_cb(jack_nframes_t nframes, void* data) -{ - Jalv* const jalv = (Jalv*)data; - jalv->block_length = nframes; - jalv->buf_size_set = true; -#ifdef HAVE_JACK_PORT_TYPE_GET_BUFFER_SIZE - jalv->midi_buf_size = jack_port_type_get_buffer_size( - jalv->jack_client, JACK_DEFAULT_MIDI_TYPE); -#endif - jalv_allocate_port_buffers(jalv); - return 0; -} - -/** Jack shutdown callback. */ -static void -jack_shutdown_cb(void* data) -{ - Jalv* const jalv = (Jalv*)data; - jalv_close_ui(jalv); - zix_sem_post(jalv->done); -} - -/** Jack process callback. */ -static REALTIME int -jack_process_cb(jack_nframes_t nframes, void* data) -{ - Jalv* const jalv = (Jalv*)data; - - /* Get Jack transport position */ - jack_position_t pos; - const bool rolling = (jack_transport_query(jalv->jack_client, &pos) - == JackTransportRolling); - - /* If transport state is not as expected, then something has changed */ - const bool xport_changed = (rolling != jalv->rolling || - pos.frame != jalv->position || - pos.beats_per_minute != jalv->bpm); - - uint8_t pos_buf[256]; - LV2_Atom* lv2_pos = (LV2_Atom*)pos_buf; - if (xport_changed) { - /* Build an LV2 position object to report change to plugin */ - lv2_atom_forge_set_buffer(&jalv->forge, pos_buf, sizeof(pos_buf)); - LV2_Atom_Forge* forge = &jalv->forge; - LV2_Atom_Forge_Frame frame; - lv2_atom_forge_object(forge, &frame, 0, jalv->urids.time_Position); - lv2_atom_forge_key(forge, jalv->urids.time_frame); - lv2_atom_forge_long(forge, pos.frame); - lv2_atom_forge_key(forge, jalv->urids.time_speed); - lv2_atom_forge_float(forge, rolling ? 1.0 : 0.0); - if (pos.valid & JackPositionBBT) { - lv2_atom_forge_key(forge, jalv->urids.time_barBeat); - lv2_atom_forge_float( - forge, pos.beat - 1 + (pos.tick / pos.ticks_per_beat)); - lv2_atom_forge_key(forge, jalv->urids.time_bar); - lv2_atom_forge_long(forge, pos.bar - 1); - lv2_atom_forge_key(forge, jalv->urids.time_beatUnit); - lv2_atom_forge_int(forge, pos.beat_type); - lv2_atom_forge_key(forge, jalv->urids.time_beatsPerBar); - lv2_atom_forge_float(forge, pos.beats_per_bar); - lv2_atom_forge_key(forge, jalv->urids.time_beatsPerMinute); - lv2_atom_forge_float(forge, pos.beats_per_minute); - } - - if (jalv->opts.dump) { - char* str = sratom_to_turtle( - jalv->sratom, &jalv->unmap, "time:", NULL, NULL, - lv2_pos->type, lv2_pos->size, LV2_ATOM_BODY(lv2_pos)); - jalv_ansi_start(stdout, 36); - printf("\n## Position ##\n%s\n", str); - jalv_ansi_reset(stdout); - free(str); - } - } - - /* Update transport state to expected values for next cycle */ - jalv->position = rolling ? pos.frame + nframes : pos.frame; - jalv->bpm = pos.beats_per_minute; - jalv->rolling = rolling; - - switch (jalv->play_state) { - case JALV_PAUSE_REQUESTED: - jalv->play_state = JALV_PAUSED; - zix_sem_post(&jalv->paused); - break; - case JALV_PAUSED: - for (uint32_t p = 0; p < jalv->num_ports; ++p) { - jack_port_t* jport = jalv->ports[p].jack_port; - if (jport && jalv->ports[p].flow == FLOW_OUTPUT) { - void* buf = jack_port_get_buffer(jport, nframes); - if (jalv->ports[p].type == TYPE_EVENT) { - jack_midi_clear_buffer(buf); - } else { - memset(buf, '\0', nframes * sizeof(float)); - } - } - } - return 0; - default: - break; - } - - /* Prepare port buffers */ - for (uint32_t p = 0; p < jalv->num_ports; ++p) { - struct Port* port = &jalv->ports[p]; - if (port->type == TYPE_AUDIO && port->jack_port) { - /* Connect plugin port directly to Jack port buffer */ - lilv_instance_connect_port( - jalv->instance, p, - jack_port_get_buffer(port->jack_port, nframes)); -#ifdef HAVE_JACK_METADATA - } else if (port->type == TYPE_CV && port->jack_port) { - /* Connect plugin port directly to Jack port buffer */ - lilv_instance_connect_port( - jalv->instance, p, - jack_port_get_buffer(port->jack_port, nframes)); -#endif - } else if (port->type == TYPE_EVENT && port->flow == FLOW_INPUT) { - lv2_evbuf_reset(port->evbuf, true); - - /* Write transport change event if applicable */ - LV2_Evbuf_Iterator iter = lv2_evbuf_begin(port->evbuf); - if (xport_changed) { - lv2_evbuf_write(&iter, 0, 0, - lv2_pos->type, lv2_pos->size, - (const uint8_t*)LV2_ATOM_BODY(lv2_pos)); - } - - if (jalv->request_update) { - /* Plugin state has changed, request an update */ - const LV2_Atom_Object get = { - { sizeof(LV2_Atom_Object_Body), jalv->urids.atom_Object }, - { 0, jalv->urids.patch_Get } }; - lv2_evbuf_write(&iter, 0, 0, - get.atom.type, get.atom.size, - (const uint8_t*)LV2_ATOM_BODY(&get)); - } - - if (port->jack_port) { - /* Write Jack MIDI input */ - void* buf = jack_port_get_buffer(port->jack_port, nframes); - for (uint32_t i = 0; i < jack_midi_get_event_count(buf); ++i) { - jack_midi_event_t ev; - jack_midi_event_get(&ev, buf, i); - lv2_evbuf_write(&iter, - ev.time, 0, - jalv->midi_event_id, - ev.size, ev.buffer); - } - } - } else if (port->type == TYPE_EVENT) { - /* Clear event output for plugin to write to */ - lv2_evbuf_reset(port->evbuf, false); - } - } - jalv->request_update = false; - - /* Read and apply control change events from UI */ - if (jalv->has_ui) { - ControlChange ev; - const size_t space = jack_ringbuffer_read_space(jalv->ui_events); - for (size_t i = 0; i < space; i += sizeof(ev) + ev.size) { - jack_ringbuffer_read(jalv->ui_events, (char*)&ev, sizeof(ev)); - char body[ev.size]; - if (jack_ringbuffer_read(jalv->ui_events, body, ev.size) != ev.size) { - fprintf(stderr, "error: Error reading from UI ring buffer\n"); - break; - } - assert(ev.index < jalv->num_ports); - struct Port* const port = &jalv->ports[ev.index]; - if (ev.protocol == 0) { - assert(ev.size == sizeof(float)); - port->control = *(float*)body; - } else if (ev.protocol == jalv->urids.atom_eventTransfer) { - LV2_Evbuf_Iterator e = lv2_evbuf_end(port->evbuf); - const LV2_Atom* const atom = (const LV2_Atom*)body; - lv2_evbuf_write(&e, nframes, 0, atom->type, atom->size, - (const uint8_t*)LV2_ATOM_BODY_CONST(atom)); - } else { - fprintf(stderr, "error: Unknown control change protocol %d\n", - ev.protocol); - } - } - } - - /* Run plugin for this cycle */ - lilv_instance_run(jalv->instance, nframes); - - /* Process any worker replies. */ - jalv_worker_emit_responses(&jalv->state_worker, jalv->instance); - jalv_worker_emit_responses(&jalv->worker, jalv->instance); - - /* Notify the plugin the run() cycle is finished */ - if (jalv->worker.iface && jalv->worker.iface->end_run) { - jalv->worker.iface->end_run(jalv->instance->lv2_handle); - } - - /* Check if it's time to send updates to the UI */ - jalv->event_delta_t += nframes; - bool send_ui_updates = false; - jack_nframes_t update_frames = jalv->sample_rate / jalv->ui_update_hz; - if (jalv->has_ui && (jalv->event_delta_t > update_frames)) { - send_ui_updates = true; - jalv->event_delta_t = 0; - } - - /* Deliver MIDI output and UI events */ - for (uint32_t p = 0; p < jalv->num_ports; ++p) { - struct Port* const port = &jalv->ports[p]; - if (port->flow == FLOW_OUTPUT && port->type == TYPE_CONTROL && - lilv_port_has_property(jalv->plugin, port->lilv_port, - jalv->nodes.lv2_reportsLatency)) { - if (jalv->plugin_latency != port->control) { - jalv->plugin_latency = port->control; - jack_recompute_total_latencies(jalv->jack_client); - } - } - - if (port->flow == FLOW_OUTPUT && port->type == TYPE_EVENT) { - void* buf = NULL; - if (port->jack_port) { - buf = jack_port_get_buffer(port->jack_port, nframes); - jack_midi_clear_buffer(buf); - } - - for (LV2_Evbuf_Iterator i = lv2_evbuf_begin(port->evbuf); - lv2_evbuf_is_valid(i); - i = lv2_evbuf_next(i)) { - uint32_t frames, subframes, type, size; - uint8_t* body; - lv2_evbuf_get(i, &frames, &subframes, &type, &size, &body); - if (buf && type == jalv->midi_event_id) { - jack_midi_event_write(buf, frames, body, size); - } - - /* TODO: Be more disciminate about what to send */ - if (jalv->has_ui && !port->old_api) { - char evbuf[sizeof(ControlChange) + sizeof(LV2_Atom)]; - ControlChange* ev = (ControlChange*)evbuf; - ev->index = p; - ev->protocol = jalv->urids.atom_eventTransfer; - ev->size = sizeof(LV2_Atom) + size; - LV2_Atom* atom = (LV2_Atom*)ev->body; - atom->type = type; - atom->size = size; - if (jack_ringbuffer_write_space(jalv->plugin_events) - < sizeof(evbuf) + size) { - fprintf(stderr, "Plugin => UI buffer overflow!\n"); - break; - } - jack_ringbuffer_write(jalv->plugin_events, evbuf, sizeof(evbuf)); - /* TODO: race, ensure reader handles this correctly */ - jack_ringbuffer_write(jalv->plugin_events, (const char*)body, size); - } - } - } else if (send_ui_updates - && port->flow != FLOW_INPUT - && port->type == TYPE_CONTROL) { - char buf[sizeof(ControlChange) + sizeof(float)]; - ControlChange* ev = (ControlChange*)buf; - ev->index = p; - ev->protocol = 0; - ev->size = sizeof(float); - *(float*)ev->body = port->control; - if (jack_ringbuffer_write(jalv->plugin_events, buf, sizeof(buf)) - < sizeof(buf)) { - fprintf(stderr, "Plugin => UI buffer overflow!\n"); - } - } - } - - return 0; -} - -/** Calculate latency assuming all ports depend on each other. */ -static void -jack_latency_cb(jack_latency_callback_mode_t mode, void* data) -{ - Jalv* const jalv = (Jalv*)data; - const enum PortFlow flow = ((mode == JackCaptureLatency) - ? FLOW_INPUT : FLOW_OUTPUT); - - /* First calculate the min/max latency of all feeding ports */ - uint32_t ports_found = 0; - jack_latency_range_t range = { UINT32_MAX, 0 }; - for (uint32_t p = 0; p < jalv->num_ports; ++p) { - struct Port* port = &jalv->ports[p]; - if (port->jack_port && port->flow == flow) { - jack_latency_range_t r; - jack_port_get_latency_range(port->jack_port, mode, &r); - if (r.min < range.min) { range.min = r.min; } - if (r.max > range.max) { range.max = r.max; } - ++ports_found; - } - } - - if (ports_found == 0) { - range.min = 0; - } - - /* Add the plugin's own latency */ - range.min += jalv->plugin_latency; - range.max += jalv->plugin_latency; - - /* Tell Jack about it */ - for (uint32_t p = 0; p < jalv->num_ports; ++p) { - struct Port* port = &jalv->ports[p]; - if (port->jack_port && port->flow == flow) { - jack_port_set_latency_range(port->jack_port, mode, &range); - } - } -} - -#ifdef JALV_JACK_SESSION -static void -jack_session_cb(jack_session_event_t* event, void* arg) -{ - Jalv* const jalv = (Jalv*)arg; - - #define MAX_CMD_LEN 256 - event->command_line = (char*)malloc(MAX_CMD_LEN); - snprintf(event->command_line, MAX_CMD_LEN, "%s -u %s -l \"${SESSION_DIR}\"", - jalv->prog_name, - event->client_uuid); - - switch (event->type) { - case JackSessionSave: - case JackSessionSaveTemplate: - jalv_save(jalv, event->session_dir); - break; - case JackSessionSaveAndQuit: - jalv_save(jalv, event->session_dir); - jalv_close_ui(jalv); - break; - } - - jack_session_reply(jalv->jack_client, event); - jack_session_event_free(event); -} -#endif /* JALV_JACK_SESSION */ - void jalv_ui_instantiate(Jalv* jalv, const char* native_ui_type, void* parent) { @@ -986,7 +552,7 @@ jalv_ui_write(SuilController controller, ev->protocol = protocol; ev->size = buffer_size; memcpy(ev->body, buffer, buffer_size); - jack_ringbuffer_write(jalv->ui_events, buf, sizeof(buf)); + zix_ring_write(jalv->ui_events, buf, sizeof(buf)); } uint32_t @@ -1039,19 +605,19 @@ jalv_update(Jalv* jalv) /* Emit UI events. */ ControlChange ev; - const size_t space = jack_ringbuffer_read_space(jalv->plugin_events); + const size_t space = zix_ring_read_space(jalv->plugin_events); for (size_t i = 0; i + sizeof(ev) + sizeof(float) <= space; i += sizeof(ev) + ev.size) { /* Read event header to get the size */ - jack_ringbuffer_read(jalv->plugin_events, (char*)&ev, sizeof(ev)); + zix_ring_read(jalv->plugin_events, (char*)&ev, sizeof(ev)); /* Resize read buffer if necessary */ jalv->ui_event_buf = realloc(jalv->ui_event_buf, ev.size); void* const buf = jalv->ui_event_buf; /* Read event body */ - jack_ringbuffer_read(jalv->plugin_events, (char*)buf, ev.size); + zix_ring_read(jalv->plugin_events, (char*)buf, ev.size); if (jalv->opts.dump && ev.protocol == jalv->urids.atom_eventTransfer) { /* Dump event in Turtle to the console */ @@ -1380,57 +946,11 @@ main(int argc, char** argv) jalv_create_controls(&jalv, true); jalv_create_controls(&jalv, false); - /* Determine the name of the JACK client */ - char* jack_name = NULL; - if (jalv.opts.name) { - /* Name given on command line */ - jack_name = jalv_strdup(jalv.opts.name); - } else { - /* Use plugin name */ - LilvNode* name = lilv_plugin_get_name(jalv.plugin); - jack_name = jalv_strdup(lilv_node_as_string(name)); - lilv_node_free(name); - } - - /* Truncate client name to suit JACK if necessary */ - if (strlen(jack_name) >= (unsigned)jack_client_name_size() - 1) { - jack_name[jack_client_name_size() - 1] = '\0'; - } - - /* Connect to JACK */ - printf("JACK Name: %s\n", jack_name); -#ifdef JALV_JACK_SESSION - if (jalv.opts.uuid) { - jalv.jack_client = jack_client_open( - jack_name, - (jack_options_t)(JackSessionID | - (jalv.opts.name_exact ? JackUseExactName : 0)), - NULL, - jalv.opts.uuid); - } -#endif - - if (!jalv.jack_client) { - jalv.jack_client = jack_client_open( - jack_name, - (jalv.opts.name_exact ? JackUseExactName : JackNullOption), - NULL); + if (!(jalv.backend = jalv_backend_init(&jalv))) { + die("Failed to connect to audio system"); } - free(jack_name); - - if (!jalv.jack_client) - die("Failed to connect to JACK.\n"); - - jalv.sample_rate = jack_get_sample_rate(jalv.jack_client); - jalv.block_length = jack_get_buffer_size(jalv.jack_client); -#ifdef HAVE_JACK_PORT_TYPE_GET_BUFFER_SIZE - jalv.midi_buf_size = jack_port_type_get_buffer_size( - jalv.jack_client, JACK_DEFAULT_MIDI_TYPE); -#else - jalv.midi_buf_size = 4096; - fprintf(stderr, "warning: No jack_port_type_get_buffer_size.\n"); -#endif + printf("Sample rate: %u Hz\n", jalv.sample_rate); printf("Block length: %u frames\n", jalv.block_length); printf("MIDI buffers: %zu bytes\n", jalv.midi_buf_size); @@ -1478,10 +998,10 @@ main(int argc, char** argv) options_feature.data = (void*)&options; /* Create Plugin <=> UI communication buffers */ - jalv.ui_events = jack_ringbuffer_create(jalv.opts.buffer_size); - jalv.plugin_events = jack_ringbuffer_create(jalv.opts.buffer_size); - jack_ringbuffer_mlock(jalv.ui_events); - jack_ringbuffer_mlock(jalv.plugin_events); + jalv.ui_events = zix_ring_new(jalv.opts.buffer_size); + jalv.plugin_events = zix_ring_new(jalv.opts.buffer_size); + zix_ring_mlock(jalv.ui_events); + zix_ring_mlock(jalv.plugin_events); /* Instantiate the plugin */ jalv.instance = lilv_plugin_instantiate( @@ -1521,22 +1041,11 @@ main(int argc, char** argv) } /* Set Jack callbacks */ - jack_set_process_callback(jalv.jack_client, - &jack_process_cb, (void*)(&jalv)); - jack_set_buffer_size_callback(jalv.jack_client, - &jack_buffer_size_cb, (void*)(&jalv)); - jack_on_shutdown(jalv.jack_client, - &jack_shutdown_cb, (void*)(&jalv)); - jack_set_latency_callback(jalv.jack_client, - &jack_latency_cb, (void*)(&jalv)); -#ifdef JALV_JACK_SESSION - jack_set_session_callback(jalv.jack_client, - &jack_session_cb, (void*)(&jalv)); -#endif + jalv_backend_init(&jalv); /* Create Jack ports and connect plugin ports to buffers */ for (uint32_t i = 0; i < jalv.num_ports; ++i) { - activate_port(&jalv, i); + jalv_backend_activate_port(&jalv, i); } /* Print initial control values */ @@ -1555,9 +1064,8 @@ main(int argc, char** argv) jalv.has_ui = jalv_discover_ui(&jalv); /* Activate Jack */ - jack_activate(jalv.jack_client); - jalv.sample_rate = jack_get_sample_rate(jalv.jack_client); - jalv.play_state = JALV_RUNNING; + jalv_backend_activate(&jalv); + jalv.play_state = JALV_RUNNING; /* Run UI (or prompt at console) */ jalv_open_ui(&jalv); @@ -1571,14 +1079,14 @@ main(int argc, char** argv) /* Terminate the worker */ jalv_worker_finish(&jalv.worker); - /* Deactivate JACK */ - jack_deactivate(jalv.jack_client); + /* Deactivate audio */ + jalv_backend_deactivate(&jalv); for (uint32_t i = 0; i < jalv.num_ports; ++i) { if (jalv.ports[i].evbuf) { lv2_evbuf_free(jalv.ports[i].evbuf); } } - jack_client_close(jalv.jack_client); + jalv_backend_close(&jalv); /* Deactivate plugin */ suil_instance_free(jalv.ui_instance); @@ -1587,8 +1095,8 @@ main(int argc, char** argv) /* Clean up */ free(jalv.ports); - jack_ringbuffer_free(jalv.ui_events); - jack_ringbuffer_free(jalv.plugin_events); + zix_ring_free(jalv.ui_events); + zix_ring_free(jalv.plugin_events); for (LilvNode** n = (LilvNode**)&jalv.nodes; *n; ++n) { lilv_node_free(*n); } |