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