Switch to threaded mode by default, run engine at 120FPS
[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");
b0f59128
MS
226
227 lx.engine.framesPerSecond.setValue(120);
228 lx.engine.setThreaded(true);
49815cc0
MS
229}
230
dde75983
MS
231public SCPattern getPattern() {
232 return (SCPattern) lx.getPattern();
233}
234
235/**
236 * Subclass of LXPattern specific to sugar cubes. These patterns
0c7bfdb5 237 * get access to the state and geometry, and have some
dde75983
MS
238 * little helpers for interacting with the model.
239 */
240public static abstract class SCPattern extends LXPattern {
241
242 protected SCPattern(LX lx) {
243 super(lx);
244 }
245
246 /**
247 * Reset this pattern to its default state.
248 */
249 public final void reset() {
250 for (LXParameter parameter : getParameters()) {
251 parameter.reset();
252 }
253 onReset();
254 }
255
256 /**
257 * Subclasses may override to add additional reset functionality.
258 */
259 protected /*abstract*/ void onReset() {}
260
261 /**
262 * Invoked by the engine when a grid controller button press occurs
263 *
264 * @param row Row index on the gird
265 * @param col Column index on the grid
266 * @return True if the event was consumed, false otherwise
267 */
268 public boolean gridPressed(int row, int col) {
269 return false;
270 }
271
272 /**
273 * Invoked by the engine when a grid controller button release occurs
274 *
275 * @param row Row index on the gird
276 * @param col Column index on the grid
277 * @return True if the event was consumed, false otherwise
278 */
279 public boolean gridReleased(int row, int col) {
280 return false;
281 }
282
283 /**
284 * Invoked by engine when this pattern is focused an a midi note is received.
285 *
286 * @param note
287 * @return True if the pattern has consumed this note, false if the top-level
288 * may handle it
289 */
290 public boolean noteOn(rwmidi.Note note) {
291 return false;
292 }
293
294 /**
295 * Invoked by engine when this pattern is focused an a midi note off is received.
296 *
297 * @param note
298 * @return True if the pattern has consumed this note, false if the top-level
299 * may handle it
300 */
301 public boolean noteOff(rwmidi.Note note) {
302 return false;
303 }
304
305 /**
306 * Invoked by engine when this pattern is focused an a controller is received
307 *
308 * @param note
309 * @return True if the pattern has consumed this controller, false if the top-level
310 * may handle it
311 */
312 public boolean controllerChange(rwmidi.Controller controller) {
313 return false;
314 }
315}
316
e18b4cb7
MS
317long simulationNanos = 0;
318
34327c96
MS
319/**
320 * Core render loop and drawing functionality.
321 */
49815cc0 322void draw() {
73678c57
MS
323 long drawStart = System.nanoTime();
324
4e6626a9 325 // Set background
0a9f99cc 326 background(40);
4e6626a9
MS
327
328 // Send colors
0c7bfdb5 329 color[] sendColors = lx.getColors();
73678c57 330 long gammaStart = System.nanoTime();
7974acd6
MS
331 // Gamma correction here. Apply a cubic to the brightness
332 // for better representation of dynamic range
333 for (int i = 0; i < sendColors.length; ++i) {
334 lx.RGBtoHSB(sendColors[i], hsb);
335 float b = hsb[2];
336 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
337 }
73678c57 338 long gammaNanos = System.nanoTime() - gammaStart;
4e6626a9 339
e037f60f 340 // Always draw FPS meter
4e6626a9 341 drawFPS();
4e6626a9
MS
342
343 // TODO(mcslee): fix
73678c57 344 long drawNanos = System.nanoTime() - drawStart;
e18b4cb7
MS
345 long uiNanos = 0;
346
73678c57 347 if (diagnosticsOn) {
e037f60f 348 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos);
4e6626a9
MS
349 }
350}
351
e037f60f 352void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos) {
73678c57
MS
353 float ws = 4 / 1000000.;
354 int thirtyfps = 1000000000 / 30;
355 int sixtyfps = 1000000000 / 60;
356 int x = width - 138;
357 int y = height - 14;
358 int h = 10;
359 noFill();
360 stroke(#999999);
361 rect(x, y, thirtyfps * ws, h);
362 noStroke();
363 int xp = x;
364 float hv = 0;
b0f59128 365 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, lx.engine.timer.outputNanos }) {
73678c57
MS
366 fill(lx.hsb(hv % 360, 100, 80));
367 rect(xp, y, val * ws, h-1);
368 hv += 140;
369 xp += val * ws;
370 }
371 noFill();
372 stroke(#333333);
373 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
ef7118ad
MS
374
375 y = y - 14;
376 xp = x;
377 float tw = thirtyfps * ws;
378 noFill();
379 stroke(#999999);
380 rect(x, y, tw, h);
bae2197a 381 h = 5;
ef7118ad
MS
382 noStroke();
383 for (long val : new long[] {
384 lx.engine.timer.deckNanos,
bae2197a 385 lx.engine.timer.copyNanos,
ef7118ad
MS
386 lx.engine.timer.fxNanos}) {
387 float amt = val / (float) lx.timer.drawNanos;
388 fill(lx.hsb(hv % 360, 100, 80));
389 rect(xp, y, amt * tw, h-1);
390 hv += 140;
391 xp += amt * tw;
bae2197a
MS
392 }
393
394 xp = x;
395 y += h;
396 hv = 120;
397 for (long val : new long[] {
398 lx.engine.getDeck(0).timer.runNanos,
399 lx.engine.getDeck(1).timer.runNanos,
400 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
401 float amt = val / (float) lx.timer.drawNanos;
402 fill(lx.hsb(hv % 360, 100, 80));
403 rect(xp, y, amt * tw, h-1);
404 hv += 140;
405 xp += amt * tw;
406 }
7974acd6
MS
407}
408
4e6626a9 409void drawFPS() {
d626bc9b
MS
410 // Always draw FPS meter
411 fill(#555555);
412 textSize(9);
413 textAlign(LEFT, BASELINE);
414 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
49815cc0
MS
415}
416
4c640acc 417
34327c96
MS
418/**
419 * Top-level keyboard event handling
420 */
49815cc0 421void keyPressed() {
bf551144 422 if (mappingMode) {
d626bc9b 423 mappingTool.keyPressed(uiMapping);
bf551144 424 }
3f8be614 425 switch (key) {
2b068dd5
MS
426 case '1':
427 case '2':
428 case '3':
429 case '4':
430 case '5':
431 case '6':
432 case '7':
433 case '8':
434 if (!midiEngine.isQwertyEnabled()) {
435 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
436 }
437 break;
438
439 case '!':
440 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
441 break;
442 case '@':
443 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
444 break;
445 case '#':
446 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
447 break;
448 case '$':
449 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
450 break;
451 case '%':
452 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
453 break;
454 case '^':
455 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
456 break;
457 case '&':
458 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
459 break;
460 case '*':
461 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
462 break;
463
0e3c5542
MS
464 case '-':
465 case '_':
466 frameRate(--targetFramerate);
467 break;
468 case '=':
469 case '+':
470 frameRate(++targetFramerate);
75e1ddde
MS
471 break;
472 case 'b':
24fc0330 473 effects.boom.trigger();
75e1ddde 474 break;
cc9fcf4b 475 case 'd':
d6ac1ee8 476 if (!midiEngine.isQwertyEnabled()) {
a8d55ade
MS
477 debugMode = !debugMode;
478 println("Debug output: " + (debugMode ? "ON" : "OFF"));
479 }
554e38ff 480 break;
bf551144 481 case 'm':
d6ac1ee8 482 if (!midiEngine.isQwertyEnabled()) {
a8d55ade
MS
483 mappingMode = !mappingMode;
484 uiPatternA.setVisible(!mappingMode);
485 uiMapping.setVisible(mappingMode);
486 if (mappingMode) {
487 restoreToPattern = lx.getPattern();
488 lx.setPatterns(new LXPattern[] { mappingTool });
489 } else {
490 lx.setPatterns(patterns);
491 LXTransition pop = restoreToPattern.getTransition();
492 restoreToPattern.setTransition(null);
493 lx.goPattern(restoreToPattern);
494 restoreToPattern.setTransition(pop);
495 }
bf551144
MS
496 }
497 break;
19d16a16
MS
498 case 't':
499 if (!midiEngine.isQwertyEnabled()) {
500 lx.engine.setThreaded(!lx.engine.isThreaded());
501 }
502 break;
e037f60f 503 case 'o':
e73ef85d 504 case 'p':
e037f60f
MS
505 for (LXOutput output : grizzlies) {
506 output.enabled.toggle();
79ae8245 507 }
cc9fcf4b 508 break;
73678c57
MS
509 case 'q':
510 if (!midiEngine.isQwertyEnabled()) {
511 diagnosticsOn = !diagnosticsOn;
512 }
513 break;
7974acd6
MS
514 case 's':
515 if (!midiEngine.isQwertyEnabled()) {
516 simulationOn = !simulationOn;
517 }
518 break;
d626bc9b 519 }
0a9f99cc 520}
73687629 521