Now with shader language and many fps
[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 logTime("Created viewport");
138
0c7bfdb5
MS
139 // Create the model
140 model = buildModel();
141 logTime("Built Model");
142
143 // LX engine
144 lx = new LX(this, model);
cc9fcf4b 145 lx.enableKeyboardTempo();
0c7bfdb5 146 logTime("Built LX engine");
2bb56822 147
49815cc0 148 // Set the patterns
42a424d7 149 LXEngine engine = lx.engine;
dde75983
MS
150 engine.setPatterns(patterns = _leftPatterns(lx));
151 engine.addDeck(_rightPatterns(lx));
49815cc0 152 logTime("Built patterns");
3f16fd02
MS
153
154 // Transitions
155 transitions = transitions(lx);
0c7bfdb5 156 lx.engine.getDeck(RIGHT_DECK).setFaderTransition(transitions[0]);
a898d79b 157 logTime("Built transitions");
3f16fd02
MS
158
159 // Effects
0c7bfdb5
MS
160 lx.addEffects(effectsArr = _effectsArray(effects = new Effects()));
161 selectedEffect = new DiscreteParameter("EFFECT", effectsArr.length);
49815cc0 162 logTime("Built effects");
4214e9a2 163
e0794d3a
MS
164 // Preset manager
165 presetManager = new PresetManager();
166 logTime("Loaded presets");
4214e9a2 167
1f974cbc
MS
168 // MIDI devices
169 midiEngine = new MidiEngine();
1f974cbc
MS
170 logTime("Setup MIDI devices");
171
e73ef85d 172 // Build output driver
e037f60f 173 grizzlies = new GrizzlyOutput[]{};
34490867 174 try {
e037f60f 175 grizzlies = buildGrizzlies();
34490867
MS
176 for (LXOutput output : grizzlies) {
177 lx.addOutput(output);
178 }
179 } catch (Exception x) {
180 x.printStackTrace();
181 }
e037f60f 182 logTime("Built Grizzly Outputs");
0c7bfdb5
MS
183
184 // Mapping tool
dde75983 185 mappingTool = new MappingTool(lx);
0c7bfdb5 186 logTime("Built Mapping Tool");
34490867 187
49815cc0 188 // Build overlay UI
e037f60f
MS
189 UILayer[] layers = new UILayer[] {
190 // Camera layer
191 new UICameraLayer(lx.ui)
e18b4cb7 192 .setCenter(model.cx, model.cy, model.cz)
e037f60f
MS
193 .setRadius(290).addComponent(new UICubesLayer()),
194
195 // Left controls
0c7bfdb5 196 uiPatternA = new UIPatternDeck(lx.ui, lx.engine.getDeck(LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
a8d55ade
MS
197 new UIBlendMode(4, 332, 140, 86),
198 new UIEffects(4, 422, 140, 144),
199 new UITempo(4, 570, 140, 50),
fa4f822d 200 uiSpeed = new UISpeed(4, 624, 140, 50),
a8d55ade 201
e037f60f 202 // Right controls
0c7bfdb5 203 new UIPatternDeck(lx.ui, lx.engine.getDeck(RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
d6ac1ee8 204 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
e037f60f 205 new UIOutput(grizzlies, width-144, 494, 140, 106),
d626bc9b 206
e037f60f 207 // Crossfader
a8d55ade 208 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
d626bc9b 209
e037f60f 210 // Overlays
a8d55ade 211 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
4e6626a9 212 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324)
d626bc9b 213 };
e037f60f
MS
214 uiMapping.setVisible(false);
215 for (UILayer layer : layers) {
216 lx.ui.addLayer(layer);
4e6626a9 217 }
e037f60f 218 logTime("Built UI");
4c640acc
MS
219
220 // Load logo image
221 logo = loadImage("data/logo.png");
e037f60f 222 logTime("Loaded logo image");
4e6626a9 223
49815cc0 224 println("Total setup: " + (millis() - startMillis) + "ms");
e037f60f 225 println("Hit the 'o' key to toggle live output");
49815cc0
MS
226}
227
dde75983
MS
228public SCPattern getPattern() {
229 return (SCPattern) lx.getPattern();
230}
231
232/**
233 * Subclass of LXPattern specific to sugar cubes. These patterns
0c7bfdb5 234 * get access to the state and geometry, and have some
dde75983
MS
235 * little helpers for interacting with the model.
236 */
237public 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
e18b4cb7
MS
314long simulationNanos = 0;
315
34327c96
MS
316/**
317 * Core render loop and drawing functionality.
318 */
49815cc0 319void draw() {
73678c57
MS
320 long drawStart = System.nanoTime();
321
4e6626a9 322 // Set background
0a9f99cc 323 background(40);
4e6626a9
MS
324
325 // Send colors
0c7bfdb5 326 color[] sendColors = lx.getColors();
73678c57 327 long gammaStart = System.nanoTime();
7974acd6
MS
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 }
73678c57 335 long gammaNanos = System.nanoTime() - gammaStart;
4e6626a9 336
e037f60f 337 // Always draw FPS meter
4e6626a9 338 drawFPS();
4e6626a9
MS
339
340 // TODO(mcslee): fix
73678c57 341 long drawNanos = System.nanoTime() - drawStart;
e18b4cb7
MS
342 long uiNanos = 0;
343
73678c57 344 if (diagnosticsOn) {
e037f60f 345 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos);
4e6626a9
MS
346 }
347}
348
e037f60f 349void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos) {
73678c57
MS
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;
e037f60f 362 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, lx.timer.outputNanos }) {
73678c57
MS
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);
ef7118ad
MS
371
372 y = y - 14;
373 xp = x;
374 float tw = thirtyfps * ws;
375 noFill();
376 stroke(#999999);
377 rect(x, y, tw, h);
bae2197a 378 h = 5;
ef7118ad
MS
379 noStroke();
380 for (long val : new long[] {
381 lx.engine.timer.deckNanos,
bae2197a 382 lx.engine.timer.copyNanos,
ef7118ad
MS
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;
bae2197a
MS
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 }
7974acd6
MS
404}
405
4e6626a9 406void drawFPS() {
d626bc9b
MS
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);
49815cc0
MS
412}
413
4c640acc 414
34327c96
MS
415/**
416 * Top-level keyboard event handling
417 */
49815cc0 418void keyPressed() {
bf551144 419 if (mappingMode) {
d626bc9b 420 mappingTool.keyPressed(uiMapping);
bf551144 421 }
3f8be614 422 switch (key) {
2b068dd5
MS
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
0e3c5542
MS
461 case '-':
462 case '_':
463 frameRate(--targetFramerate);
464 break;
465 case '=':
466 case '+':
467 frameRate(++targetFramerate);
75e1ddde
MS
468 break;
469 case 'b':
24fc0330 470 effects.boom.trigger();
75e1ddde 471 break;
cc9fcf4b 472 case 'd':
d6ac1ee8 473 if (!midiEngine.isQwertyEnabled()) {
a8d55ade
MS
474 debugMode = !debugMode;
475 println("Debug output: " + (debugMode ? "ON" : "OFF"));
476 }
554e38ff 477 break;
bf551144 478 case 'm':
d6ac1ee8 479 if (!midiEngine.isQwertyEnabled()) {
a8d55ade
MS
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 }
bf551144
MS
493 }
494 break;
19d16a16
MS
495 case 't':
496 if (!midiEngine.isQwertyEnabled()) {
497 lx.engine.setThreaded(!lx.engine.isThreaded());
498 }
499 break;
e037f60f 500 case 'o':
e73ef85d 501 case 'p':
e037f60f
MS
502 for (LXOutput output : grizzlies) {
503 output.enabled.toggle();
79ae8245 504 }
cc9fcf4b 505 break;
73678c57
MS
506 case 'q':
507 if (!midiEngine.isQwertyEnabled()) {
508 diagnosticsOn = !diagnosticsOn;
509 }
510 break;
7974acd6
MS
511 case 's':
512 if (!midiEngine.isQwertyEnabled()) {
513 simulationOn = !simulationOn;
514 }
515 break;
d626bc9b 516 }
0a9f99cc 517}
73687629 518