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