redo with new anti-GLucose
[SugarCubes.git] / Internals.pde
1 /**
2 * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND
3 *
4 * //\\ //\\ //\\ //\\
5 * ///\\\ ///\\\ ///\\\ ///\\\
6 * \\\/// \\\/// \\\/// \\\///
7 * \\// \\// \\// \\//H
8 *
9 * EXPERTS ONLY!! EXPERTS ONLY!!
10 *
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
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.*;
27 import ddf.minim.*;
28 import ddf.minim.analysis.*;
29 import processing.opengl.*;
30 import rwmidi.*;
31 import java.lang.reflect.*;
32 import java.util.ArrayList;
33 import java.util.Collections;
34 import java.util.List;
35
36 static final int VIEWPORT_WIDTH = 900;
37 static final int VIEWPORT_HEIGHT = 700;
38
39 static final int LEFT_DECK = 0;
40 static final int RIGHT_DECK = 1;
41
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;
46
47 int targetFramerate = 60;
48 int startMillis, lastMillis;
49
50 // Core engine variables
51 LX lx;
52 Model model;
53 LXPattern[] patterns;
54 LXTransition[] transitions;
55 Effects effects;
56 LXEffect[] effectsArr;
57 DiscreteParameter selectedEffect;
58 MappingTool mappingTool;
59 GrizzlyOutput[] grizzlies;
60 PresetManager presetManager;
61 MidiEngine midiEngine;
62
63 // Display configuration mode
64 boolean mappingMode = false;
65 boolean debugMode = false;
66 boolean simulationOn = true;
67 boolean diagnosticsOn = false;
68 LXPattern restoreToPattern = null;
69 PImage logo;
70 float[] hsb = new float[3];
71
72 // Handles to UI objects
73 UIPatternDeck uiPatternA;
74 UICrossfader uiCrossfader;
75 UIMidi uiMidi;
76 UIMapping uiMapping;
77 UIDebugText uiDebugText;
78 UISpeed uiSpeed;
79
80 /**
81 * Engine construction and initialization.
82 */
83
84 LXTransition _transition(LX lx) {
85 return new DissolveTransition(lx).setDuration(1000);
86 }
87
88 LXPattern[] _leftPatterns(LX lx) {
89 LXPattern[] patterns = patterns(lx);
90 for (LXPattern p : patterns) {
91 p.setTransition(_transition(lx));
92 }
93 return patterns;
94 }
95
96 LXPattern[] _rightPatterns(LX lx) {
97 LXPattern[] patterns = _leftPatterns(lx);
98 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
99 int i = 0;
100 rightPatterns[i++] = new BlankPattern(lx).setTransition(_transition(lx));
101 for (LXPattern p : patterns) {
102 rightPatterns[i++] = p;
103 }
104 return rightPatterns;
105 }
106
107 LXEffect[] _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[]{});
118 }
119
120 LXEffect getSelectedEffect() {
121 return effectsArr[selectedEffect.getValuei()];
122 }
123
124 void logTime(String evt) {
125 int now = millis();
126 println(evt + ": " + (now - lastMillis) + "ms");
127 lastMillis = now;
128 }
129
130 void setup() {
131 startMillis = lastMillis = millis();
132
133 // Initialize the Processing graphics environment
134 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
135 frameRate(targetFramerate);
136 noSmooth();
137 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
138 logTime("Created viewport");
139
140 // Create the model
141 model = buildModel();
142 logTime("Built Model");
143
144 // LX engine
145 lx = new LX(this, model);
146 lx.enableKeyboardTempo();
147 logTime("Built LX engine");
148
149 // Set the patterns
150 LXEngine engine = lx.engine;
151 engine.setPatterns(patterns = _leftPatterns(lx));
152 engine.addDeck(_rightPatterns(lx));
153 logTime("Built patterns");
154
155 // Transitions
156 transitions = transitions(lx);
157 lx.engine.getDeck(RIGHT_DECK).setFaderTransition(transitions[0]);
158 logTime("Built transitions");
159
160 // Effects
161 lx.addEffects(effectsArr = _effectsArray(effects = new Effects()));
162 selectedEffect = new DiscreteParameter("EFFECT", effectsArr.length);
163 logTime("Built effects");
164
165 // Preset manager
166 presetManager = new PresetManager();
167 logTime("Loaded presets");
168
169 // MIDI devices
170 midiEngine = new MidiEngine();
171 logTime("Setup MIDI devices");
172
173 // Build output driver
174 grizzlies = new GrizzlyOutput[]{};
175 try {
176 grizzlies = buildGrizzlies();
177 for (LXOutput output : grizzlies) {
178 lx.addOutput(output);
179 }
180 } catch (Exception x) {
181 x.printStackTrace();
182 }
183 logTime("Built Grizzly Outputs");
184
185 // Mapping tool
186 mappingTool = new MappingTool(lx);
187 logTime("Built Mapping Tool");
188
189 // Build overlay UI
190 UILayer[] layers = new UILayer[] {
191 // Camera layer
192 new UICameraLayer(lx.ui)
193 .setCenter(model.cx, model.cy, model.cz)
194 .setRadius(290).addComponent(new UICubesLayer()),
195
196 // Left controls
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),
202
203 // Right controls
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),
207
208 // Crossfader
209 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
210
211 // Overlays
212 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
213 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324)
214 };
215 uiMapping.setVisible(false);
216 for (UILayer layer : layers) {
217 lx.ui.addLayer(layer);
218 }
219 logTime("Built UI");
220
221 // Load logo image
222 logo = loadImage("data/logo.png");
223 logTime("Loaded logo image");
224
225 println("Total setup: " + (millis() - startMillis) + "ms");
226 println("Hit the 'o' key to toggle live output");
227 }
228
229 public SCPattern getPattern() {
230 return (SCPattern) lx.getPattern();
231 }
232
233 /**
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.
237 */
238 public 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
315 long simulationNanos = 0;
316
317 /**
318 * Core render loop and drawing functionality.
319 */
320 void draw() {
321 long drawStart = System.nanoTime();
322
323 // Set background
324 background(40);
325
326 // Send colors
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);
333 float b = hsb[2];
334 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
335 }
336 long gammaNanos = System.nanoTime() - gammaStart;
337
338 // Always draw FPS meter
339 drawFPS();
340
341 // TODO(mcslee): fix
342 long drawNanos = System.nanoTime() - drawStart;
343 long uiNanos = 0;
344
345 if (diagnosticsOn) {
346 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos);
347 }
348 }
349
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;
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;
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);
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);
372
373 y = y - 14;
374 xp = x;
375 float tw = thirtyfps * ws;
376 noFill();
377 stroke(#999999);
378 rect(x, y, tw, h);
379 h = 5;
380 noStroke();
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);
388 hv += 140;
389 xp += amt * tw;
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 }
405 }
406
407 void drawFPS() {
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);
413 }
414
415
416 /**
417 * Top-level keyboard event handling
418 */
419 void keyPressed() {
420 if (mappingMode) {
421 mappingTool.keyPressed(uiMapping);
422 }
423 switch (key) {
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
462 case '-':
463 case '_':
464 frameRate(--targetFramerate);
465 break;
466 case '=':
467 case '+':
468 frameRate(++targetFramerate);
469 break;
470 case 'b':
471 effects.boom.trigger();
472 break;
473 case 'd':
474 if (!midiEngine.isQwertyEnabled()) {
475 debugMode = !debugMode;
476 println("Debug output: " + (debugMode ? "ON" : "OFF"));
477 }
478 break;
479 case 'm':
480 if (!midiEngine.isQwertyEnabled()) {
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 }
494 }
495 break;
496 case 't':
497 if (!midiEngine.isQwertyEnabled()) {
498 lx.engine.setThreaded(!lx.engine.isThreaded());
499 }
500 break;
501 case 'o':
502 case 'p':
503 for (LXOutput output : grizzlies) {
504 output.enabled.toggle();
505 }
506 break;
507 case 'q':
508 if (!midiEngine.isQwertyEnabled()) {
509 diagnosticsOn = !diagnosticsOn;
510 }
511 break;
512 case 's':
513 if (!midiEngine.isQwertyEnabled()) {
514 simulationOn = !simulationOn;
515 }
516 break;
517 }
518 }
519