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 final int VIEWPORT_WIDTH = 900;
38 final int VIEWPORT_HEIGHT = 700;
40 // The trailer is measured from the outside of the black metal (but not including the higher welded part on the front)
41 final float TRAILER_WIDTH = 192;
42 final float TRAILER_DEPTH = 192;
43 final float TRAILER_HEIGHT = 33;
45 int targetFramerate = 60;
46 int startMillis, lastMillis;
48 // Core engine variables
54 MappingTool mappingTool;
55 GrizzlyOutput[] grizzlies;
56 PresetManager presetManager;
57 MidiEngine midiEngine;
59 // Display configuration mode
60 boolean mappingMode = false;
61 boolean debugMode = false;
62 boolean simulationOn = true;
63 boolean diagnosticsOn = false;
64 LXPattern restoreToPattern = null;
66 float[] hsb = new float[3];
68 // Handles to UI objects
69 UIPatternDeck uiPatternA;
70 UICrossfader uiCrossfader;
73 UIDebugText uiDebugText;
77 * Engine construction and initialization.
80 LXTransition _transition(LX lx) {
81 return new DissolveTransition(lx).setDuration(1000);
84 LXPattern[] _leftPatterns(LX lx) {
85 LXPattern[] patterns = patterns(lx);
86 for (LXPattern p : patterns) {
87 p.setTransition(_transition(lx));
92 LXPattern[] _rightPatterns(LX lx) {
93 LXPattern[] patterns = _leftPatterns(lx);
94 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
96 rightPatterns[i++] = new BlankPattern(lx).setTransition(_transition(lx));
97 for (LXPattern p : patterns) {
98 rightPatterns[i++] = p;
100 return rightPatterns;
103 LXEffect[] _effectsArray(Effects effects) {
104 List<LXEffect> effectList = new ArrayList<LXEffect>();
105 for (Field f : effects.getClass().getDeclaredFields()) {
107 Object val = f.get(effects);
108 if (val instanceof LXEffect) {
109 effectList.add((LXEffect)val);
111 } catch (IllegalAccessException iax) {}
113 return effectList.toArray(new LXEffect[]{});
116 void logTime(String evt) {
118 println(evt + ": " + (now - lastMillis) + "ms");
123 startMillis = lastMillis = millis();
125 // Initialize the Processing graphics environment
126 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
127 frameRate(targetFramerate);
129 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
130 logTime("Created viewport");
132 // Create the GLucose engine to run the cubes
133 glucose = new GLucose(this, model = buildModel());
135 lx.enableKeyboardTempo();
136 logTime("Built GLucose engine");
139 LXEngine engine = lx.engine;
140 engine.setPatterns(patterns = _leftPatterns(lx));
141 engine.addDeck(_rightPatterns(lx));
142 logTime("Built patterns");
143 glucose.setTransitions(transitions(lx));
144 logTime("Built transitions");
145 glucose.lx.addEffects(_effectsArray(effects = new Effects()));
146 logTime("Built effects");
149 presetManager = new PresetManager();
150 logTime("Loaded presets");
153 midiEngine = new MidiEngine();
154 logTime("Setup MIDI devices");
156 // Build output driver
157 grizzlies = new GrizzlyOutput[]{};
159 grizzlies = buildGrizzlies();
160 for (LXOutput output : grizzlies) {
161 lx.addOutput(output);
163 } catch (Exception x) {
166 logTime("Built Grizzly Outputs");
169 mappingTool = new MappingTool(lx);
172 UILayer[] layers = new UILayer[] {
174 new UICameraLayer(lx.ui)
175 .setCenter(model.cx, model.cy, model.cz)
176 .setRadius(290).addComponent(new UICubesLayer()),
179 uiPatternA = new UIPatternDeck(lx.ui, lx.engine.getDeck(GLucose.LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
180 new UIBlendMode(4, 332, 140, 86),
181 new UIEffects(4, 422, 140, 144),
182 new UITempo(4, 570, 140, 50),
183 uiSpeed = new UISpeed(4, 624, 140, 50),
186 new UIPatternDeck(lx.ui, lx.engine.getDeck(GLucose.RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
187 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
188 new UIOutput(grizzlies, width-144, 494, 140, 106),
191 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
194 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
195 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324)
197 uiMapping.setVisible(false);
198 for (UILayer layer : layers) {
199 lx.ui.addLayer(layer);
204 logo = loadImage("data/logo.png");
205 logTime("Loaded logo image");
207 println("Total setup: " + (millis() - startMillis) + "ms");
208 println("Hit the 'o' key to toggle live output");
211 public SCPattern getPattern() {
212 return (SCPattern) lx.getPattern();
216 * Subclass of LXPattern specific to sugar cubes. These patterns
217 * get access to the glucose state and geometry, and have some
218 * little helpers for interacting with the model.
220 public static abstract class SCPattern extends LXPattern {
222 protected SCPattern(LX lx) {
227 * Reset this pattern to its default state.
229 public final void reset() {
230 for (LXParameter parameter : getParameters()) {
237 * Subclasses may override to add additional reset functionality.
239 protected /*abstract*/ void onReset() {}
242 * Invoked by the engine when a grid controller button press occurs
244 * @param row Row index on the gird
245 * @param col Column index on the grid
246 * @return True if the event was consumed, false otherwise
248 public boolean gridPressed(int row, int col) {
253 * Invoked by the engine when a grid controller button release occurs
255 * @param row Row index on the gird
256 * @param col Column index on the grid
257 * @return True if the event was consumed, false otherwise
259 public boolean gridReleased(int row, int col) {
264 * Invoked by engine when this pattern is focused an a midi note is received.
267 * @return True if the pattern has consumed this note, false if the top-level
270 public boolean noteOn(rwmidi.Note note) {
275 * Invoked by engine when this pattern is focused an a midi note off is received.
278 * @return True if the pattern has consumed this note, false if the top-level
281 public boolean noteOff(rwmidi.Note note) {
286 * Invoked by engine when this pattern is focused an a controller is received
289 * @return True if the pattern has consumed this controller, false if the top-level
292 public boolean controllerChange(rwmidi.Controller controller) {
297 long simulationNanos = 0;
300 * Core render loop and drawing functionality.
303 long drawStart = System.nanoTime();
309 color[] sendColors = glucose.getColors();
310 long gammaStart = System.nanoTime();
311 // Gamma correction here. Apply a cubic to the brightness
312 // for better representation of dynamic range
313 for (int i = 0; i < sendColors.length; ++i) {
314 lx.RGBtoHSB(sendColors[i], hsb);
316 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
318 long gammaNanos = System.nanoTime() - gammaStart;
320 // Always draw FPS meter
324 long drawNanos = System.nanoTime() - drawStart;
328 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos);
332 class UICubesLayer extends UICameraComponent {
334 color[] simulationColors = glucose.getColors();
335 String displayMode = uiCrossfader.getDisplayMode();
336 if (displayMode == "A") {
337 simulationColors = lx.engine.getDeck(GLucose.LEFT_DECK).getColors();
338 } else if (displayMode == "B") {
339 simulationColors = lx.engine.getDeck(GLucose.RIGHT_DECK).getColors();
342 long simulationStart = System.nanoTime();
344 drawSimulation(simulationColors);
346 simulationNanos = System.nanoTime() - simulationStart;
349 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
350 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
351 ((PGraphicsOpenGL)g).endGL();
355 void drawSimulation(color[] simulationColors) {
360 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
365 vertex(TRAILER_WIDTH, 0, 0);
366 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
367 vertex(0, 0, TRAILER_DEPTH);
370 // Draw the logo on the front of platform
375 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
379 for (Cube c : model.cubes) {
386 for (LXPoint p : model.points) {
387 stroke(simulationColors[p.index]);
388 vertex(p.x, p.y, p.z);
393 void drawCube(Cube c) {
397 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);
400 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
403 rotate(rx / 180. * PI, -1, 0, 0);
404 rotate(ry / 180. * PI, 0, -1, 0);
405 rotate(rz / 180. * PI, 0, 0, -1);
406 for (int i = 0; i < 4; ++i) {
407 float wid = (i % 2 == 0) ? xd : zd;
413 vertex(wid - sw, yd);
414 vertex(wid - sw, sw);
420 vertex(wid - sw, yd);
421 vertex(wid - sw, yd - sw);
426 translate(wid, 0, 0);
427 rotate(HALF_PI, 0, -1, 0);
433 void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos) {
434 float ws = 4 / 1000000.;
435 int thirtyfps = 1000000000 / 30;
436 int sixtyfps = 1000000000 / 60;
442 rect(x, y, thirtyfps * ws, h);
446 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, lx.timer.outputNanos }) {
447 fill(lx.hsb(hv % 360, 100, 80));
448 rect(xp, y, val * ws, h-1);
454 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
458 float tw = thirtyfps * ws;
464 for (long val : new long[] {
465 lx.engine.timer.deckNanos,
466 lx.engine.timer.copyNanos,
467 lx.engine.timer.fxNanos}) {
468 float amt = val / (float) lx.timer.drawNanos;
469 fill(lx.hsb(hv % 360, 100, 80));
470 rect(xp, y, amt * tw, h-1);
478 for (long val : new long[] {
479 lx.engine.getDeck(0).timer.runNanos,
480 lx.engine.getDeck(1).timer.runNanos,
481 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
482 float amt = val / (float) lx.timer.drawNanos;
483 fill(lx.hsb(hv % 360, 100, 80));
484 rect(xp, y, amt * tw, h-1);
491 // Always draw FPS meter
494 textAlign(LEFT, BASELINE);
495 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
500 * Top-level keyboard event handling
504 mappingTool.keyPressed(uiMapping);
515 if (!midiEngine.isQwertyEnabled()) {
516 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
521 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
524 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
527 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
530 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
533 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
536 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
539 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
542 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
547 frameRate(--targetFramerate);
551 frameRate(++targetFramerate);
554 effects.boom.trigger();
557 if (!midiEngine.isQwertyEnabled()) {
558 debugMode = !debugMode;
559 println("Debug output: " + (debugMode ? "ON" : "OFF"));
563 if (!midiEngine.isQwertyEnabled()) {
564 mappingMode = !mappingMode;
565 uiPatternA.setVisible(!mappingMode);
566 uiMapping.setVisible(mappingMode);
568 restoreToPattern = lx.getPattern();
569 lx.setPatterns(new LXPattern[] { mappingTool });
571 lx.setPatterns(patterns);
572 LXTransition pop = restoreToPattern.getTransition();
573 restoreToPattern.setTransition(null);
574 lx.goPattern(restoreToPattern);
575 restoreToPattern.setTransition(pop);
580 if (!midiEngine.isQwertyEnabled()) {
581 lx.engine.setThreaded(!lx.engine.isThreaded());
586 for (LXOutput output : grizzlies) {
587 output.enabled.toggle();
591 if (!midiEngine.isQwertyEnabled()) {
592 diagnosticsOn = !diagnosticsOn;
596 if (!midiEngine.isQwertyEnabled()) {
597 simulationOn = !simulationOn;