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