Now with shader language and many fps
[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 logTime("Created viewport");
138
139 // Create the model
140 model = buildModel();
141 logTime("Built Model");
142
143 // LX engine
144 lx = new LX(this, model);
145 lx.enableKeyboardTempo();
146 logTime("Built LX engine");
147
148 // Set the patterns
149 LXEngine engine = lx.engine;
150 engine.setPatterns(patterns = _leftPatterns(lx));
151 engine.addDeck(_rightPatterns(lx));
152 logTime("Built patterns");
153
154 // Transitions
155 transitions = transitions(lx);
156 lx.engine.getDeck(RIGHT_DECK).setFaderTransition(transitions[0]);
157 logTime("Built transitions");
158
159 // Effects
160 lx.addEffects(effectsArr = _effectsArray(effects = new Effects()));
161 selectedEffect = new DiscreteParameter("EFFECT", effectsArr.length);
162 logTime("Built effects");
163
164 // Preset manager
165 presetManager = new PresetManager();
166 logTime("Loaded presets");
167
168 // MIDI devices
169 midiEngine = new MidiEngine();
170 logTime("Setup MIDI devices");
171
172 // Build output driver
173 grizzlies = new GrizzlyOutput[]{};
174 try {
175 grizzlies = buildGrizzlies();
176 for (LXOutput output : grizzlies) {
177 lx.addOutput(output);
178 }
179 } catch (Exception x) {
180 x.printStackTrace();
181 }
182 logTime("Built Grizzly Outputs");
183
184 // Mapping tool
185 mappingTool = new MappingTool(lx);
186 logTime("Built Mapping Tool");
187
188 // Build overlay UI
189 UILayer[] layers = new UILayer[] {
190 // Camera layer
191 new UICameraLayer(lx.ui)
192 .setCenter(model.cx, model.cy, model.cz)
193 .setRadius(290).addComponent(new UICubesLayer()),
194
195 // Left controls
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),
201
202 // Right controls
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),
206
207 // Crossfader
208 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
209
210 // Overlays
211 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
212 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324)
213 };
214 uiMapping.setVisible(false);
215 for (UILayer layer : layers) {
216 lx.ui.addLayer(layer);
217 }
218 logTime("Built UI");
219
220 // Load logo image
221 logo = loadImage("data/logo.png");
222 logTime("Loaded logo image");
223
224 println("Total setup: " + (millis() - startMillis) + "ms");
225 println("Hit the 'o' key to toggle live output");
226 }
227
228 public SCPattern getPattern() {
229 return (SCPattern) lx.getPattern();
230 }
231
232 /**
233 * Subclass of LXPattern specific to sugar cubes. These patterns
234 * get access to the state and geometry, and have some
235 * little helpers for interacting with the model.
236 */
237 public static abstract class SCPattern extends LXPattern {
238
239 protected SCPattern(LX lx) {
240 super(lx);
241 }
242
243 /**
244 * Reset this pattern to its default state.
245 */
246 public final void reset() {
247 for (LXParameter parameter : getParameters()) {
248 parameter.reset();
249 }
250 onReset();
251 }
252
253 /**
254 * Subclasses may override to add additional reset functionality.
255 */
256 protected /*abstract*/ void onReset() {}
257
258 /**
259 * Invoked by the engine when a grid controller button press occurs
260 *
261 * @param row Row index on the gird
262 * @param col Column index on the grid
263 * @return True if the event was consumed, false otherwise
264 */
265 public boolean gridPressed(int row, int col) {
266 return false;
267 }
268
269 /**
270 * Invoked by the engine when a grid controller button release occurs
271 *
272 * @param row Row index on the gird
273 * @param col Column index on the grid
274 * @return True if the event was consumed, false otherwise
275 */
276 public boolean gridReleased(int row, int col) {
277 return false;
278 }
279
280 /**
281 * Invoked by engine when this pattern is focused an a midi note is received.
282 *
283 * @param note
284 * @return True if the pattern has consumed this note, false if the top-level
285 * may handle it
286 */
287 public boolean noteOn(rwmidi.Note note) {
288 return false;
289 }
290
291 /**
292 * Invoked by engine when this pattern is focused an a midi note off is received.
293 *
294 * @param note
295 * @return True if the pattern has consumed this note, false if the top-level
296 * may handle it
297 */
298 public boolean noteOff(rwmidi.Note note) {
299 return false;
300 }
301
302 /**
303 * Invoked by engine when this pattern is focused an a controller is received
304 *
305 * @param note
306 * @return True if the pattern has consumed this controller, false if the top-level
307 * may handle it
308 */
309 public boolean controllerChange(rwmidi.Controller controller) {
310 return false;
311 }
312 }
313
314 long simulationNanos = 0;
315
316 /**
317 * Core render loop and drawing functionality.
318 */
319 void draw() {
320 long drawStart = System.nanoTime();
321
322 // Set background
323 background(40);
324
325 // Send colors
326 color[] sendColors = lx.getColors();
327 long gammaStart = System.nanoTime();
328 // Gamma correction here. Apply a cubic to the brightness
329 // for better representation of dynamic range
330 for (int i = 0; i < sendColors.length; ++i) {
331 lx.RGBtoHSB(sendColors[i], hsb);
332 float b = hsb[2];
333 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
334 }
335 long gammaNanos = System.nanoTime() - gammaStart;
336
337 // Always draw FPS meter
338 drawFPS();
339
340 // TODO(mcslee): fix
341 long drawNanos = System.nanoTime() - drawStart;
342 long uiNanos = 0;
343
344 if (diagnosticsOn) {
345 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos);
346 }
347 }
348
349 void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos) {
350 float ws = 4 / 1000000.;
351 int thirtyfps = 1000000000 / 30;
352 int sixtyfps = 1000000000 / 60;
353 int x = width - 138;
354 int y = height - 14;
355 int h = 10;
356 noFill();
357 stroke(#999999);
358 rect(x, y, thirtyfps * ws, h);
359 noStroke();
360 int xp = x;
361 float hv = 0;
362 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, lx.timer.outputNanos }) {
363 fill(lx.hsb(hv % 360, 100, 80));
364 rect(xp, y, val * ws, h-1);
365 hv += 140;
366 xp += val * ws;
367 }
368 noFill();
369 stroke(#333333);
370 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
371
372 y = y - 14;
373 xp = x;
374 float tw = thirtyfps * ws;
375 noFill();
376 stroke(#999999);
377 rect(x, y, tw, h);
378 h = 5;
379 noStroke();
380 for (long val : new long[] {
381 lx.engine.timer.deckNanos,
382 lx.engine.timer.copyNanos,
383 lx.engine.timer.fxNanos}) {
384 float amt = val / (float) lx.timer.drawNanos;
385 fill(lx.hsb(hv % 360, 100, 80));
386 rect(xp, y, amt * tw, h-1);
387 hv += 140;
388 xp += amt * tw;
389 }
390
391 xp = x;
392 y += h;
393 hv = 120;
394 for (long val : new long[] {
395 lx.engine.getDeck(0).timer.runNanos,
396 lx.engine.getDeck(1).timer.runNanos,
397 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
398 float amt = val / (float) lx.timer.drawNanos;
399 fill(lx.hsb(hv % 360, 100, 80));
400 rect(xp, y, amt * tw, h-1);
401 hv += 140;
402 xp += amt * tw;
403 }
404 }
405
406 void drawFPS() {
407 // Always draw FPS meter
408 fill(#555555);
409 textSize(9);
410 textAlign(LEFT, BASELINE);
411 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
412 }
413
414
415 /**
416 * Top-level keyboard event handling
417 */
418 void keyPressed() {
419 if (mappingMode) {
420 mappingTool.keyPressed(uiMapping);
421 }
422 switch (key) {
423 case '1':
424 case '2':
425 case '3':
426 case '4':
427 case '5':
428 case '6':
429 case '7':
430 case '8':
431 if (!midiEngine.isQwertyEnabled()) {
432 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
433 }
434 break;
435
436 case '!':
437 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
438 break;
439 case '@':
440 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
441 break;
442 case '#':
443 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
444 break;
445 case '$':
446 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
447 break;
448 case '%':
449 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
450 break;
451 case '^':
452 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
453 break;
454 case '&':
455 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
456 break;
457 case '*':
458 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
459 break;
460
461 case '-':
462 case '_':
463 frameRate(--targetFramerate);
464 break;
465 case '=':
466 case '+':
467 frameRate(++targetFramerate);
468 break;
469 case 'b':
470 effects.boom.trigger();
471 break;
472 case 'd':
473 if (!midiEngine.isQwertyEnabled()) {
474 debugMode = !debugMode;
475 println("Debug output: " + (debugMode ? "ON" : "OFF"));
476 }
477 break;
478 case 'm':
479 if (!midiEngine.isQwertyEnabled()) {
480 mappingMode = !mappingMode;
481 uiPatternA.setVisible(!mappingMode);
482 uiMapping.setVisible(mappingMode);
483 if (mappingMode) {
484 restoreToPattern = lx.getPattern();
485 lx.setPatterns(new LXPattern[] { mappingTool });
486 } else {
487 lx.setPatterns(patterns);
488 LXTransition pop = restoreToPattern.getTransition();
489 restoreToPattern.setTransition(null);
490 lx.goPattern(restoreToPattern);
491 restoreToPattern.setTransition(pop);
492 }
493 }
494 break;
495 case 't':
496 if (!midiEngine.isQwertyEnabled()) {
497 lx.engine.setThreaded(!lx.engine.isThreaded());
498 }
499 break;
500 case 'o':
501 case 'p':
502 for (LXOutput output : grizzlies) {
503 output.enabled.toggle();
504 }
505 break;
506 case 'q':
507 if (!midiEngine.isQwertyEnabled()) {
508 diagnosticsOn = !diagnosticsOn;
509 }
510 break;
511 case 's':
512 if (!midiEngine.isQwertyEnabled()) {
513 simulationOn = !simulationOn;
514 }
515 break;
516 }
517 }
518