more bug fixes in 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 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
125 else if (cc.getCC() == 15 && cc.getChannel() == 0) {
126 lx.engine.getDeck(1).getCrossfader().setValue( 1.*cc.getValue()/127.);
127 }
128
129 //else { println(cc.getCC() + " " + cc.getChannel() + " " + cc.getValue()); }
130 }
131
132 void Deactivate (DPat p) { if (p == CurPat) { uiDebugText.setText(""); CurPat = NextPat; } NextPat = null; }
133 void Activate (DPat p) {
134 NextPat = CurPat; CurPat = p;
135 while (lx.tempo.bpm() > 40) lx.tempo.setBpm(lx.tempo.bpm()/2);
136 for (int i=0; i<p.paramlist.size(); i++) ((_DhP)p.paramlist.get(i)).reset();
137 UpdateLights();
138 }
139
140 void UpdateLights() {
141 for (int i=0; i<NumApcRows ; i++) for (int j=0; j<NumApcCols; j++) SetLight(i, j, 0);
142 for (int i=48;i< 56 ; i++) SetKnob(0, i, 0);
143 for (int i=16;i< 20 ; i++) SetKnob(0, i, 0);
144 for (int i=0; i<CurPat.picks.size() ; i++) {
145 Pick P = (Pick)CurPat.picks.get(i); SetLight(P.CurRow, P.CurCol, 3);
146 }
147 SetLight(82, 0, _XSym ? 3 : 0);
148 SetLight(83, 0, _YSym ? 3 : 0);
149 SetLight(84, 0, _ZSym ? 3 : 0);
150 SetLight(85, 0, _RSym ? 3 : 0);
151
152 for (int i=0; i<CurPat.paramlist.size(); i++) {
153 _DhP Param = (_DhP)CurPat.paramlist.get(i);
154 SetKnob ( 0, i<=55 ? 48+i : 16 + i - 8, int(Param.Val()*127) );
155 }
156 }
157
158 double Tap1 = 0;
159 double getNow() { return millis() + 1000*second() + 60*1000*minute() + 3600*1000*hour(); }
160
161 void noteOffReceived(Note note) {
162 if (CurPat == null) return;
163 int row = DG.mapRow(note.getPitch()), col = note.getChannel();
164
165 if (row == 50 && col == 0 && btwn(getNow() - Tap1,5000,300*1000)) { // hackish tapping mechanism
166 double bpm = 32.*60000./(getNow()-Tap1);
167 while (bpm < 20) bpm*=2;
168 while (bpm > 40) bpm/=2;
169 lx.tempo.setBpm(bpm); lx.tempo.trigger(); Tap1=0; println("Tap Set - " + bpm + " bpm");
170 }
171
172 UpdateLights();
173 }
174
175 void noteOnReceived (Note note) {
176 if (CurPat == null) return;
177 int row = mapRow(note.getPitch()), col = note.getChannel();
178
179 if (row == 50 && col == 0) { lx.tempo.trigger(); Tap1 = getNow(); }
180 else if (row == 82 && col == 0) _XSym = !_XSym ;
181 else if (row == 83 && col == 0) _YSym = !_YSym ;
182 else if (row == 84 && col == 0) _ZSym = !_ZSym ;
183 else if (row == 85 && col == 0) _RSym = !_RSym ;
184 else {
185 for (int i=0; i<CurPat.picks.size(); i++) { Pick P = (Pick)CurPat.picks.get(i);
186 if (!btwn(row,P.StartRow,P.EndRow) ) continue;
187 if (!btwn(col,0,NumApcCols-1) ) continue;
188 if (!btwn((row-P.StartRow)*NumApcCols + col,0,P.NumPicks-1) ) continue;
189 P.CurRow=row; P.CurCol=col; return;
190 }
191 //println(row + " " + col);
192 }
193 }
194 }
195 //----------------------------------------------------------------------------------------------------------------------------------
196 public class DPat extends SCPattern
197 {
198 ArrayList picks = new ArrayList();
199 ArrayList paramlist = new ArrayList();
200 int nMaxRow = 0;
201 float zSpinHue = 0;
202 int nPoint , nPoints;
203 xyz xyzHalf = new xyz(.5,.5,.5),
204 xyzdMax = new xyz(),
205 xyzMid = new xyz();
206
207 float NoiseMove = random(10000);
208 _DhP pSharp, pRotX, pRotY, pRotZ;
209 float Dist (xyz a, xyz b) { return dist(a.x,a.y,a.z,b.x,b.y,b.z); }
210 int c1c (float a) { return int(100*constrain(a,0,1)); }
211 float CalcCone (xyz v1, xyz v2, xyz c) { return degrees( acos ( v1.minus(c).dot(v2.minus(c)) /
212 (sqrt(v1.minus(c).dot(v1.minus(c))) * sqrt(v2.minus(c).dot(v2.minus(c))) ) )); }
213 void StartRun(double deltaMs) { }
214 color CalcPoint(xyz p) { return color(0,0,0); }
215 boolean IsActive() { return this == DG.CurPat; }
216 void onInactive() { DG.Deactivate(this); }
217 void onActive () { DG.Activate(this); }
218
219 _DhP addParam(String label, double value) {
220 _DhP P = new _DhP(label, value);
221 super.addParameter(P);
222 paramlist.add(P); return P;
223 }
224
225 Pick addPick(String name, int def, int nmax, String[] desc) {
226 Pick P = new Pick(name, def, nmax, desc);
227 P.StartRow = nMaxRow;
228 P.EndRow = P.StartRow + int((nmax-1) / NumApcCols);
229 nMaxRow = P.EndRow + 1;
230 P.CurCol = def % NumApcCols;
231 P.CurRow = P.StartRow + def / NumApcCols;
232 picks.add(P);
233 return P;
234 }
235
236 DPat(GLucose glucose) {
237 super(glucose);
238 DG.Init();
239 pSharp = addParam("Shrp" , 0);
240 nPoints = model.points.size();
241 xdMax = model.xMax;
242 ydMax = model.yMax;
243 zdMax = model.zMax;
244 xyzdMax = new xyz(xdMax,ydMax,zdMax);
245 xyzMid = new xyz(xdMax/2, ydMax/2, zdMax/2);
246 }
247
248 void run(double deltaMs)
249 {
250 NoiseMove += deltaMs;
251 StartRun (deltaMs);
252 zSpinHue += DG._SpinHue ()*deltaMs*.05;
253 xyz P = new xyz();
254 float modhue = DG._ModHue ()==0 ? 0 : DG._ModHue ()*360;
255 float fSharp = 1/(1.01-pSharp.Val());
256
257 DG.SetText();
258 nPoint = 0;
259 for (Point p : model.points) { nPoint++;
260 if (!IsActive()) { colors[p.index] = color(0,0,0); continue; }
261
262 P.set(p);
263
264 if (DG._Spark () > 0) P.y += DG._Spark () * (noise(P.x,P.y+NoiseMove/30 ,P.z)*ydMax - ydMax/2.);
265 if (DG._Wiggle() > 0) P.y += DG._Wiggle() * (noise(P.x/(xdMax*.3)-NoiseMove/1500.) - .5) * (ydMax/2.);
266
267 color cOld = colors[p.index];
268
269 color cNew = CalcPoint(P);
270 if (DG._XSym) cNew = blendColor(cNew, CalcPoint(new xyz(xdMax-P.x,P.y,P.z)), ADD);
271 if (DG._YSym) cNew = blendColor(cNew, CalcPoint(new xyz(P.x,ydMax-P.y,P.z)), ADD);
272 if (DG._ZSym) cNew = blendColor(cNew, CalcPoint(new xyz(P.x,P.y,zdMax-P.z)), ADD);
273
274 float b = brightness(cNew)/100.;
275 b = b < .5 ? pow(b,fSharp) : 1-pow(1-b,fSharp);
276
277 float noizhue = DG._NoiseHue()==0 ? 0 : DG._NoiseHue()*360*noise(
278 P.x/(xdMax*.3)+NoiseMove*.0003,
279 P.y/(ydMax*.3)+NoiseMove*.00025,
280 P.z/(zdMax*.3)+NoiseMove*.0002 );
281
282 cNew = color( (hue(cNew) + modhue + zSpinHue - noizhue) % 360,
283 saturation(cNew) + 100*DG._Saturate(),
284 100 * (DG._Trails()==0 ? b : max(b, (float) (brightness(cOld)/100. - (1-DG._Trails()) * deltaMs/200.)))
285 * (DG._Dim ()==0 ? 1 : 1-DG._Dim())
286 );
287
288 colors[p.index] = cNew;
289 }
290 }
291 }
292 //----------------------------------------------------------------------------------------------------------------------------------