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.
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.*;
28 import ddf.minim.analysis.*;
29 import processing.opengl.*;
31 import java.lang.reflect.*;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
36 static final int VIEWPORT_WIDTH = 900;
37 static final int VIEWPORT_HEIGHT = 700;
39 static final int LEFT_DECK = 0;
40 static final int RIGHT_DECK = 1;
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;
47 int targetFramerate = 60;
48 int startMillis, lastMillis;
50 // Core engine variables
54 LXTransition[] transitions;
56 LXEffect[] effectsArr;
57 DiscreteParameter selectedEffect;
58 MappingTool mappingTool;
59 GrizzlyOutput[] grizzlies;
60 PresetManager presetManager;
61 MidiEngine midiEngine;
63 // Display configuration mode
64 boolean mappingMode = false;
65 boolean debugMode = false;
66 boolean simulationOn = true;
67 boolean diagnosticsOn = false;
68 LXPattern restoreToPattern = null;
70 float[] hsb = new float[3];
72 // Handles to UI objects
73 UIPatternDeck uiPatternA;
74 UICrossfader uiCrossfader;
77 UIDebugText uiDebugText;
81 * Engine construction and initialization.
84 LXTransition _transition(LX lx) {
85 return new DissolveTransition(lx).setDuration(1000);
88 LXPattern[] _leftPatterns(LX lx) {
89 LXPattern[] patterns = patterns(lx);
90 for (LXPattern p : patterns) {
91 p.setTransition(_transition(lx));
96 LXPattern[] _rightPatterns(LX lx) {
97 LXPattern[] patterns = _leftPatterns(lx);
98 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
100 rightPatterns[i++] = new BlankPattern(lx).setTransition(_transition(lx));
101 for (LXPattern p : patterns) {
102 rightPatterns[i++] = p;
104 return rightPatterns;
107 LXEffect[] _effectsArray(Effects effects) {
108 List<LXEffect> effectList = new ArrayList<LXEffect>();
109 for (Field f : effects.getClass().getDeclaredFields()) {
111 Object val = f.get(effects);
112 if (val instanceof LXEffect) {
113 effectList.add((LXEffect)val);
115 } catch (IllegalAccessException iax) {}
117 return effectList.toArray(new LXEffect[]{});
120 LXEffect getSelectedEffect() {
121 return effectsArr[selectedEffect.getValuei()];
124 void logTime(String evt) {
126 println(evt + ": " + (now - lastMillis) + "ms");
131 startMillis = lastMillis = millis();
133 // Initialize the Processing graphics environment
134 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
135 frameRate(targetFramerate);
137 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
138 logTime("Created viewport");
141 model = buildModel();
142 logTime("Built Model");
145 lx = new LX(this, model);
146 lx.enableKeyboardTempo();
147 logTime("Built LX engine");
150 LXEngine engine = lx.engine;
151 engine.setPatterns(patterns = _leftPatterns(lx));
152 engine.addDeck(_rightPatterns(lx));
153 logTime("Built patterns");
156 transitions = transitions(lx);
157 lx.engine.getDeck(RIGHT_DECK).setFaderTransition(transitions[0]);
158 logTime("Built transitions");
161 lx.addEffects(effectsArr = _effectsArray(effects = new Effects()));
162 selectedEffect = new DiscreteParameter("EFFECT", effectsArr.length);
163 logTime("Built effects");
166 presetManager = new PresetManager();
167 logTime("Loaded presets");
170 midiEngine = new MidiEngine();
171 logTime("Setup MIDI devices");
173 // Build output driver
174 grizzlies = new GrizzlyOutput[]{};
176 grizzlies = buildGrizzlies();
177 for (LXOutput output : grizzlies) {
178 lx.addOutput(output);
180 } catch (Exception x) {
183 logTime("Built Grizzly Outputs");
186 mappingTool = new MappingTool(lx);
187 logTime("Built Mapping Tool");
190 UILayer[] layers = new UILayer[] {
192 new UICameraLayer(lx.ui)
193 .setCenter(model.cx, model.cy, model.cz)
194 .setRadius(290).addComponent(new UICubesLayer()),
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),
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),
209 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
212 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
213 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324)
215 uiMapping.setVisible(false);
216 for (UILayer layer : layers) {
217 lx.ui.addLayer(layer);
222 logo = loadImage("data/logo.png");
223 logTime("Loaded logo image");
225 println("Total setup: " + (millis() - startMillis) + "ms");
226 println("Hit the 'o' key to toggle live output");
229 public SCPattern getPattern() {
230 return (SCPattern) lx.getPattern();
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.
238 public static abstract class SCPattern extends LXPattern {
240 protected SCPattern(LX lx) {
245 * Reset this pattern to its default state.
247 public final void reset() {
248 for (LXParameter parameter : getParameters()) {
255 * Subclasses may override to add additional reset functionality.
257 protected /*abstract*/ void onReset() {}
260 * Invoked by the engine when a grid controller button press occurs
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
266 public boolean gridPressed(int row, int col) {
271 * Invoked by the engine when a grid controller button release occurs
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
277 public boolean gridReleased(int row, int col) {
282 * Invoked by engine when this pattern is focused an a midi note is received.
285 * @return True if the pattern has consumed this note, false if the top-level
288 public boolean noteOn(rwmidi.Note note) {
293 * Invoked by engine when this pattern is focused an a midi note off is received.
296 * @return True if the pattern has consumed this note, false if the top-level
299 public boolean noteOff(rwmidi.Note note) {
304 * Invoked by engine when this pattern is focused an a controller is received
307 * @return True if the pattern has consumed this controller, false if the top-level
310 public boolean controllerChange(rwmidi.Controller controller) {
315 long simulationNanos = 0;
318 * Core render loop and drawing functionality.
321 long drawStart = System.nanoTime();
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);
334 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
336 long gammaNanos = System.nanoTime() - gammaStart;
338 // Always draw FPS meter
342 long drawNanos = System.nanoTime() - drawStart;
346 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos);
350 void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos) {
351 float ws = 4 / 1000000.;
352 int thirtyfps = 1000000000 / 30;
353 int sixtyfps = 1000000000 / 60;
359 rect(x, y, thirtyfps * ws, h);
363 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, lx.timer.outputNanos }) {
364 fill(lx.hsb(hv % 360, 100, 80));
365 rect(xp, y, val * ws, h-1);
371 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
375 float tw = thirtyfps * ws;
381 for (long val : new long[] {
382 lx.engine.timer.deckNanos,
383 lx.engine.timer.copyNanos,
384 lx.engine.timer.fxNanos}) {
385 float amt = val / (float) lx.timer.drawNanos;
386 fill(lx.hsb(hv % 360, 100, 80));
387 rect(xp, y, amt * tw, h-1);
395 for (long val : new long[] {
396 lx.engine.getDeck(0).timer.runNanos,
397 lx.engine.getDeck(1).timer.runNanos,
398 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
399 float amt = val / (float) lx.timer.drawNanos;
400 fill(lx.hsb(hv % 360, 100, 80));
401 rect(xp, y, amt * tw, h-1);
408 // Always draw FPS meter
411 textAlign(LEFT, BASELINE);
412 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
417 * Top-level keyboard event handling
421 mappingTool.keyPressed(uiMapping);
432 if (!midiEngine.isQwertyEnabled()) {
433 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
438 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
441 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
444 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
447 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
450 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
453 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
456 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
459 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
464 frameRate(--targetFramerate);
468 frameRate(++targetFramerate);
471 effects.boom.trigger();
474 if (!midiEngine.isQwertyEnabled()) {
475 debugMode = !debugMode;
476 println("Debug output: " + (debugMode ? "ON" : "OFF"));
480 if (!midiEngine.isQwertyEnabled()) {
481 mappingMode = !mappingMode;
482 uiPatternA.setVisible(!mappingMode);
483 uiMapping.setVisible(mappingMode);
485 restoreToPattern = lx.getPattern();
486 lx.setPatterns(new LXPattern[] { mappingTool });
488 lx.setPatterns(patterns);
489 LXTransition pop = restoreToPattern.getTransition();
490 restoreToPattern.setTransition(null);
491 lx.goPattern(restoreToPattern);
492 restoreToPattern.setTransition(pop);
497 if (!midiEngine.isQwertyEnabled()) {
498 lx.engine.setThreaded(!lx.engine.isThreaded());
503 for (LXOutput output : grizzlies) {
504 output.enabled.toggle();
508 if (!midiEngine.isQwertyEnabled()) {
509 diagnosticsOn = !diagnosticsOn;
513 if (!midiEngine.isQwertyEnabled()) {
514 simulationOn = !simulationOn;