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.*;
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
55 LXTransition[] transitions;
57 LXEffect[] effectsArr;
58 DiscreteParameter selectedEffect;
59 MappingTool mappingTool;
60 IPOutput[] outputs = {};
61 PresetManager presetManager;
62 MidiEngine midiEngine;
64 // Display configuration mode
65 boolean mappingMode = false;
66 boolean debugMode = false;
67 boolean simulationOn = true;
68 boolean structureOn = false;
69 boolean diagnosticsOn = false;
70 LXPattern restoreToPattern = null;
72 float[] hsb = new float[3];
74 // Handles to UI objects
75 UIPatternDeck uiPatternA;
76 UICrossfader uiCrossfader;
79 UIDebugText uiDebugText;
83 * Engine construction and initialization.
86 LXTransition _transition(LX lx) {
87 return new DissolveTransition(lx).setDuration(1000);
90 LXPattern[] _leftPatterns(LX lx) {
91 LXPattern[] patterns = patterns(lx);
92 for (LXPattern p : patterns) {
93 p.setTransition(_transition(lx));
98 LXPattern[] _rightPatterns(LX lx) {
99 LXPattern[] patterns = _leftPatterns(lx);
100 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
102 rightPatterns[i++] = new BlankPattern(lx).setTransition(_transition(lx));
103 for (LXPattern p : patterns) {
104 rightPatterns[i++] = p;
106 return rightPatterns;
109 LXEffect[] _effectsArray(Effects effects) {
110 List<LXEffect> effectList = new ArrayList<LXEffect>();
111 for (Field f : effects.getClass().getDeclaredFields()) {
113 Object val = f.get(effects);
114 if (val instanceof LXEffect) {
115 effectList.add((LXEffect)val);
117 } catch (IllegalAccessException iax) {}
119 return effectList.toArray(new LXEffect[]{});
122 LXEffect getSelectedEffect() {
123 return effectsArr[selectedEffect.getValuei()];
126 void logTime(String evt) {
128 println(evt + ": " + (now - lastMillis) + "ms");
133 startMillis = lastMillis = millis();
135 // Initialize the Processing graphics environment
136 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
137 frameRate(targetFramerate);
139 logTime("Created viewport");
142 model = buildModel();
143 logTime("Built Model");
146 lx = new LX(this, model);
147 lx.enableKeyboardTempo();
148 logTime("Built LX engine");
151 LXEngine engine = lx.engine;
152 engine.setPatterns(patterns = _leftPatterns(lx));
153 engine.addDeck(_rightPatterns(lx));
154 logTime("Built patterns");
157 transitions = transitions(lx);
158 lx.engine.getDeck(RIGHT_DECK).setFaderTransition(transitions[0]);
159 logTime("Built transitions");
162 lx.addEffects(effectsArr = _effectsArray(effects = new Effects()));
163 selectedEffect = new DiscreteParameter("EFFECT", effectsArr.length);
164 logTime("Built effects");
167 presetManager = new PresetManager();
168 logTime("Loaded presets");
171 midiEngine = new MidiEngine();
172 logTime("Setup MIDI devices");
174 // Build output driver
176 outputs = buildOutputs();
177 for (LXOutput output : outputs) {
178 lx.addOutput(output);
180 } catch (Exception x) {
183 logTime("Built Grizzly + Panda 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(outputs, 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");
228 lx.engine.framesPerSecond.setValue(60);
229 lx.engine.setThreaded(true);
232 public SCPattern getPattern() {
233 return (SCPattern) lx.getPattern();
237 * Subclass of LXPattern specific to sugar cubes. These patterns
238 * get access to the state and geometry, and have some
239 * little helpers for interacting with the model.
241 public static abstract class SCPattern extends LXPattern {
243 protected SCPattern(LX lx) {
248 * Reset this pattern to its default state.
250 public final void reset() {
251 for (LXParameter parameter : getParameters()) {
258 * Subclasses may override to add additional reset functionality.
260 protected /*abstract*/ void onReset() {}
263 * Invoked by the engine when a grid controller button press occurs
265 * @param row Row index on the gird
266 * @param col Column index on the grid
267 * @return True if the event was consumed, false otherwise
269 public boolean gridPressed(int row, int col) {
274 * Invoked by the engine when a grid controller button release occurs
276 * @param row Row index on the gird
277 * @param col Column index on the grid
278 * @return True if the event was consumed, false otherwise
280 public boolean gridReleased(int row, int col) {
285 * Invoked by engine when this pattern is focused an a midi note is received.
288 * @return True if the pattern has consumed this note, false if the top-level
291 public boolean noteOn(rwmidi.Note note) {
296 * Invoked by engine when this pattern is focused an a midi note off is received.
299 * @return True if the pattern has consumed this note, false if the top-level
302 public boolean noteOff(rwmidi.Note note) {
307 * Invoked by engine when this pattern is focused an a controller is received
310 * @return True if the pattern has consumed this controller, false if the top-level
313 public boolean controllerChange(rwmidi.Controller controller) {
318 long simulationNanos = 0;
321 * Core render loop and drawing functionality.
324 long drawStart = System.nanoTime();
330 color[] sendColors = lx.getColors();
331 long gammaStart = System.nanoTime();
332 // Gamma correction here. Apply a cubic to the brightness
333 // for better representation of dynamic range
334 for (int i = 0; i < sendColors.length; ++i) {
335 lx.RGBtoHSB(sendColors[i], hsb);
337 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
339 long gammaNanos = System.nanoTime() - gammaStart;
341 // Always draw FPS meter
345 long drawNanos = System.nanoTime() - drawStart;
349 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos);
353 void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos) {
354 float ws = 4 / 1000000.;
355 int thirtyfps = 1000000000 / 30;
356 int sixtyfps = 1000000000 / 60;
362 rect(x, y, thirtyfps * ws, h);
366 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, lx.engine.timer.outputNanos }) {
367 fill(lx.hsb(hv % 360, 100, 80));
368 rect(xp, y, val * ws, h-1);
374 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
378 float tw = thirtyfps * ws;
384 for (long val : new long[] {
385 lx.engine.timer.deckNanos,
386 lx.engine.timer.copyNanos,
387 lx.engine.timer.fxNanos}) {
388 float amt = val / (float) lx.timer.drawNanos;
389 fill(lx.hsb(hv % 360, 100, 80));
390 rect(xp, y, amt * tw, h-1);
398 for (long val : new long[] {
399 lx.engine.getDeck(0).timer.runNanos,
400 lx.engine.getDeck(1).timer.runNanos,
401 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
402 float amt = val / (float) lx.timer.drawNanos;
403 fill(lx.hsb(hv % 360, 100, 80));
404 rect(xp, y, amt * tw, h-1);
411 // Always draw FPS meter
414 textAlign(LEFT, BASELINE);
415 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
420 * Top-level keyboard event handling
424 mappingTool.keyPressed(uiMapping);
435 if (!midiEngine.isQwertyEnabled()) {
436 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
441 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
444 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
447 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
450 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
453 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
456 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
459 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
462 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
467 frameRate(--targetFramerate);
471 frameRate(++targetFramerate);
474 effects.boom.trigger();
477 if (!midiEngine.isQwertyEnabled()) {
478 debugMode = !debugMode;
479 println("Debug output: " + (debugMode ? "ON" : "OFF"));
483 if (!midiEngine.isQwertyEnabled()) {
484 mappingMode = !mappingMode;
485 uiPatternA.setVisible(!mappingMode);
486 uiMapping.setVisible(mappingMode);
488 restoreToPattern = lx.getPattern();
489 lx.setPatterns(new LXPattern[] { mappingTool });
491 lx.setPatterns(patterns);
492 LXTransition pop = restoreToPattern.getTransition();
493 restoreToPattern.setTransition(null);
494 lx.goPattern(restoreToPattern);
495 restoreToPattern.setTransition(pop);
500 if (!midiEngine.isQwertyEnabled()) {
501 lx.engine.setThreaded(!lx.engine.isThreaded());
506 for (LXOutput output : outputs) {
507 output.enabled.toggle();
511 if (!midiEngine.isQwertyEnabled()) {
512 diagnosticsOn = !diagnosticsOn;
516 if (!midiEngine.isQwertyEnabled()) {
517 simulationOn = !simulationOn;
521 if (!midiEngine.isQwertyEnabled()) {
522 structureOn = !structureOn;