From 58a885c9e5e791b2bc260a14166819a4c898d91f Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Wed, 14 Dec 2016 19:32:04 -0500
Subject: [PATCH] report userful value for "dsp-status" method of [pdinfo] by
 ignorning suspend/resume states

---
 pd/src/g_canvas.c    | 20 ++++++++++++++++----
 pd/src/m_imp.h       |  1 +
 pd/src/m_pd.c        |  1 +
 pd/src/x_interface.c |  5 ++++-
 4 files changed, 22 insertions(+), 5 deletions(-)

diff --git a/pd/src/g_canvas.c b/pd/src/g_canvas.c
index f8b9fd7c3..1eaade008 100644
--- a/pd/src/g_canvas.c
+++ b/pd/src/g_canvas.c
@@ -1539,7 +1539,7 @@ static void canvas_dsp(t_canvas *x, t_signal **sp)
 }
 
     /* this routine starts DSP for all root canvases. */
-static void canvas_start_dsp(void)
+static void canvas_dostart_dsp(void)
 {
     t_canvas *x;
     if (pd_this->pd_dspstate)
@@ -1556,7 +1556,13 @@ static void canvas_start_dsp(void)
         pd_bang(gensym("pd-dsp-started")->s_thing);
 }
 
-static void canvas_stop_dsp(void)
+static void canvas_start_dsp(void)
+{
+    pd_this->pd_dspstate_user = 1;
+    canvas_dostart_dsp();
+}
+
+static void canvas_dostop_dsp(void)
 {
     if (pd_this->pd_dspstate)
     {
@@ -1568,6 +1574,12 @@ static void canvas_stop_dsp(void)
     }
 }
 
+static void canvas_stop_dsp(void)
+{
+    pd_this->pd_dspstate_user = 0;
+    canvas_dostop_dsp();
+}
+
     /* DSP can be suspended before, and resumed after, operations which
     might affect the DSP chain.  For example, we suspend before loading and
     resume afterward, so that DSP doesn't get resorted for every DSP object
@@ -1577,14 +1589,14 @@ int canvas_suspend_dsp(void)
 {
     //fprintf(stderr,"canvas_suspend_dsp %d\n", rval);
     int rval = pd_this->pd_dspstate;
-    if (rval) canvas_stop_dsp();
+    if (rval) canvas_dostop_dsp();
     return (rval);
 }
 
 void canvas_resume_dsp(int oldstate)
 {
     //fprintf(stderr,"canvas_resume_dsp %d\n", oldstate);
-    if (oldstate) canvas_start_dsp();
+    if (oldstate) canvas_dostart_dsp();
 }
 
     /* this is equivalent to suspending and resuming in one step. */
diff --git a/pd/src/m_imp.h b/pd/src/m_imp.h
index c54267124..9512a1abd 100644
--- a/pd/src/m_imp.h
+++ b/pd/src/m_imp.h
@@ -64,6 +64,7 @@ struct _pdinstance
     int pd_dspchainsize;        /* number of elements in DSP chain */
     t_canvas *pd_canvaslist;    /* list of all root canvases */
     int pd_dspstate;            /* whether DSP is on or off */
+    int pd_dspstate_user;       /* dsp state excluding temporary suspensions */
     t_signal *pd_signals;       /* list of signals used by DSP chain */
     t_symbol *pd_midiin_sym;    /* symbols bound to incoming MIDI... */
     t_symbol *pd_sysexin_sym;
diff --git a/pd/src/m_pd.c b/pd/src/m_pd.c
index 4732f7cf8..1bf74a280 100644
--- a/pd/src/m_pd.c
+++ b/pd/src/m_pd.c
@@ -469,6 +469,7 @@ static t_pdinstance *pdinstance_donew(int useprefix)
     x->pd_dspchainsize = 0;
     x->pd_canvaslist = 0;
     x->pd_dspstate = 0;
+    x->pd_dspstate_user = 0;
     x->pd_midiin_sym = midi_gensym(midiprefix, "#midiin");
     x->pd_sysexin_sym = midi_gensym(midiprefix, "#sysexin");
     x->pd_notein_sym = midi_gensym(midiprefix, "#notein");
diff --git a/pd/src/x_interface.c b/pd/src/x_interface.c
index 6724a4383..52faf175c 100644
--- a/pd/src/x_interface.c
+++ b/pd/src/x_interface.c
@@ -535,10 +535,13 @@ void pdinfo_dir(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
     info_out((t_text *)x, s, 1, at);
 }
 
+/* Instead of reporting the actual value for dsp when it's temporarily
+   suspended, we report what it will be when dsp is resumed. This way the
+   user can get a meaningful value at load time. */
 void pdinfo_dsp(t_pdinfo *x, t_symbol *s, int argc, t_atom *argv)
 {
     t_atom at[1];
-    SETFLOAT(at, (t_float)pd_getdspstate());
+    SETFLOAT(at, (t_float)(pd_this->pd_dspstate_user));
     info_out((t_text *)x, s, 1, at);
 }
 
-- 
GitLab