From 9965f3bdcec286e1ffdf0f2ffb9ba36e0be82290 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Sun, 17 Mar 2019 14:12:08 +0000
Subject: [PATCH] Properly expose humanize-enable boolean control

And remove some obsolete state management code.
---
 plugin/hydrumkit.ttl | 10 +++++++
 plugin/plugin.c      | 31 +++++++-------------
 plugin/ports.h       |  3 +-
 plugin/ui.c          | 68 ++++++--------------------------------------
 plugin/uris.h        | 15 ----------
 5 files changed, 31 insertions(+), 96 deletions(-)

diff --git a/plugin/hydrumkit.ttl b/plugin/hydrumkit.ttl
index a63c9ab..bebb167 100644
--- a/plugin/hydrumkit.ttl
+++ b/plugin/hydrumkit.ttl
@@ -129,6 +129,16 @@
                 lv2:default 0.0 ;
                 lv2:minimum 0.0 ;
                 lv2:maximum 1.0 ;
+        ] , [
+                a lv2:InputPort ,
+                        lv2:ControlPort ;
+                lv2:index 10 ;
+                lv2:symbol "hu_enable" ;
+                lv2:name "Enable Humanizer" ;
+                lv2:portProperty lv2:toggled ;
+                lv2:default 0.0 ;
+                lv2:minimum 0.0 ;
+                lv2:maximum 1.0 ;
         ];
 	state:state [
 		<http://lv2.incal.net/plugins/hydrumkit#drumkit> </usr/share/hydrogen/data/drumkits/GMkit/drumkit.xml>
diff --git a/plugin/plugin.c b/plugin/plugin.c
index 1723695..73f1a3b 100644
--- a/plugin/plugin.c
+++ b/plugin/plugin.c
@@ -250,12 +250,20 @@ static void handle_event(struct sampler_plugin *plugin, LV2_Atom_Event *ev) {
 }
 
 static void update_hsettings(struct sampler_plugin *plugin) {
-    // Apply settings to sampler.
+  // Apply settings to sampler.
+  if (*plugin->input_ports[PORT_HUMANIZE_ENABLE] > 0) {
     plugin->hsettings.latency_max_ms = *plugin->input_ports[PORT_HUMANIZE_LATENCY_MAX];
     plugin->hsettings.latency_stddev_ms = *plugin->input_ports[PORT_HUMANIZE_LATENCY_STDDEV];
     plugin->hsettings.latency_laid_back_ms = *plugin->input_ports[PORT_HUMANIZE_LATENCY_LAID_BACK];
     plugin->hsettings.latency_regain = *plugin->input_ports[PORT_HUMANIZE_LATENCY_REGAIN];
     plugin->hsettings.velocity_stddev = *plugin->input_ports[PORT_HUMANIZE_VELOCITY_STDDEV];
+  } else {
+    plugin->hsettings.latency_max_ms = 0;
+    plugin->hsettings.latency_stddev_ms = 0;
+    plugin->hsettings.latency_laid_back_ms = 0;
+    plugin->hsettings.latency_regain = 1;
+    plugin->hsettings.velocity_stddev = 0;
+  }
 }
 
 static void render(struct sampler_plugin *plugin, uint32_t start,
@@ -277,26 +285,7 @@ static void run(LV2_Handle instance, uint32_t sample_count) {
 
   if (plugin->send_settings_to_ui) {
     plugin->send_settings_to_ui = 0;
-
-#if 0
-    // Forge container object of type 'ui_state'
-    LV2_Atom_Forge_Frame frame;
-    lv2_atom_forge_frame_time(&plugin->forge, 0);
-    lv2_atom_forge_object(&plugin->forge, &frame, 0, plugin->uris.ui_State);
-
-    // Add UI state as properties
-    lv2_atom_forge_key(&plugin->forge, plugin->uris.ui_hu_latency_max);
-    lv2_atom_forge_float(&plugin->forge, plugin->hsettings.latency_max_ms);
-    lv2_atom_forge_key(&plugin->forge, plugin->uris.ui_hu_latency_stddev);
-    lv2_atom_forge_float(&plugin->forge, plugin->hsettings.latency_stddev_ms);
-    lv2_atom_forge_key(&plugin->forge, plugin->uris.ui_hu_latency_laidback);
-    lv2_atom_forge_float(&plugin->forge, plugin->hsettings.latency_laid_back_ms);
-    lv2_atom_forge_key(&plugin->forge, plugin->uris.ui_hu_latency_regain);
-    lv2_atom_forge_float(&plugin->forge, plugin->hsettings.latency_regain);
-    lv2_atom_forge_key(&plugin->forge, plugin->uris.ui_hu_velocity_stddev);
-    lv2_atom_forge_float(&plugin->forge, plugin->hsettings.velocity_stddev);
-    lv2_atom_forge_pop(&plugin->forge, &frame);
-#endif
+    // Currently a no-op.
   }
 
   // Incrementally process control events and render audio data.
diff --git a/plugin/ports.h b/plugin/ports.h
index 2ffbfcd..4c56d2f 100644
--- a/plugin/ports.h
+++ b/plugin/ports.h
@@ -12,6 +12,7 @@
 #define PORT_HUMANIZE_LATENCY_LAID_BACK 7
 #define PORT_HUMANIZE_LATENCY_REGAIN 8
 #define PORT_HUMANIZE_VELOCITY_STDDEV 9
-#define PORT_LAST 10
+#define PORT_HUMANIZE_ENABLE 10
+#define PORT_LAST 11
 
 #endif
diff --git a/plugin/ui.c b/plugin/ui.c
index 9cf9f7c..9b072be 100644
--- a/plugin/ui.c
+++ b/plugin/ui.c
@@ -111,12 +111,12 @@ static void on_load_dialog_response(GtkDialog *widget, gint response, void *hand
   };
 }
 
-#define WRITECONTROLVALUE(expr, port) \
-  val = ui->humanize_enabled ? expr : 0; \
-  ui->write(ui->controller, port, sizeof(float), 0, (const void *)&val)
+#define WRITECONTROLVALUE(expr, port) {                                     \
+    float val_ = (expr);                                                    \
+    ui->write(ui->controller, port, sizeof(float), 0, (const void *)&val_); \
+  }
 
 static void write_control_values(struct plugin_ui *ui) {
-  float val;
   WRITECONTROLVALUE(ui->humanize_latency_max_value * 400.0, PORT_HUMANIZE_LATENCY_MAX);
   WRITECONTROLVALUE(ui->humanize_latency_stddev_value * 100.0, PORT_HUMANIZE_LATENCY_STDDEV);
   WRITECONTROLVALUE(ui->humanize_latency_laid_back_value* 100.0, PORT_HUMANIZE_LATENCY_LAID_BACK);
@@ -128,7 +128,7 @@ static void on_humanize_button_click(GtkButton *widget, void *handle) {
   struct plugin_ui *ui = (struct plugin_ui *)handle;
 
   ui->humanize_enabled = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
-  write_control_values(ui);
+  WRITECONTROLVALUE((float)ui->humanize_enabled, PORT_HUMANIZE_ENABLE);
 }
 
 static void on_humanize_knob_change(KnobWidget *widget, void *handle) {
@@ -282,56 +282,6 @@ static void cleanup(LV2UI_Handle handle) {
   free(ui);
 }
 
-static int recv_ui_state(struct plugin_ui *ui, const LV2_Atom_Object *obj) {
-#if 0
-  const LV2_Atom *hu_latency_max_val = NULL;
-  const LV2_Atom *hu_latency_stddev_val = NULL;
-  const LV2_Atom *hu_latency_laidback_val = NULL;
-  const LV2_Atom *hu_latency_regain_val = NULL;
-  const LV2_Atom *hu_velocity_stddev_val = NULL;
-  const int n_props = lv2_atom_object_get(obj,
-                                          ui->uris.ui_hu_latency_max, &hu_latency_max_val,
-                                          ui->uris.ui_hu_latency_stddev, &hu_latency_stddev_val,
-                                          ui->uris.ui_hu_latency_laidback, &hu_latency_laidback_val,
-                                          ui->uris.ui_hu_latency_regain, &hu_latency_regain_val,
-                                          ui->uris.ui_hu_velocity_stddev, &hu_velocity_stddev_val,
-                                          NULL);
-
-  if (n_props != 5 ||
-      hu_latency_max_val->type != ui->uris.atom_Float ||
-      hu_latency_stddev_val->type != ui->uris.atom_Float ||
-      hu_latency_laidback_val->type != ui->uris.atom_Float ||
-      hu_latency_regain_val->type != ui->uris.atom_Float ||
-      hu_velocity_stddev_val->type != ui->uris.atom_Float) {
-    // Object does not have the required properties with correct types.
-    return 0;
-  }
-
-  // Get the values we need from the body of the property value atoms.
-  ui->humanize_latency_max_value = 400 * ((const LV2_Atom_Float *)hu_latency_max_val)->body;
-  ui->humanize_latency_stddev_value = 100 * ((const LV2_Atom_Float *)hu_latency_stddev_val)->body;
-  ui->humanize_latency_laid_back_value = 100 * ((const LV2_Atom_Float *)hu_latency_laidback_val)->body;
-  ui->humanize_latency_regain_value = ((const LV2_Atom_Float *)hu_latency_regain_val)->body;
-  ui->humanize_velocity_stddev_value = ((const LV2_Atom_Float *)hu_velocity_stddev_val)->body;
-
-  printf("received UI state update, huLat=%g/%g/%g/%g, huVel=%g\n",
-         ui->humanize_latency_max_value,
-         ui->humanize_latency_stddev_value,
-         ui->humanize_latency_laid_back_value,
-         ui->humanize_latency_regain_value,
-         ui->humanize_velocity_stddev_value);
-  
-  // Update UI.
-  gtk_widget_queue_draw(ui->humanize_latency_max_knob);
-  gtk_widget_queue_draw(ui->humanize_latency_stddev_knob);
-  gtk_widget_queue_draw(ui->humanize_latency_laid_back_knob);
-  gtk_widget_queue_draw(ui->humanize_latency_regain_knob);
-  gtk_widget_queue_draw(ui->humanize_velocity_stddev_knob);
-#endif
-
-  return 1;
-}
-
 // The drumkit "name" that we show to the user is just the basename of
 // the drumkit directory.
 static char *get_drumkit_label(const char *filename) {
@@ -370,10 +320,6 @@ static void port_event(LV2UI_Handle handle, uint32_t port_index,
         } else if (!path) {
           lv2_log_warning(&ui->logger, "Set message has no path\n");
         }
-      } else if (obj->body.otype == ui->uris.ui_State) {
-        if (!recv_ui_state(ui, obj)) {
-          lv2_log_error(&ui->logger, "Error receiving UI state\n");
-        }
       }
     } else {
       lv2_log_error(&ui->logger, "Unknown message type\n");
@@ -398,6 +344,10 @@ static void port_event(LV2UI_Handle handle, uint32_t port_index,
     float val = *(float *)buffer;
     ui->humanize_velocity_stddev_value = val;
     gtk_widget_queue_draw(ui->humanize_velocity_stddev_knob);
+  } else if (port_index == PORT_HUMANIZE_ENABLE) {
+    float val = *(float *)buffer;
+    int enabled = (val > 0) ? 1 : 0;
+    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(ui->humanize_button), enabled);
   } else {
     lv2_log_warning(&ui->logger, "Unknown port event format\n");
   }
diff --git a/plugin/uris.h b/plugin/uris.h
index 656db0c..29f5aca 100644
--- a/plugin/uris.h
+++ b/plugin/uris.h
@@ -19,11 +19,6 @@
 #define EG_SAMPLER__freeDrumkit EG_SAMPLER_URI "#freeDrumkit"
 #define EG_SAMPLER__drumkit EG_SAMPLER_URI "#drumkit"
 #define EG_SAMPLER__uiState EG_SAMPLER_URI "#UiState"
-#define EG_SAMPLER__huLatencyMax EG_SAMPLER_URI "#hu-latency-max"
-#define EG_SAMPLER__huLatencyStdDev EG_SAMPLER_URI "#hu-latency-stddev"
-#define EG_SAMPLER__huLatencyLaidBack EG_SAMPLER_URI "#hu-latency-laidback"
-#define EG_SAMPLER__huLatencyRegain EG_SAMPLER_URI "#hu-latency-regain"
-#define EG_SAMPLER__huVelocityStdDev EG_SAMPLER_URI "#hu-velocity-stddev"
 
 typedef struct {
   LV2_URID atom_Float;
@@ -41,11 +36,6 @@ typedef struct {
   LV2_URID patch_property;
   LV2_URID patch_value;
   LV2_URID ui_State;
-  LV2_URID ui_hu_latency_max;
-  LV2_URID ui_hu_latency_stddev;
-  LV2_URID ui_hu_latency_laidback;
-  LV2_URID ui_hu_latency_regain;
-  LV2_URID ui_hu_velocity_stddev;
 } plugin_uris;
 
 static inline void map_sampler_uris(LV2_URID_Map *map, plugin_uris *uris) {
@@ -64,11 +54,6 @@ static inline void map_sampler_uris(LV2_URID_Map *map, plugin_uris *uris) {
   uris->patch_property = map->map(map->handle, LV2_PATCH__property);
   uris->patch_value = map->map(map->handle, LV2_PATCH__value);
   uris->ui_State = map->map(map->handle, EG_SAMPLER__uiState);
-  uris->ui_hu_latency_max = map->map(map->handle, EG_SAMPLER__huLatencyMax);
-  uris->ui_hu_latency_stddev = map->map(map->handle, EG_SAMPLER__huLatencyStdDev);
-  uris->ui_hu_latency_laidback = map->map(map->handle, EG_SAMPLER__huLatencyLaidBack);
-  uris->ui_hu_latency_regain = map->map(map->handle, EG_SAMPLER__huLatencyRegain);
-  uris->ui_hu_velocity_stddev = map->map(map->handle, EG_SAMPLER__huVelocityStdDev);
 }
 
 /**
-- 
GitLab