Latest version of GLucose.jar
[SugarCubes.git] / DanUtil.pde
index 16e19359ed4c2412fd2e54621b1f84c2c2a2f397..9f4b3a03e4abf43efe159af38136cd58b723afc4 100644 (file)
@@ -1,23 +1,23 @@
 //----------------------------------------------------------------------------------------------------------------------------------
 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;      }
 
 public class Pick {
-       Pick    (String label, int _Def, int _Max)      { NumPicks=_Max; Default = _Def; tag=label;     }
+       Pick    (String label, int _Def, int _Max,      String d[])     { NumPicks=_Max; Default = _Def; tag=label; Desc = d; }
        int             Cur()                                                           { return (CurRow-StartRow)*NumApcCols + CurCol;         }
        int     NumPicks, Default, CurRow, CurCol, StartRow, EndRow;
-       String  tag;
+       String  Desc[]  ;       
+       String  tag             ;
 }
 //----------------------------------------------------------------------------------------------------------------------------------
 public class _DhP extends BasicParameter {
-       double  dflt;   
+       double  dflt;
        _DhP    (String label, double value)            { super(label,value); dflt=value;               }
-       void    Set                     (double value)                  { super.updateValue(value);                             }
-       void    reset           ()                                              { super.updateValue(dflt);                              }
+       void    Set                     (double value)                  { super.setValue(value);                                }
+       void    reset           ()                                              { super.setValue(dflt);                                 }
        float   Val                     ()                                              { return getValuef();                                   }
        boolean ZeroOrOne       ()                                              { return Val()==0 || Val() == 1;                }
 }
