2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
9 * EXPERTS ONLY!! EXPERTS ONLY!!
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.
17 import heronarts.lx.*;
18 import heronarts.lx.effect.*;
19 import heronarts.lx.model.*;
20 import heronarts.lx.modulator.*;
21 import heronarts.lx.parameter.*;
22 import heronarts.lx.pattern.*;
23 import heronarts.lx.transform.*;
24 import heronarts.lx.transition.*;
25 import heronarts.lx.ui.*;
26 import heronarts.lx.ui.component.*;
27 import heronarts.lx.ui.control.*;
29 import ddf.minim.analysis.*;
30 import processing.opengl.*;
32 import java.lang.reflect.*;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
37 static final int VIEWPORT_WIDTH = 900;
38 static final int VIEWPORT_HEIGHT = 700;
40 static final int LEFT_DECK = 0;
41 static final int RIGHT_DECK = 1;
43 // The trailer is measured from the outside of the black metal (but not including the higher welded part on the front)
44 static final float TRAILER_WIDTH = 192;
45 static final float TRAILER_DEPTH = 192;
46 static final float TRAILER_HEIGHT = 33;
48 int targetFramerate = 60;
49 int startMillis, lastMillis;
51 // Core engine variables
56 LXTransition[] transitions;
57 DiscreteParameter activeTransition;
59 MappingTool mappingTool;
60 GrizzlyOutput[] grizzlies;
61 PresetManager presetManager;
62 MidiEngine midiEngine;
64 // Display configuration mode
65 boolean mappingMode = false;
66 boolean debugMode = false;
67 boolean simulationOn = true;
68 boolean diagnosticsOn = false;
69 LXPattern restoreToPattern = null;
71 float[] hsb = new float[3];
73 // Handles to UI objects
74 UIPatternDeck uiPatternA;
75 UICrossfader uiCrossfader;
78 UIDebugText uiDebugText;
82 * Engine construction and initialization.
85 LXTransition _transition(LX lx) {
86 return new DissolveTransition(lx).setDuration(1000);
89 LXPattern[] _leftPatterns(LX lx) {
90 LXPattern[] patterns = patterns(lx);
91 for (LXPattern p : patterns) {
92 p.setTransition(_transition(lx));
97 LXPattern[] _rightPatterns(LX lx) {
98 LXPattern[] patterns = _leftPatterns(lx);
99 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
101 rightPatterns[i++] = new BlankPattern(lx).setTransition(_transition(lx));
102 for (LXPattern p : patterns) {
103 rightPatterns[i++] = p;
105 return rightPatterns;
108 LXEffect[] _effectsArray(Effects effects) {
109 List<LXEffect> effectList = new ArrayList<LXEffect>();
110 for (Field f : effects.getClass().getDeclaredFields()) {
112 Object val = f.get(effects);
113 if (val instanceof LXEffect) {
114 effectList.add((LXEffect)val);
116 } catch (IllegalAccessException iax) {}
118 return effectList.toArray(new LXEffect[]{});
121 void logTime(String evt) {
123 println(evt + ": " + (now - lastMillis) + "ms");
128 startMillis = lastMillis = millis();
130 // Initialize the Processing graphics environment
131 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
132 frameRate(targetFramerate);
134 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
135 logTime("Created viewport");
137 // Create the GLucose engine to run the cubes
138 glucose = new GLucose(this, model = buildModel());
140 lx.enableKeyboardTempo();
141 logTime("Built GLucose engine");
144 LXEngine engine = lx.engine;
145 engine.setPatterns(patterns = _leftPatterns(lx));
146 engine.addDeck(_rightPatterns(lx));
147 logTime("Built patterns");
150 transitions = transitions(lx);
151 activeTransition = new DiscreteParameter("TRANSITION", transitions.length);
152 activeTransition.addListener(new LXParameterListener() {
153 public void onParameterChanged(LXParameter parameter) {
154 lx.engine.getDeck(RIGHT_DECK).setFaderTransition(transitions[activeTransition.getValuei()]);
157 lx.engine.getDeck(RIGHT_DECK).setFaderTransition(transitions[activeTransition.getValuei()]);
158 logTime("Built transitions");
161 glucose.lx.addEffects(_effectsArray(effects = new Effects()));
162 logTime("Built effects");
165 presetManager = new PresetManager();
166 logTime("Loaded presets");
169 midiEngine = new MidiEngine();
170 logTime("Setup MIDI devices");
172 // Build output driver
173 grizzlies = new GrizzlyOutput[]{};
175 grizzlies = buildGrizzlies();
176 for (LXOutput output : grizzlies) {
177 lx.addOutput(output);
179 } catch (Exception x) {
182 logTime("Built Grizzly Outputs");
185 mappingTool = new MappingTool(lx);
188 UILayer[] layers = new UILayer[] {
190 new UICameraLayer(lx.ui)
191 .setCenter(model.cx, model.cy, model.cz)
192 .setRadius(290).addComponent(new UICubesLayer()),
195 uiPatternA = new UIPatternDeck(lx.ui, lx.engine.getDeck(GLucose.LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
196 new UIBlendMode(4, 332, 140, 86),
197 new UIEffects(4, 422, 140, 144),
198 new UITempo(4, 570, 140, 50),
199 uiSpeed = new UISpeed(4, 624, 140, 50),
202 new UIPatternDeck(lx.ui, lx.engine.getDeck(GLucose.RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
203 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
204 new UIOutput(grizzlies, width-144, 494, 140, 106),
207 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
210 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
211 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324)
213 uiMapping.setVisible(false);
214 for (UILayer layer : layers) {
215 lx.ui.addLayer(layer);
220 logo = loadImage("data/logo.png");
221 logTime("Loaded logo image");
223 println("Total setup: " + (millis() - startMillis) + "ms");
224 println("Hit the 'o' key to toggle live output");
227 public SCPattern getPattern() {
228 return (SCPattern) lx.getPattern();
232 * Subclass of LXPattern specific to sugar cubes. These patterns
233 * get access to the glucose state and geometry, and have some
234 * little helpers for interacting with the model.
236 public static abstract class SCPattern extends LXPattern {
238 protected SCPattern(LX lx) {
243 * Reset this pattern to its default state.
245 public final void reset() {
246 for (LXParameter parameter : getParameters()) {
253 * Subclasses may override to add additional reset functionality.
255 protected /*abstract*/ void onReset() {}
258 * Invoked by the engine when a grid controller button press occurs
260 * @param row Row index on the gird
261 * @param col Column index on the grid
262 * @return True if the event was consumed, false otherwise
264 public boolean gridPressed(int row, int col) {
269 * Invoked by the engine when a grid controller button release occurs
271 * @param row Row index on the gird
272 * @param col Column index on the grid
273 * @return True if the event was consumed, false otherwise
275 public boolean gridReleased(int row, int col) {
280 * Invoked by engine when this pattern is focused an a midi note is received.
283 * @return True if the pattern has consumed this note, false if the top-level
286 public boolean noteOn(rwmidi.Note note) {
291 * Invoked by engine when this pattern is focused an a midi note off is received.
294 * @return True if the pattern has consumed this note, false if the top-level
297 public boolean noteOff(rwmidi.Note note) {
302 * Invoked by engine when this pattern is focused an a controller is received
305 * @return True if the pattern has consumed this controller, false if the top-level
308 public boolean controllerChange(rwmidi.Controller controller) {
313 long simulationNanos = 0;
316 * Core render loop and drawing functionality.
319 long drawStart = System.nanoTime();
325 color[] sendColors = glucose.getColors();
326 long gammaStart = System.nanoTime();
327 // Gamma correction here. Apply a cubic to the brightness
328 // for better representation of dynamic range
329 for (int i = 0; i < sendColors.length; ++i) {
330 lx.RGBtoHSB(sendColors[i], hsb);
332 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
334 long gammaNanos = System.nanoTime() - gammaStart;
336 // Always draw FPS meter
340 long drawNanos = System.nanoTime() - drawStart;
344 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos);
348 class UICubesLayer extends UICameraComponent {
350 color[] simulationColors = glucose.getColors();
351 String displayMode = uiCrossfader.getDisplayMode();
352 if (displayMode == "A") {
353 simulationColors = lx.engine.getDeck(GLucose.LEFT_DECK).getColors();
354 } else if (displayMode == "B") {
355 simulationColors = lx.engine.getDeck(GLucose.RIGHT_DECK).getColors();
358 long simulationStart = System.nanoTime();
360 drawSimulation(simulationColors);
362 simulationNanos = System.nanoTime() - simulationStart;
365 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
366 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
367 ((PGraphicsOpenGL)g).endGL();
371 void drawSimulation(color[] simulationColors) {
376 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
381 vertex(TRAILER_WIDTH, 0, 0);
382 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
383 vertex(0, 0, TRAILER_DEPTH);
386 // Draw the logo on the front of platform
391 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
395 for (Cube c : model.cubes) {
402 for (LXPoint p : model.points) {
403 stroke(simulationColors[p.index]);
404 vertex(p.x, p.y, p.z);
409 void drawCube(Cube c) {
413 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 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
419 rotate(rx / 180. * PI, -1, 0, 0);
420 rotate(ry / 180. * PI, 0, -1, 0);
421 rotate(rz / 180. * PI, 0, 0, -1);
422 for (int i = 0; i < 4; ++i) {
423 float wid = (i % 2 == 0) ? xd : zd;
429 vertex(wid - sw, yd);
430 vertex(wid - sw, sw);
436 vertex(wid - sw, yd);
437 vertex(wid - sw, yd - sw);
442 translate(wid, 0, 0);
443 rotate(HALF_PI, 0, -1, 0);
449 void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos) {
450 float ws = 4 / 1000000.;
451 int thirtyfps = 1000000000 / 30;
452 int sixtyfps = 1000000000 / 60;
458 rect(x, y, thirtyfps * ws, h);
462 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, lx.timer.outputNanos }) {
463 fill(lx.hsb(hv % 360, 100, 80));
464 rect(xp, y, val * ws, h-1);
470 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
474 float tw = thirtyfps * ws;
480 for (long val : new long[] {
481 lx.engine.timer.deckNanos,
482 lx.engine.timer.copyNanos,
483 lx.engine.timer.fxNanos}) {
484 float amt = val / (float) lx.timer.drawNanos;
485 fill(lx.hsb(hv % 360, 100, 80));
486 rect(xp, y, amt * tw, h-1);
494 for (long val : new long[] {
495 lx.engine.getDeck(0).timer.runNanos,
496 lx.engine.getDeck(1).timer.runNanos,
497 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
498 float amt = val / (float) lx.timer.drawNanos;
499 fill(lx.hsb(hv % 360, 100, 80));
500 rect(xp, y, amt * tw, h-1);
507 // Always draw FPS meter
510 textAlign(LEFT, BASELINE);
511 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
516 * Top-level keyboard event handling
520 mappingTool.keyPressed(uiMapping);
531 if (!midiEngine.isQwertyEnabled()) {
532 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
537 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
540 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
543 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
546 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
549 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
552 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
555 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
558 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
563 frameRate(--targetFramerate);
567 frameRate(++targetFramerate);
570 effects.boom.trigger();
573 if (!midiEngine.isQwertyEnabled()) {
574 debugMode = !debugMode;
575 println("Debug output: " + (debugMode ? "ON" : "OFF"));
579 if (!midiEngine.isQwertyEnabled()) {
580 mappingMode = !mappingMode;
581 uiPatternA.setVisible(!mappingMode);
582 uiMapping.setVisible(mappingMode);
584 restoreToPattern = lx.getPattern();
585 lx.setPatterns(new LXPattern[] { mappingTool });
587 lx.setPatterns(patterns);
588 LXTransition pop = restoreToPattern.getTransition();
589 restoreToPattern.setTransition(null);
590 lx.goPattern(restoreToPattern);
591 restoreToPattern.setTransition(pop);
596 if (!midiEngine.isQwertyEnabled()) {
597 lx.engine.setThreaded(!lx.engine.isThreaded());
602 for (LXOutput output : grizzlies) {
603 output.enabled.toggle();
607 if (!midiEngine.isQwertyEnabled()) {
608 diagnosticsOn = !diagnosticsOn;
612 if (!midiEngine.isQwertyEnabled()) {
613 simulationOn = !simulationOn;