diff options
author | David Robillard <d@drobilla.net> | 2024-11-17 20:34:23 -0500 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2024-11-24 19:07:47 -0500 |
commit | 3d28124c93ac8a9e4ad5e244b64cc02daac5e430 (patch) | |
tree | c4bfeff3fbb280486e06e61e52175f9b1e0cc470 /src | |
parent | f80653bc457aef835cd49ad68a5b7fa53db262f9 (diff) | |
download | jalv-3d28124c93ac8a9e4ad5e244b64cc02daac5e430.tar.gz jalv-3d28124c93ac8a9e4ad5e244b64cc02daac5e430.tar.bz2 jalv-3d28124c93ac8a9e4ad5e244b64cc02daac5e430.zip |
Clean up backend allocation and setup
Separate the concerns of backend allocation and initialization, and always
handle allocation at the top level. This avoids multiple confusing paths of
backend allocation and configuration in the internal Jack backend.
Diffstat (limited to 'src')
-rw-r--r-- | src/backend.h | 20 | ||||
-rw-r--r-- | src/jack.c | 47 | ||||
-rw-r--r-- | src/jalv.c | 15 | ||||
-rw-r--r-- | src/portaudio.c | 28 |
4 files changed, 66 insertions, 44 deletions
diff --git a/src/backend.h b/src/backend.h index fe2fdf3..9cb6508 100644 --- a/src/backend.h +++ b/src/backend.h @@ -12,9 +12,21 @@ // Interface that must be implemented by audio/MIDI backends JALV_BEGIN_DECLS -/// Initialize the audio and MIDI systems +/// Allocate a new uninitialized backend JalvBackend* -jalv_backend_init(Jalv* jalv); +jalv_backend_allocate(void); + +/// Free a backend allocated with jalv_backend_allocate() +void +jalv_backend_free(JalvBackend* backend); + +/// Open the audio/MIDI system +int +jalv_backend_open(Jalv* jalv); + +/// Close the audio/MIDI system +void +jalv_backend_close(Jalv* jalv); /// Activate the backend and start processing audio void @@ -24,10 +36,6 @@ jalv_backend_activate(Jalv* jalv); void jalv_backend_deactivate(Jalv* jalv); -/// Close the backend -void -jalv_backend_close(Jalv* jalv); - /// Expose a port to the system (if applicable) and connect it to its buffer void jalv_backend_activate_port(Jalv* jalv, uint32_t port_index); @@ -342,13 +342,25 @@ jack_create_client(Jalv* jalv) } JalvBackend* -jalv_backend_init(Jalv* jalv) +jalv_backend_allocate(void) +{ + return (JalvBackend*)calloc(1, sizeof(JalvBackend)); +} + +void +jalv_backend_free(JalvBackend* const backend) +{ + free(backend); +} + +int +jalv_backend_open(Jalv* jalv) { jack_client_t* const client = - jalv->backend ? jalv->backend->client : jack_create_client(jalv); + jalv->backend->client ? jalv->backend->client : jack_create_client(jalv); if (!client) { - return NULL; + return 1; } jalv_log(JALV_LOG_INFO, "JACK name: %s\n", jack_get_client_name(client)); @@ -369,29 +381,17 @@ jalv_backend_init(Jalv* jalv) jack_on_shutdown(client, &jack_shutdown_cb, arg); jack_set_latency_callback(client, &jack_latency_cb, arg); - if (jalv->backend) { - /* Internal JACK client, jalv->backend->is_internal_client was already set - in jack_initialize() when allocating the backend. */ - return jalv->backend; - } - - // External JACK client, allocate and return opaque backend - JalvBackend* backend = (JalvBackend*)calloc(1, sizeof(JalvBackend)); - backend->client = client; - backend->is_internal_client = false; - return backend; + jalv->backend->client = client; + jalv->backend->is_internal_client = false; + return 0; } void jalv_backend_close(Jalv* jalv) { - if (jalv->backend) { - if (!jalv->backend->is_internal_client) { - jack_client_close(jalv->backend->client); - } - - free(jalv->backend); - jalv->backend = NULL; + if (jalv->backend && jalv->backend->client && + !jalv->backend->is_internal_client) { + jack_client_close(jalv->backend->client); } } @@ -404,7 +404,7 @@ jalv_backend_activate(Jalv* jalv) void jalv_backend_deactivate(Jalv* jalv) { - if (!jalv->backend->is_internal_client) { + if (!jalv->backend->is_internal_client && jalv->backend->client) { jack_deactivate(jalv->backend->client); } } @@ -514,7 +514,7 @@ jack_initialize(jack_client_t* const client, const char* const load_init) return ENOMEM; } - if (!(jalv->backend = (JalvBackend*)calloc(1, sizeof(JalvBackend)))) { + if (!(jalv->backend = jalv_backend_allocate())) { free(jalv); return ENOMEM; } @@ -573,6 +573,7 @@ jack_finish(void* const arg) jalv_log(JALV_LOG_ERR, "Failed to close Jalv\n"); } + jalv_backend_free(jalv->backend); free(jalv); } } @@ -966,7 +966,7 @@ jalv_open(Jalv* const jalv, int* argc, char*** argv) jalv_create_controls(jalv, true); jalv_create_controls(jalv, false); - if (!(jalv->backend = jalv_backend_init(jalv))) { + if (jalv_backend_open(jalv)) { jalv_log(JALV_LOG_ERR, "Failed to connect to audio system\n"); return -6; } @@ -1093,10 +1093,12 @@ jalv_deactivate(Jalv* const jalv) { if (jalv->backend) { jalv_backend_deactivate(jalv); + } + if (jalv->instance) { lilv_instance_deactivate(jalv->instance); - if (jalv->worker) { - jalv_worker_exit(jalv->worker); - } + } + if (jalv->worker) { + jalv_worker_exit(jalv->worker); } jalv->run_state = JALV_PAUSED; @@ -1181,6 +1183,7 @@ main(int argc, char** argv) { Jalv jalv; memset(&jalv, '\0', sizeof(Jalv)); + jalv.backend = jalv_backend_allocate(); // Initialize application if (jalv_open(&jalv, &argc, &argv)) { @@ -1200,5 +1203,7 @@ main(int argc, char** argv) // Deactivate audio processing and tear down application jalv_deactivate(&jalv); - return jalv_close(&jalv); + const int ret = jalv_close(&jalv); + jalv_backend_free(jalv.backend); + return ret; } diff --git a/src/portaudio.c b/src/portaudio.c index c1ea759..6a4316d 100644 --- a/src/portaudio.c +++ b/src/portaudio.c @@ -104,16 +104,28 @@ pa_process_cb(const void* inputs, return paContinue; } -static JalvBackend* +static int pa_error(const char* msg, PaError err) { jalv_log(JALV_LOG_ERR, "%s (%s)\n", msg, Pa_GetErrorText(err)); Pa_Terminate(); - return NULL; + return 1; } JalvBackend* -jalv_backend_init(Jalv* jalv) +jalv_backend_allocate(void) +{ + return (JalvBackend*)calloc(1, sizeof(JalvBackend)); +} + +void +jalv_backend_free(JalvBackend* const backend) +{ + free(backend); +} + +int +jalv_backend_open(Jalv* jalv) { PaStreamParameters inputParameters; PaStreamParameters outputParameters; @@ -177,18 +189,14 @@ jalv_backend_init(Jalv* jalv) // jalv->block_length = FIXME jalv->midi_buf_size = 4096; - // Allocate and return opaque backend - JalvBackend* backend = (JalvBackend*)calloc(1, sizeof(JalvBackend)); - backend->stream = stream; - return backend; + jalv->backend->stream = stream; + return 0; } void -jalv_backend_close(Jalv* jalv) +jalv_backend_close(Jalv* ZIX_UNUSED(jalv)) { Pa_Terminate(); - free(jalv->backend); - jalv->backend = NULL; } void |