aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2024-11-20 16:14:04 -0500
committerDavid Robillard <d@drobilla.net>2024-11-24 19:11:32 -0500
commit724aab7a868ed0200afbeecf056e53b5ea16b23d (patch)
tree1f18f1d9185465b253c4de21112e05aeb6739e2f
parent9ff22388ddcb5ae22aed51302889098cb246a47c (diff)
downloadjalv-724aab7a868ed0200afbeecf056e53b5ea16b23d.tar.gz
jalv-724aab7a868ed0200afbeecf056e53b5ea16b23d.tar.bz2
jalv-724aab7a868ed0200afbeecf056e53b5ea16b23d.zip
Simplify port buffer allocation
-rw-r--r--src/jack.c5
-rw-r--r--src/jalv.c28
-rw-r--r--src/jalv.h5
-rw-r--r--src/lv2_evbuf.c4
4 files changed, 24 insertions, 18 deletions
diff --git a/src/jack.c b/src/jack.c
index 28a4d08..a5f26c6 100644
--- a/src/jack.c
+++ b/src/jack.c
@@ -52,12 +52,13 @@ jack_buffer_size_cb(jack_nframes_t nframes, void* data)
{
Jalv* const jalv = (Jalv*)data;
jalv->block_length = nframes;
- jalv->buf_size_set = true;
#if USE_JACK_PORT_TYPE_GET_BUFFER_SIZE
jalv->midi_buf_size = jack_port_type_get_buffer_size(jalv->backend->client,
JACK_DEFAULT_MIDI_TYPE);
#endif
- jalv_allocate_port_buffers(jalv);
+ if (jalv->run_state == JALV_RUNNING) {
+ jalv_allocate_port_buffers(jalv);
+ }
return 0;
}
diff --git a/src/jalv.c b/src/jalv.c
index 734172b..82674a0 100644
--- a/src/jalv.c
+++ b/src/jalv.c
@@ -219,21 +219,28 @@ jalv_allocate_port_buffers(Jalv* jalv)
for (uint32_t i = 0; i < jalv->num_ports; ++i) {
JalvPort* const port = &jalv->ports[i];
if (port->type == TYPE_EVENT) {
- lv2_evbuf_free(port->evbuf);
-
const size_t size = port->buf_size ? port->buf_size : jalv->midi_buf_size;
+ lv2_evbuf_free(port->evbuf);
port->evbuf =
lv2_evbuf_new(size, urids->atom_Chunk, urids->atom_Sequence);
+ lv2_evbuf_reset(port->evbuf, port->flow == FLOW_INPUT);
lilv_instance_connect_port(
jalv->instance, i, lv2_evbuf_get_buffer(port->evbuf));
-
- lv2_evbuf_reset(port->evbuf, port->flow == FLOW_INPUT);
}
}
}
+void
+jalv_free_port_buffers(Jalv* const jalv)
+{
+ for (uint32_t i = 0; i < jalv->num_ports; ++i) {
+ lv2_evbuf_free(jalv->ports[i].evbuf);
+ lilv_instance_connect_port(jalv->instance, i, NULL);
+ }
+}
+
/**
Get a port structure by symbol.
@@ -1017,9 +1024,7 @@ jalv_open(Jalv* const jalv, int* argc, char*** argv)
jalv->state_worker, worker_iface, jalv->instance->lv2_handle);
jalv_log(JALV_LOG_INFO, "\n");
- if (!jalv->buf_size_set) {
- jalv_allocate_port_buffers(jalv);
- }
+ jalv_allocate_port_buffers(jalv);
// Apply loaded state to plugin instance if necessary
if (state) {
@@ -1080,18 +1085,13 @@ jalv_deactivate(Jalv* const jalv)
int
jalv_close(Jalv* const jalv)
{
+ // Stop audio processing, free event port buffers, and close backend
jalv_deactivate(jalv);
+ jalv_free_port_buffers(jalv);
if (jalv->backend) {
jalv_backend_close(jalv);
}
- // Free event port buffers
- for (uint32_t i = 0; i < jalv->num_ports; ++i) {
- if (jalv->ports[i].evbuf) {
- lv2_evbuf_free(jalv->ports[i].evbuf);
- }
- }
-
// Destroy the worker
jalv_worker_free(jalv->worker);
jalv_worker_free(jalv->state_worker);
diff --git a/src/jalv.h b/src/jalv.h
index e4d7d90..7caf8a6 100644
--- a/src/jalv.h
+++ b/src/jalv.h
@@ -86,7 +86,6 @@ struct JalvImpl {
uint32_t position; ///< Transport position in frames
float bpm; ///< Transport tempo in beats per minute
bool rolling; ///< Transport speed (0=stop, 1=play)
- bool buf_size_set; ///< True iff buffer size callback fired
bool has_ui; ///< True iff a control UI is present
bool safe_restore; ///< Plugin restore() is thread-safe
JalvFeatures features;
@@ -113,6 +112,10 @@ jalv_deactivate(Jalv* jalv);
void
jalv_allocate_port_buffers(Jalv* jalv);
+/// Clean up memory allocated by jalv_process_activate() and disconnect plugin
+void
+jalv_free_port_buffers(Jalv* jalv);
+
/// Find a port by symbol
JalvPort*
jalv_port_by_symbol(Jalv* jalv, const char* sym);
diff --git a/src/lv2_evbuf.c b/src/lv2_evbuf.c
index 054e2a1..cdb5dc2 100644
--- a/src/lv2_evbuf.c
+++ b/src/lv2_evbuf.c
@@ -50,7 +50,9 @@ lv2_evbuf_new(uint32_t capacity, uint32_t atom_Chunk, uint32_t atom_Sequence)
void
lv2_evbuf_free(LV2_Evbuf* evbuf)
{
- free(evbuf);
+ if (evbuf) {
+ free(evbuf);
+ }
}
void