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 logTime("Created viewport");
140 model = buildModel();
141 logTime("Built Model");
144 lx = new LX(this, model);
145 lx.enableKeyboardTempo();
146 logTime("Built LX engine");
149 LXEngine engine = lx.engine;
150 engine.setPatterns(patterns = _leftPatterns(lx));
151 engine.addDeck(_rightPatterns(lx));
152 logTime("Built patterns");
155 transitions = transitions(lx);
156 lx.engine.getDeck(RIGHT_DECK).setFaderTransition(transitions[0]);
157 logTime("Built transitions");
160 lx.addEffects(effectsArr = _effectsArray(effects = new Effects()));
161 selectedEffect = new DiscreteParameter("EFFECT", effectsArr.length);
162 logTime("Built effects");
165 presetManager = new PresetManager();
166 logTime("Loaded presets");
169 midiEngine = new MidiEngine();
170 logTime("Setup MIDI devices");
172 // Build output driver
173 grizzlies = new GrizzlyOutput[]{};
175 grizzlies = buildGrizzlies();
176 for (LXOutput output : grizzlies) {
177 lx.addOutput(output);
179 } catch (Exception x) {
182 logTime("Built Grizzly Outputs");
185 mappingTool = new MappingTool(lx);
186 logTime("Built Mapping Tool");
189 UILayer[] layers = new UILayer[] {
191 new UICameraLayer(lx.ui)
192 .setCenter(model.cx, model.cy, model.cz)
193 .setRadius(290).addComponent(new UICubesLayer()),
196 uiPatternA = new UIPatternDeck(lx.ui, lx.engine.getDeck(LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
197 new UIBlendMode(4, 332, 140, 86),
198 new UIEffects(4, 422, 140, 144),
199 new UITempo(4, 570, 140, 50),
200 uiSpeed = new UISpeed(4, 624, 140, 50),
203 new UIPatternDeck(lx.ui, lx.engine.getDeck(RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
204 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
205 new UIOutput(grizzlies, width-144, 494, 140, 106),
208 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
211 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
212 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324)
214 uiMapping.setVisible(false);
215 for (UILayer layer : layers) {
216 lx.ui.addLayer(layer);
221 logo = loadImage("data/logo.png");
222 logTime("Loaded logo image");
224 println("Total setup: " + (millis() - startMillis) + "ms");
225 println("Hit the 'o' key to toggle live output");
227 lx.engine.framesPerSecond.setValue(120);
228 lx.engine.setThreaded(true);
231 public SCPattern getPattern() {
232 return (SCPattern) lx.getPattern();
236 * Subclass of LXPattern specific to sugar cubes. These patterns
237 * get access to the state and geometry, and have some
238 * little helpers for interacting with the model.
240 public static abstract class SCPattern extends LXPattern {
242 protected SCPattern(LX lx) {
247 * Reset this pattern to its default state.
249 public final void reset() {
250 for (LXParameter parameter : getParameters()) {
257 * Subclasses may override to add additional reset functionality.
259 protected /*abstract*/ void onReset() {}
262 * Invoked by the engine when a grid controller button press occurs
264 * @param row Row index on the gird
265 * @param col Column index on the grid
266 * @return True if the event was consumed, false otherwise
268 public boolean gridPressed(int row, int col) {
273 * Invoked by the engine when a grid controller button release occurs
275 * @param row Row index on the gird
276 * @param col Column index on the grid
277 * @return True if the event was consumed, false otherwise
279 public boolean gridReleased(int row, int col) {
284 * Invoked by engine when this pattern is focused an a midi note is received.
287 * @return True if the pattern has consumed this note, false if the top-level
290 public boolean noteOn(rwmidi.Note note) {
295 * Invoked by engine when this pattern is focused an a midi note off is received.
298 * @return True if the pattern has consumed this note, false if the top-level
301 public boolean noteOff(rwmidi.Note note) {
306 * Invoked by engine when this pattern is focused an a controller is received
309 * @return True if the pattern has consumed this controller, false if the top-level
312 public boolean controllerChange(rwmidi.Controller controller) {
317 long simulationNanos = 0;
320 * Core render loop and drawing functionality.
323 long drawStart = System.nanoTime();
329 color[] sendColors = lx.getColors();
330 long gammaStart = System.nanoTime();
331 // Gamma correction here. Apply a cubic to the brightness
332 // for better representation of dynamic range
333 for (int i = 0; i < sendColors.length; ++i) {
334 lx.RGBtoHSB(sendColors[i], hsb);
336 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
338 long gammaNanos = System.nanoTime() - gammaStart;
340 // Always draw FPS meter
344 long drawNanos = System.nanoTime() - drawStart;
348 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos);
352 void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos) {
353 float ws = 4 / 1000000.;
354 int thirtyfps = 1000000000 / 30;
355 int sixtyfps = 1000000000 / 60;
361 rect(x, y, thirtyfps * ws, h);
365 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, lx.engine.timer.outputNanos }) {
366 fill(lx.hsb(hv % 360, 100, 80));
367 rect(xp, y, val * ws, h-1);
373 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
377 float tw = thirtyfps * ws;
383 for (long val : new long[] {
384 lx.engine.timer.deckNanos,
385 lx.engine.timer.copyNanos,
386 lx.engine.timer.fxNanos}) {
387 float amt = val / (float) lx.timer.drawNanos;
388 fill(lx.hsb(hv % 360, 100, 80));
389 rect(xp, y, amt * tw, h-1);
397 for (long val : new long[] {
398 lx.engine.getDeck(0).timer.runNanos,
399 lx.engine.getDeck(1).timer.runNanos,
400 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
401 float amt = val / (float) lx.timer.drawNanos;
402 fill(lx.hsb(hv % 360, 100, 80));
403 rect(xp, y, amt * tw, h-1);
410 // Always draw FPS meter
413 textAlign(LEFT, BASELINE);
414 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
419 * Top-level keyboard event handling
423 mappingTool.keyPressed(uiMapping);
434 if (!midiEngine.isQwertyEnabled()) {
435 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
440 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
443 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
446 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
449 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
452 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
455 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
458 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
461 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
466 frameRate(--targetFramerate);
470 frameRate(++targetFramerate);
473 effects.boom.trigger();
476 if (!midiEngine.isQwertyEnabled()) {
477 debugMode = !debugMode;
478 println("Debug output: " + (debugMode ? "ON" : "OFF"));
482 if (!midiEngine.isQwertyEnabled()) {
483 mappingMode = !mappingMode;
484 uiPatternA.setVisible(!mappingMode);
485 uiMapping.setVisible(mappingMode);
487 restoreToPattern = lx.getPattern();
488 lx.setPatterns(new LXPattern[] { mappingTool });
490 lx.setPatterns(patterns);
491 LXTransition pop = restoreToPattern.getTransition();
492 restoreToPattern.setTransition(null);
493 lx.goPattern(restoreToPattern);
494 restoreToPattern.setTransition(pop);
499 if (!midiEngine.isQwertyEnabled()) {
500 lx.engine.setThreaded(!lx.engine.isThreaded());
505 for (LXOutput output : grizzlies) {
506 output.enabled.toggle();
510 if (!midiEngine.isQwertyEnabled()) {
511 diagnosticsOn = !diagnosticsOn;
515 if (!midiEngine.isQwertyEnabled()) {
516 simulationOn = !simulationOn;