aboutsummaryrefslogtreecommitdiffstats
path: root/src/process.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2024-11-22 19:12:33 -0500
committerDavid Robillard <d@drobilla.net>2024-11-24 19:12:14 -0500
commit57006d3bf443f2ade18abe7021f8aa8a11b08bcb (patch)
treec61b54c1b8133e4b05f4250c1dac48a933815908 /src/process.c
parentacdbc427a6c6bb55e0073b6f6910543184f35177 (diff)
downloadjalv-57006d3bf443f2ade18abe7021f8aa8a11b08bcb.tar.gz
jalv-57006d3bf443f2ade18abe7021f8aa8a11b08bcb.tar.bz2
jalv-57006d3bf443f2ade18abe7021f8aa8a11b08bcb.zip
Cleanly separate audio thread from the rest of the application
Diffstat (limited to 'src/process.c')
-rw-r--r--src/process.c78
1 files changed, 34 insertions, 44 deletions
diff --git a/src/process.c b/src/process.c
index 3348877..efb2602 100644
--- a/src/process.c
+++ b/src/process.c
@@ -4,10 +4,8 @@
#include "process.h"
#include "comm.h"
-#include "jalv.h"
#include "log.h"
#include "lv2_evbuf.h"
-#include "port.h"
#include "types.h"
#include "worker.h"
@@ -28,13 +26,9 @@ ring_error(const char* const message)
}
static int
-apply_ui_events(Jalv* const jalv, const uint32_t nframes)
+apply_ui_events(JalvProcess* const proc, const uint32_t nframes)
{
- if (!jalv->has_ui) {
- return 0;
- }
-
- ZixRing* const ring = jalv->ui_to_plugin;
+ ZixRing* const ring = proc->ui_to_plugin;
JalvMessageHeader header = {NO_MESSAGE, 0U};
const size_t space = zix_ring_read_space(ring);
for (size_t i = 0; i < space; i += sizeof(header) + header.size) {
@@ -50,38 +44,37 @@ apply_ui_events(Jalv* const jalv, const uint32_t nframes)
return ring_error("Failed to read control value from UI ring\n");
}
- assert(msg.port_index < jalv->num_ports);
- jalv->controls_buf[msg.port_index] = msg.value;
+ assert(msg.port_index < proc->num_ports);
+ proc->controls_buf[msg.port_index] = msg.value;
} else if (header.type == EVENT_TRANSFER) {
- assert(header.size <= jalv->msg_buf_size);
- void* const body = jalv->audio_msg;
+ assert(header.size <= proc->process_msg_size);
+ void* const body = proc->process_msg;
if (zix_ring_read(ring, body, header.size) != header.size) {
return ring_error("Failed to read event from UI ring\n");
}
const JalvEventTransfer* const msg = (const JalvEventTransfer*)body;
- assert(msg->port_index < jalv->num_ports);
- JalvPort* const port = &jalv->ports[msg->port_index];
- LV2_Evbuf_Iterator e = lv2_evbuf_end(port->evbuf);
- const LV2_Atom* const atom = &msg->atom;
+ assert(msg->port_index < proc->num_ports);
+ JalvProcessPort* const port = &proc->ports[msg->port_index];
+ LV2_Evbuf_Iterator e = lv2_evbuf_end(port->evbuf);
+ const LV2_Atom* const atom = &msg->atom;
lv2_evbuf_write(
&e, nframes, 0U, atom->type, atom->size, LV2_ATOM_BODY_CONST(atom));
} else if (header.type == STATE_REQUEST) {
- JalvPort* const port = &jalv->ports[jalv->control_in];
+ JalvProcessPort* const port = &proc->ports[proc->control_in];
assert(port->type == TYPE_EVENT);
assert(port->flow == FLOW_INPUT);
assert(port->evbuf);
- LV2_Evbuf_Iterator iter = lv2_evbuf_end(port->evbuf);
- const LV2_Atom_Object get = {
- {sizeof(LV2_Atom_Object_Body), jalv->urids.atom_Object},
- {0U, jalv->urids.patch_Get},
- };
-
- lv2_evbuf_write(
- &iter, nframes, 0U, get.atom.type, get.atom.size, &get.body);
+ LV2_Evbuf_Iterator e = lv2_evbuf_end(port->evbuf);
+ lv2_evbuf_write(&e,
+ nframes,
+ 0U,
+ proc->get_msg.atom.type,
+ proc->get_msg.atom.size,
+ &proc->get_msg.body);
} else if (header.type == RUN_STATE_CHANGE) {
assert(header.size == sizeof(JalvRunStateChange));
@@ -90,9 +83,9 @@ apply_ui_events(Jalv* const jalv, const uint32_t nframes)
return ring_error("Failed to read run state change from UI ring\n");
}
- jalv->run_state = msg.state;
+ proc->run_state = msg.state;
if (msg.state == JALV_PAUSED) {
- zix_sem_post(&jalv->paused);
+ zix_sem_post(&proc->paused);
}
} else {
@@ -104,37 +97,34 @@ apply_ui_events(Jalv* const jalv, const uint32_t nframes)
}
bool
-jalv_run(Jalv* const jalv, const uint32_t nframes)
+jalv_run(JalvProcess* const proc, const uint32_t nframes)
{
// Read and apply control change events from UI
- apply_ui_events(jalv, nframes);
+ apply_ui_events(proc, nframes);
// Run plugin for this cycle
- lilv_instance_run(jalv->instance, nframes);
+ lilv_instance_run(proc->instance, nframes);
// Process any worker replies and end the cycle
- LV2_Handle handle = lilv_instance_get_handle(jalv->instance);
- jalv_worker_emit_responses(jalv->state_worker, handle);
- jalv_worker_emit_responses(jalv->worker, handle);
- jalv_worker_end_run(jalv->worker);
+ LV2_Handle handle = lilv_instance_get_handle(proc->instance);
+ jalv_worker_emit_responses(proc->state_worker, handle);
+ jalv_worker_emit_responses(proc->worker, handle);
+ jalv_worker_end_run(proc->worker);
// Check if it's time to send updates to the UI
- jalv->event_delta_t += nframes;
- bool send_ui_updates = false;
- const uint32_t update_frames =
- (uint32_t)(jalv->settings.sample_rate / jalv->settings.ui_update_hz);
- if (jalv->has_ui && (jalv->event_delta_t > update_frames)) {
- send_ui_updates = true;
- jalv->event_delta_t = 0U;
+ proc->pending_frames += nframes;
+ if (proc->update_frames && proc->pending_frames > proc->update_frames) {
+ proc->pending_frames = 0U;
+ return true;
}
- return send_ui_updates;
+ return false;
}
int
-jalv_bypass(Jalv* const jalv, const uint32_t nframes)
+jalv_bypass(JalvProcess* const proc, const uint32_t nframes)
{
// Read and apply control change events from UI
- apply_ui_events(jalv, nframes);
+ apply_ui_events(proc, nframes);
return 0;
}