Refactor MIDI stuff so deck focusing is listenable and controllable
[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.setValue(value); }
21 void reset () { super.setValue(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 boolean isZero () {return x==0 && y==0 && z==0; }
40
41 void RotateZ (xyz o, float nSin, float nCos) {
42 float nX = nCos*(x-o.x) - nSin*(y-o.y) + o.x;
43 float nY = nSin*(x-o.x) + nCos*(y-o.y) + o.y;
44 x = nX; y = nY;
45 }
46
47 void RotateX (xyz o, float nSin, float nCos) {
48 float nY = nCos*(y-o.y) - nSin*(z-o.z) + o.y;
49 float nZ = nSin*(y-o.y) + nCos*(z-o.z) + o.z;
50 y = nY; z = nZ;
51 }
52
53 void RotateY (xyz o, float nSin, float nCos) {
54 float nZ = nCos*(z-o.z) - nSin*(x-o.x) + o.z;
55 float nX = nSin*(z-o.z) + nCos*(x-o.x) + o.x;
56 z = nZ; x = nX;
57 }
58
59 xyz setRand () { return new xyz ( random(xdMax), random(ydMax), random(zdMax)); }
60 xyz setNorm () { return new xyz ( x / xdMax, y / ydMax, z / zdMax); }
61
62 float interp (float a, float b, float c) { return (1-a)*b + a*c; }
63 xyz interpolate(float i, xyz d) { return new xyz ( interp(i,x,d.x), interp(i,y,d.y), interp(i,z,d.z)); }
64 }
65 //----------------------------------------------------------------------------------------------------------------------------------
66 public class DGlobals {
67 boolean bInit = false;
68 MidiOutput APCOut = null;
69 MidiInput APCIn = null, OxygenIn = null;
70 DPat CurPat = null, NextPat = null;
71 boolean _XSym = false, _YSym = false,
72 _ZSym = false, _RSym = false;
73 String Text1 = "", Text2 = "";
74
75 float Sliders[] = new float[] {0,0,0,0,0,0,0,0};
76 String SliderText[] = new String[] {"Trails", "Dim", "Saturate", "SpinHue", "Hue", "NoiseHue", "Spark", "Wiggle"};
77
78 int mapRow (int a) { return btwn(a,53,57) ? a-53 : a; }
79 int unmapRow (int a) { return btwn(a,0 , 4) ? a+53 : a; }
80
81 void SetLight (int row, int col, int clr){ if (APCOut != null) APCOut.sendNoteOn(col, unmapRow(row), clr); }
82 void SetKnob (int cc , int chan,int val){ if (APCOut != null) APCOut.sendController(cc , chan , val); }
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.Desc[P.Cur()] + " ";
113 }
114
115 Text2 = "SLIDERS: ";
116 for (int i=0; i<8; i++) if (SliderText[i] != "") {
117 Text2 += SliderText[i] + ": " + int(100*Sliders[i]) + " "; }
118
119 uiDebugText.setText(Text1, Text2);
120 }
121
122 void controllerChangeReceived(rwmidi.Controller cc) {
123 if (cc.getCC() == 7 && btwn(cc.getChannel(),0,7)) { Sliders[cc.getChannel()] = 1.*cc.getValue()/127.; }
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=48;i< 56 ; i++) SetKnob(0, i, 0);
142 for (int i=16;i< 20 ; i++) SetKnob(0, i, 0);
143 for (int i=0; i<CurPat.picks.size() ; i++) {
144 Pick P = (Pick)CurPat.picks.get(i); SetLight(P.CurRow, P.CurCol, 3);
145 }
146 SetLight(82, 0, _XSym ? 3 : 0);
147 SetLight(83, 0, _YSym ? 3 : 0);
148 SetLight(84, 0, _ZSym ? 3 : 0);
149 SetLight(85, 0, _RSym ? 3 : 0);
150
151 for (int i=0; i<CurPat.paramlist.size(); i++) {
152 _DhP Param = (_DhP)CurPat.paramlist.get(i);
153 SetKnob ( 0, i<=55 ? 48+i : 16 + i - 8, int(Param.Val()*127) );
154 }
155 }
156
157 double Tap1 = 0;
158 double getNow() { return millis() + 1000*second() + 60*1000*minute() + 3600*1000*hour(); }
159 void noteOffReceived(Note note) {
160 if (CurPat == null) return;
161 int row = DG.mapRow(note.getPitch()), col = note.getChannel();
162
163 if (row == 50 && col == 0 && btwn(getNow() - Tap1,5000,300*1000)) { // hackish tapping mechanism
164 double bpm = 32.*60000./(getNow()-Tap1);
165 while (bpm < 20) bpm*=2;
166 while (bpm > 40) bpm/=2;
167 lx.tempo.setBpm(bpm); lx.tempo.trigger(); Tap1=0; println("Tap Set - " + bpm + " bpm");
168 }
169
170 UpdateLights();
171 }
172
173 void noteOnReceived (Note note) {
174 if (CurPat == null) return;
175 int row = mapRow(note.getPitch()), col = note.getChannel();
176
177 if (row == 50 && col == 0) { lx.tempo.trigger(); Tap1 = getNow(); }
178 else if (row == 82 && col == 0) _XSym = !_XSym ;
179 else if (row == 83 && col == 0) _YSym = !_YSym ;
180 else if (row == 84 && col == 0) _ZSym = !_ZSym ;
181 else if (row == 85 && col == 0) _RSym = !_RSym ;
182 else {
183 for (int i=0; i<CurPat.picks.size(); i++) { Pick P = (Pick)CurPat.picks.get(i);
184 if (!btwn(row,P.StartRow,P.EndRow) ) continue;
185 if (!btwn(col,0,NumApcCols-1) ) continue;
186 if (!btwn((row-P.StartRow)*NumApcCols + col,0,P.NumPicks-1) ) continue;
187 P.CurRow=row; P.CurCol=col; return;
188 }
189 //println(row + " " + col);
190 }
191 }
192 }
193 //----------------------------------------------------------------------------------------------------------------------------------
194 public class DPat extends SCPattern
195 {
196 ArrayList picks = new ArrayList();
197 ArrayList paramlist = new ArrayList();
198 int nMaxRow = 0;
199 float zSpinHue = 0;
200 int nPoint , nPoints;
201 xyz xyzHalf = new xyz(.5,.5,.5),
202 xyzdMax = new xyz(),
203 xyzMid = new xyz();
204
205 float NoiseMove = random(10000);
206 _DhP pSharp, pRotX, pRotY, pRotZ;
207 float Dist (xyz a, xyz b) { return dist(a.x,a.y,a.z,b.x,b.y,b.z); }
208 int c1c (float a) { return int(100*constrain(a,0,1)); }
209 float CalcCone (xyz v1, xyz v2, xyz c) { return degrees( acos ( v1.minus(c).dot(v2.minus(c)) /
210 (sqrt(v1.minus(c).dot(v1.minus(c))) * sqrt(v2.minus(c).dot(v2.minus(c))) ) )); }
211 void StartRun(double deltaMs) { }
212 color CalcPoint(xyz p) { return color(0,0,0); }
213 boolean IsActive() { return this == DG.CurPat; }
214 void onInactive() { DG.Deactivate(this); }
215 void onActive () { DG.Activate(this); }
216
217 _DhP addParam(String label, double value) {
218 _DhP P = new _DhP(label, value);
219 super.addParameter(P);
220 paramlist.add(P); return P;
221 }
222
223 Pick addPick(String name, int def, int nmax, String[] desc) {
224 Pick P = new Pick(name, def, nmax, desc);
225 P.StartRow = nMaxRow;
226 P.EndRow = P.StartRow + int((nmax-1) / NumApcCols);
227 nMaxRow = P.EndRow + 1;
228 P.CurCol = def % NumApcCols;
229 P.CurRow = P.StartRow + def / NumApcCols;
230 picks.add(P);
231 return P;
232 }
233
234 DPat(GLucose glucose) {
235 super(glucose);
236 DG.Init();
237 pSharp = addParam("Shrp" , 0);
238 nPoints = model.points.size();
239 xdMax = model.xMax;
240 ydMax = model.yMax;
241 zdMax = model.zMax;
242 xyzdMax = new xyz(xdMax,ydMax,zdMax);
243 xyzMid = new xyz(xdMax/2, ydMax/2, zdMax/2);
244 }
245
246 void run(double deltaMs)
247 {
248 NoiseMove += deltaMs;
249 StartRun (deltaMs);
250 zSpinHue += DG._SpinHue ()*deltaMs*.05;
251 xyz P = new xyz();
252 float modhue = DG._ModHue ()==0 ? 0 : DG._ModHue ()*360;
253 float fSharp = 1/(1.01-pSharp.Val());
254
255 DG.SetText();
256 nPoint = 0;
257 for (Point p : model.points) { nPoint++;
258 if (!IsActive()) { colors[p.index] = color(0,0,0); continue; }
259
260 P.set(p);
261
262 if (DG._Spark () > 0) P.y += DG._Spark () * (noise(P.x,P.y+NoiseMove/30 ,P.z)*ydMax - ydMax/2.);
263 if (DG._Wiggle() > 0) P.y += DG._Wiggle() * (noise(P.x/(xdMax*.3)-NoiseMove/1500.) - .5) * (ydMax/2.);
264
265 color cOld = colors[p.index];
266
267 color cNew = CalcPoint(P);
268 if (DG._XSym) cNew = blendColor(cNew, CalcPoint(new xyz(xdMax-P.x,P.y,P.z)), ADD);
269 if (DG._YSym) cNew = blendColor(cNew, CalcPoint(new xyz(P.x,ydMax-P.y,P.z)), ADD);
270 if (DG._ZSym) cNew = blendColor(cNew, CalcPoint(new xyz(P.x,P.y,zdMax-P.z)), ADD);
271
272 float b = brightness(cNew)/100.;
273 b = b < .5 ? pow(b,fSharp) : 1-pow(1-b,fSharp);
274
275 float noizhue = DG._NoiseHue()==0 ? 0 : DG._NoiseHue()*360*noise(
276 P.x/(xdMax*.3)+NoiseMove*.0003,
277 P.y/(ydMax*.3)+NoiseMove*.00025,
278 P.z/(zdMax*.3)+NoiseMove*.0002 );
279
280 cNew = color( (hue(cNew) + modhue + zSpinHue - noizhue) % 360,
281 saturation(cNew) + 100*DG._Saturate(),
282 100 * (DG._Trails()==0 ? b : max(b, (float) (brightness(cOld)/100. - (1-DG._Trails()) * deltaMs/200.)))
283 * (DG._Dim ()==0 ? 1 : 1-DG._Dim())
284 );
285
286 colors[p.index] = cNew;
287 }
288 }
289 }
290 //----------------------------------------------------------------------------------------------------------------------------------