From 4fc2ea3d3ca4cbff734140a290f3a1a500cb5404 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Fri, 14 Aug 2015 12:06:32 -0400
Subject: [PATCH] ported bugfix from pd-l2ork github:
 78ee42bb822d21c017401400de638055a3b2cc5f

---
 externals/ggee/gui/envgen.c | 49 ++++++++++++++++++++++++++-----------
 1 file changed, 35 insertions(+), 14 deletions(-)

diff --git a/externals/ggee/gui/envgen.c b/externals/ggee/gui/envgen.c
index 79346fc39..f1826e9eb 100644
--- a/externals/ggee/gui/envgen.c
+++ b/externals/ggee/gui/envgen.c
@@ -2,7 +2,8 @@
 
 #include <m_pd.h>
 
-//#define DEBUG
+#define DEBUG(x)
+//#define DEBUG(x) x
 /* ------------------------ envgen~ ----------------------------- */
 
 #define NONE    0
@@ -37,6 +38,7 @@ char dumpy[2000];
 
 void envgen_resize(t_envgen* x,int ns)
 {
+    DEBUG(post("envgen_resize"););
      if (ns > x->args) {
 	  int newargs = ns*sizeof(t_float); 
 
@@ -50,12 +52,13 @@ void envgen_resize(t_envgen* x,int ns)
 
 void envgen_totaldur(t_envgen* x,t_float dur)
 {
+    DEBUG(post("envgen_totaldur"););
      int i;
      float f = dur/x->duration[x->last_state];
 
      if (dur < 10) {
-	post("envgen: duration too small %f",dur);
-        return;
+         pd_error(x, "envgen: duration too small %f",dur);
+         return;
      }
 
      for (i=1;i<=x->last_state;i++)
@@ -65,6 +68,7 @@ void envgen_totaldur(t_envgen* x,t_float dur)
 
 static void envgen_dump(t_envgen* e)
 {
+    DEBUG(post("envgen_dump"););
      t_atom argv[50];
      int argc= 0;
      t_atom* a = argv;
@@ -83,6 +87,7 @@ static void envgen_dump(t_envgen* e)
 
 void envgen_init(t_envgen *x,int argc,t_atom* argv)
 {
+    DEBUG(post("envgen_init"););
      t_float* dur;
      t_float* val;
      t_float tdur = 0;
@@ -104,18 +109,14 @@ void envgen_init(t_envgen *x,int argc,t_atom* argv)
      dur++;val++;argc--;
      for (;argc > 0;argc--) {
 	  tdur += atom_getfloat(argv++);
-#ifdef DEBUG
-	  post("dur =%f",tdur);
-#endif
+	  DEBUG(post("dur =%f",tdur););
 	  *dur++ = tdur; 
 	  argc--;
 	  if (argc > 0)
 	       *val++ = atom_getfloat(argv++); 
 	  else
 	       *val++ = 0; 
-#ifdef DEBUG
-	  post("val =%f",*(val-1));
-#endif
+      DEBUG(post("val =%f",*(val-1)););
 
      }
 
@@ -127,6 +128,7 @@ void envgen_init(t_envgen *x,int argc,t_atom* argv)
 
 void envgen_list(t_envgen *x,t_symbol* s, int argc,t_atom* argv)
 {
+    DEBUG(post("envgen_list"););
      envgen_init(x,argc,argv);
      if (glist_isvisible(x->w.glist)) {
 	  envgen_drawme(x, x->w.glist, 0);
@@ -135,12 +137,14 @@ void envgen_list(t_envgen *x,t_symbol* s, int argc,t_atom* argv)
 
 void envgen_setresize(t_envgen *x, t_floatarg f)
 {
+    DEBUG(post("envgen_setresize"););
      x->resizeable = f;
 }
 
 
 void envgen_float(t_envgen *x, t_floatarg f)
 {
+    DEBUG(post("envgen_float"););
      int state = 0;
      float val;
 
@@ -166,6 +170,7 @@ void envgen_float(t_envgen *x, t_floatarg f)
 
 void envgen_bang(t_envgen *x)
 {
+    DEBUG(post("envgen_bang"););
      t_atom   a[2];
 
      SETFLOAT(a,x->finalvalues[NONE]);
@@ -188,6 +193,7 @@ void envgen_bang(t_envgen *x)
 }
 
 void envgen_release(t_envgen* x) {
+    DEBUG(post("envgen_release"););
      t_atom a[2];
      float del = x->duration[x->x_state] - x->duration[x->x_state-1];
      if (x->x_state <= x->sustain_state) {
@@ -201,6 +207,7 @@ void envgen_release(t_envgen* x) {
 
 static void envgen_sustain(t_envgen *x, t_floatarg f)
 {
+    DEBUG(post("envgen_sustain"););
      if (f > 0 && f < x->last_state) 
         x->sustain_state = f;
      else
@@ -210,6 +217,7 @@ static void envgen_sustain(t_envgen *x, t_floatarg f)
 
 static void envgen_tick(t_envgen* x)
 {
+    DEBUG(post("envgen_tick"););
      t_atom a[2];
      x->x_state++;
      if (x->x_state <= x->last_state && x->x_state != x->sustain_state) {
@@ -225,27 +233,34 @@ static void envgen_tick(t_envgen* x)
 
 static void envgen_freeze(t_envgen* x, t_floatarg f)
 {
+    DEBUG(post("envgen_freeze"););
      x->x_freeze = f;
 }
 
 
 static void bindsym(t_pd* x,t_symbol* o,t_symbol* s)
 {
+    DEBUG(post("bindsym"););
      if (o != &s_) pd_unbind(x,o);
      o = s;
      pd_bind(x,s);
 }
 
+static void envgen_free(t_envgen* x)
+{
+    clock_free(x->x_clock);
+}
+
 static void *envgen_new(t_symbol *s,int argc,t_atom* argv)
 {
+    DEBUG(post("envgen_new"););
      t_envgen *x = (t_envgen *)pd_new(envgen_class);
      
      x->args = STATES;
      x->finalvalues = getbytes( x->args*sizeof(t_float));
      x->duration = getbytes( x->args*sizeof(t_float));
-#ifdef DEBUG
-     post("finalvalues %x",x->finalvalues);
-#endif
+     DEBUG(post("finalvalues %lx",x->finalvalues););
+     
      /* widget */
      
      x->w.grabbed = 0;
@@ -315,8 +330,14 @@ t_widgetbehavior envgen_widgetbehavior;
 
 void envgen_setup(void)
 {
-    envgen_class = class_new(gensym("envgen"), (t_newmethod)envgen_new, 0,
-    	sizeof(t_envgen), 0,A_GIMME,0);
+    DEBUG(post("envgen_setup"););
+    envgen_class = class_new(gensym("envgen"),
+                             (t_newmethod)envgen_new,
+                             (t_method) envgen_free,
+                             sizeof(t_envgen),
+                             0,
+                             A_GIMME,
+                             0);
 
     class_addcreator((t_newmethod)envgen_new,gensym("envgen~"),A_GIMME,0);
     class_addfloat(envgen_class, envgen_float);
-- 
GitLab