updates on 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 int Cur() { return (CurRow-StartRow)*NumApcCols + CurCol; }
11 String CurDesc() { return Desc[Cur()]; }
12 void reset() { CurCol = Default % NumApcCols; CurRow = StartRow + Default / NumApcCols; }
13
14 int NumPicks, Default ,
15 CurRow , CurCol ,
16 StartRow, EndRow ;
17 String tag , Desc[] ;
18
19 Pick (String label, int _Def, int _Max, int nStart, String d[]) {
20 NumPicks = _Max; Default = _Def;
21 StartRow = nStart; EndRow = StartRow + int((NumPicks-1) / NumApcCols);
22 tag = label; Desc = d;
23 reset();
24 }
25 }
26
27 public class DBool {
28 boolean def, b;
29 String tag;
30 int row, col;
31 void reset() { b = def; }
32 DBool(String _tag, boolean _def, int _row, int _col) {
33 def = _def; b = _def; tag = _tag; row = _row; col = _col;
34 }
35 }
36
37 public class DParam extends BasicParameter {
38 double dflt;
39 DParam (String label, double value) { super(label,value); dflt=value; }
40 void Set (double value) { super.setValue(value); }
41 void reset () { super.setValue(dflt); }
42 float Val () { return getValuef(); }
43 }
44 //----------------------------------------------------------------------------------------------------------------------------------
45 public class xyz { float x,y,z;
46 xyz() {x=y=z=0;}
47 xyz(Point p ) {x=p.fx ; y=p.fy; z=p.fz;}
48 xyz(float _x,float _y,float _z) {x=_x ; y=_y ; z=_z ;}
49 void set(Point p ) {x=p.fx ; y=p.fy; z=p.fz;}
50 void set(float _x,float _y,float _z) {x=_x ; y=_y ; z=_z ;}
51 float distance(xyz b) {return dist(x,y,z,b.x,b.y,b.z); }
52 float dot (xyz b) {return x*b.x + y*b.y + z*b.z; }
53 xyz minus (xyz b) {return new xyz(x-b.x,y-b.y,z-b.z); }
54 void add (xyz b) {x += b.x; y += b.y; z += b.z; }
55 void add (float b) {x += b ; y += b ; z += b ; }
56
57 void RotateZ (xyz o, float nSin, float nCos) {
58 float nX = nCos*(x-o.x) - nSin*(y-o.y) + o.x;
59 float nY = nSin*(x-o.x) + nCos*(y-o.y) + o.y;
60 x = nX; y = nY;
61 }
62
63 void RotateX (xyz o, float nSin, float nCos) {
64 float nY = nCos*(y-o.y) - nSin*(z-o.z) + o.y;
65 float nZ = nSin*(y-o.y) + nCos*(z-o.z) + o.z;
66 y = nY; z = nZ;
67 }
68
69 void RotateY (xyz o, float nSin, float nCos) {
70 float nZ = nCos*(z-o.z) - nSin*(x-o.x) + o.z;
71 float nX = nSin*(z-o.z) + nCos*(x-o.x) + o.x;
72 z = nZ; x = nX;
73 }
74
75 xyz setRand () { return new xyz ( random(xdMax), random(ydMax), random(zdMax)); }
76 xyz setNorm () { return new xyz ( x / xdMax, y / ydMax, z / zdMax); }
77
78 float interp (float a, float b, float c) { return (1-a)*b + a*c; }
79 xyz interpolate(float i, xyz d) { return new xyz ( interp(i,x,d.x), interp(i,y,d.y), interp(i,z,d.z)); }
80 }
81 //----------------------------------------------------------------------------------------------------------------------------------
82 public class DGlobals {
83 boolean bInit = false;
84 MidiOutput APCOut = null;
85 MidiInput APCIn = null, OxygenIn = null;
86 DPat CurPat = null;
87
88 float Sliders[] = new float [] {1,0,0,0,0,0,0,0};
89 String SliderText[] = new String[] {"Level", "SpinHue", "Spark", "Wiggle", "Trails", "??", "??", "??"};
90
91 int mapRow (int a) { return btwn(a,53,57) ? a-53 : a; }
92 int unmapRow (int a) { return btwn(a,0 , 4) ? a+53 : a; }
93
94 void SetLight (int row, int col, int clr){ if (APCOut != null) APCOut.sendNoteOn(col, unmapRow(row), clr); }
95 void SetKnob (int cc , int c , int v ){ if (APCOut != null) APCOut.sendController(cc , c, v); }
96
97 DBool GetBool (int i) { return (DBool)CurPat.bools .get(i); }
98 Pick GetPick (int i) { return (Pick) CurPat.picks .get(i); }
99 DParam GetParam(int i) { return (DParam) CurPat.params.get(i); }
100
101 float _Dim () { return Sliders[0]; }
102 float _SpinHue () { return Sliders[1]; }
103 float _Spark () { return Sliders[2]; }
104 float _Wiggle () { return Sliders[3]; }
105 float _Trails () { return Sliders[4]; }
106
107 void Init () {
108 if (bInit) return; bInit=true;
109 for (MidiOutputDevice o: RWMidi.getOutputDevices()) { if (o.toString().contains("APC")) { APCOut = o.createOutput(); break;}}
110 for (MidiInputDevice i: RWMidi.getInputDevices ()) { if (i.toString().contains("APC")) { i.createInput (this); break;}}
111 }
112
113 boolean isFocused () { return CurPat != null && CurPat == midiEngine.getFocusedDeck().getActivePattern(); }
114 void Deactivate (DPat p) { if (p != CurPat) return; uiDebugText.setText(""); CurPat = null; }
115 void Activate (DPat p) {
116 CurPat = p;
117 while (lx.tempo.bpm() > 40) lx.tempo.setBpm(lx.tempo.bpm()/2);
118 for (int i=0; i<p.params.size(); i++) GetParam(i).reset();
119 for (int i=0; i<p.bools .size(); i++) GetBool (i).reset();
120 for (int i=0; i<p.picks .size(); i++) GetPick (i).reset();
121 UpdateLights();
122 }
123
124 void SetText() {
125 if (!isFocused()) return;
126 String Text1="", Text2="";
127 for (int i=0; i<CurPat.bools.size(); i++) if (GetBool(i).b) Text1 += " " + GetBool(i).tag + " ";
128 for (int i=0; i<CurPat.picks.size(); i++) Text1 += GetPick(i).tag + ": " + GetPick(i).CurDesc() + " ";
129 for (int i=0; i<5; i++) Text2 += SliderText[i] + ": " + int(100*Sliders[i]) + " ";
130 uiDebugText.setText(Text1, Text2);
131 }
132
133 void UpdateLights() {
134 if (!isFocused()) return;
135 for (int i=0; i<NumApcRows ; i++) for (int j=0; j<NumApcCols; j++) SetLight(i, j, 0);
136 for (int i=48;i< 56 ; i++) SetKnob(0, i, 0);
137 for (int i=16;i< 20 ; i++) SetKnob(0, i, 0);
138
139 for (int i=0; i<CurPat.picks.size() ; i++) SetLight (GetPick(i).CurRow, GetPick(i).CurCol, 3);
140 for (int i=0; i<CurPat.bools.size() ; i++) SetLight (GetBool(i).row, GetBool(i).col, GetBool(i).b ? 3 : 0);
141 for (int i=0; i<CurPat.params.size(); i++) SetKnob ( 0, i<=55 ? 48+i : 16 + i - 8, int(GetParam(i).Val()*127) );
142 }
143
144 void controllerChangeReceived(rwmidi.Controller cc) {
145 if (cc.getCC() == 7 && btwn(cc.getChannel(),0,7)) { Sliders[cc.getChannel()] = 1.*cc.getValue()/127.; }
146 }
147
148 double Tap1 = 0;
149 double getNow() { return millis() + 1000*second() + 60*1000*minute() + 3600*1000*hour(); }
150
151 void noteOffReceived(Note note) {
152 if (!isFocused()) return;
153 int row = 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(); return; }
170
171 for (int i=0; i<CurPat.picks.size(); i++) { Pick P = GetPick(i);
172 if (!btwn(row,P.StartRow,P.EndRow) ) continue;
173 if (!btwn(col,0,NumApcCols-1) ) continue;
174 if (!btwn((row-P.StartRow)*NumApcCols + col,0,P.NumPicks-1) ) continue;
175 P.CurRow=row; P.CurCol=col; return;
176 }
177
178 for (int i=0; i<CurPat.bools.size(); i++) { DBool B = GetBool(i);
179 if (row == GetBool(i).row && col == B.col) { B.b = !B.b; return; }
180 }
181 }
182
183
184 }
185 //----------------------------------------------------------------------------------------------------------------------------------
186 public class DPat extends SCPattern
187 {
188 ArrayList picks = new ArrayList();
189 ArrayList params = new ArrayList();
190 ArrayList bools = new ArrayList();
191 int nMaxRow = 0;
192 float zSpinHue = 0;
193 float LastQuant = -1, LastJog = -1;
194 int nPoint , nPoints;
195 xyz xyzHalf = new xyz(.5,.5,.5),
196 xyzdMax = new xyz(),
197 xyzMid = new xyz(),
198 xyzJog = new xyz(0,0,0);
199
200 float NoiseMove = random(10000);
201 DParam pSharp, pQuantize, pSaturate;
202 DBool pXsym, pYsym, pZsym, pJog;
203 float Dist (xyz a, xyz b) { return dist(a.x,a.y,a.z,b.x,b.y,b.z); }
204 int c1c (float a) { return int(100*constrain(a,0,1)); }
205 float CalcCone (xyz v1, xyz v2, xyz c) { return degrees( acos ( v1.minus(c).dot(v2.minus(c)) /
206 (sqrt(v1.minus(c).dot(v1.minus(c))) * sqrt(v2.minus(c).dot(v2.minus(c))) ) )); }
207 void StartRun(double deltaMs) { }
208 color CalcPoint(xyz p) { return color(0,0,0); }
209 boolean IsActive() { return this == DG.CurPat; }
210 boolean IsFocused() { return this == midiEngine.getFocusedDeck().getActivePattern(); }
211 void onInactive() { UpdateState(); }
212 void onActive () { UpdateState(); }
213 void UpdateState() { if (IsFocused() != IsActive()) { if (IsFocused()) DG.Activate(this); else DG.Deactivate(this); } }
214
215 DParam addParam(String label, double value) {
216 DParam P = new DParam(label, value);
217 super.addParameter(P);
218 params.add(P); return P;
219 }
220
221 Pick addPick(String name, int def, int nmax, String[] desc) {
222 Pick P = new Pick(name, def, nmax, nMaxRow, desc);
223 nMaxRow = P.EndRow + 1;
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 pQuantize = addParam("Qunt", 0 );
233 pSaturate = addParam("Sat" , .5);
234
235 nPoints = model.points.size();
236 xdMax = model.xMax;
237 ydMax = model.yMax;
238 zdMax = model.zMax;
239 xyzdMax = new xyz(xdMax,ydMax,zdMax);
240 xyzMid = new xyz(xdMax/2, ydMax/2, zdMax/2);
241
242 bools.add(pXsym = new DBool("X-SYM", false, 82, 0));
243 bools.add(pYsym = new DBool("Y-SYM", false, 83, 0));
244 bools.add(pZsym = new DBool("Z-SYM", false, 84, 0));
245 bools.add(pJog = new DBool("JOGGER",false, 85, 0));
246 }
247
248 void run(double deltaMs)
249 {
250 UpdateState();
251 NoiseMove += deltaMs;
252 StartRun (deltaMs);
253 zSpinHue += DG._SpinHue ()*deltaMs*.05;
254 xyz P = new xyz();
255 float fSharp = 1/(1.0001-pSharp.Val());
256 float fQuant = pQuantize.Val();
257 float fSaturate = pSaturate.Val();
258
259 DG.SetText();
260 nPoint = 0;
261
262 if (fQuant > 0) {
263 float tRamp = (lx.tempo.rampf() % (1./pow(2,int((1-fQuant) * 4))));
264 float f = LastQuant; LastQuant = tRamp; if (tRamp > f) return;
265 }
266
267
268 if (pJog.b) {
269 float tRamp = (lx.tempo.rampf() % .25);
270 if (tRamp < LastJog)
271 xyzJog.set(random(xdMax*.2)-.1,
272 random(ydMax*.2)-.1,
273 random(zdMax*.2)-.1);
274 LastJog = tRamp;
275 }
276
277 for (Point p : model.points) { nPoint++;
278 P.set(p);
279 if (pJog.b) P.add(xyzJog);
280 if (DG._Spark () > 0) P.y += DG._Spark () * (noise(P.x,P.y+NoiseMove/30 ,P.z)*ydMax - ydMax/2.);
281 if (DG._Wiggle() > 0) P.y += DG._Wiggle() * (noise(P.x/(xdMax*.3)-NoiseMove/1500.) - .5) * (ydMax/2.);
282
283 color cOld = colors[p.index];
284 color cNew = CalcPoint(P);
285
286 if (pXsym.b) cNew = blendColor(cNew, CalcPoint(new xyz(xdMax-P.x,P.y,P.z)), ADD);
287 if (pYsym.b) cNew = blendColor(cNew, CalcPoint(new xyz(P.x,ydMax-P.y,P.z)), ADD);
288 if (pZsym.b) cNew = blendColor(cNew, CalcPoint(new xyz(P.x,P.y,zdMax-P.z)), ADD);
289
290 float b = brightness(cNew)/100.;
291
292 if (pSharp.Val()>0) b = b < .5 ? pow(b,fSharp) : 1-pow(1-b,fSharp);
293 if (DG._Trails()>0 && fQuant == 0) b = max(b, (float) (brightness(cOld)/100. - (1-DG._Trails()) * deltaMs/200.));
294
295 colors[p.index] = color(
296 (hue(cNew) + zSpinHue) % 360,
297 saturation(cNew) + 100*(fSaturate*2-1),
298 100 * b * DG._Dim()
299 );
300 }
301 }
302 }
303 //----------------------------------------------------------------------------------------------------------------------------------