More DPat Fixes
[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 Pick (String label, int _Def, int _Max, String d[]) { NumPicks=_Max; Default = _Def; tag=label; Desc = d; }
11 int Cur() { return (CurRow-StartRow)*NumApcCols + CurCol; }
12 int NumPicks, Default, CurRow, CurCol, StartRow, EndRow;
13 String Desc[] ;
14 String tag ;
15 }
16 //----------------------------------------------------------------------------------------------------------------------------------
17 public class _DhP extends BasicParameter {
18 double dflt;
19 _DhP (String label, double value) { super(label,value); dflt=value; }
20 void Set (double value) { super.updateValue(value); }
21 void reset () { super.updateValue(dflt); }
22 float Val () { return getValuef(); }
23 boolean ZeroOrOne () { return Val()==0 || Val() == 1; }
24 }
25 //----------------------------------------------------------------------------------------------------------------------------------
26 public class xyz { float x,y,z;
27 xyz() {x=y=z=0;}
28 xyz(Point p ) {x=p.fx ; y=p.fy; z=p.fz;}
29 xyz(float _x,float _y,float _z) {x=_x ; y=_y ; z=_z ;}
30 void set(Point p ) {x=p.fx ; y=p.fy; z=p.fz;}
31 void set(float _x,float _y,float _z) {x=_x ; y=_y ; z=_z ;}
32 float distance(xyz b) {return dist(x,y,z,b.x,b.y,b.z); }
33 float dot (xyz b) {return x*b.x + y*b.y + z*b.z; }
34 xyz minus (xyz b) {return new xyz(x-b.x,y-b.y,z-b.z); }
35 xyz plus (xyz b) {return new xyz(x+b.x,y+b.y,z+b.z); }
36 xyz plus (float b) {return new xyz(x+b ,y+b ,z+b ); }
37 xyz over (xyz b) {return new xyz(x/b.x,y/b.y,z/b.z); }
38 xyz times (float b) {return new xyz(x*b ,y*b ,z*b ); }
39
40 xyz RotateX (xyz o, float a) { return new xyz ( x,
41 cos(a)*(y-o.y) - sin(a)*(z-o.z) + o.y,
42 sin(a)*(y-o.y) + cos(a)*(z-o.z) + o.z); }
43
44 xyz RotateY (xyz o, float a) { return new xyz ( cos(a)*(x-o.x) - sin(a)*(z-o.z) + o.x,
45 y,
46 sin(a)*(x-o.x) + cos(a)*(z-o.z) + o.z); }
47
48 xyz RotateZ (xyz o, float a) { return new xyz ( cos(a)*(x-o.x) - sin(a)*(y-o.y) + o.x,
49 sin(a)*(x-o.x) + cos(a)*(y-o.y) + o.y,
50 z ); }
51
52
53 void RotateXYZ (xyz o, xyz t, xyz tsin, xyz tcos) {
54 { x -= o.x; y -= o.y; z -= o.z; }
55 if (t.x != 0) { y = y*tcos.x - z*tsin.x; z = y*tsin.x + z*tcos.x; }
56 if (t.y != 0) { z = z*tcos.y - x*tsin.y; x = z*tsin.y + x*tcos.y; }
57 if (t.z != 0) { x = x*tcos.z - y*tsin.z; y = x*tsin.z + y*tcos.z; }
58 { x += o.x; y += o.y; z += o.z; }
59 }
60
61 xyz setRand () { return new xyz ( random(xdMax), random(ydMax), random(zdMax)); }
62 xyz setNorm () { return new xyz ( x / xdMax, y / ydMax, z / zdMax); }
63
64 float interp (float a, float b, float c) { return (1-a)*b + a*c; }
65 xyz interpolate(float i, xyz d) { return new xyz ( interp(i,x,d.x), interp(i,y,d.y), interp(i,z,d.z)); }
66 }
67 //----------------------------------------------------------------------------------------------------------------------------------
68 public class DGlobals {
69 boolean bInit = false;
70 MidiOutput APCOut = null;
71 MidiInput APCIn = null, OxygenIn = null;
72 DPat CurPat = null, NextPat = null;
73 boolean _XSym = false, _YSym = false,
74 _ZSym = false, _RSym = false;
75 String Text1 = "", Text2 = "";
76
77 float Sliders[] = new float[] {0,0,0,0,0,0,0,0};
78 String SliderText[] = new String[] {"Trails", "Dim", "Saturate", "SpinHue", "Hue", "NoiseHue", "Spark", "Wiggle"};
79
80 int mapRow (int a) { return btwn(a,53,57) ? a-53 : a; }
81 int unmapRow (int a) { return btwn(a,0 , 4) ? a+53 : a; }
82
83 void SetLight (int row, int col, int clr){ if (APCOut != null) APCOut.sendNoteOn(col, unmapRow(row), clr); }
84 void SetKnob (int cc , int chan,int val){ if (APCOut != null) APCOut.sendController(cc , chan , val); }
85
86 float _Trails () { return Sliders[0]; }
87 float _Dim () { return Sliders[1]; }
88 float _Saturate () { return Sliders[2]; }
89 float _SpinHue () { return Sliders[3]; }
90 float _ModHue () { return Sliders[4]; }
91 float _NoiseHue () { return Sliders[5]; }
92 float _Spark () { return Sliders[6]; }
93 float _Wiggle () { return Sliders[7]; }
94
95 void Init () {
96 if (bInit) return; bInit=true;
97 for (MidiOutputDevice output : RWMidi.getOutputDevices()) {
98 if (APCOut == null && output.toString().contains("APC")) APCOut = output.createOutput();
99 }
100
101 for (MidiInputDevice input : RWMidi.getInputDevices ()) {
102 if (input.toString().contains("APC")) input.createInput (this);
103 }
104 }
105
106 void SetText()
107 {
108 Text1 = ""; Text2 = "";
109 Text1 += " XSym: " + (_XSym ? "ON" : "OFF") + " ";
110 Text1 += " YSym: " + (_YSym ? "ON" : "OFF") + " ";
111 Text1 += " ZSym: " + (_ZSym ? "ON" : "OFF") + " ";
112 Text1 += " RSym: " + (_RSym ? "ON" : "OFF") + " ";
113 for (int i=0; i<CurPat.picks.size(); i++) {
114 Pick P = (Pick)CurPat.picks.get(i); Text1 += P.tag + ": " + P.Desc[P.Cur()] + " ";
115 }
116
117 Text2 = "SLIDERS: ";
118 for (int i=0; i<8; i++) if (SliderText[i] != "") {
119 Text2 += SliderText[i] + ": " + int(100*Sliders[i]) + " "; }
120
121 uiDebugText.setText(Text1, Text2);
122 }
123
124 void controllerChangeReceived(rwmidi.Controller cc) {
125 if (cc.getCC() == 7 && btwn(cc.getChannel(),0,7)) { Sliders[cc.getChannel()] = 1.*cc.getValue()/127.; }
126
127 else if (cc.getCC() == 15 && cc.getChannel() == 0) {
128 lx.engine.getDeck(1).getCrossfader().setValue( 1.*cc.getValue()/127.);
129 }
130
131 //else { println(cc.getCC() + " " + cc.getChannel() + " " + cc.getValue()); }
132 }
133
134 void Deactivate (DPat p) { if (p == CurPat) { uiDebugText.setText(""); CurPat = NextPat; } NextPat = null; }
135 void Activate (DPat p) {
136 NextPat = CurPat; CurPat = p;
137 while (lx.tempo.bpm() > 40) lx.tempo.setBpm(lx.tempo.bpm()/2);
138 for (int i=0; i<p.paramlist.size(); i++) ((_DhP)p.paramlist.get(i)).reset();
139 UpdateLights();
140 }
141
142 void UpdateLights() {
143 for (int i=0; i<NumApcRows ; i++) for (int j=0; j<NumApcCols; j++) SetLight(i, j, 0);
144 for (int i=48;i< 56 ; i++) SetKnob(0, i, 0);
145 for (int i=16;i< 20 ; i++) SetKnob(0, i, 0);
146 for (int i=0; i<CurPat.picks.size() ; i++) {
147 Pick P = (Pick)CurPat.picks.get(i); SetLight(P.CurRow, P.CurCol, 3);
148 }
149 SetLight(82, 0, _XSym ? 3 : 0);
150 SetLight(83, 0, _YSym ? 3 : 0);
151 SetLight(84, 0, _ZSym ? 3 : 0);
152 SetLight(85, 0, _RSym ? 3 : 0);
153
154 for (int i=0; i<CurPat.paramlist.size(); i++) {
155 _DhP Param = (_DhP)CurPat.paramlist.get(i);
156 SetKnob ( 0, i<=55 ? 48+i : 16 + i - 8, int(Param.Val()*127) );
157 }
158 }
159
160 double Tap1 = 0;
161 double getNow() { return millis() + 1000*second() + 60*1000*minute() + 3600*1000*hour(); }
162
163 void noteOffReceived(Note note) {
164 if (CurPat == null) return;
165 int row = DG.mapRow(note.getPitch()), col = note.getChannel();
166
167 if (row == 50 && col == 0 && btwn(getNow() - Tap1,5000,300*1000)) { // hackish tapping mechanism
168 double bpm = 32.*60000./(getNow()-Tap1);
169 while (bpm < 20) bpm*=2;
170 while (bpm > 40) bpm/=2;
171 lx.tempo.setBpm(bpm); lx.tempo.trigger(); Tap1=0; println("Tap Set - " + bpm + " bpm");
172 }
173
174 UpdateLights();
175 }
176
177 void noteOnReceived (Note note) {
178 if (CurPat == null) return;
179 int row = mapRow(note.getPitch()), col = note.getChannel();
180
181 if (row == 50 && col == 0) { lx.tempo.trigger(); Tap1 = getNow(); }
182 else if (row == 82 && col == 0) _XSym = !_XSym ;
183 else if (row == 83 && col == 0) _YSym = !_YSym ;
184 else if (row == 84 && col == 0) _ZSym = !_ZSym ;
185 else if (row == 85 && col == 0) _RSym = !_RSym ;
186 else {
187 for (int i=0; i<CurPat.picks.size(); i++) { Pick P = (Pick)CurPat.picks.get(i);
188 if (!btwn(row,P.StartRow,P.EndRow) ) continue;
189 if (!btwn(col,0,NumApcCols-1) ) continue;
190 if (!btwn((row-P.StartRow)*NumApcCols + col,0,P.NumPicks-1) ) continue;
191 P.CurRow=row; P.CurCol=col; return;
192 }
193 //println(row + " " + col);
194 }
195 }
196 }
197 //----------------------------------------------------------------------------------------------------------------------------------
198 public class DPat extends SCPattern
199 {
200 ArrayList picks = new ArrayList();
201 ArrayList paramlist = new ArrayList();
202 int nMaxRow = 0;
203 float zSpinHue = 0;
204 int nPoint , nPoints;
205 xyz xyzHalf = new xyz(.5,.5,.5),
206 xyzdMax = new xyz(),
207 xyzMid = new xyz();
208
209 float NoiseMove = random(10000);
210 _DhP pSharp, pRotX, pRotY, pRotZ;
211 float Dist (xyz a, xyz b) { return dist(a.x,a.y,a.z,b.x,b.y,b.z); }
212 int c1c (float a) { return int(100*constrain(a,0,1)); }
213 float CalcCone (xyz v1, xyz v2, xyz c) { return degrees( acos ( v1.minus(c).dot(v2.minus(c)) /
214 (sqrt(v1.minus(c).dot(v1.minus(c))) * sqrt(v2.minus(c).dot(v2.minus(c))) ) )); }
215 void StartRun(double deltaMs) { }
216 color CalcPoint(xyz p) { return color(0,0,0); }
217 boolean IsActive() { return this == DG.CurPat; }
218 void onInactive() { DG.Deactivate(this); }
219 void onActive () { DG.Activate(this); }
220
221 _DhP addParam(String label, double value) {
222 _DhP P = new _DhP(label, value);
223 super.addParameter(P);
224 paramlist.add(P); return P;
225 }
226
227 Pick addPick(String name, int def, int nmax, String[] desc) {
228 Pick P = new Pick(name, def, nmax, desc);
229 P.StartRow = nMaxRow;
230 P.EndRow = P.StartRow + int((nmax-1) / NumApcCols);
231 nMaxRow = P.EndRow + 1;
232 P.CurCol = def % NumApcCols;
233 P.CurRow = P.StartRow + def / NumApcCols;
234 picks.add(P);
235 return P;
236 }
237
238 DPat(GLucose glucose) {
239 super(glucose);
240 DG.Init();
241 pSharp = addParam("Shrp" , 0);
242 nPoints = model.points.size();
243 xdMax = model.xMax;
244 ydMax = model.yMax;
245 zdMax = model.zMax;
246 xyzdMax = new xyz(xdMax,ydMax,zdMax);
247 xyzMid = new xyz(xdMax/2, ydMax/2, zdMax/2);
248 }
249
250 void run(double deltaMs)
251 {
252 NoiseMove += deltaMs;
253 StartRun (deltaMs);
254 zSpinHue += DG._SpinHue ()*deltaMs*.05;
255 xyz P = new xyz();
256 float modhue = DG._ModHue ()==0 ? 0 : DG._ModHue ()*360;
257 float fSharp = 1/(1.01-pSharp.Val());
258
259 DG.SetText();
260 nPoint = 0;
261 for (Point p : model.points) { nPoint++;
262 if (!IsActive()) { colors[p.index] = color(0,0,0); continue; }
263
264 P.set(p);
265
266 if (DG._Spark () > 0) P.y += DG._Spark () * (noise(P.x,P.y+NoiseMove/30 ,P.z)*ydMax - ydMax/2.);
267 if (DG._Wiggle() > 0) P.y += DG._Wiggle() * (noise(P.x/(xdMax*.3)-NoiseMove/1500.) - .5) * (ydMax/2.);
268
269 color cOld = colors[p.index];
270
271 color cNew = CalcPoint(P);
272 if (DG._XSym) cNew = blendColor(cNew, CalcPoint(new xyz(xdMax-P.x,P.y,P.z)), ADD);
273 if (DG._YSym) cNew = blendColor(cNew, CalcPoint(new xyz(P.x,ydMax-P.y,P.z)), ADD);
274 if (DG._ZSym) cNew = blendColor(cNew, CalcPoint(new xyz(P.x,P.y,zdMax-P.z)), ADD);
275
276 float b = brightness(cNew)/100.;
277 b = b < .5 ? pow(b,fSharp) : 1-pow(1-b,fSharp);
278
279 float noizhue = DG._NoiseHue()==0 ? 0 : DG._NoiseHue()*360*noise(
280 P.x/(xdMax*.3)+NoiseMove*.0003,
281 P.y/(ydMax*.3)+NoiseMove*.00025,
282 P.z/(zdMax*.3)+NoiseMove*.0002 );
283
284 cNew = color( (hue(cNew) + modhue + zSpinHue - noizhue) % 360,
285 saturation(cNew) + 100*DG._Saturate(),
286 100 * (DG._Trails()==0 ? b : max(b, (float) (brightness(cOld)/100. - (1-DG._Trails()) * deltaMs/200.)))
287 * (DG._Dim ()==0 ? 1 : 1-DG._Dim())
288 );
289
290 colors[p.index] = cNew;
291 }
292 }
293 }
294 //----------------------------------------------------------------------------------------------------------------------------------