@@ -28,34 +28,31 @@ public class xyz {  float x,y,z;
                        xyz(float _x,float _y,float _z) {x=_x   ; y=_y  ; z=_z  ;}
        void    set(Point p                                       ) {x=p.fx     ; y=p.fy; z=p.fz;}
        void    set(float _x,float _y,float _z) {x=_x   ; y=_y  ; z=_z  ;}
-       float   distance(xyz b)                                 {return dist(x,y,z,b.x,b.y,b.z);         }
-       float   dot     (xyz b)                                 {return x*b.x + y*b.y + z*b.z;           }
-       xyz             minus   (xyz b)                                 {return new xyz(x-b.x,y-b.y,z-b.z);  }
-       xyz             plus    (xyz b)                                 {return new xyz(x+b.x,y+b.y,z+b.z);  }
-       xyz             plus    (float b)                               {return new xyz(x+b  ,y+b  ,z+b  );  }
-       xyz             over    (xyz b)                                 {return new xyz(x/b.x,y/b.y,z/b.z);  }
-       xyz             times   (float b)                               {return new xyz(x*b  ,y*b  ,z*b  );  }
-
-       xyz             RotateX (xyz o, float a)                { return new xyz (      x,
-                                                                                                                               cos(a)*(y-o.y) - sin(a)*(z-o.z) + o.y,
-                                                                                                                               sin(a)*(y-o.y) + cos(a)*(z-o.z) + o.z);         }
-       
-       xyz             RotateY (xyz o, float a)                { return new xyz (      cos(a)*(x-o.x) - sin(a)*(z-o.z) + o.x,
-                                                                                                                               y,
-                                                                                                                               sin(a)*(x-o.x) + cos(a)*(z-o.z) + o.z);         }
-       
-       xyz             RotateZ (xyz o, float a)                { return new xyz (      cos(a)*(x-o.x) - sin(a)*(y-o.y) + o.x,
-                                                                                                                               sin(a)*(x-o.x) + cos(a)*(y-o.y) + o.y,
-                                                                                                                               z       );                                                                              }
-       
+       float   distance(xyz b)                                 {return dist(x,y,z,b.x,b.y,b.z);                }
+       float   dot     (xyz b)                                 {return x*b.x + y*b.y + z*b.z;                  }
+       xyz             minus   (xyz b)                                 {return new xyz(x-b.x,y-b.y,z-b.z);     }
+       xyz             plus    (xyz b)                                 {return new xyz(x+b.x,y+b.y,z+b.z);     }
+       xyz             plus    (float b)                               {return new xyz(x+b  ,y+b  ,z+b  );     }
+       xyz             over    (xyz b)                                 {return new xyz(x/b.x,y/b.y,z/b.z);     }
+       xyz             times   (float b)                               {return new xyz(x*b  ,y*b  ,z*b  );     }
+       boolean isZero  ()                                              {return x==0 && y==0 && z==0;                   }
+
+       void    RotateZ   (xyz o, float nSin, float nCos) {
+               float nX = nCos*(x-o.x) - nSin*(y-o.y) + o.x;
+               float nY = nSin*(x-o.x) + nCos*(y-o.y) + o.y;
+               x = nX; y = nY;
+       }
 
-       void    RotateXYZ (xyz o, xyz t, xyz tsin, xyz tcos) {
-                                         {     x -= o.x; y -= o.y; z -= o.z; }
-               if (t.x != 0) { y = y*tcos.x - z*tsin.x; z = y*tsin.x + z*tcos.x; }
-               if (t.y != 0) { z = z*tcos.y - x*tsin.y; x = z*tsin.y + x*tcos.y; }
-               if (t.z != 0) { x = x*tcos.z - y*tsin.z; y = x*tsin.z + y*tcos.z; }
-                                         { x += o.x; y += o.y; z += o.z; }
+       void    RotateX   (xyz o, float nSin, float nCos) {
+               float nY = nCos*(y-o.y) - nSin*(z-o.z) + o.y;
+               float nZ = nSin*(y-o.y) + nCos*(z-o.z) + o.z;
+               y = nY; z = nZ;
+       }
 
+       void    RotateY   (xyz o, float nSin, float nCos) {
+               float nZ = nCos*(z-o.z) - nSin*(x-o.x) + o.z;
+               float nX = nSin*(z-o.z) + nCos*(x-o.x) + o.x;
+               z = nZ; x = nX;
        }
 
        xyz             setRand ()                                              { return new xyz ( random(xdMax), random(ydMax), random(zdMax));                }
@@ -66,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;
@@ -79,7 +88,9 @@ public class DGlobals {
 
        int     mapRow   (int a)                                        { return btwn(a,53,57) ? a-53 : a;                      }
        int     unmapRow (int a)                                        { return btwn(a,0 , 4) ? a+53 : a;                      }
+
        void    SetLight (int row, int col, int clr){ if (APCOut != null) APCOut.sendNoteOn(col, unmapRow(row), clr); }
+       void    SetKnob  (int cc , int chan,int val){ if (APCOut != null) APCOut.sendController(cc , chan                 , val); }
 
        float   _Trails         ()                                              { return Sliders[0]; }
        float   _Dim            ()                                              { return Sliders[1]; }
@@ -90,44 +101,46 @@ 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") + "    ";
                Text1  += " ZSym:  " + (_ZSym ? "ON" : "OFF") + "    ";
                Text1  += " RSym:  " + (_RSym ? "ON" : "OFF") + "    ";
                for (int i=0; i<CurPat.picks.size(); i++) {
-                       Pick P = (Pick)CurPat.picks.get(i); Text1 += P.tag + ": " + P.Cur() + "    ";
+                       Pick P = (Pick)CurPat.picks.get(i); Text1 += P.tag + ": " + P.Desc[P.Cur()] + "    ";
                }
 
                Text2  = "SLIDERS: ";
-               for (int i=0; i<8; i++) if (SliderText[i] != "") { Text2 += SliderText[i] + ": " + Sliders[i] + "     "; }
+               for (int i=0; i<8; i++) if (SliderText[i] != "") {
+                       Text2 += SliderText[i] + ": " + int(100*Sliders[i]) + "     "; }
 
                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; }
        void    Activate   (DPat p) {
                NextPat = CurPat; CurPat = p;
@@ -137,7 +150,11 @@ 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);
                for (int i=0; i<CurPat.picks.size()     ; i++) {
                        Pick P = (Pick)CurPat.picks.get(i); SetLight(P.CurRow, P.CurCol, 3);
                }
@@ -145,60 +162,69 @@ public class DGlobals {
                SetLight(83, 0, _YSym ? 3 : 0);
                SetLight(84, 0, _ZSym ? 3 : 0);
                SetLight(85, 0, _RSym ? 3 : 0);
+               
+               for (int i=0; i<CurPat.paramlist.size(); i++) {
+                       _DhP Param = (_DhP)CurPat.paramlist.get(i);
+                       SetKnob ( 0, i<=55 ? 48+i : 16 + i - 8, int(Param.Val()*127) );
+               }
        }
        
        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);                       
+                       //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;
        float           zSpinHue        = 0;
        int             nPoint  , nPoints;
-       xyz                     xyzHalf          = new xyz(.5,.5,.5),
-                               xyzdMax,
-                               xyzMid;
+       xyz                     xyzHalf         = new xyz(.5,.5,.5),
+                               xyzdMax         = new xyz(),
+                               xyzMid          = new xyz();
        
        float           NoiseMove       = random(10000);
-       _DhP            pSharp;
-
+       _DhP            pSharp, pRotX, pRotY, pRotZ;
        float           Dist     (xyz a, xyz b)                         { return dist(a.x,a.y,a.z,b.x,b.y,b.z);         }
        int                     c1c              (float a)                                      { return int(100*constrain(a,0,1));                     }
        float           CalcCone (xyz v1, xyz v2, xyz c)        { return degrees( acos ( v1.minus(c).dot(v2.minus(c)) /
@@ -215,8 +241,8 @@ public class DPat extends SCPattern
                paramlist.add(P); return P;
        }
                
-       Pick addPick(String name, int def, int nmax) {
-               Pick P          = new Pick(name, def, nmax); 
+       Pick addPick(String name, int def, int nmax, String[] desc) {
+               Pick P          = new Pick(name, def, nmax, desc); 
                P.StartRow      = nMaxRow;
                P.EndRow        = P.StartRow + int((nmax-1) / NumApcCols);
                nMaxRow         = P.EndRow + 1;
@@ -229,7 +255,7 @@ public class DPat extends SCPattern
        DPat(GLucose glucose) {
                super(glucose);
                DG.Init();
-               pSharp          =  addParam("Shrp", 0);
+               pSharp          =  addParam("Shrp"      ,  0);
                nPoints         =  model.points.size();
                xdMax           =  model.xMax;
                ydMax           =  model.yMax;
@@ -237,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)
        {
@@ -246,7 +284,7 @@ public class DPat extends SCPattern
                xyz P                   = new xyz();
                float modhue    = DG._ModHue  ()==0 ? 0 : DG._ModHue  ()*360;
                float fSharp    = 1/(1.01-pSharp.Val());
-               
+
                DG.SetText();
                nPoint  = 0;
                for (Point p : model.points)    { nPoint++;