Changes to make DPat able to run multiple instances
authorMark Slee <mcslee@Mark-Slees-MacBook-Pro.local>
Wed, 25 Sep 2013 01:17:11 +0000 (18:17 -0700)
committerMark Slee <mcslee@Mark-Slees-MacBook-Pro.local>
Wed, 25 Sep 2013 01:17:11 +0000 (18:17 -0700)
DanUtil.pde
_Internals.pde

index c27eaed0adacc2c26cd541c499f0d2d7899b5911..9f4b3a03e4abf43efe159af38136cd58b723afc4 100644 (file)
@@ -1,7 +1,6 @@
 //----------------------------------------------------------------------------------------------------------------------------------
 float          xdMax,ydMax,zdMax;
 int                    NumApcRows = 5, NumApcCols = 8;
-DGlobals       DG = new DGlobals();
 
 boolean btwn  (int             a,int    b,int          c)              { return a >= b && a <= c;      }
 boolean btwn  (double  a,double b,double       c)              { return a >= b && a <= c;      }
@@ -64,6 +63,18 @@ public class xyz {   float x,y,z;
 }
 //----------------------------------------------------------------------------------------------------------------------------------
 public class DGlobals {
+       
+       DGlobals() {
+               midiEngine.addListener(new MidiEngineListener() {
+                       public void onFocusedDeck(int deckIndex) {
+                               if (isFocused()) {
+                                       UpdateLights();
+                                       SetText();
+                               }
+                       }
+               });
+       }
+       
        boolean         bInit                   = false;
        MidiOutput      APCOut                  = null;
        MidiInput       APCIn                   = null,         OxygenIn                = null;
@@ -90,19 +101,21 @@ public class DGlobals {
        float   _Spark          ()                                              { return Sliders[6]; }
        float   _Wiggle         ()                                              { return Sliders[7]; }
 
+       boolean isFocused       () {
+               return CurPat == midiEngine.getFocusedDeck().getActivePattern();
+       }
+
        void    Init            () {
                if (bInit) return; bInit=true;
            for (MidiOutputDevice output : RWMidi.getOutputDevices()) {
                        if (APCOut == null && output.toString().contains("APC")) APCOut = output.createOutput();
                }
-
-               for (MidiInputDevice  input  : RWMidi.getInputDevices ()) {
-                       if (input.toString().contains("APC")) input.createInput (this);
-               }
        }
 
        void SetText()
        {
+               if (!isFocused()) return;
+               
                Text1 = ""; Text2 = "";
                Text1  += " XSym:  " + (_XSym ? "ON" : "OFF") + "    ";
                Text1  += " YSym:  " + (_YSym ? "ON" : "OFF") + "    ";
@@ -119,13 +132,13 @@ public class DGlobals {
                uiDebugText.setText(Text1, Text2);
        }
 
-       void    controllerChangeReceived(rwmidi.Controller cc) {
-               if (cc.getCC() == 7 && btwn(cc.getChannel(),0,7)) { Sliders[cc.getChannel()] = 1.*cc.getValue()/127.; }
-               else if (cc.getCC() == 15 && cc.getChannel() == 0) {
-                       lx.engine.getDeck(1).getCrossfader().setValue( 1.*cc.getValue()/127.);
+       public boolean  controllerChangeReceived(rwmidi.Controller cc) {
+               if (cc.getCC() == 7 && btwn(cc.getChannel(),0,7)) {
+                       Sliders[cc.getChannel()] = 1.*cc.getValue()/127.;
+                       return true;
                }
-
                //else { println(cc.getCC() + " " + cc.getChannel() + " " + cc.getValue()); }
+               return false;
        }
 
        void    Deactivate (DPat p) { if (p == CurPat) { uiDebugText.setText(""); CurPat = NextPat; } NextPat = null; }
@@ -137,6 +150,8 @@ public class DGlobals {
        }
 
        void    UpdateLights() {
+               if (!isFocused()) return;
+               
                for (int i=0; i<NumApcRows      ; i++) for (int j=0; j<NumApcCols; j++) SetLight(i, j, 0);
                for (int i=48;i< 56             ; i++) SetKnob(0, i, 0);
                for (int i=16;i< 20             ; i++) SetKnob(0, i, 0);
@@ -156,43 +171,49 @@ public class DGlobals {
        
        double Tap1 = 0;
        double getNow() { return millis() + 1000*second() + 60*1000*minute() + 3600*1000*hour(); }
-       void noteOffReceived(Note note) {
-               if (CurPat == null) return;
-               int row = DG.mapRow(note.getPitch()), col = note.getChannel();
+       public boolean noteOffReceived(Note note) {
+               if (CurPat == null) return false;
+               int row = mapRow(note.getPitch()), col = note.getChannel();
+               boolean consumed = false;
 
                if (row == 50 && col == 0 && btwn(getNow() - Tap1,5000,300*1000)) {     // hackish tapping mechanism
                        double bpm = 32.*60000./(getNow()-Tap1);
                        while (bpm < 20) bpm*=2;
                        while (bpm > 40) bpm/=2;
                        lx.tempo.setBpm(bpm); lx.tempo.trigger(); Tap1=0; println("Tap Set - " + bpm + " bpm");
+                       consumed = true;
                }
 
                UpdateLights();
+               return consumed;
        }
 
-       void noteOnReceived (Note note) {
-               if (CurPat == null) return;
+       public boolean noteOnReceived (Note note) {
+               if (CurPat == null) return false;
                int row = mapRow(note.getPitch()), col = note.getChannel();
                
-                        if (row == 50 && col == 0)     { lx.tempo.trigger(); Tap1 = getNow();  }
-               else if (row == 82 && col == 0)         _XSym = !_XSym  ;
-               else if (row == 83 && col == 0)         _YSym = !_YSym  ;
-               else if (row == 84 && col == 0)         _ZSym = !_ZSym  ;
-               else if (row == 85 && col == 0)         _RSym = !_RSym  ;
+                        if (row == 50 && col == 0)     { lx.tempo.trigger(); Tap1 = getNow(); return true;     }
+               else if (row == 82 && col == 0)         { _XSym = !_XSym        ; return true; }
+               else if (row == 83 && col == 0)         { _YSym = !_YSym        ; return true; }
+               else if (row == 84 && col == 0)         { _ZSym = !_ZSym        ; return true; }
+               else if (row == 85 && col == 0)         { _RSym = !_RSym        ; return true; }
                else {
                        for (int i=0; i<CurPat.picks.size(); i++) { Pick P = (Pick)CurPat.picks.get(i);
                                if (!btwn(row,P.StartRow,P.EndRow)                                                      ) continue;
                                if (!btwn(col,0,NumApcCols-1)                                                           ) continue;
                                if (!btwn((row-P.StartRow)*NumApcCols + col,0,P.NumPicks-1)     ) continue;
-                               P.CurRow=row; P.CurCol=col; return;
+                               P.CurRow=row; P.CurCol=col; return true;
                        }
                        //println(row + " " + col); 
                }
+               return false;
        }
 }
 //----------------------------------------------------------------------------------------------------------------------------------
 public class DPat extends SCPattern
 {
+       DGlobals        DG = new DGlobals();
+       
        ArrayList       picks           = new ArrayList();
        ArrayList       paramlist       = new ArrayList();
        int                     nMaxRow         = 0;
@@ -242,6 +263,18 @@ public class DPat extends SCPattern
                xyzdMax         =  new xyz(xdMax,ydMax,zdMax);
                xyzMid          =  new xyz(xdMax/2, ydMax/2, zdMax/2);
        }
+       
+       public boolean noteOnReceived(Note note) {
+               return DG.noteOnReceived(note);
+       }
+       
+       public boolean noteOffReceived(Note note) {
+               return DG.noteOffReceived(note);
+       }
+       
+       public boolean controllerChangeReceived(rwmidi.Controller cc) {
+               return DG.controllerChangeReceived(cc);
+       }
 
        void run(double deltaMs)
        {
index 60a5f3296e11abc121aec4607abd44edb95832a8..eba174ca9cbcc1156a363f02e4842f2bd04bf786 100644 (file)
@@ -102,6 +102,10 @@ void setup() {
   lx.enableKeyboardTempo();
   logTime("Built GLucose engine");
   
+  // MIDI devices
+  midiEngine = new MidiEngine();
+  logTime("Setup MIDI devices");
+
   // Set the patterns
   Engine engine = lx.engine;
   engine.setPatterns(patterns = _patterns(glucose));
@@ -122,10 +126,6 @@ void setup() {
   mappingTool = new MappingTool(glucose, pandaMappings);
   logTime("Built PandaDriver");
 
-  // MIDI devices
-  midiEngine = new MidiEngine();
-  logTime("Setup MIDI devices");
-
   // Build overlay UI
   debugUI = new DebugUI(pandaMappings);
   overlays = new UIContext[] {