Commit | Line | Data |
---|---|---|
e28f168c | 1 | //---------------------------------------------------------------------------------------------------------------------------------- |
dd00b10f | 2 | float xdMax,ydMax,zdMax; |
3 | int NumApcRows = 5, NumApcCols = 8; | |
59b99905 | 4 | DGlobals DG = new DGlobals(); |
e28f168c AG |
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 { | |
6680a94a | 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 | } | |
e28f168c | 25 | } |
6680a94a | 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 { | |
ef52f20b | 38 | double dflt; |
6680a94a | 39 | DParam (String label, double value) { super(label,value); dflt=value; } |
b91ad0b6 | 40 | void Set (double value) { super.setValue(value); } |
41 | void reset () { super.setValue(dflt); } | |
e28f168c | 42 | float Val () { return getValuef(); } |
e28f168c | 43 | } |
e28f168c AG |
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 ;} | |
dd00b10f | 49 | void set(Point p ) {x=p.fx ; y=p.fy; z=p.fz;} |
e28f168c | 50 | void set(float _x,float _y,float _z) {x=_x ; y=_y ; z=_z ;} |
c9e7c456 | 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); } | |
6680a94a | 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 ; } | |
c9e7c456 | 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 | } | |
dd00b10f | 62 | |
c9e7c456 | 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; | |
dd00b10f | 73 | } |
74 | ||
e28f168c AG |
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 | //---------------------------------------------------------------------------------------------------------------------------------- | |
dd00b10f | 82 | public class DGlobals { |
83 | boolean bInit = false; | |
84 | MidiOutput APCOut = null; | |
85 | MidiInput APCIn = null, OxygenIn = null; | |
6680a94a | 86 | DPat CurPat = null; |
e28f168c | 87 | |
6680a94a | 88 | float Sliders[] = new float [] {1,0,0,0,0,0,0,0}; |
89 | String SliderText[] = new String[] {"Level", "SpinHue", "Spark", "Wiggle", "Trails", "??", "??", "??"}; | |
e28f168c | 90 | |
dd00b10f | 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; } | |
ef52f20b | 93 | |
dd00b10f | 94 | void SetLight (int row, int col, int clr){ if (APCOut != null) APCOut.sendNoteOn(col, unmapRow(row), clr); } |
6680a94a | 95 | void SetKnob (int cc , int c , int v ){ if (APCOut != null) APCOut.sendController(cc , c, v); } |
e28f168c | 96 | |
6680a94a | 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]; } | |
e28f168c | 106 | |
dd00b10f | 107 | void Init () { |
108 | if (bInit) return; bInit=true; | |
6680a94a | 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;}} | |
c9e7c456 | 111 | } |
6680a94a | 112 | |
113 | boolean isFocused () { return CurPat != null && CurPat == midiEngine.getFocusedDeck().getActivePattern(); } | |
114 | void Deactivate (DPat p) { if (p != CurPat) return; uiDebugText.setText(""); CurPat = null; } | |
dd00b10f | 115 | void Activate (DPat p) { |
59b99905 | 116 | CurPat = p; |
dd00b10f | 117 | while (lx.tempo.bpm() > 40) lx.tempo.setBpm(lx.tempo.bpm()/2); |
6680a94a | 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(); | |
dd00b10f | 121 | UpdateLights(); |
e28f168c AG |
122 | } |
123 | ||
6680a94a | 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 | ||
dd00b10f | 133 | void UpdateLights() { |
6680a94a | 134 | if (!isFocused()) return; |
dd00b10f | 135 | for (int i=0; i<NumApcRows ; i++) for (int j=0; j<NumApcCols; j++) SetLight(i, j, 0); |
ef52f20b | 136 | for (int i=48;i< 56 ; i++) SetKnob(0, i, 0); |
137 | for (int i=16;i< 20 ; i++) SetKnob(0, i, 0); | |
6680a94a | 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) ); | |
e28f168c AG |
142 | } |
143 | ||
59b99905 | 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 | ||
6680a94a | 148 | double Tap1 = 0; |
149 | double getNow() { return millis() + 1000*second() + 60*1000*minute() + 3600*1000*hour(); } | |
150 | ||
59b99905 | 151 | void noteOffReceived(Note note) { |
152 | if (!isFocused()) return; | |
6680a94a | 153 | int row = mapRow(note.getPitch()), col = note.getChannel(); |
e28f168c AG |
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 | ||
59b99905 | 165 | void noteOnReceived (Note note) { |
166 | if (!isFocused()) return; | |
e28f168c | 167 | int row = mapRow(note.getPitch()), col = note.getChannel(); |
dd00b10f | 168 | |
6680a94a | 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; } | |
dd00b10f | 180 | } |
e28f168c | 181 | } |
6680a94a | 182 | |
183 | ||
dd00b10f | 184 | } |
185 | //---------------------------------------------------------------------------------------------------------------------------------- | |
186 | public class DPat extends SCPattern | |
187 | { | |
188 | ArrayList picks = new ArrayList(); | |
6680a94a | 189 | ArrayList params = new ArrayList(); |
190 | ArrayList bools = new ArrayList(); | |
dd00b10f | 191 | int nMaxRow = 0; |
192 | float zSpinHue = 0; | |
6680a94a | 193 | float LastQuant = -1, LastJog = -1; |
dd00b10f | 194 | int nPoint , nPoints; |
ef52f20b | 195 | xyz xyzHalf = new xyz(.5,.5,.5), |
196 | xyzdMax = new xyz(), | |
6680a94a | 197 | xyzMid = new xyz(), |
198 | xyzJog = new xyz(0,0,0); | |
dd00b10f | 199 | |
200 | float NoiseMove = random(10000); | |
6680a94a | 201 | DParam pSharp, pQuantize, pSaturate; |
202 | DBool pXsym, pYsym, pZsym, pJog; | |
dd00b10f | 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); } | |
59b99905 | 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); } } | |
e28f168c | 214 | |
6680a94a | 215 | DParam addParam(String label, double value) { |
216 | DParam P = new DParam(label, value); | |
e28f168c | 217 | super.addParameter(P); |
6680a94a | 218 | params.add(P); return P; |
e28f168c | 219 | } |
6680a94a | 220 | |
ef52f20b | 221 | Pick addPick(String name, int def, int nmax, String[] desc) { |
6680a94a | 222 | Pick P = new Pick(name, def, nmax, nMaxRow, desc); |
e28f168c | 223 | nMaxRow = P.EndRow + 1; |
dd00b10f | 224 | picks.add(P); |
e28f168c AG |
225 | return P; |
226 | } | |
6680a94a | 227 | |
dd00b10f | 228 | DPat(GLucose glucose) { |
229 | super(glucose); | |
230 | DG.Init(); | |
6680a94a | 231 | pSharp = addParam("Shrp", 0 ); |
232 | pQuantize = addParam("Qunt", 0 ); | |
233 | pSaturate = addParam("Sat" , .5); | |
234 | ||
dd00b10f | 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); | |
6680a94a | 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)); | |
dd00b10f | 246 | } |
247 | ||
248 | void run(double deltaMs) | |
249 | { | |
59b99905 | 250 | UpdateState(); |
dd00b10f | 251 | NoiseMove += deltaMs; |
252 | StartRun (deltaMs); | |
253 | zSpinHue += DG._SpinHue ()*deltaMs*.05; | |
254 | xyz P = new xyz(); | |
6680a94a | 255 | float fSharp = 1/(1.0001-pSharp.Val()); |
256 | float fQuant = pQuantize.Val(); | |
257 | float fSaturate = pSaturate.Val(); | |
258 | ||
dd00b10f | 259 | DG.SetText(); |
260 | nPoint = 0; | |
6680a94a | 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 | ||
dd00b10f | 277 | for (Point p : model.points) { nPoint++; |
dd00b10f | 278 | P.set(p); |
6680a94a | 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.); | |
dd00b10f | 282 | |
6680a94a | 283 | color cOld = colors[p.index]; |
dd00b10f | 284 | color cNew = CalcPoint(P); |
6680a94a | 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 | ); | |
dd00b10f | 300 | } |
301 | } | |
e28f168c | 302 | } |
59b99905 | 303 | //---------------------------------------------------------------------------------------------------------------------------------- |