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(GLucose glucose) {
81 return new DissolveTransition(glucose.lx).setDuration(1000);
84 LXPattern[] _leftPatterns(GLucose glucose) {
85 LXPattern[] patterns = patterns(glucose);
86 for (LXPattern p : patterns) {
87 p.setTransition(_transition(glucose));
92 LXPattern[] _rightPatterns(GLucose glucose) {
93 LXPattern[] patterns = _leftPatterns(glucose);
94 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
96 rightPatterns[i++] = new BlankPattern(glucose).setTransition(_transition(glucose));
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(glucose));
141 engine.addDeck(_rightPatterns(glucose));
142 logTime("Built patterns");
143 glucose.setTransitions(transitions(glucose));
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(glucose);
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 long simulationNanos = 0;
214 * Core render loop and drawing functionality.
217 long drawStart = System.nanoTime();
223 color[] sendColors = glucose.getColors();
224 long gammaStart = System.nanoTime();
225 // Gamma correction here. Apply a cubic to the brightness
226 // for better representation of dynamic range
227 for (int i = 0; i < sendColors.length; ++i) {
228 lx.RGBtoHSB(sendColors[i], hsb);
230 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
232 long gammaNanos = System.nanoTime() - gammaStart;
234 // Always draw FPS meter
238 long drawNanos = System.nanoTime() - drawStart;
242 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos);
246 class UICubesLayer extends UICameraComponent {
248 color[] simulationColors = glucose.getColors();
249 String displayMode = uiCrossfader.getDisplayMode();
250 if (displayMode == "A") {
251 simulationColors = lx.engine.getDeck(GLucose.LEFT_DECK).getColors();
252 } else if (displayMode == "B") {
253 simulationColors = lx.engine.getDeck(GLucose.RIGHT_DECK).getColors();
256 long simulationStart = System.nanoTime();
258 drawSimulation(simulationColors);
260 simulationNanos = System.nanoTime() - simulationStart;
263 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
264 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
265 ((PGraphicsOpenGL)g).endGL();
269 void drawSimulation(color[] simulationColors) {
274 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
279 vertex(TRAILER_WIDTH, 0, 0);
280 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
281 vertex(0, 0, TRAILER_DEPTH);
284 // Draw the logo on the front of platform
289 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
293 for (Cube c : model.cubes) {
300 for (LXPoint p : model.points) {
301 stroke(simulationColors[p.index]);
302 vertex(p.x, p.y, p.z);
307 void drawCube(Cube c) {
311 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);
314 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
317 rotate(rx / 180. * PI, -1, 0, 0);
318 rotate(ry / 180. * PI, 0, -1, 0);
319 rotate(rz / 180. * PI, 0, 0, -1);
320 for (int i = 0; i < 4; ++i) {
321 float wid = (i % 2 == 0) ? xd : zd;
327 vertex(wid - sw, yd);
328 vertex(wid - sw, sw);
334 vertex(wid - sw, yd);
335 vertex(wid - sw, yd - sw);
340 translate(wid, 0, 0);
341 rotate(HALF_PI, 0, -1, 0);
347 void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos) {
348 float ws = 4 / 1000000.;
349 int thirtyfps = 1000000000 / 30;
350 int sixtyfps = 1000000000 / 60;
356 rect(x, y, thirtyfps * ws, h);
360 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, lx.timer.outputNanos }) {
361 fill(lx.hsb(hv % 360, 100, 80));
362 rect(xp, y, val * ws, h-1);
368 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
372 float tw = thirtyfps * ws;
378 for (long val : new long[] {
379 lx.engine.timer.deckNanos,
380 lx.engine.timer.copyNanos,
381 lx.engine.timer.fxNanos}) {
382 float amt = val / (float) lx.timer.drawNanos;
383 fill(lx.hsb(hv % 360, 100, 80));
384 rect(xp, y, amt * tw, h-1);
392 for (long val : new long[] {
393 lx.engine.getDeck(0).timer.runNanos,
394 lx.engine.getDeck(1).timer.runNanos,
395 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
396 float amt = val / (float) lx.timer.drawNanos;
397 fill(lx.hsb(hv % 360, 100, 80));
398 rect(xp, y, amt * tw, h-1);
405 // Always draw FPS meter
408 textAlign(LEFT, BASELINE);
409 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
414 * Top-level keyboard event handling
418 mappingTool.keyPressed(uiMapping);
429 if (!midiEngine.isQwertyEnabled()) {
430 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
435 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
438 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
441 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
444 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
447 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
450 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
453 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
456 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
461 frameRate(--targetFramerate);
465 frameRate(++targetFramerate);
468 effects.boom.trigger();
471 if (!midiEngine.isQwertyEnabled()) {
472 debugMode = !debugMode;
473 println("Debug output: " + (debugMode ? "ON" : "OFF"));
477 if (!midiEngine.isQwertyEnabled()) {
478 mappingMode = !mappingMode;
479 uiPatternA.setVisible(!mappingMode);
480 uiMapping.setVisible(mappingMode);
482 restoreToPattern = lx.getPattern();
483 lx.setPatterns(new LXPattern[] { mappingTool });
485 lx.setPatterns(patterns);
486 LXTransition pop = restoreToPattern.getTransition();
487 restoreToPattern.setTransition(null);
488 lx.goPattern(restoreToPattern);
489 restoreToPattern.setTransition(pop);
494 if (!midiEngine.isQwertyEnabled()) {
495 lx.engine.setThreaded(!lx.engine.isThreaded());
500 for (LXOutput output : grizzlies) {
501 output.enabled.toggle();
505 if (!midiEngine.isQwertyEnabled()) {
506 diagnosticsOn = !diagnosticsOn;
510 if (!midiEngine.isQwertyEnabled()) {
511 simulationOn = !simulationOn;