From e7af05eead88b20314e3f1e2131b0b6861c41c1a Mon Sep 17 00:00:00 2001 From: Paolo Date: Mon, 16 Jun 2025 12:05:29 +0200 Subject: [PATCH] lv2 messaging: send/receive work --- templates/lv2/src/lv2.c | 123 ++++++++++++++++------------------------ test/plugin.h | 10 ++-- test/plugin_ui.h | 12 ++-- 3 files changed, 60 insertions(+), 85 deletions(-) diff --git a/templates/lv2/src/lv2.c b/templates/lv2/src/lv2.c index f5d4fc3..e3abe35 100644 --- a/templates/lv2/src/lv2.c +++ b/templates/lv2/src/lv2.c @@ -203,10 +203,9 @@ static void state_set_parameter_cb(void *handle, size_t index, float value) { #ifdef DATA_MESSAGING static char send_to_ui (void *handle, const void *data, size_t bytes) { + plugin_instance *i = (plugin_instance *) handle; - plugin_instance * i = (plugin_instance *) handle; - - if (!data || bytes <= 0 || bytes > DATA_MESSAGING_MAX) { + if (!data || bytes == 0 || bytes > DATA_MESSAGING_MAX) { return 1; } @@ -403,31 +402,31 @@ static void activate(LV2_Handle instance) { static void run(LV2_Handle instance, uint32_t sample_count) { plugin_instance * i = (plugin_instance *)instance; - if (i->control) { - const LV2_Atom_Event* ev = lv2_atom_sequence_begin(&(i->control)->body); - while (!lv2_atom_sequence_is_end(&i->control->body, i->control->atom.size, ev)) { - if (lv2_atom_forge_is_object_type(&i->forge, ev->body.type)) { - const LV2_Atom_Object* obj = (const LV2_Atom_Object*)&ev->body; +#ifdef DATA_MESSAGING + LV2_ATOM_SEQUENCE_FOREACH(i->control, ev) { + if (!lv2_atom_forge_is_object_type(&i->forge, ev->body.type)) + continue; - printf("audio - received object, otype: %d\n", obj->body.otype); + const LV2_Atom_Object* obj = (const LV2_Atom_Object*)&ev->body; - const LV2_Atom* msg_atom = NULL; + if (obj->body.otype != i->c_uris.prop_customMessage) + continue; - lv2_atom_object_get(obj, i->c_uris.prop_customMessage, &msg_atom, 0); + const LV2_Atom* msg_length = NULL; + const LV2_Atom* msg_data = NULL; - printf("audio - custom msg %d \n", i->c_uris.prop_customMessage); - printf("audio - %p \n", (void*) msg_atom); - printf("audio - %d %d \n", msg_atom->type, i->c_uris.atom_String); + lv2_atom_object_get(obj, + i->c_uris.length, + &msg_length, + i->c_uris.prop_customMessage, + &msg_data, 0); - if (msg_atom && msg_atom->type == i->c_uris.atom_String) { - const char* msg_str = (const char*)(msg_atom + 1); - printf("audio - received string from UI: %s\n", msg_str); - } - } - ev = lv2_atom_sequence_next(ev); - } + size_t *length = (size_t*) LV2_ATOM_BODY(msg_length); + uint8_t *data = (uint8_t*) msg_data; // LV2_ATOM_BODY(msg_data); + + plugin_receive_from_ui(&i->p, data, *length); } - +#endif #if defined(__aarch64__) uint64_t fpcr; @@ -678,16 +677,12 @@ static void ui_set_parameter_end_cb(void *handle, size_t index, float value) { static char send_to_dsp (void *handle, const void *data, size_t bytes) { ui_instance* instance = (ui_instance*) handle; - printf("send_to_dsp A \n"); fflush(stdout); - - if (!data || bytes <= 0 || bytes > DATA_MESSAGING_MAX) { + if (!data || bytes == 0 || bytes > DATA_MESSAGING_MAX) { return 1; } lv2_atom_forge_set_buffer(&instance->forge, instance->obj_buf, sizeof(instance->obj_buf)); - lv2_atom_forge_sequence_head(&instance->forge, &instance->frame, 0); - lv2_atom_forge_frame_time(&instance->forge, 0); LV2_Atom* msg = (LV2_Atom*) lv2_atom_forge_object(&instance->forge, &instance->frame, 0, instance->c_uris.prop_customMessage); lv2_atom_forge_key(&instance->forge, instance->c_uris.length); @@ -698,16 +693,6 @@ static char send_to_dsp (void *handle, const void *data, size_t bytes) { lv2_atom_forge_pop(&instance->forge, &instance->frame); - -/* - LV2_Atom* msg = (LV2_Atom*)lv2_atom_forge_object(&instance->forge, &instance->frame, 0, instance->c_uris.ui_On); - assert(msg); - - lv2_atom_forge_key(&instance->forge, instance->c_uris.prop_customMessage); - lv2_atom_forge_string(&instance->forge, data, bytes); -*/ - - // Send the forged atom to the host instance->write(instance->controller, DATA_MESSAGING_PORT_IN, lv2_atom_total_size(msg), instance->c_uris.atom_eventTransfer, msg); return 0; @@ -797,53 +782,43 @@ static void ui_cleanup(LV2UI_Handle handle) { free(instance); } -# if DATA_PRODUCT_CONTROL_INPUTS_N + DATA_PRODUCT_CONTROL_OUTPUTS_N > 0 +# if DATA_PRODUCT_CONTROL_INPUTS_N + DATA_PRODUCT_CONTROL_OUTPUTS_N > 0 || defined(DATA_MESSAGING) static void ui_port_event(LV2UI_Handle handle, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void * buffer) { - (void)buffer_size; - ui_instance *instance = (ui_instance *)handle; + + if (format == 0 && buffer_size == sizeof(float)) { # if DATA_PRODUCT_CONTROL_INPUTS_N > 0 - if (port_index < CONTROL_OUTPUT_INDEX_OFFSET) { - size_t index = port_index - CONTROL_INPUT_INDEX_OFFSET; - plugin_ui_set_parameter(instance->ui, param_data[index].index, adjust_param(index, *((float *)buffer))); - } + if (port_index < CONTROL_OUTPUT_INDEX_OFFSET) { + size_t index = port_index - CONTROL_INPUT_INDEX_OFFSET; + plugin_ui_set_parameter(instance->ui, param_data[index].index, adjust_param(index, *((float *)buffer))); + } # endif # if DATA_PRODUCT_CONTROL_OUTPUTS_N > 0 # if DATA_PRODUCT_CONTROL_INPUTS_N > 0 - else + else # endif - plugin_ui_set_parameter(instance->ui, param_out_index[port_index - CONTROL_OUTPUT_INDEX_OFFSET], *((float *)buffer)); + plugin_ui_set_parameter(instance->ui, param_out_index[port_index - CONTROL_OUTPUT_INDEX_OFFSET], *((float *)buffer)); # endif - - const LV2_Atom* atom = (const LV2_Atom*)buffer; - - /* Check type of data received - * - format == 0: Control port event (float) - * - format > 0: Message (atom) - */ - - if (format == instance->c_uris.atom_eventTransfer && lv2_atom_forge_is_object_type(&instance->forge, atom->type)) { - const LV2_Atom_Object* obj = (const LV2_Atom_Object*)atom; - if (obj->body.otype == instance->c_uris.prop_customMessage) { - printf("received something from audio INCREDIBLE \n"); - const LV2_Atom* len_a = NULL; - const LV2_Atom* data_a = NULL; - const int n_props = lv2_atom_object_get(obj, instance->c_uris.length, &len_a, instance->c_uris.prop_customMessage, &data_a, NULL); - const int32_t len = ((const LV2_Atom_Int*) len_a)->body; - printf("and n_props is %d \n", n_props); - printf("and len is %d \n", len); - printf("and data_a is %p \n", (void*)data_a); - //printf("and data_a type: %u, size: %u \n", data_a->type, data_a->size); - const uint8_t* data = (uint8_t*) data_a; - printf("data... \n"); - for (int x = 0; x < len; x++) { - printf("%u ", data[x]); - } - printf("\n"); - } } + else if (format == instance->c_uris.atom_eventTransfer) { + const LV2_Atom* atom = (const LV2_Atom*)buffer; + + if (lv2_atom_forge_is_object_type(&instance->forge, atom->type)) { + const LV2_Atom_Object* obj = (const LV2_Atom_Object*)atom; + if (obj->body.otype == instance->c_uris.prop_customMessage) { + const LV2_Atom* len_a = NULL; + const LV2_Atom* data_a = NULL; + lv2_atom_object_get(obj, + instance->c_uris.length, &len_a, + instance->c_uris.prop_customMessage, &data_a, NULL); + const int32_t len = ((const LV2_Atom_Int*) len_a)->body; + const uint8_t* data = (uint8_t*) data_a; + plugin_ui_receive_from_dsp(instance->ui, data, len); + } + } + } } # endif @@ -864,7 +839,7 @@ static const LV2UI_Descriptor ui_descriptor = { /* .URI = */ DATA_LV2_UI_URI, /* .instantiate = */ ui_instantiate, /* .cleanup = */ ui_cleanup, -# if DATA_PRODUCT_CONTROL_INPUTS_N + DATA_PRODUCT_CONTROL_OUTPUTS_N > 0 +# if DATA_PRODUCT_CONTROL_INPUTS_N + DATA_PRODUCT_CONTROL_OUTPUTS_N > 0 || defined(DATA_MESSAGING) /* .port_event = */ ui_port_event, # else /* .port_event = */ NULL, diff --git a/test/plugin.h b/test/plugin.h index 6a3ce73..df79ee0 100644 --- a/test/plugin.h +++ b/test/plugin.h @@ -43,15 +43,15 @@ typedef struct plugin { #if TEMPLATE_SUPPORTS_MESSAGING #include static void plugin_receive_from_ui (plugin *instance, const void *data, size_t bytes) { - printf("plugin_receive_from_ui %ld bytes at %p: \n", bytes, data); + printf("plugin_receive_from_ui %ld bytes at %p: ", bytes, data); for (size_t i = 0; i < bytes; i++) { - printf("%d ", ((uint8_t*) data)[i]); + printf("%c", ((uint8_t*) data)[i]); } instance->communication_state = 1; printf("\nplugin_receive_from_ui END - going to reply at next proc \n"); } -#define RANDOM_DATA_UI_SIZE 13 -const uint8_t random_ui_data[RANDOM_DATA_UI_SIZE] = { 66, 69, 2, 3, 4, 5, 6, 7, 8, 9, 6, 9, 6 }; +#define RANDOM_DATA_SIZE 7 +const uint8_t random_data[RANDOM_DATA_SIZE] = { 'w', 'o', 'r', 'l', 'd', '!', 0 }; #endif @@ -132,7 +132,7 @@ static void plugin_process(plugin *instance, const float **inputs, float **outpu } #ifdef TEMPLATE_SUPPORTS_MESSAGING if (instance->communication_state == 1) { - instance->cbs.send_to_ui(instance->cbs.handle, random_ui_data, RANDOM_DATA_UI_SIZE); + instance->cbs.send_to_ui(instance->cbs.handle, random_data, RANDOM_DATA_SIZE); instance->communication_state = 2; } #endif diff --git a/test/plugin_ui.h b/test/plugin_ui.h index 75f72e1..79fd6c5 100644 --- a/test/plugin_ui.h +++ b/test/plugin_ui.h @@ -41,14 +41,14 @@ typedef struct { #ifdef TEMPLATE_SUPPORTS_MESSAGING static void plugin_ui_receive_from_dsp (plugin_ui *instance, const void *data, size_t bytes) { (void) instance; - printf("plugin_ui_receive_from_dsp %ld bytes at %p: \n", bytes, data); + printf("plugin_ui_receive_from_dsp %ld bytes at %p: ", bytes, data); for (size_t i = 0; i < bytes; i++) { - printf("%d ", ((uint8_t*) data)[i]); + printf("%c", ((uint8_t*) data)[i]); } - printf("plugin_ui_receive_from_dsp END \n"); + printf("\nplugin_ui_receive_from_dsp END \n"); } -#define RANDOM_DATA_SIZE 11 -const uint8_t random_data[RANDOM_DATA_SIZE] = { 2, 3, 4, 5, 6, 7, 8, 9, 6, 9, 6 }; +#define RANDOM_UI_DATA_SIZE 6 +const uint8_t random_ui_data[RANDOM_UI_DATA_SIZE] = { 'h', 'e', 'l', 'l', 'o', 0 }; #endif #define WIDTH 600.0 @@ -126,7 +126,7 @@ static void on_mouse_release (window *win, int32_t x, int32_t y, uint32_t state) pui->cbs.set_parameter(pui->cbs.handle, 3, pui->bypass ? 1.f : 0.f); draw_button(pui, 3, pui->bypass); #if TEMPLATE_SUPPORTS_MESSAGING - pui->cbs.send_to_dsp(pui->cbs.handle, (const void*) random_data, RANDOM_DATA_SIZE); + pui->cbs.send_to_dsp(pui->cbs.handle, (const void*) random_ui_data, RANDOM_UI_DATA_SIZE); #endif }