Commit | Line | Data |
---|---|---|
e28f168c | 1 | //---------------------------------------------------------------------------------------------------------------------------------- |
4404e6b8 | 2 | xyz mMax, mCtr, mHalf; |
dd00b10f | 3 | int NumApcRows = 5, NumApcCols = 8; |
59b99905 | 4 | DGlobals DG = new DGlobals(); |
e28f168c | 5 | |
4404e6b8 | 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 | float interp (float a, float b, float c) { return (1-a)*b + a*c; } | |
9 | float randctr (float a) { return random(a) - a*.5; } | |
10 | float min (float a, float b, float c, float d) { return min(min(a,b),min(c,d)); } | |
11 | ||
12 | float distToSeg (float x, float y, float x1, float y1, float x2, float y2) { | |
13 | float A = x - x1, B = y - y1, C = x2 - x1, D = y2 - y1; | |
14 | float dot = A * C + B * D, len_sq = C * C + D * D; | |
15 | float xx, yy,param = dot / len_sq; | |
16 | ||
17 | if (param < 0 || (x1 == x2 && y1 == y2)) { xx = x1; yy = y1; } | |
18 | else if (param > 1) { xx = x2; yy = y2; } | |
19 | else { xx = x1 + param * C; | |
20 | yy = y1 + param * D; } | |
21 | float dx = x - xx, dy = y - yy; | |
22 | return sqrt(dx * dx + dy * dy); | |
23 | } | |
24 | ||
e28f168c AG |
25 | |
26 | public class Pick { | |
3661fcee | 27 | int NumPicks, Default , CurRow , CurCol , |
6680a94a | 28 | StartRow, EndRow ; |
29 | String tag , Desc[] ; | |
30 | ||
32986078 | 31 | Pick (String label, int _Def, int _Num, int nStart, String d[]) { |
32 | NumPicks = _Num; Default = _Def; | |
33 | StartRow = nStart; EndRow = StartRow + floor((NumPicks-1) / NumApcCols); | |
6680a94a | 34 | tag = label; Desc = d; |
35 | reset(); | |
36 | } | |
3661fcee | 37 | |
38 | int Cur() { return (CurRow-StartRow)*NumApcCols + CurCol; } | |
39 | String CurDesc() { return Desc[Cur()]; } | |
40 | void reset() { CurCol = Default % NumApcCols; CurRow = StartRow + Default / NumApcCols; } | |
41 | ||
42 | boolean set(int r, int c) { | |
43 | if (!btwn(r,StartRow,EndRow) || !btwn(c,0,NumApcCols-1) || | |
44 | !btwn((r-StartRow)*NumApcCols + c,0,NumPicks-1)) return false; | |
45 | CurRow=r; CurCol=c; return true; | |
46 | } | |
e28f168c | 47 | } |
6680a94a | 48 | |
49 | public class DBool { | |
50 | boolean def, b; | |
51 | String tag; | |
52 | int row, col; | |
53 | void reset() { b = def; } | |
3661fcee | 54 | boolean set (int r, int c, boolean val) { if (r != row || c != col) return false; b = val; return true; } |
6680a94a | 55 | DBool(String _tag, boolean _def, int _row, int _col) { |
56 | def = _def; b = _def; tag = _tag; row = _row; col = _col; | |
57 | } | |
58 | } | |
59 | ||
60 | public class DParam extends BasicParameter { | |
ef52f20b | 61 | double dflt; |
6680a94a | 62 | DParam (String label, double value) { super(label,value); dflt=value; } |
3661fcee | 63 | void set (double value) { super.setValue(value); } |
b91ad0b6 | 64 | void reset () { super.setValue(dflt); } |
e28f168c | 65 | float Val () { return getValuef(); } |
e28f168c | 66 | } |
e28f168c AG |
67 | //---------------------------------------------------------------------------------------------------------------------------------- |
68 | public class xyz { float x,y,z; | |
69 | xyz() {x=y=z=0;} | |
70 | xyz(Point p ) {x=p.fx ; y=p.fy; z=p.fz;} | |
4404e6b8 | 71 | xyz(xyz p ) {set(p); } |
e28f168c | 72 | xyz(float _x,float _y,float _z) {x=_x ; y=_y ; z=_z ;} |
dd00b10f | 73 | void set(Point p ) {x=p.fx ; y=p.fy; z=p.fz;} |
3661fcee | 74 | void set(xyz p ) {x=p.x ; y=p.y ; z=p.z ;} |
e28f168c | 75 | void set(float _x,float _y,float _z) {x=_x ; y=_y ; z=_z ;} |
3661fcee | 76 | |
4404e6b8 | 77 | void zoomX (float zx) {x = x*zx - mMax.x*(zx-1)/2; } |
78 | void zoomY (float zy) {y = y*zy - mMax.y*(zy-1)/2; } | |
3661fcee | 79 | |
c9e7c456 | 80 | float distance(xyz b) {return dist(x,y,z,b.x,b.y,b.z); } |
4404e6b8 | 81 | float distance(float _x, float _y) {return dist(x,y,_x,_y); } |
c9e7c456 | 82 | float dot (xyz b) {return x*b.x + y*b.y + z*b.z; } |
6680a94a | 83 | void add (xyz b) {x += b.x; y += b.y; z += b.z; } |
84 | void add (float b) {x += b ; y += b ; z += b ; } | |
3661fcee | 85 | void subtract(xyz b) {x -= b.x; y -= b.y; z -= b.z; } |
4404e6b8 | 86 | void scale (float b) {x *= b ; y *= b ; z *= b ; } |
c9e7c456 | 87 | |
88 | void RotateZ (xyz o, float nSin, float nCos) { | |
89 | float nX = nCos*(x-o.x) - nSin*(y-o.y) + o.x; | |
90 | float nY = nSin*(x-o.x) + nCos*(y-o.y) + o.y; | |
91 | x = nX; y = nY; | |
92 | } | |
dd00b10f | 93 | |
c9e7c456 | 94 | void RotateX (xyz o, float nSin, float nCos) { |
95 | float nY = nCos*(y-o.y) - nSin*(z-o.z) + o.y; | |
96 | float nZ = nSin*(y-o.y) + nCos*(z-o.z) + o.z; | |
97 | y = nY; z = nZ; | |
98 | } | |
99 | ||
100 | void RotateY (xyz o, float nSin, float nCos) { | |
101 | float nZ = nCos*(z-o.z) - nSin*(x-o.x) + o.z; | |
102 | float nX = nSin*(z-o.z) + nCos*(x-o.x) + o.x; | |
103 | z = nZ; x = nX; | |
dd00b10f | 104 | } |
105 | ||
4404e6b8 | 106 | void setRand () { x = random(mMax.x); y = random(mMax.y); z = random(mMax.z); } |
107 | void setNorm () { x /= mMax.x; y /= mMax.y; z /= mMax.z; } | |
3661fcee | 108 | void interpolate(float i, xyz d) { x = interp(i,x,d.x); y = interp(i,y,d.y); z = interp(i,z,d.z); } |
e28f168c AG |
109 | } |
110 | //---------------------------------------------------------------------------------------------------------------------------------- | |
dd00b10f | 111 | public class DGlobals { |
112 | boolean bInit = false; | |
113 | MidiOutput APCOut = null; | |
114 | MidiInput APCIn = null, OxygenIn = null; | |
6680a94a | 115 | DPat CurPat = null; |
e28f168c | 116 | |
d5ce8c1c | 117 | |
6680a94a | 118 | float Sliders[] = new float [] {1,0,0,0,0,0,0,0}; |
32986078 | 119 | String SliderText[] = new String[] {"Level", "SpinHue", "Spark", "Xwave", "Ywave", "Trails", "??", "??", "??"}; |
3661fcee | 120 | |
121 | void SetNoteOn (int row, int col, int clr){ if (APCOut != null) APCOut.sendNoteOn (col, row, clr); } | |
122 | void SetNoteOff (int row, int col, int clr){ if (APCOut != null) APCOut.sendNoteOff (col, row, clr); } | |
123 | void SetKnob (int cc , int c , int v ){ if (APCOut != null) APCOut.sendController (cc , c, v); } | |
e28f168c | 124 | |
3661fcee | 125 | DBool GetBool (int i) { return (DBool)CurPat.bools .get(i); } |
126 | Pick GetPick (int i) { return (Pick) CurPat.picks .get(i); } | |
127 | DParam GetParam(int i) { return (DParam) CurPat.params.get(i); } | |
6680a94a | 128 | |
3661fcee | 129 | float _Dim () { return Sliders[0]; } |
130 | float _SpinHue () { return Sliders[1]; } | |
131 | float _Spark () { return Sliders[2]; } | |
32986078 | 132 | float _XWave () { return Sliders[3]; } |
133 | float _YWave () { return Sliders[4]; } | |
134 | float _Trails () { return Sliders[5]; } | |
e28f168c | 135 | |
3661fcee | 136 | void Init () { |
dd00b10f | 137 | if (bInit) return; bInit=true; |
6680a94a | 138 | for (MidiOutputDevice o: RWMidi.getOutputDevices()) { if (o.toString().contains("APC")) { APCOut = o.createOutput(); break;}} |
139 | for (MidiInputDevice i: RWMidi.getInputDevices ()) { if (i.toString().contains("APC")) { i.createInput (this); break;}} | |
c9e7c456 | 140 | } |
6680a94a | 141 | |
3661fcee | 142 | boolean isFocused () { return CurPat != null && CurPat == midiEngine.getFocusedDeck().getActivePattern(); } |
143 | void Deactivate (DPat p) { if (p != CurPat) return; uiDebugText.setText(""); CurPat = null; } | |
144 | void Activate (DPat p) { | |
59b99905 | 145 | CurPat = p; |
dd00b10f | 146 | while (lx.tempo.bpm() > 40) lx.tempo.setBpm(lx.tempo.bpm()/2); |
6680a94a | 147 | for (int i=0; i<p.params.size(); i++) GetParam(i).reset(); |
148 | for (int i=0; i<p.bools .size(); i++) GetBool (i).reset(); | |
149 | for (int i=0; i<p.picks .size(); i++) GetPick (i).reset(); | |
dd00b10f | 150 | UpdateLights(); |
e28f168c AG |
151 | } |
152 | ||
3661fcee | 153 | void SetText() { |
6680a94a | 154 | if (!isFocused()) return; |
155 | String Text1="", Text2=""; | |
156 | for (int i=0; i<CurPat.bools.size(); i++) if (GetBool(i).b) Text1 += " " + GetBool(i).tag + " "; | |
157 | for (int i=0; i<CurPat.picks.size(); i++) Text1 += GetPick(i).tag + ": " + GetPick(i).CurDesc() + " "; | |
d5ce8c1c | 158 | for (int i=0; i<8; i++) Text2 += SliderText[i] + ": " + round(100*Sliders[i]) + " "; |
6680a94a | 159 | uiDebugText.setText(Text1, Text2); |
160 | } | |
161 | ||
dd00b10f | 162 | void UpdateLights() { |
3661fcee | 163 | if (!isFocused() || APCOut == null) return; |
164 | for (int i=53;i< 58; i++) for (int j=0; j<NumApcCols; j++) SetNoteOn(i, j, 0); | |
165 | for (int i=48;i< 56; i++) SetKnob(0, i, 0); | |
166 | for (int i=16;i< 20; i++) SetKnob(0, i, 0); | |
167 | ||
32986078 | 168 | for (int i=0; i<CurPat.params.size(); i++) SetKnob ( 0, i<8 ? 48+i : 16 + i - 8, round(GetParam(i).Val()*127) ); |
3661fcee | 169 | for (int i=0; i<CurPat.picks .size(); i++) SetNoteOn (GetPick(i).CurRow, GetPick(i).CurCol, 3); |
170 | for (int i=0; i<CurPat.bools .size(); i++) if (GetBool(i).b) SetNoteOn (GetBool(i).row, GetBool(i).col, 1); | |
171 | else SetNoteOff (GetBool(i).row, GetBool(i).col, 0); | |
e28f168c | 172 | } |
3661fcee | 173 | |
59b99905 | 174 | void controllerChangeReceived(rwmidi.Controller cc) { |
175 | if (cc.getCC() == 7 && btwn(cc.getChannel(),0,7)) { Sliders[cc.getChannel()] = 1.*cc.getValue()/127.; } | |
176 | } | |
177 | ||
3661fcee | 178 | void noteOffReceived(Note note) { if (!isFocused()) return; |
179 | int row = note.getPitch(), col = note.getChannel(); | |
180 | for (int i=0; i<CurPat.bools.size(); i++) if (GetBool(i).set(row, col, false)) return; | |
e28f168c AG |
181 | UpdateLights(); |
182 | } | |
183 | ||
3661fcee | 184 | void noteOnReceived (Note note) { if (!isFocused()) return; |
185 | int row = note.getPitch(), col = note.getChannel(); | |
186 | for (int i=0; i<CurPat.picks.size(); i++) if (GetPick(i).set(row, col)) return; | |
187 | for (int i=0; i<CurPat.bools.size(); i++) if (GetBool(i).set(row, col, true)) return; | |
4404e6b8 | 188 | //println("row: " + row + " col: " + col); |
e28f168c | 189 | } |
dd00b10f | 190 | } |
191 | //---------------------------------------------------------------------------------------------------------------------------------- | |
192 | public class DPat extends SCPattern | |
193 | { | |
d5ce8c1c | 194 | ArrayList picks = new ArrayList(); // should be ArrayList<Pick> picks = new ArrayList<Pick>(); |
6680a94a | 195 | ArrayList params = new ArrayList(); |
196 | ArrayList bools = new ArrayList(); | |
3661fcee | 197 | int nMaxRow = 53; |
dd00b10f | 198 | float zSpinHue = 0; |
6680a94a | 199 | float LastQuant = -1, LastJog = -1; |
32986078 | 200 | float[] xWaveNz, yWaveNz; |
dd00b10f | 201 | int nPoint , nPoints; |
4404e6b8 | 202 | xyz xyzJog = new xyz(), vT1 = new xyz(), vT2 = new xyz(); |
203 | xyz modmin; | |
dd00b10f | 204 | |
205 | float NoiseMove = random(10000); | |
6680a94a | 206 | DParam pSharp, pQuantize, pSaturate; |
4404e6b8 | 207 | DBool pXsym, pYsym, pRsym, pXdup, pXtrip, pJog; |
dd00b10f | 208 | float Dist (xyz a, xyz b) { return dist(a.x,a.y,a.z,b.x,b.y,b.z); } |
32986078 | 209 | int c1c (float a) { return round(100*constrain(a,0,1)); } |
210 | float interpWv(float i, float[] vals) { return interp(i-floor(i), vals[floor(i)], vals[ceil(i)]); } | |
211 | ||
4404e6b8 | 212 | float CalcCone (xyz v1, xyz v2, xyz c) { vT1.set(v1); vT2.set(v2); vT1.subtract(c); vT2.subtract(c); |
213 | return degrees( acos ( vT1.dot(vT2) / (sqrt(vT1.dot(vT1)) * sqrt(vT2.dot(vT2)) ) )); } | |
32986078 | 214 | |
215 | void StartPattern() { } | |
dd00b10f | 216 | void StartRun(double deltaMs) { } |
217 | color CalcPoint(xyz p) { return color(0,0,0); } | |
59b99905 | 218 | boolean IsActive() { return this == DG.CurPat; } |
219 | boolean IsFocused() { return this == midiEngine.getFocusedDeck().getActivePattern(); } | |
220 | void onInactive() { UpdateState(); } | |
32986078 | 221 | void onActive () { UpdateState(); StartPattern(); } |
59b99905 | 222 | void UpdateState() { if (IsFocused() != IsActive()) { if (IsFocused()) DG.Activate(this); else DG.Deactivate(this); } } |
4404e6b8 | 223 | color blend3(color c1, color c2, color c3){ return blendColor(c1,blendColor(c2,c3,ADD),ADD); } |
e28f168c | 224 | |
6680a94a | 225 | DParam addParam(String label, double value) { |
226 | DParam P = new DParam(label, value); | |
e28f168c | 227 | super.addParameter(P); |
6680a94a | 228 | params.add(P); return P; |
e28f168c | 229 | } |
32986078 | 230 | Pick addPick(String name, int def, int _max, String[] desc) { |
231 | Pick P = new Pick(name, def, _max+1, nMaxRow, desc); | |
e28f168c | 232 | nMaxRow = P.EndRow + 1; |
dd00b10f | 233 | picks.add(P); |
e28f168c AG |
234 | return P; |
235 | } | |
3661fcee | 236 | |
dd00b10f | 237 | DPat(GLucose glucose) { |
238 | super(glucose); | |
4404e6b8 | 239 | pSharp = addParam("Shrp", 0 ); |
240 | pQuantize = addParam("Qunt", 0 ); | |
241 | pSaturate = addParam("Sat" , .5); | |
242 | nPoints = model.points.size(); | |
243 | pXsym = new DBool("X-SYM", false, 49, 0); bools.add(pXsym ); | |
244 | pYsym = new DBool("Y-SYM", false, 49, 1); bools.add(pYsym ); | |
245 | pRsym = new DBool("R-SYM", false, 49, 2); bools.add(pRsym ); | |
246 | pXdup = new DBool("X-DUP", false, 49, 3); bools.add(pXdup ); | |
247 | pJog = new DBool("JOGGER",false, 49, 4); bools.add(pJog ); | |
248 | modmin = new xyz(model.xMin, model.yMin, model.zMin); | |
249 | mMax = new xyz(model.xMax, model.yMax, model.zMax); mMax.subtract(modmin); | |
250 | mCtr = new xyz(mMax); mCtr.scale(.5); | |
251 | mHalf = new xyz(.5,.5,.5); | |
32986078 | 252 | xWaveNz = new float[ceil(mMax.y)+1]; |
253 | yWaveNz = new float[ceil(mMax.x)+1]; | |
254 | ||
4404e6b8 | 255 | //println (model.xMin + " " + model.yMin + " " + model.zMin); |
256 | //println (model.xMax + " " + model.yMax + " " + model.zMax); | |
dd00b10f | 257 | DG.Init(); |
dd00b10f | 258 | } |
259 | ||
260 | void run(double deltaMs) | |
261 | { | |
59b99905 | 262 | UpdateState(); |
32986078 | 263 | NoiseMove += deltaMs; NoiseMove = NoiseMove % 1e7; |
dd00b10f | 264 | StartRun (deltaMs); |
32986078 | 265 | zSpinHue += DG._SpinHue ()*deltaMs*.05; zSpinHue = zSpinHue % 5000.; |
3661fcee | 266 | xyz P = new xyz(), tP = new xyz(), pSave = new xyz(); |
6680a94a | 267 | float fSharp = 1/(1.0001-pSharp.Val()); |
268 | float fQuant = pQuantize.Val(); | |
269 | float fSaturate = pSaturate.Val(); | |
270 | ||
dd00b10f | 271 | DG.SetText(); |
272 | nPoint = 0; | |
6680a94a | 273 | |
274 | if (fQuant > 0) { | |
32986078 | 275 | float tRamp = (lx.tempo.rampf() % (1./pow(2,floor((1-fQuant) * 4)))); |
6680a94a | 276 | float f = LastQuant; LastQuant = tRamp; if (tRamp > f) return; |
277 | } | |
278 | ||
6680a94a | 279 | if (pJog.b) { |
280 | float tRamp = (lx.tempo.rampf() % .25); | |
4404e6b8 | 281 | if (tRamp < LastJog) xyzJog.set(randctr(mMax.x*.2), randctr(mMax.y*.2), randctr(mMax.z*.2)); |
6680a94a | 282 | LastJog = tRamp; |
283 | } | |
284 | ||
32986078 | 285 | // precalculate this stuff |
286 | float yWv = DG._YWave(), xWv = DG._XWave(), sprk = DG._Spark(); | |
287 | if (yWv > 0) for (int i=0; i<ceil(mMax.x)+1; i++) | |
288 | yWaveNz[i] = yWv * (noise(i/(mMax.x*.3)-(2e3+NoiseMove)/1500.) - .5) * (mMax.y/2.); | |
289 | ||
290 | if (xWv > 0) for (int i=0; i<ceil(mMax.y)+1; i++) | |
291 | xWaveNz[i] = xWv * (noise(i/(mMax.y*.3)-(1e3+NoiseMove)/1500.) - .5) * (mMax.x/2.); | |
292 | ||
293 | for (Point p : model.points) { nPoint++; | |
dd00b10f | 294 | P.set(p); |
4404e6b8 | 295 | P.subtract(modmin); |
32986078 | 296 | if (sprk > 0) { P.y += sprk*randctr(50); P.x += sprk*randctr(50); P.z += sprk*randctr(50); } |
297 | if (yWv > 0) P.y += interpWv(p.x-modmin.x, yWaveNz); | |
298 | if (xWv > 0) P.x += interpWv(p.y-modmin.y, xWaveNz); | |
299 | if (pJog.b) P.add(xyzJog); | |
300 | ||
dd00b10f | 301 | |
4404e6b8 | 302 | color cNew, cOld = colors[p.index]; |
303 | { tP.set(P); cNew = CalcPoint(tP); } | |
304 | if (pXsym.b) { tP.set(mMax.x-P.x,P.y,P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); } | |
305 | if (pYsym.b) { tP.set(P.x,mMax.y-P.y,P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); } | |
306 | if (pRsym.b) { tP.set(mMax.x-P.x,mMax.y-P.y,mMax.z-P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); } | |
307 | if (pXdup.b) { tP.set((P.x+mMax.x*.5)%mMax.x,P.y,P.z); cNew = blendColor(cNew, CalcPoint(tP), ADD); } | |
6680a94a | 308 | |
309 | float b = brightness(cNew)/100.; | |
6680a94a | 310 | if (pSharp.Val()>0) b = b < .5 ? pow(b,fSharp) : 1-pow(1-b,fSharp); |
311 | if (DG._Trails()>0 && fQuant == 0) b = max(b, (float) (brightness(cOld)/100. - (1-DG._Trails()) * deltaMs/200.)); | |
312 | ||
313 | colors[p.index] = color( | |
314 | (hue(cNew) + zSpinHue) % 360, | |
315 | saturation(cNew) + 100*(fSaturate*2-1), | |
316 | 100 * b * DG._Dim() | |
317 | ); | |
4404e6b8 | 318 | |
319 | // colors[p.index] = color(0,0, p.fx >= modmin.x && p.fy >= modmin.y && p.fz >= modmin.z && | |
320 | // p.fx <= modmin.x+mMax.x && p.fy <= modmin.y+mMax.y && p.fz <= modmin.z+mMax.z ? 100 : 0); | |
dd00b10f | 321 | } |
322 | } | |
e28f168c | 323 | } |
59b99905 | 324 | //---------------------------------------------------------------------------------------------------------------------------------- |