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 glucose.model.*;
18 import heronarts.lx.*;
19 import heronarts.lx.effect.*;
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.*;
34 final int VIEWPORT_WIDTH = 900;
35 final int VIEWPORT_HEIGHT = 700;
37 // The trailer is measured from the outside of the black metal (but not including the higher welded part on the front)
38 final float TRAILER_WIDTH = 240;
39 final float TRAILER_DEPTH = 97;
40 final float TRAILER_HEIGHT = 33;
42 int targetFramerate = 60;
43 int startMillis, lastMillis;
45 // Core engine variables
50 MappingTool mappingTool;
51 PandaDriver[] pandaBoards;
52 PresetManager presetManager;
53 MidiEngine midiEngine;
55 // Display configuration mode
56 boolean mappingMode = false;
57 boolean debugMode = false;
60 boolean simulationOn = true;
61 boolean diagnosticsOn = false;
62 LXPattern restoreToPattern = null;
64 float[] hsb = new float[3];
66 // Handles to UI objects
67 UIPatternDeck uiPatternA;
68 UICrossfader uiCrossfader;
71 UIDebugText uiDebugText;
75 * Engine construction and initialization.
78 LXTransition _transition(GLucose glucose) {
79 return new DissolveTransition(glucose.lx).setDuration(1000);
82 LXPattern[] _leftPatterns(GLucose glucose) {
83 LXPattern[] patterns = patterns(glucose);
84 for (LXPattern p : patterns) {
85 p.setTransition(_transition(glucose));
90 LXPattern[] _rightPatterns(GLucose glucose) {
91 LXPattern[] patterns = _leftPatterns(glucose);
92 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
94 rightPatterns[i++] = new BlankPattern(glucose).setTransition(_transition(glucose));
95 for (LXPattern p : patterns) {
96 rightPatterns[i++] = p;
101 LXEffect[] _effectsArray(Effects effects) {
102 List<LXEffect> effectList = new ArrayList<LXEffect>();
103 for (Field f : effects.getClass().getDeclaredFields()) {
105 Object val = f.get(effects);
106 if (val instanceof LXEffect) {
107 effectList.add((LXEffect)val);
109 } catch (IllegalAccessException iax) {}
111 return effectList.toArray(new LXEffect[]{});
114 void logTime(String evt) {
116 println(evt + ": " + (now - lastMillis) + "ms");
121 startMillis = lastMillis = millis();
123 // Initialize the Processing graphics environment
124 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
125 frameRate(targetFramerate);
127 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
128 logTime("Created viewport");
130 // Create the GLucose engine to run the cubes
131 glucose = new GLucose(this, buildModel());
133 lx.enableKeyboardTempo();
134 logTime("Built GLucose engine");
137 LXEngine engine = lx.engine;
138 engine.setPatterns(patterns = _leftPatterns(glucose));
139 engine.addDeck(_rightPatterns(glucose));
140 logTime("Built patterns");
141 glucose.setTransitions(transitions(glucose));
142 logTime("Built transitions");
143 glucose.lx.addEffects(_effectsArray(effects = new Effects()));
144 logTime("Built effects");
147 presetManager = new PresetManager();
148 logTime("Loaded presets");
151 midiEngine = new MidiEngine();
152 logTime("Setup MIDI devices");
154 // Build output driver
155 PandaMapping[] pandaMappings = buildPandaList();
156 pandaBoards = new PandaDriver[pandaMappings.length];
158 for (PandaMapping pm : pandaMappings) {
159 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
161 mappingTool = new MappingTool(glucose, pandaMappings);
162 logTime("Built PandaDriver");
165 debugUI = new DebugUI(pandaMappings);
166 UIContext[] contexts = new UIContext[] {
167 uiPatternA = new UIPatternDeck(lx.ui, lx.engine.getDeck(GLucose.LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
168 new UIBlendMode(4, 332, 140, 86),
169 new UIEffects(4, 422, 140, 144),
170 new UITempo(4, 570, 140, 50),
171 uiSpeed = new UISpeed(4, 624, 140, 50),
173 new UIPatternDeck(lx.ui, lx.engine.getDeck(GLucose.RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
174 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
175 new UIOutput(width-144, 494, 140, 106),
177 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
179 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
180 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324)
182 uiMapping.setVisible(false);
183 lx.ui.addLayer(new UICameraLayer(lx.ui).setCenter(TRAILER_WIDTH/2., glucose.model.yMax/2, TRAILER_DEPTH/2.).setRadius(290).addComponent(new UICubesLayer()));
184 for (UIContext context : contexts) {
185 lx.ui.addLayer(context);
187 logTime("Built overlay UI");
190 logo = loadImage("data/logo.png");
192 println("Total setup: " + (millis() - startMillis) + "ms");
193 println("Hit the 'p' key to toggle Panda Board output");
197 * Core render loop and drawing functionality.
200 long drawStart = System.nanoTime();
206 color[] sendColors = glucose.getColors();
208 debugUI.maskColors(sendColors);
211 long gammaStart = System.nanoTime();
212 // Gamma correction here. Apply a cubic to the brightness
213 // for better representation of dynamic range
214 for (int i = 0; i < sendColors.length; ++i) {
215 lx.RGBtoHSB(sendColors[i], hsb);
217 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
219 long gammaNanos = System.nanoTime() - gammaStart;
221 long sendStart = System.nanoTime();
222 for (PandaDriver p : pandaBoards) {
225 long sendNanos = System.nanoTime() - sendStart;
233 long drawNanos = System.nanoTime() - drawStart;
235 long simulationNanos = 0, uiNanos = 0;
237 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos, sendNanos);
241 class UICubesLayer extends UICameraComponent {
243 color[] simulationColors = glucose.getColors();
244 String displayMode = uiCrossfader.getDisplayMode();
245 if (displayMode == "A") {
246 simulationColors = lx.engine.getDeck(GLucose.LEFT_DECK).getColors();
247 } else if (displayMode == "B") {
248 simulationColors = lx.engine.getDeck(GLucose.RIGHT_DECK).getColors();
251 debugUI.maskColors(simulationColors);
254 long simulationStart = System.nanoTime();
256 drawSimulation(simulationColors);
258 long simulationNanos = System.nanoTime() - simulationStart;
261 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
262 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
263 ((PGraphicsOpenGL)g).endGL();
268 void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos, long sendNanos) {
269 float ws = 4 / 1000000.;
270 int thirtyfps = 1000000000 / 30;
271 int sixtyfps = 1000000000 / 60;
277 rect(x, y, thirtyfps * ws, h);
281 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, sendNanos }) {
282 fill(lx.hsb(hv % 360, 100, 80));
283 rect(xp, y, val * ws, h-1);
289 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
293 float tw = thirtyfps * ws;
299 for (long val : new long[] {
300 lx.engine.timer.deckNanos,
301 lx.engine.timer.copyNanos,
302 lx.engine.timer.fxNanos}) {
303 float amt = val / (float) lx.timer.drawNanos;
304 fill(lx.hsb(hv % 360, 100, 80));
305 rect(xp, y, amt * tw, h-1);
313 for (long val : new long[] {
314 lx.engine.getDeck(0).timer.runNanos,
315 lx.engine.getDeck(1).timer.runNanos,
316 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
317 float amt = val / (float) lx.timer.drawNanos;
318 fill(lx.hsb(hv % 360, 100, 80));
319 rect(xp, y, amt * tw, h-1);
325 void drawSimulation(color[] simulationColors) {
330 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
335 vertex(TRAILER_WIDTH, 0, 0);
336 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
337 vertex(0, 0, TRAILER_DEPTH);
340 // Draw the logo on the front of platform
345 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
349 if (glucose.model.bassBox.exists) {
350 drawBassBox(glucose.model.bassBox, false);
352 for (Speaker speaker : glucose.model.speakers) {
353 drawSpeaker(speaker);
355 for (Cube c : glucose.model.cubes) {
362 for (LXPoint p : glucose.model.points) {
363 stroke(simulationColors[p.index]);
364 vertex(p.x, p.y, p.z);
369 void drawBassBox(BassBox b, boolean hasSub) {
377 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
378 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
384 drawBox(b.x+in, b.y+in, b.z+in, 0, 0, 0, BassBox.EDGE_WIDTH-in*2, BassBox.EDGE_HEIGHT-in*2, BassBox.EDGE_DEPTH-in*2, Cube.CHANNEL_WIDTH-in);
387 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
388 float lastOffset = 0;
389 for (float offset : BoothFloor.STRIP_OFFSETS) {
390 translate(offset - lastOffset, 0, 0);
391 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
397 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
398 for (int j = 0; j < 2; ++j) {
400 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
401 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
402 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
405 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
410 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
411 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
412 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
413 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
418 void drawCube(Cube c) {
422 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);
425 void drawSpeaker(Speaker s) {
431 translate(s.x, s.y, s.z);
432 rotate(s.ry / 180. * PI, 0, -1, 0);
433 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
434 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
435 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
438 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
443 drawBox(s.x+in, s.y+in, s.z+in, 0, s.ry, 0, Speaker.EDGE_WIDTH-in*2, Speaker.EDGE_HEIGHT-in*2, Speaker.EDGE_DEPTH-in*2, Cube.CHANNEL_WIDTH-in);
446 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
449 rotate(rx / 180. * PI, -1, 0, 0);
450 rotate(ry / 180. * PI, 0, -1, 0);
451 rotate(rz / 180. * PI, 0, 0, -1);
452 for (int i = 0; i < 4; ++i) {
453 float wid = (i % 2 == 0) ? xd : zd;
459 vertex(wid - sw, yd);
460 vertex(wid - sw, sw);
466 vertex(wid - sw, yd);
467 vertex(wid - sw, yd - sw);
472 translate(wid, 0, 0);
473 rotate(HALF_PI, 0, -1, 0);
479 // Always draw FPS meter
482 textAlign(LEFT, BASELINE);
483 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
488 * Top-level keyboard event handling
492 mappingTool.keyPressed(uiMapping);
503 if (!midiEngine.isQwertyEnabled()) {
504 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
509 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
512 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
515 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
518 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
521 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
524 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
527 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
530 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
535 frameRate(--targetFramerate);
539 frameRate(++targetFramerate);
542 effects.boom.trigger();
545 if (!midiEngine.isQwertyEnabled()) {
546 debugMode = !debugMode;
547 println("Debug output: " + (debugMode ? "ON" : "OFF"));
551 if (!midiEngine.isQwertyEnabled()) {
552 mappingMode = !mappingMode;
553 uiPatternA.setVisible(!mappingMode);
554 uiMapping.setVisible(mappingMode);
556 restoreToPattern = lx.getPattern();
557 lx.setPatterns(new LXPattern[] { mappingTool });
559 lx.setPatterns(patterns);
560 LXTransition pop = restoreToPattern.getTransition();
561 restoreToPattern.setTransition(null);
562 lx.goPattern(restoreToPattern);
563 restoreToPattern.setTransition(pop);
568 if (!midiEngine.isQwertyEnabled()) {
569 lx.engine.setThreaded(!lx.engine.isThreaded());
573 for (PandaDriver p : pandaBoards) {
578 if (!midiEngine.isQwertyEnabled()) {
579 diagnosticsOn = !diagnosticsOn;
583 if (!midiEngine.isQwertyEnabled()) {
584 simulationOn = !simulationOn;
588 if (!midiEngine.isQwertyEnabled()) {
596 * Top-level mouse event handling
598 void mousePressed() {
600 debugUI.mousePressed();