The death of GLucose! Hear ye, hear ye.
[SugarCubes.git] / _Internals.pde
1 /**
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//H
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
11 * If you are an artist, you may ignore this file! It just sets
12 * up the framework to run the patterns. Should not need modification
13 * for general animation work.
14 */
15
16 import heronarts.lx.*;
17 import heronarts.lx.effect.*;
18 import heronarts.lx.model.*;
19 import heronarts.lx.modulator.*;
20 import heronarts.lx.parameter.*;
21 import heronarts.lx.pattern.*;
22 import heronarts.lx.transform.*;
23 import heronarts.lx.transition.*;
24 import heronarts.lx.ui.*;
25 import heronarts.lx.ui.component.*;
26 import heronarts.lx.ui.control.*;
27 import ddf.minim.*;
28 import ddf.minim.analysis.*;
29 import processing.opengl.*;
30 import rwmidi.*;
31 import java.lang.reflect.*;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
35
36 static final int VIEWPORT_WIDTH = 900;
37 static final int VIEWPORT_HEIGHT = 700;
38
39 static final int LEFT_DECK = 0;
40 static final int RIGHT_DECK = 1;
41
42 // The trailer is measured from the outside of the black metal (but not including the higher welded part on the front)
43 static final float TRAILER_WIDTH = 192;
44 static final float TRAILER_DEPTH = 192;
45 static final float TRAILER_HEIGHT = 33;
46
47 int targetFramerate = 60;
48 int startMillis, lastMillis;
49
50 // Core engine variables
51 LX lx;
52 Model model;
53 LXPattern[] patterns;
54 LXTransition[] transitions;
55 Effects effects;
56 LXEffect[] effectsArr;
57 DiscreteParameter selectedEffect;
58 MappingTool mappingTool;
59 GrizzlyOutput[] grizzlies;
60 PresetManager presetManager;
61 MidiEngine midiEngine;
62
63 // Display configuration mode
64 boolean mappingMode = false;
65 boolean debugMode = false;
66 boolean simulationOn = true;
67 boolean diagnosticsOn = false;
68 LXPattern restoreToPattern = null;
69 PImage logo;
70 float[] hsb = new float[3];
71
72 // Handles to UI objects
73 UIPatternDeck uiPatternA;
74 UICrossfader uiCrossfader;
75 UIMidi uiMidi;
76 UIMapping uiMapping;
77 UIDebugText uiDebugText;
78 UISpeed uiSpeed;
79
80 /**
81 * Engine construction and initialization.
82 */
83
84 LXTransition _transition(LX lx) {
85 return new DissolveTransition(lx).setDuration(1000);
86 }
87
88 LXPattern[] _leftPatterns(LX lx) {
89 LXPattern[] patterns = patterns(lx);
90 for (LXPattern p : patterns) {
91 p.setTransition(_transition(lx));
92 }
93 return patterns;
94 }
95
96 LXPattern[] _rightPatterns(LX lx) {
97 LXPattern[] patterns = _leftPatterns(lx);
98 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
99 int i = 0;
100 rightPatterns[i++] = new BlankPattern(lx).setTransition(_transition(lx));
101 for (LXPattern p : patterns) {
102 rightPatterns[i++] = p;
103 }
104 return rightPatterns;
105 }
106
107 LXEffect[] _effectsArray(Effects effects) {
108 List<LXEffect> effectList = new ArrayList<LXEffect>();
109 for (Field f : effects.getClass().getDeclaredFields()) {
110 try {
111 Object val = f.get(effects);
112 if (val instanceof LXEffect) {
113 effectList.add((LXEffect)val);
114 }
115 } catch (IllegalAccessException iax) {}
116 }
117 return effectList.toArray(new LXEffect[]{});
118 }
119
120 LXEffect getSelectedEffect() {
121 return effectsArr[selectedEffect.getValuei()];
122 }
123
124 void logTime(String evt) {
125 int now = millis();
126 println(evt + ": " + (now - lastMillis) + "ms");
127 lastMillis = now;
128 }
129
130 void setup() {
131 startMillis = lastMillis = millis();
132
133 // Initialize the Processing graphics environment
134 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
135 frameRate(targetFramerate);
136 noSmooth();
137 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
138 logTime("Created viewport");
139
140 // Create the model
141 model = buildModel();
142 logTime("Built Model");
143
144 // LX engine
145 lx = new LX(this, model);
146 lx.enableKeyboardTempo();
147 logTime("Built LX engine");
148
149 // Set the patterns
150 LXEngine engine = lx.engine;
151 engine.setPatterns(patterns = _leftPatterns(lx));
152 engine.addDeck(_rightPatterns(lx));
153 logTime("Built patterns");
154
155 // Transitions
156 transitions = transitions(lx);
157 lx.engine.getDeck(RIGHT_DECK).setFaderTransition(transitions[0]);
158 logTime("Built transitions");
159
160 // Effects
161 lx.addEffects(effectsArr = _effectsArray(effects = new Effects()));
162 selectedEffect = new DiscreteParameter("EFFECT", effectsArr.length);
163 logTime("Built effects");
164
165 // Preset manager
166 presetManager = new PresetManager();
167 logTime("Loaded presets");
168
169 // MIDI devices
170 midiEngine = new MidiEngine();
171 logTime("Setup MIDI devices");
172
173 // Build output driver
174 grizzlies = new GrizzlyOutput[]{};
175 try {
176 grizzlies = buildGrizzlies();
177 for (LXOutput output : grizzlies) {
178 lx.addOutput(output);
179 }
180 } catch (Exception x) {
181 x.printStackTrace();
182 }
183 logTime("Built Grizzly Outputs");
184
185 // Mapping tool
186 mappingTool = new MappingTool(lx);
187 logTime("Built Mapping Tool");
188
189 // Build overlay UI
190 UILayer[] layers = new UILayer[] {
191 // Camera layer
192 new UICameraLayer(lx.ui)
193 .setCenter(model.cx, model.cy, model.cz)
194 .setRadius(290).addComponent(new UICubesLayer()),
195
196 // Left controls
197 uiPatternA = new UIPatternDeck(lx.ui, lx.engine.getDeck(LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
198 new UIBlendMode(4, 332, 140, 86),
199 new UIEffects(4, 422, 140, 144),
200 new UITempo(4, 570, 140, 50),
201 uiSpeed = new UISpeed(4, 624, 140, 50),
202
203 // Right controls
204 new UIPatternDeck(lx.ui, lx.engine.getDeck(RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
205 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
206 new UIOutput(grizzlies, width-144, 494, 140, 106),
207
208 // Crossfader
209 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
210
211 // Overlays
212 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
213 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324)
214 };
215 uiMapping.setVisible(false);
216 for (UILayer layer : layers) {
217 lx.ui.addLayer(layer);
218 }
219 logTime("Built UI");
220
221 // Load logo image
222 logo = loadImage("data/logo.png");
223 logTime("Loaded logo image");
224
225 println("Total setup: " + (millis() - startMillis) + "ms");
226 println("Hit the 'o' key to toggle live output");
227 }
228
229 public SCPattern getPattern() {
230 return (SCPattern) lx.getPattern();
231 }
232
233 /**
234 * Subclass of LXPattern specific to sugar cubes. These patterns
235 * get access to the state and geometry, and have some
236 * little helpers for interacting with the model.
237 */
238 public static abstract class SCPattern extends LXPattern {
239
240 protected SCPattern(LX lx) {
241 super(lx);
242 }
243
244 /**
245 * Reset this pattern to its default state.
246 */
247 public final void reset() {
248 for (LXParameter parameter : getParameters()) {
249 parameter.reset();
250 }
251 onReset();
252 }
253
254 /**
255 * Subclasses may override to add additional reset functionality.
256 */
257 protected /*abstract*/ void onReset() {}
258
259 /**
260 * Invoked by the engine when a grid controller button press occurs
261 *
262 * @param row Row index on the gird
263 * @param col Column index on the grid
264 * @return True if the event was consumed, false otherwise
265 */
266 public boolean gridPressed(int row, int col) {
267 return false;
268 }
269
270 /**
271 * Invoked by the engine when a grid controller button release occurs
272 *
273 * @param row Row index on the gird
274 * @param col Column index on the grid
275 * @return True if the event was consumed, false otherwise
276 */
277 public boolean gridReleased(int row, int col) {
278 return false;
279 }
280
281 /**
282 * Invoked by engine when this pattern is focused an a midi note is received.
283 *
284 * @param note
285 * @return True if the pattern has consumed this note, false if the top-level
286 * may handle it
287 */
288 public boolean noteOn(rwmidi.Note note) {
289 return false;
290 }
291
292 /**
293 * Invoked by engine when this pattern is focused an a midi note off is received.
294 *
295 * @param note
296 * @return True if the pattern has consumed this note, false if the top-level
297 * may handle it
298 */
299 public boolean noteOff(rwmidi.Note note) {
300 return false;
301 }
302
303 /**
304 * Invoked by engine when this pattern is focused an a controller is received
305 *
306 * @param note
307 * @return True if the pattern has consumed this controller, false if the top-level
308 * may handle it
309 */
310 public boolean controllerChange(rwmidi.Controller controller) {
311 return false;
312 }
313 }
314
315 long simulationNanos = 0;
316
317 /**
318 * Core render loop and drawing functionality.
319 */
320 void draw() {
321 long drawStart = System.nanoTime();
322
323 // Set background
324 background(40);
325
326 // Send colors
327 color[] sendColors = lx.getColors();
328 long gammaStart = System.nanoTime();
329 // Gamma correction here. Apply a cubic to the brightness
330 // for better representation of dynamic range
331 for (int i = 0; i < sendColors.length; ++i) {
332 lx.RGBtoHSB(sendColors[i], hsb);
333 float b = hsb[2];
334 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
335 }
336 long gammaNanos = System.nanoTime() - gammaStart;
337
338 // Always draw FPS meter
339 drawFPS();
340
341 // TODO(mcslee): fix
342 long drawNanos = System.nanoTime() - drawStart;
343 long uiNanos = 0;
344
345 if (diagnosticsOn) {
346 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos);
347 }
348 }
349
350 class UICubesLayer extends UICameraComponent {
351 void onDraw(UI ui) {
352 color[] simulationColors = lx.getColors();
353 String displayMode = uiCrossfader.getDisplayMode();
354 if (displayMode == "A") {
355 simulationColors = lx.engine.getDeck(LEFT_DECK).getColors();
356 } else if (displayMode == "B") {
357 simulationColors = lx.engine.getDeck(RIGHT_DECK).getColors();
358 }
359
360 long simulationStart = System.nanoTime();
361 if (simulationOn) {
362 drawSimulation(simulationColors);
363 }
364 simulationNanos = System.nanoTime() - simulationStart;
365
366 camera();
367 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
368 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
369 ((PGraphicsOpenGL)g).endGL();
370 strokeWeight(1);
371 }
372
373 void drawSimulation(color[] simulationColors) {
374 translate(0, 30, 0);
375
376 noStroke();
377 fill(#141414);
378 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
379 fill(#070707);
380 stroke(#222222);
381 beginShape();
382 vertex(0, 0, 0);
383 vertex(TRAILER_WIDTH, 0, 0);
384 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
385 vertex(0, 0, TRAILER_DEPTH);
386 endShape();
387
388 // Draw the logo on the front of platform
389 pushMatrix();
390 translate(0, 0, -1);
391 float s = .07;
392 scale(s, -s, s);
393 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
394 popMatrix();
395
396 noStroke();
397 for (Cube c : model.cubes) {
398 drawCube(c);
399 }
400
401 noFill();
402 strokeWeight(2);
403 beginShape(POINTS);
404 for (LXPoint p : model.points) {
405 stroke(simulationColors[p.index]);
406 vertex(p.x, p.y, p.z);
407 }
408 endShape();
409 }
410
411 void drawCube(Cube c) {
412 float in = .15;
413 noStroke();
414 fill(#393939);
415 drawBox(c.x+in, c.y+in, c.z+in, c.rx, c.ry, c.rz, Cube.EDGE_WIDTH-in*2, Cube.EDGE_HEIGHT-in*2, Cube.EDGE_WIDTH-in*2, Cube.CHANNEL_WIDTH-in);
416 }
417
418 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
419 pushMatrix();
420 translate(x, y, z);
421 rotate(rx / 180. * PI, -1, 0, 0);
422 rotate(ry / 180. * PI, 0, -1, 0);
423 rotate(rz / 180. * PI, 0, 0, -1);
424 for (int i = 0; i < 4; ++i) {
425 float wid = (i % 2 == 0) ? xd : zd;
426
427 beginShape();
428 vertex(0, 0);
429 vertex(wid, 0);
430 vertex(wid, yd);
431 vertex(wid - sw, yd);
432 vertex(wid - sw, sw);
433 vertex(0, sw);
434 endShape();
435 beginShape();
436 vertex(0, sw);
437 vertex(0, yd);
438 vertex(wid - sw, yd);
439 vertex(wid - sw, yd - sw);
440 vertex(sw, yd - sw);
441 vertex(sw, sw);
442 endShape();
443
444 translate(wid, 0, 0);
445 rotate(HALF_PI, 0, -1, 0);
446 }
447 popMatrix();
448 }
449 }
450
451 void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos) {
452 float ws = 4 / 1000000.;
453 int thirtyfps = 1000000000 / 30;
454 int sixtyfps = 1000000000 / 60;
455 int x = width - 138;
456 int y = height - 14;
457 int h = 10;
458 noFill();
459 stroke(#999999);
460 rect(x, y, thirtyfps * ws, h);
461 noStroke();
462 int xp = x;
463 float hv = 0;
464 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, lx.timer.outputNanos }) {
465 fill(lx.hsb(hv % 360, 100, 80));
466 rect(xp, y, val * ws, h-1);
467 hv += 140;
468 xp += val * ws;
469 }
470 noFill();
471 stroke(#333333);
472 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
473
474 y = y - 14;
475 xp = x;
476 float tw = thirtyfps * ws;
477 noFill();
478 stroke(#999999);
479 rect(x, y, tw, h);
480 h = 5;
481 noStroke();
482 for (long val : new long[] {
483 lx.engine.timer.deckNanos,
484 lx.engine.timer.copyNanos,
485 lx.engine.timer.fxNanos}) {
486 float amt = val / (float) lx.timer.drawNanos;
487 fill(lx.hsb(hv % 360, 100, 80));
488 rect(xp, y, amt * tw, h-1);
489 hv += 140;
490 xp += amt * tw;
491 }
492
493 xp = x;
494 y += h;
495 hv = 120;
496 for (long val : new long[] {
497 lx.engine.getDeck(0).timer.runNanos,
498 lx.engine.getDeck(1).timer.runNanos,
499 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
500 float amt = val / (float) lx.timer.drawNanos;
501 fill(lx.hsb(hv % 360, 100, 80));
502 rect(xp, y, amt * tw, h-1);
503 hv += 140;
504 xp += amt * tw;
505 }
506 }
507
508 void drawFPS() {
509 // Always draw FPS meter
510 fill(#555555);
511 textSize(9);
512 textAlign(LEFT, BASELINE);
513 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
514 }
515
516
517 /**
518 * Top-level keyboard event handling
519 */
520 void keyPressed() {
521 if (mappingMode) {
522 mappingTool.keyPressed(uiMapping);
523 }
524 switch (key) {
525 case '1':
526 case '2':
527 case '3':
528 case '4':
529 case '5':
530 case '6':
531 case '7':
532 case '8':
533 if (!midiEngine.isQwertyEnabled()) {
534 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
535 }
536 break;
537
538 case '!':
539 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
540 break;
541 case '@':
542 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
543 break;
544 case '#':
545 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
546 break;
547 case '$':
548 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
549 break;
550 case '%':
551 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
552 break;
553 case '^':
554 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
555 break;
556 case '&':
557 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
558 break;
559 case '*':
560 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
561 break;
562
563 case '-':
564 case '_':
565 frameRate(--targetFramerate);
566 break;
567 case '=':
568 case '+':
569 frameRate(++targetFramerate);
570 break;
571 case 'b':
572 effects.boom.trigger();
573 break;
574 case 'd':
575 if (!midiEngine.isQwertyEnabled()) {
576 debugMode = !debugMode;
577 println("Debug output: " + (debugMode ? "ON" : "OFF"));
578 }
579 break;
580 case 'm':
581 if (!midiEngine.isQwertyEnabled()) {
582 mappingMode = !mappingMode;
583 uiPatternA.setVisible(!mappingMode);
584 uiMapping.setVisible(mappingMode);
585 if (mappingMode) {
586 restoreToPattern = lx.getPattern();
587 lx.setPatterns(new LXPattern[] { mappingTool });
588 } else {
589 lx.setPatterns(patterns);
590 LXTransition pop = restoreToPattern.getTransition();
591 restoreToPattern.setTransition(null);
592 lx.goPattern(restoreToPattern);
593 restoreToPattern.setTransition(pop);
594 }
595 }
596 break;
597 case 't':
598 if (!midiEngine.isQwertyEnabled()) {
599 lx.engine.setThreaded(!lx.engine.isThreaded());
600 }
601 break;
602 case 'o':
603 case 'p':
604 for (LXOutput output : grizzlies) {
605 output.enabled.toggle();
606 }
607 break;
608 case 'q':
609 if (!midiEngine.isQwertyEnabled()) {
610 diagnosticsOn = !diagnosticsOn;
611 }
612 break;
613 case 's':
614 if (!midiEngine.isQwertyEnabled()) {
615 simulationOn = !simulationOn;
616 }
617 break;
618 }
619 }
620