New LX fixes FlashEffect bug
[SugarCubes.git] / Internals.pde
CommitLineData
49815cc0 1/**
1ecdb44a
MS
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
d12e46b6 7 * \\// \\// \\// \\//H
1ecdb44a
MS
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
49815cc0
MS
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.
14 */
15
49815cc0
MS
16import heronarts.lx.*;
17import heronarts.lx.effect.*;
b9b7b3d4 18import heronarts.lx.model.*;
49815cc0 19import heronarts.lx.modulator.*;
b8bb2748 20import heronarts.lx.parameter.*;
f3f5a876 21import heronarts.lx.pattern.*;
9fa29818 22import heronarts.lx.transform.*;
49815cc0 23import heronarts.lx.transition.*;
4e6626a9
MS
24import heronarts.lx.ui.*;
25import heronarts.lx.ui.component.*;
26import heronarts.lx.ui.control.*;
49815cc0
MS
27import ddf.minim.*;
28import ddf.minim.analysis.*;
29import processing.opengl.*;
5d70e4d7 30import rwmidi.*;
24fc0330 31import java.lang.reflect.*;
bffb6a80 32import java.net.*;
b9b7b3d4
MS
33import java.util.ArrayList;
34import java.util.Collections;
35import java.util.List;
49815cc0 36
3f16fd02
MS
37static final int VIEWPORT_WIDTH = 900;
38static final int VIEWPORT_HEIGHT = 700;
39
40static final int LEFT_DECK = 0;
41static final int RIGHT_DECK = 1;
0e3c5542 42
92c06c97 43// The trailer is measured from the outside of the black metal (but not including the higher welded part on the front)
3f16fd02
MS
44static final float TRAILER_WIDTH = 192;
45static final float TRAILER_DEPTH = 192;
46static final float TRAILER_HEIGHT = 33;
87998ff3 47
51d0d59a 48int targetFramerate = 60;
49815cc0 49int startMillis, lastMillis;
a898d79b
MS
50
51// Core engine variables
d12e46b6 52LX lx;
34490867 53Model model;
49815cc0 54LXPattern[] patterns;
3f16fd02 55LXTransition[] transitions;
24fc0330 56Effects effects;
0c7bfdb5
MS
57LXEffect[] effectsArr;
58DiscreteParameter selectedEffect;
a898d79b 59MappingTool mappingTool;
bffb6a80 60IPOutput[] outputs = {};
e0794d3a 61PresetManager presetManager;
1f974cbc 62MidiEngine midiEngine;
a898d79b
MS
63
64// Display configuration mode
bf551144 65boolean mappingMode = false;
cc9fcf4b 66boolean debugMode = false;
7974acd6 67boolean simulationOn = true;
bffb6a80 68boolean structureOn = false;
73678c57 69boolean diagnosticsOn = false;
34327c96 70LXPattern restoreToPattern = null;
4c640acc 71PImage logo;
a41f334c 72float[] hsb = new float[3];
d626bc9b 73
a898d79b 74// Handles to UI objects
d626bc9b 75UIPatternDeck uiPatternA;
a898d79b 76UICrossfader uiCrossfader;
a8d55ade 77UIMidi uiMidi;
d626bc9b
MS
78UIMapping uiMapping;
79UIDebugText uiDebugText;
fa4f822d 80UISpeed uiSpeed;
cc9fcf4b 81
34327c96
MS
82/**
83 * Engine construction and initialization.
84 */
d1dcc4b5 85
dde75983
MS
86LXTransition _transition(LX lx) {
87 return new DissolveTransition(lx).setDuration(1000);
d1dcc4b5
MS
88}
89
dde75983
MS
90LXPattern[] _leftPatterns(LX lx) {
91 LXPattern[] patterns = patterns(lx);
d626bc9b 92 for (LXPattern p : patterns) {
dde75983 93 p.setTransition(_transition(lx));
d626bc9b
MS
94 }
95 return patterns;
96}
97
dde75983
MS
98LXPattern[] _rightPatterns(LX lx) {
99 LXPattern[] patterns = _leftPatterns(lx);
d1dcc4b5
MS
100 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
101 int i = 0;
dde75983 102 rightPatterns[i++] = new BlankPattern(lx).setTransition(_transition(lx));
d1dcc4b5
MS
103 for (LXPattern p : patterns) {
104 rightPatterns[i++] = p;
105 }
106 return rightPatterns;
107}
24fc0330
MS
108
109LXEffect[] _effectsArray(Effects effects) {
110 List<LXEffect> effectList = new ArrayList<LXEffect>();
111 for (Field f : effects.getClass().getDeclaredFields()) {
112 try {
113 Object val = f.get(effects);
114 if (val instanceof LXEffect) {
115 effectList.add((LXEffect)val);
116 }
117 } catch (IllegalAccessException iax) {}
118 }
119 return effectList.toArray(new LXEffect[]{});
0c7bfdb5
MS
120}
121
122LXEffect getSelectedEffect() {
123 return effectsArr[selectedEffect.getValuei()];
124}
d1dcc4b5 125
34327c96
MS
126void logTime(String evt) {
127 int now = millis();
128 println(evt + ": " + (now - lastMillis) + "ms");
129 lastMillis = now;
130}
131
49815cc0
MS
132void setup() {
133 startMillis = lastMillis = millis();
134
135 // Initialize the Processing graphics environment
136 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
0e3c5542 137 frameRate(targetFramerate);
3f8be614 138 noSmooth();
49815cc0
MS
139 logTime("Created viewport");
140
0c7bfdb5
MS
141 // Create the model
142 model = buildModel();
143 logTime("Built Model");
144
145 // LX engine
146 lx = new LX(this, model);
cc9fcf4b 147 lx.enableKeyboardTempo();
0c7bfdb5 148 logTime("Built LX engine");
2bb56822 149
49815cc0 150 // Set the patterns
42a424d7 151 LXEngine engine = lx.engine;
dde75983
MS
152 engine.setPatterns(patterns = _leftPatterns(lx));
153 engine.addDeck(_rightPatterns(lx));
49815cc0 154 logTime("Built patterns");
3f16fd02
MS
155
156 // Transitions
157 transitions = transitions(lx);
0c7bfdb5 158 lx.engine.getDeck(RIGHT_DECK).setFaderTransition(transitions[0]);
a898d79b 159 logTime("Built transitions");
3f16fd02
MS
160
161 // Effects
0c7bfdb5
MS
162 lx.addEffects(effectsArr = _effectsArray(effects = new Effects()));
163 selectedEffect = new DiscreteParameter("EFFECT", effectsArr.length);
49815cc0 164 logTime("Built effects");
4214e9a2 165
e0794d3a
MS
166 // Preset manager
167 presetManager = new PresetManager();
168 logTime("Loaded presets");
4214e9a2 169
1f974cbc
MS
170 // MIDI devices
171 midiEngine = new MidiEngine();
1f974cbc
MS
172 logTime("Setup MIDI devices");
173
e73ef85d 174 // Build output driver
34490867 175 try {
bffb6a80
MS
176 outputs = buildOutputs();
177 for (LXOutput output : outputs) {
34490867
MS
178 lx.addOutput(output);
179 }
180 } catch (Exception x) {
181 x.printStackTrace();
182 }
bffb6a80 183 logTime("Built Grizzly + Panda Outputs");
0c7bfdb5
MS
184
185 // Mapping tool
dde75983 186 mappingTool = new MappingTool(lx);
0c7bfdb5 187 logTime("Built Mapping Tool");
34490867 188
49815cc0 189 // Build overlay UI
e037f60f
MS
190 UILayer[] layers = new UILayer[] {
191 // Camera layer
192 new UICameraLayer(lx.ui)
e18b4cb7 193 .setCenter(model.cx, model.cy, model.cz)
e037f60f
MS
194 .setRadius(290).addComponent(new UICubesLayer()),
195
196 // Left controls
0c7bfdb5 197 uiPatternA = new UIPatternDeck(lx.ui, lx.engine.getDeck(LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
a8d55ade
MS
198 new UIBlendMode(4, 332, 140, 86),
199 new UIEffects(4, 422, 140, 144),
200 new UITempo(4, 570, 140, 50),
fa4f822d 201 uiSpeed = new UISpeed(4, 624, 140, 50),
a8d55ade 202
e037f60f 203 // Right controls
0c7bfdb5 204 new UIPatternDeck(lx.ui, lx.engine.getDeck(RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
d6ac1ee8 205 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
bffb6a80 206 new UIOutput(outputs, width-144, 494, 140, 106),
d626bc9b 207
e037f60f 208 // Crossfader
a8d55ade 209 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
d626bc9b 210
e037f60f 211 // Overlays
a8d55ade 212 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
4e6626a9 213 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324)
d626bc9b 214 };
e037f60f
MS
215 uiMapping.setVisible(false);
216 for (UILayer layer : layers) {
217 lx.ui.addLayer(layer);
4e6626a9 218 }
e037f60f 219 logTime("Built UI");
4c640acc
MS
220
221 // Load logo image
222 logo = loadImage("data/logo.png");
e037f60f 223 logTime("Loaded logo image");
4e6626a9 224
49815cc0 225 println("Total setup: " + (millis() - startMillis) + "ms");
e037f60f 226 println("Hit the 'o' key to toggle live output");
b0f59128 227
bffb6a80 228 lx.engine.framesPerSecond.setValue(60);
b0f59128 229 lx.engine.setThreaded(true);
49815cc0
MS
230}
231
dde75983
MS
232public SCPattern getPattern() {
233 return (SCPattern) lx.getPattern();
234}
235
236/**
237 * Subclass of LXPattern specific to sugar cubes. These patterns
0c7bfdb5 238 * get access to the state and geometry, and have some
dde75983
MS
239 * little helpers for interacting with the model.
240 */
241public static abstract class SCPattern extends LXPattern {
242
243 protected SCPattern(LX lx) {
244 super(lx);
245 }
246
247 /**
248 * Reset this pattern to its default state.
249 */
250 public final void reset() {
251 for (LXParameter parameter : getParameters()) {
252 parameter.reset();
253 }
254 onReset();
255 }
256
257 /**
258 * Subclasses may override to add additional reset functionality.
259 */
260 protected /*abstract*/ void onReset() {}
261
262 /**
263 * Invoked by the engine when a grid controller button press occurs
264 *
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
268 */
269 public boolean gridPressed(int row, int col) {
270 return false;
271 }
272
273 /**
274 * Invoked by the engine when a grid controller button release occurs
275 *
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
279 */
280 public boolean gridReleased(int row, int col) {
281 return false;
282 }
283
284 /**
285 * Invoked by engine when this pattern is focused an a midi note is received.
286 *
287 * @param note
288 * @return True if the pattern has consumed this note, false if the top-level
289 * may handle it
290 */
291 public boolean noteOn(rwmidi.Note note) {
292 return false;
293 }
294
295 /**
296 * Invoked by engine when this pattern is focused an a midi note off is received.
297 *
298 * @param note
299 * @return True if the pattern has consumed this note, false if the top-level
300 * may handle it
301 */
302 public boolean noteOff(rwmidi.Note note) {
303 return false;
304 }
305
306 /**
307 * Invoked by engine when this pattern is focused an a controller is received
308 *
309 * @param note
310 * @return True if the pattern has consumed this controller, false if the top-level
311 * may handle it
312 */
313 public boolean controllerChange(rwmidi.Controller controller) {
314 return false;
315 }
316}
317
e18b4cb7
MS
318long simulationNanos = 0;
319
34327c96
MS
320/**
321 * Core render loop and drawing functionality.
322 */
49815cc0 323void draw() {
73678c57
MS
324 long drawStart = System.nanoTime();
325
4e6626a9 326 // Set background
0a9f99cc 327 background(40);
4e6626a9
MS
328
329 // Send colors
0c7bfdb5 330 color[] sendColors = lx.getColors();
73678c57 331 long gammaStart = System.nanoTime();
7974acd6
MS
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);
336 float b = hsb[2];
337 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
338 }
73678c57 339 long gammaNanos = System.nanoTime() - gammaStart;
4e6626a9 340
e037f60f 341 // Always draw FPS meter
4e6626a9 342 drawFPS();
4e6626a9
MS
343
344 // TODO(mcslee): fix
73678c57 345 long drawNanos = System.nanoTime() - drawStart;
e18b4cb7
MS
346 long uiNanos = 0;
347
73678c57 348 if (diagnosticsOn) {
e037f60f 349 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos);
4e6626a9
MS
350 }
351}
352
e037f60f 353void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos) {
73678c57
MS
354 float ws = 4 / 1000000.;
355 int thirtyfps = 1000000000 / 30;
356 int sixtyfps = 1000000000 / 60;
357 int x = width - 138;
358 int y = height - 14;
359 int h = 10;
360 noFill();
361 stroke(#999999);
362 rect(x, y, thirtyfps * ws, h);
363 noStroke();
364 int xp = x;
365 float hv = 0;
b0f59128 366 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, lx.engine.timer.outputNanos }) {
73678c57
MS
367 fill(lx.hsb(hv % 360, 100, 80));
368 rect(xp, y, val * ws, h-1);
369 hv += 140;
370 xp += val * ws;
371 }
372 noFill();
373 stroke(#333333);
374 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
ef7118ad
MS
375
376 y = y - 14;
377 xp = x;
378 float tw = thirtyfps * ws;
379 noFill();
380 stroke(#999999);
381 rect(x, y, tw, h);
bae2197a 382 h = 5;
ef7118ad
MS
383 noStroke();
384 for (long val : new long[] {
385 lx.engine.timer.deckNanos,
bae2197a 386 lx.engine.timer.copyNanos,
ef7118ad
MS
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);
391 hv += 140;
392 xp += amt * tw;
bae2197a
MS
393 }
394
395 xp = x;
396 y += h;
397 hv = 120;
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);
405 hv += 140;
406 xp += amt * tw;
407 }
7974acd6
MS
408}
409
4e6626a9 410void drawFPS() {
d626bc9b
MS
411 // Always draw FPS meter
412 fill(#555555);
413 textSize(9);
414 textAlign(LEFT, BASELINE);
415 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
49815cc0
MS
416}
417
4c640acc 418
34327c96
MS
419/**
420 * Top-level keyboard event handling
421 */
49815cc0 422void keyPressed() {
bf551144 423 if (mappingMode) {
d626bc9b 424 mappingTool.keyPressed(uiMapping);
bf551144 425 }
3f8be614 426 switch (key) {
2b068dd5
MS
427 case '1':
428 case '2':
429 case '3':
430 case '4':
431 case '5':
432 case '6':
433 case '7':
434 case '8':
435 if (!midiEngine.isQwertyEnabled()) {
436 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
437 }
438 break;
439
440 case '!':
441 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
442 break;
443 case '@':
444 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
445 break;
446 case '#':
447 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
448 break;
449 case '$':
450 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
451 break;
452 case '%':
453 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
454 break;
455 case '^':
456 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
457 break;
458 case '&':
459 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
460 break;
461 case '*':
462 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
463 break;
464
0e3c5542
MS
465 case '-':
466 case '_':
467 frameRate(--targetFramerate);
468 break;
469 case '=':
470 case '+':
471 frameRate(++targetFramerate);
75e1ddde
MS
472 break;
473 case 'b':
24fc0330 474 effects.boom.trigger();
75e1ddde 475 break;
cc9fcf4b 476 case 'd':
d6ac1ee8 477 if (!midiEngine.isQwertyEnabled()) {
a8d55ade
MS
478 debugMode = !debugMode;
479 println("Debug output: " + (debugMode ? "ON" : "OFF"));
480 }
554e38ff 481 break;
bf551144 482 case 'm':
d6ac1ee8 483 if (!midiEngine.isQwertyEnabled()) {
a8d55ade
MS
484 mappingMode = !mappingMode;
485 uiPatternA.setVisible(!mappingMode);
486 uiMapping.setVisible(mappingMode);
487 if (mappingMode) {
488 restoreToPattern = lx.getPattern();
489 lx.setPatterns(new LXPattern[] { mappingTool });
490 } else {
491 lx.setPatterns(patterns);
492 LXTransition pop = restoreToPattern.getTransition();
493 restoreToPattern.setTransition(null);
494 lx.goPattern(restoreToPattern);
495 restoreToPattern.setTransition(pop);
496 }
bf551144
MS
497 }
498 break;
19d16a16
MS
499 case 't':
500 if (!midiEngine.isQwertyEnabled()) {
501 lx.engine.setThreaded(!lx.engine.isThreaded());
502 }
503 break;
e037f60f 504 case 'o':
e73ef85d 505 case 'p':
bffb6a80 506 for (LXOutput output : outputs) {
e037f60f 507 output.enabled.toggle();
79ae8245 508 }
cc9fcf4b 509 break;
73678c57
MS
510 case 'q':
511 if (!midiEngine.isQwertyEnabled()) {
512 diagnosticsOn = !diagnosticsOn;
513 }
514 break;
7974acd6
MS
515 case 's':
516 if (!midiEngine.isQwertyEnabled()) {
517 simulationOn = !simulationOn;
518 }
519 break;
bffb6a80
MS
520 case 'S':
521 if (!midiEngine.isQwertyEnabled()) {
522 structureOn = !structureOn;
523 }
524 break;
d626bc9b 525 }
0a9f99cc 526}
73687629 527