updates on dpat
[SugarCubes.git] / DanUtil.pde
1 //----------------------------------------------------------------------------------------------------------------------------------
2 float xdMax,ydMax,zdMax;
3 int NumApcRows = 5, NumApcCols = 8;
4 DGlobals DG = new DGlobals();
5
6 boolean btwn (int a,int b,int c) { return a >= b && a <= c; }
7 boolean btwn (double a,double b,double c) { return a >= b && a <= c; }
8
9 public class Pick {
10 int NumPicks, Default , CurRow , CurCol ,
11 StartRow, EndRow ;
12 String tag , Desc[] ;
13
14 Pick (String label, int _Def, int _Max, int nStart, String d[]) {
15 NumPicks = _Max; Default = _Def;
16 StartRow = nStart; EndRow = StartRow + int((NumPicks-1) / NumApcCols);
17 tag = label; Desc = d;
18 reset();
19 }
20
21 int Cur() { return (CurRow-StartRow)*NumApcCols + CurCol; }
22 String CurDesc() { return Desc[Cur()]; }
23 void reset() { CurCol = Default % NumApcCols; CurRow = StartRow + Default / NumApcCols; }
24
25 boolean set(int r, int c) {
26 if (!btwn(r,StartRow,EndRow) || !btwn(c,0,NumApcCols-1) ||
27 !btwn((r-StartRow)*NumApcCols + c,0,NumPicks-1)) return false;
28 CurRow=r; CurCol=c; return true;
29 }
30 }
31
32 public class DBool {
33 boolean def, b;
34 String tag;
35 int row, col;
36 void reset() { b = def; }
37 boolean set (int r, int c, boolean val) { if (r != row || c != col) return false; b = val; return true; }
38 DBool(String _tag, boolean _def, int _row, int _col) {
39 def = _def; b = _def; tag = _tag; row = _row; col = _col;
40 }
41 }
42
43 public class DParam extends BasicParameter {
44 double dflt;
45 DParam (String label, double value) { super(label,value); dflt=value; }
46 void set (double value) { super.setValue(value); }
47 void reset () { super.setValue(dflt); }
48 float Val () { return getValuef(); }
49 }
50 //----------------------------------------------------------------------------------------------------------------------------------
51 public class xyz { float x,y,z;
52 xyz() {x=y=z=0;}
53 xyz(Point p ) {x=p.fx ; y=p.fy; z=p.fz;}
54 xyz(float _x,float _y,float _z) {x=_x ; y=_y ; z=_z ;}
55 void set(Point p ) {x=p.fx ; y=p.fy; z=p.fz;}
56 void set(xyz p ) {x=p.x ; y=p.y ; z=p.z ;}
57 void set(float _x,float _y,float _z) {x=_x ; y=_y ; z=_z ;}
58
59 void zoomX (float zx) {x = x*zx - xdMax*(zx-1)/2; }
60 void zoomY (float zy) {y = y*zy - ydMax*(zy-1)/2; }
61
62 float distance(xyz b) {return dist(x,y,z,b.x,b.y,b.z); }
63 float dot (xyz b) {return x*b.x + y*b.y + z*b.z; }
64 void add (xyz b) {x += b.x; y += b.y; z += b.z; }
65 void add (float b) {x += b ; y += b ; z += b ; }
66 void subtract(xyz b) {x -= b.x; y -= b.y; z -= b.z; }
67
68 void RotateZ (xyz o, float nSin, float nCos) {
69 float nX = nCos*(x-o.x) - nSin*(y-o.y) + o.x;
70 float nY = nSin*(x-o.x) + nCos*(y-o.y) + o.y;
71 x = nX; y = nY;
72 }
73
74 void RotateX (xyz o, float nSin, float nCos) {
75 float nY = nCos*(y-o.y) - nSin*(z-o.z) + o.y;
76 float nZ = nSin*(y-o.y) + nCos*(z-o.z) + o.z;
77 y = nY; z = nZ;
78 }
79
80 void RotateY (xyz o, float nSin, float nCos) {
81 float nZ = nCos*(z-o.z) - nSin*(x-o.x) + o.z;
82 float nX = nSin*(z-o.z) + nCos*(x-o.x) + o.x;
83 z = nZ; x = nX;
84 }
85
86 void setRand () { x = random(xdMax); y = random(ydMax); z = random(zdMax); }
87 void setNorm () { x /= xdMax; y /= ydMax; z /= zdMax; }
88
89 float interp (float a, float b, float c) { return (1-a)*b + a*c; }
90
91 void interpolate(float i, xyz d) { x = interp(i,x,d.x); y = interp(i,y,d.y); z = interp(i,z,d.z); }
92 }
93 //----------------------------------------------------------------------------------------------------------------------------------
94 public class DGlobals {
95 boolean bInit = false;
96 MidiOutput APCOut = null;
97 MidiInput APCIn = null, OxygenIn = null;
98 DPat CurPat = null;
99
100 float Sliders[] = new float [] {1,0,0,0,0,0,0,0};
101 String SliderText[] = new String[] {"Level", "SpinHue", "Spark", "Wiggle", "Trails", "??", "??", "??"};
102
103 void SetNoteOn (int row, int col, int clr){ if (APCOut != null) APCOut.sendNoteOn (col, row, clr); }
104 void SetNoteOff (int row, int col, int clr){ if (APCOut != null) APCOut.sendNoteOff (col, row, clr); }
105 void SetKnob (int cc , int c , int v ){ if (APCOut != null) APCOut.sendController (cc , c, v); }
106
107 DBool GetBool (int i) { return (DBool)CurPat.bools .get(i); }
108 Pick GetPick (int i) { return (Pick) CurPat.picks .get(i); }
109 DParam GetParam(int i) { return (DParam) CurPat.params.get(i); }
110
111 float _Dim () { return Sliders[0]; }
112 float _SpinHue () { return Sliders[1]; }
113 float _Spark () { return Sliders[2]; }
114 float _Wiggle () { return Sliders[3]; }
115 float _Trails () { return Sliders[4]; }
116
117 void Init () {
118 if (bInit) return; bInit=true;
119 for (MidiOutputDevice o: RWMidi.getOutputDevices()) { if (o.toString().contains("APC")) { APCOut = o.createOutput(); break;}}
120 for (MidiInputDevice i: RWMidi.getInputDevices ()) { if (i.toString().contains("APC")) { i.createInput (this); break;}}
121 }
122
123 boolean isFocused () { return CurPat != null && CurPat == midiEngine.getFocusedDeck().getActivePattern(); }
124 void Deactivate (DPat p) { if (p != CurPat) return; uiDebugText.setText(""); CurPat = null; }
125 void Activate (DPat p) {
126 CurPat = p;
127 while (lx.tempo.bpm() > 40) lx.tempo.setBpm(lx.tempo.bpm()/2);
128 for (int i=0; i<p.params.size(); i++) GetParam(i).reset();
129 for (int i=0; i<p.bools .size(); i++) GetBool (i).reset();
130 for (int i=0; i<p.picks .size(); i++) GetPick (i).reset();
131 UpdateLights();
132 }
133
134 void SetText() {
135 if (!isFocused()) return;
136 String Text1="", Text2="";
137 for (int i=0; i<CurPat.bools.size(); i++) if (GetBool(i).b) Text1 += " " + GetBool(i).tag + " ";
138 for (int i=0; i<CurPat.picks.size(); i++) Text1 += GetPick(i).tag + ": " + GetPick(i).CurDesc() + " ";
139 for (int i=0; i<5; i++) Text2 += SliderText[i] + ": " + int(100*Sliders[i]) + " ";
140 uiDebugText.setText(Text1, Text2);
141 }
142
143 void UpdateLights() {
144 if (!isFocused() || APCOut == null) return;
145 for (int i=53;i< 58; i++) for (int j=0; j<NumApcCols; j++) SetNoteOn(i, j, 0);
146 for (int i=48;i< 56; i++) SetKnob(0, i, 0);
147 for (int i=16;i< 20; i++) SetKnob(0, i, 0);
148
149 for (int i=0; i<CurPat.params.size(); i++) SetKnob ( 0, i<8 ? 48+i : 16 + i - 8, int(GetParam(i).Val()*127) );
150 for (int i=0; i<CurPat.picks .size(); i++) SetNoteOn (GetPick(i).CurRow, GetPick(i).CurCol, 3);
151 for (int i=0; i<CurPat.bools .size(); i++) if (GetBool(i).b) SetNoteOn (GetBool(i).row, GetBool(i).col, 1);
152 else SetNoteOff (GetBool(i).row, GetBool(i).col, 0);
153 }
154
155 void controllerChangeReceived(rwmidi.Controller cc) {
156 if (cc.getCC() == 7 && btwn(cc.getChannel(),0,7)) { Sliders[cc.getChannel()] = 1.*cc.getValue()/127.; }
157 }
158
159 void noteOffReceived(Note note) { if (!isFocused()) return;
160 int row = note.getPitch(), col = note.getChannel();
161 for (int i=0; i<CurPat.bools.size(); i++) if (GetBool(i).set(row, col, false)) return;
162 UpdateLights();
163 }
164
165 void noteOnReceived (Note note) { if (!isFocused()) return;
166 int row = note.getPitch(), col = note.getChannel();
167 for (int i=0; i<CurPat.picks.size(); i++) if (GetPick(i).set(row, col)) return;
168 for (int i=0; i<CurPat.bools.size(); i++) if (GetBool(i).set(row, col, true)) return;
169 println("row: " + row + " col: " + col);
170 }
171 }
172 //----------------------------------------------------------------------------------------------------------------------------------
173 public class DPat extends SCPattern
174 {
175 ArrayList picks = new ArrayList();
176 ArrayList params = new ArrayList();
177 ArrayList bools = new ArrayList();
178 int nMaxRow = 53;
179 float zSpinHue = 0;
180 float LastQuant = -1, LastJog = -1;
181 int nPoint , nPoints;
182 xyz xyzHalf = new xyz(.5,.5,.5),
183 xyzdMax = new xyz(),
184 xyzMid = new xyz(),
185 xyzJog = new xyz(0,0,0);
186
187 float NoiseMove = random(10000);
188 DParam pSharp, pQuantize, pSaturate;
189 DBool pXsym, pYsym, pZsym, pJog;
190 float Dist (xyz a, xyz b) { return dist(a.x,a.y,a.z,b.x,b.y,b.z); }
191 int c1c (float a) { return int(100*constrain(a,0,1)); }
192 float CalcCone (xyz v1, xyz v2, xyz c) { v1.subtract(c); v2.subtract(c);
193 return degrees( acos ( v1.dot(v2) /
194 (sqrt(v1.dot(v1)) * sqrt(v2.dot(v2)) ) )); }
195 void StartRun(double deltaMs) { }
196 color CalcPoint(xyz p) { return color(0,0,0); }
197 boolean IsActive() { return this == DG.CurPat; }
198 boolean IsFocused() { return this == midiEngine.getFocusedDeck().getActivePattern(); }
199 void onInactive() { UpdateState(); }
200 void onActive () { UpdateState(); }
201 void UpdateState() { if (IsFocused() != IsActive()) { if (IsFocused()) DG.Activate(this); else DG.Deactivate(this); } }
202
203 DParam addParam(String label, double value) {
204 DParam P = new DParam(label, value);
205 super.addParameter(P);
206 params.add(P); return P;
207 }
208
209 Pick addPick(String name, int def, int nmax, String[] desc) {
210 Pick P = new Pick(name, def, nmax, nMaxRow, desc);
211 nMaxRow = P.EndRow + 1;
212 picks.add(P);
213 return P;
214 }
215
216 DPat(GLucose glucose) {
217 super(glucose);
218 DG.Init();
219 pSharp = addParam("Shrp", 0 );
220 pQuantize = addParam("Qunt", 0 );
221 pSaturate = addParam("Sat" , .5);
222
223 nPoints = model.points.size();
224 xdMax = model.xMax;
225 ydMax = model.yMax;
226 zdMax = model.zMax;
227 xyzdMax = new xyz(xdMax,ydMax,zdMax);
228 xyzMid = new xyz(xdMax/2, ydMax/2, zdMax/2);
229
230 bools.add(pXsym = new DBool("X-SYM", false, 49, 0));
231 bools.add(pYsym = new DBool("Y-SYM", false, 49, 1));
232 bools.add(pZsym = new DBool("Z-SYM", false, 49, 2));
233 bools.add(pJog = new DBool("JOGGER",false, 49, 3));
234 }
235
236 void run(double deltaMs)
237 {
238 UpdateState();
239 NoiseMove += deltaMs;
240 StartRun (deltaMs);
241 zSpinHue += DG._SpinHue ()*deltaMs*.05;
242 xyz P = new xyz(), tP = new xyz(), pSave = new xyz();
243 float fSharp = 1/(1.0001-pSharp.Val());
244 float fQuant = pQuantize.Val();
245 float fSaturate = pSaturate.Val();
246
247 DG.SetText();
248 nPoint = 0;
249
250 if (fQuant > 0) {
251 float tRamp = (lx.tempo.rampf() % (1./pow(2,int((1-fQuant) * 4))));
252 float f = LastQuant; LastQuant = tRamp; if (tRamp > f) return;
253 }
254
255
256 if (pJog.b) {
257 float tRamp = (lx.tempo.rampf() % .25);
258 if (tRamp < LastJog)
259 xyzJog.set(random(xdMax*.2)-.1,
260 random(ydMax*.2)-.1,
261 random(zdMax*.2)-.1);
262 LastJog = tRamp;
263 }
264
265 for (Point p : model.points) { nPoint++;
266 P.set(p);
267 if (pJog.b) P.add(xyzJog);
268 if (DG._Spark () > 0) P.y += DG._Spark () * (noise(P.x,P.y+NoiseMove/30 ,P.z)*ydMax - ydMax/2.);
269 if (DG._Wiggle() > 0) P.y += DG._Wiggle() * (noise(P.x/(xdMax*.3)-NoiseMove/1500.) - .5) * (ydMax/2.);
270
271 color cOld = colors[p.index]; pSave.set(P);
272 color cNew = CalcPoint(P);
273
274 if (pXsym.b) { P.set(pSave); tP.set(xdMax-P.x,P.y,P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); }
275 if (pYsym.b) { P.set(pSave); tP.set(P.x,ydMax-P.y,P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); }
276 if (pZsym.b) { P.set(pSave); tP.set(P.x,P.y,zdMax-P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); }
277
278 float b = brightness(cNew)/100.;
279
280 if (pSharp.Val()>0) b = b < .5 ? pow(b,fSharp) : 1-pow(1-b,fSharp);
281 if (DG._Trails()>0 && fQuant == 0) b = max(b, (float) (brightness(cOld)/100. - (1-DG._Trails()) * deltaMs/200.));
282
283 colors[p.index] = color(
284 (hue(cNew) + zSpinHue) % 360,
285 saturation(cNew) + 100*(fSaturate*2-1),
286 100 * b * DG._Dim()
287 );
288 }
289 }
290 }
291 //----------------------------------------------------------------------------------------------------------------------------------