0100c051c82d0b4bc8b6125829c5bff708e664b3
[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 glucose.*;
17 import glucose.model.*;
18 import heronarts.lx.*;
19 import heronarts.lx.effect.*;
20 import heronarts.lx.modulator.*;
21 import heronarts.lx.parameter.*;
22 import heronarts.lx.pattern.*;
23 import heronarts.lx.transform.*;
24 import heronarts.lx.transition.*;
25 import heronarts.lx.ui.*;
26 import heronarts.lx.ui.component.*;
27 import heronarts.lx.ui.control.*;
28 import ddf.minim.*;
29 import ddf.minim.analysis.*;
30 import processing.opengl.*;
31 import rwmidi.*;
32 import java.lang.reflect.*;
33
34 final int VIEWPORT_WIDTH = 900;
35 final int VIEWPORT_HEIGHT = 700;
36
37 // The trailer is measured from the outside of the black metal (but not including the higher welded part on the front)
38 final float TRAILER_WIDTH = 240;
39 final float TRAILER_DEPTH = 97;
40 final float TRAILER_HEIGHT = 33;
41
42 int targetFramerate = 60;
43 int startMillis, lastMillis;
44
45 // Core engine variables
46 GLucose glucose;
47 LX lx;
48 Model model;
49 LXPattern[] patterns;
50 Effects effects;
51 MappingTool mappingTool;
52 PandaDriver[] pandaBoards;
53 PresetManager presetManager;
54 MidiEngine midiEngine;
55
56 // Display configuration mode
57 boolean mappingMode = false;
58 boolean debugMode = false;
59 DebugUI debugUI;
60 boolean uiOn = true;
61 boolean simulationOn = true;
62 boolean diagnosticsOn = false;
63 LXPattern restoreToPattern = null;
64 PImage logo;
65 float[] hsb = new float[3];
66
67 // Handles to UI objects
68 UIPatternDeck uiPatternA;
69 UICrossfader uiCrossfader;
70 UIMidi uiMidi;
71 UIMapping uiMapping;
72 UIDebugText uiDebugText;
73 UISpeed uiSpeed;
74
75 /**
76 * Engine construction and initialization.
77 */
78
79 LXTransition _transition(GLucose glucose) {
80 return new DissolveTransition(glucose.lx).setDuration(1000);
81 }
82
83 LXPattern[] _leftPatterns(GLucose glucose) {
84 LXPattern[] patterns = patterns(glucose);
85 for (LXPattern p : patterns) {
86 p.setTransition(_transition(glucose));
87 }
88 return patterns;
89 }
90
91 LXPattern[] _rightPatterns(GLucose glucose) {
92 LXPattern[] patterns = _leftPatterns(glucose);
93 LXPattern[] rightPatterns = new LXPattern[patterns.length+1];
94 int i = 0;
95 rightPatterns[i++] = new BlankPattern(glucose).setTransition(_transition(glucose));
96 for (LXPattern p : patterns) {
97 rightPatterns[i++] = p;
98 }
99 return rightPatterns;
100 }
101
102 LXEffect[] _effectsArray(Effects effects) {
103 List<LXEffect> effectList = new ArrayList<LXEffect>();
104 for (Field f : effects.getClass().getDeclaredFields()) {
105 try {
106 Object val = f.get(effects);
107 if (val instanceof LXEffect) {
108 effectList.add((LXEffect)val);
109 }
110 } catch (IllegalAccessException iax) {}
111 }
112 return effectList.toArray(new LXEffect[]{});
113 }
114
115 void logTime(String evt) {
116 int now = millis();
117 println(evt + ": " + (now - lastMillis) + "ms");
118 lastMillis = now;
119 }
120
121 void setup() {
122 startMillis = lastMillis = millis();
123
124 // Initialize the Processing graphics environment
125 size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL);
126 frameRate(targetFramerate);
127 noSmooth();
128 // hint(ENABLE_OPENGL_4X_SMOOTH); // no discernable improvement?
129 logTime("Created viewport");
130
131 // Create the GLucose engine to run the cubes
132 glucose = new GLucose(this, model = buildModel());
133 lx = glucose.lx;
134 lx.enableKeyboardTempo();
135 logTime("Built GLucose engine");
136
137 // Set the patterns
138 LXEngine engine = lx.engine;
139 engine.setPatterns(patterns = _leftPatterns(glucose));
140 engine.addDeck(_rightPatterns(glucose));
141 logTime("Built patterns");
142 glucose.setTransitions(transitions(glucose));
143 logTime("Built transitions");
144 glucose.lx.addEffects(_effectsArray(effects = new Effects()));
145 logTime("Built effects");
146
147 // Preset manager
148 presetManager = new PresetManager();
149 logTime("Loaded presets");
150
151 // MIDI devices
152 midiEngine = new MidiEngine();
153 logTime("Setup MIDI devices");
154
155 // Build output driver
156 try {
157 GrizzlyOutput[] grizzlies = buildGrizzlies();
158 for (LXOutput output : grizzlies) {
159 lx.addOutput(output);
160 }
161 } catch (Exception x) {
162 x.printStackTrace();
163 }
164
165 PandaMapping[] pandaMappings = buildPandaList();
166 pandaBoards = new PandaDriver[pandaMappings.length];
167 int pbi = 0;
168 for (PandaMapping pm : pandaMappings) {
169 pandaBoards[pbi++] = new PandaDriver(pm.ip, glucose.model, pm);
170 }
171 mappingTool = new MappingTool(glucose, pandaMappings);
172 logTime("Built PandaDriver");
173
174 // Build overlay UI
175 debugUI = new DebugUI(pandaMappings);
176 UIContext[] contexts = new UIContext[] {
177 uiPatternA = new UIPatternDeck(lx.ui, lx.engine.getDeck(GLucose.LEFT_DECK), "PATTERN A", 4, 4, 140, 324),
178 new UIBlendMode(4, 332, 140, 86),
179 new UIEffects(4, 422, 140, 144),
180 new UITempo(4, 570, 140, 50),
181 uiSpeed = new UISpeed(4, 624, 140, 50),
182
183 new UIPatternDeck(lx.ui, lx.engine.getDeck(GLucose.RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324),
184 uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158),
185 new UIOutput(width-144, 494, 140, 106),
186
187 uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86),
188
189 uiDebugText = new UIDebugText(148, height-138, width-304, 44),
190 uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324)
191 };
192 uiMapping.setVisible(false);
193 lx.ui.addLayer(new UICameraLayer(lx.ui).setCenter(TRAILER_WIDTH/2., glucose.model.yMax/2, TRAILER_DEPTH/2.).setRadius(290).addComponent(new UICubesLayer()));
194 for (UIContext context : contexts) {
195 lx.ui.addLayer(context);
196 }
197 logTime("Built overlay UI");
198
199 // Load logo image
200 logo = loadImage("data/logo.png");
201
202 println("Total setup: " + (millis() - startMillis) + "ms");
203 println("Hit the 'p' key to toggle Panda Board output");
204 }
205
206 /**
207 * Core render loop and drawing functionality.
208 */
209 void draw() {
210 long drawStart = System.nanoTime();
211
212 // Set background
213 background(40);
214
215 // Send colors
216 color[] sendColors = glucose.getColors();
217 if (debugMode) {
218 debugUI.maskColors(sendColors);
219 }
220
221 long gammaStart = System.nanoTime();
222 // Gamma correction here. Apply a cubic to the brightness
223 // for better representation of dynamic range
224 for (int i = 0; i < sendColors.length; ++i) {
225 lx.RGBtoHSB(sendColors[i], hsb);
226 float b = hsb[2];
227 sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b));
228 }
229 long gammaNanos = System.nanoTime() - gammaStart;
230
231 long sendStart = System.nanoTime();
232 for (PandaDriver p : pandaBoards) {
233 p.send(sendColors);
234 }
235 long sendNanos = System.nanoTime() - sendStart;
236
237 drawFPS();
238 if (debugMode) {
239 debugUI.draw();
240 }
241
242 // TODO(mcslee): fix
243 long drawNanos = System.nanoTime() - drawStart;
244
245 long simulationNanos = 0, uiNanos = 0;
246 if (diagnosticsOn) {
247 drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos, sendNanos);
248 }
249 }
250
251 class UICubesLayer extends UICameraComponent {
252 void onDraw(UI ui) {
253 color[] simulationColors = glucose.getColors();
254 String displayMode = uiCrossfader.getDisplayMode();
255 if (displayMode == "A") {
256 simulationColors = lx.engine.getDeck(GLucose.LEFT_DECK).getColors();
257 } else if (displayMode == "B") {
258 simulationColors = lx.engine.getDeck(GLucose.RIGHT_DECK).getColors();
259 }
260 if (debugMode) {
261 debugUI.maskColors(simulationColors);
262 }
263
264 long simulationStart = System.nanoTime();
265 if (simulationOn) {
266 drawSimulation(simulationColors);
267 }
268 long simulationNanos = System.nanoTime() - simulationStart;
269
270 camera();
271 javax.media.opengl.GL gl = ((PGraphicsOpenGL)g).beginGL();
272 gl.glClear(javax.media.opengl.GL.GL_DEPTH_BUFFER_BIT);
273 ((PGraphicsOpenGL)g).endGL();
274 strokeWeight(1);
275 }
276 }
277
278 void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos, long sendNanos) {
279 float ws = 4 / 1000000.;
280 int thirtyfps = 1000000000 / 30;
281 int sixtyfps = 1000000000 / 60;
282 int x = width - 138;
283 int y = height - 14;
284 int h = 10;
285 noFill();
286 stroke(#999999);
287 rect(x, y, thirtyfps * ws, h);
288 noStroke();
289 int xp = x;
290 float hv = 0;
291 for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, sendNanos }) {
292 fill(lx.hsb(hv % 360, 100, 80));
293 rect(xp, y, val * ws, h-1);
294 hv += 140;
295 xp += val * ws;
296 }
297 noFill();
298 stroke(#333333);
299 line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1);
300
301 y = y - 14;
302 xp = x;
303 float tw = thirtyfps * ws;
304 noFill();
305 stroke(#999999);
306 rect(x, y, tw, h);
307 h = 5;
308 noStroke();
309 for (long val : new long[] {
310 lx.engine.timer.deckNanos,
311 lx.engine.timer.copyNanos,
312 lx.engine.timer.fxNanos}) {
313 float amt = val / (float) lx.timer.drawNanos;
314 fill(lx.hsb(hv % 360, 100, 80));
315 rect(xp, y, amt * tw, h-1);
316 hv += 140;
317 xp += amt * tw;
318 }
319
320 xp = x;
321 y += h;
322 hv = 120;
323 for (long val : new long[] {
324 lx.engine.getDeck(0).timer.runNanos,
325 lx.engine.getDeck(1).timer.runNanos,
326 lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) {
327 float amt = val / (float) lx.timer.drawNanos;
328 fill(lx.hsb(hv % 360, 100, 80));
329 rect(xp, y, amt * tw, h-1);
330 hv += 140;
331 xp += amt * tw;
332 }
333 }
334
335 void drawSimulation(color[] simulationColors) {
336 translate(0, 40, 0);
337
338 noStroke();
339 fill(#141414);
340 drawBox(0, -TRAILER_HEIGHT, 0, 0, 0, 0, TRAILER_WIDTH, TRAILER_HEIGHT, TRAILER_DEPTH, TRAILER_HEIGHT/2.);
341 fill(#070707);
342 stroke(#222222);
343 beginShape();
344 vertex(0, 0, 0);
345 vertex(TRAILER_WIDTH, 0, 0);
346 vertex(TRAILER_WIDTH, 0, TRAILER_DEPTH);
347 vertex(0, 0, TRAILER_DEPTH);
348 endShape();
349
350 // Draw the logo on the front of platform
351 pushMatrix();
352 translate(0, 0, -1);
353 float s = .07;
354 scale(s, -s, s);
355 image(logo, TRAILER_WIDTH/2/s-logo.width/2, TRAILER_HEIGHT/2/s-logo.height/2-2/s);
356 popMatrix();
357
358 noStroke();
359 if (glucose.model.bassBox.exists) {
360 drawBassBox(glucose.model.bassBox, false);
361 }
362 for (Speaker speaker : glucose.model.speakers) {
363 drawSpeaker(speaker);
364 }
365 for (Cube c : glucose.model.cubes) {
366 drawCube(c);
367 }
368
369 noFill();
370 strokeWeight(2);
371 beginShape(POINTS);
372 for (LXPoint p : glucose.model.points) {
373 stroke(simulationColors[p.index]);
374 vertex(p.x, p.y, p.z);
375 }
376 endShape();
377 }
378
379 void drawBassBox(BassBox b, boolean hasSub) {
380
381 float in = .15;
382
383 if (hasSub) {
384 noStroke();
385 fill(#191919);
386 pushMatrix();
387 translate(b.x + BassBox.EDGE_WIDTH/2., b.y + BassBox.EDGE_HEIGHT/2, b.z + BassBox.EDGE_DEPTH/2.);
388 box(BassBox.EDGE_WIDTH-20*in, BassBox.EDGE_HEIGHT-20*in, BassBox.EDGE_DEPTH-20*in);
389 popMatrix();
390 }
391
392 noStroke();
393 fill(#393939);
394 drawBox(b.x+in, b.y+in, b.z+in, 0, 0, 0, BassBox.EDGE_WIDTH-in*2, BassBox.EDGE_HEIGHT-in*2, BassBox.EDGE_DEPTH-in*2, Cube.CHANNEL_WIDTH-in);
395
396 pushMatrix();
397 translate(b.x+(Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT-in, b.z + BassBox.EDGE_DEPTH/2.);
398 float lastOffset = 0;
399 for (float offset : BoothFloor.STRIP_OFFSETS) {
400 translate(offset - lastOffset, 0, 0);
401 box(Cube.CHANNEL_WIDTH-in, 0, BassBox.EDGE_DEPTH - 2*in);
402 lastOffset = offset;
403 }
404 popMatrix();
405
406 pushMatrix();
407 translate(b.x + (Cube.CHANNEL_WIDTH-in)/2., b.y + BassBox.EDGE_HEIGHT/2., b.z + in);
408 for (int j = 0; j < 2; ++j) {
409 pushMatrix();
410 for (int i = 0; i < BassBox.NUM_FRONT_STRUTS; ++i) {
411 translate(BassBox.FRONT_STRUT_SPACING, 0, 0);
412 box(Cube.CHANNEL_WIDTH-in, BassBox.EDGE_HEIGHT - in*2, 0);
413 }
414 popMatrix();
415 translate(0, 0, BassBox.EDGE_DEPTH - 2*in);
416 }
417 popMatrix();
418
419 pushMatrix();
420 translate(b.x + in, b.y + BassBox.EDGE_HEIGHT/2., b.z + BassBox.SIDE_STRUT_SPACING + (Cube.CHANNEL_WIDTH-in)/2.);
421 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
422 translate(BassBox.EDGE_WIDTH-2*in, 0, 0);
423 box(0, BassBox.EDGE_HEIGHT - in*2, Cube.CHANNEL_WIDTH-in);
424 popMatrix();
425
426 }
427
428 void drawCube(Cube c) {
429 float in = .15;
430 noStroke();
431 fill(#393939);
432 drawBox(c.x+in, c.y+in, c.z+in, c.rx, c.ry, c.rz, Cube.EDGE_WIDTH-in*2, Cube.EDGE_HEIGHT-in*2, Cube.EDGE_WIDTH-in*2, Cube.CHANNEL_WIDTH-in);
433 }
434
435 void drawSpeaker(Speaker s) {
436 float in = .15;
437
438 noStroke();
439 fill(#191919);
440 pushMatrix();
441 translate(s.x, s.y, s.z);
442 rotate(s.ry / 180. * PI, 0, -1, 0);
443 translate(Speaker.EDGE_WIDTH/2., Speaker.EDGE_HEIGHT/2., Speaker.EDGE_DEPTH/2.);
444 box(Speaker.EDGE_WIDTH-20*in, Speaker.EDGE_HEIGHT-20*in, Speaker.EDGE_DEPTH-20*in);
445 translate(0, Speaker.EDGE_HEIGHT/2. + Speaker.EDGE_HEIGHT*.8/2, 0);
446
447 fill(#222222);
448 box(Speaker.EDGE_WIDTH*.6, Speaker.EDGE_HEIGHT*.8, Speaker.EDGE_DEPTH*.75);
449 popMatrix();
450
451 noStroke();
452 fill(#393939);
453 drawBox(s.x+in, s.y+in, s.z+in, 0, s.ry, 0, Speaker.EDGE_WIDTH-in*2, Speaker.EDGE_HEIGHT-in*2, Speaker.EDGE_DEPTH-in*2, Cube.CHANNEL_WIDTH-in);
454 }
455
456 void drawBox(float x, float y, float z, float rx, float ry, float rz, float xd, float yd, float zd, float sw) {
457 pushMatrix();
458 translate(x, y, z);
459 rotate(rx / 180. * PI, -1, 0, 0);
460 rotate(ry / 180. * PI, 0, -1, 0);
461 rotate(rz / 180. * PI, 0, 0, -1);
462 for (int i = 0; i < 4; ++i) {
463 float wid = (i % 2 == 0) ? xd : zd;
464
465 beginShape();
466 vertex(0, 0);
467 vertex(wid, 0);
468 vertex(wid, yd);
469 vertex(wid - sw, yd);
470 vertex(wid - sw, sw);
471 vertex(0, sw);
472 endShape();
473 beginShape();
474 vertex(0, sw);
475 vertex(0, yd);
476 vertex(wid - sw, yd);
477 vertex(wid - sw, yd - sw);
478 vertex(sw, yd - sw);
479 vertex(sw, sw);
480 endShape();
481
482 translate(wid, 0, 0);
483 rotate(HALF_PI, 0, -1, 0);
484 }
485 popMatrix();
486 }
487
488 void drawFPS() {
489 // Always draw FPS meter
490 fill(#555555);
491 textSize(9);
492 textAlign(LEFT, BASELINE);
493 text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4);
494 }
495
496
497 /**
498 * Top-level keyboard event handling
499 */
500 void keyPressed() {
501 if (mappingMode) {
502 mappingTool.keyPressed(uiMapping);
503 }
504 switch (key) {
505 case '1':
506 case '2':
507 case '3':
508 case '4':
509 case '5':
510 case '6':
511 case '7':
512 case '8':
513 if (!midiEngine.isQwertyEnabled()) {
514 presetManager.select(midiEngine.getFocusedDeck(), key - '1');
515 }
516 break;
517
518 case '!':
519 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0);
520 break;
521 case '@':
522 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1);
523 break;
524 case '#':
525 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2);
526 break;
527 case '$':
528 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3);
529 break;
530 case '%':
531 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4);
532 break;
533 case '^':
534 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5);
535 break;
536 case '&':
537 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6);
538 break;
539 case '*':
540 if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7);
541 break;
542
543 case '-':
544 case '_':
545 frameRate(--targetFramerate);
546 break;
547 case '=':
548 case '+':
549 frameRate(++targetFramerate);
550 break;
551 case 'b':
552 effects.boom.trigger();
553 break;
554 case 'd':
555 if (!midiEngine.isQwertyEnabled()) {
556 debugMode = !debugMode;
557 println("Debug output: " + (debugMode ? "ON" : "OFF"));
558 }
559 break;
560 case 'm':
561 if (!midiEngine.isQwertyEnabled()) {
562 mappingMode = !mappingMode;
563 uiPatternA.setVisible(!mappingMode);
564 uiMapping.setVisible(mappingMode);
565 if (mappingMode) {
566 restoreToPattern = lx.getPattern();
567 lx.setPatterns(new LXPattern[] { mappingTool });
568 } else {
569 lx.setPatterns(patterns);
570 LXTransition pop = restoreToPattern.getTransition();
571 restoreToPattern.setTransition(null);
572 lx.goPattern(restoreToPattern);
573 restoreToPattern.setTransition(pop);
574 }
575 }
576 break;
577 case 't':
578 if (!midiEngine.isQwertyEnabled()) {
579 lx.engine.setThreaded(!lx.engine.isThreaded());
580 }
581 break;
582 case 'p':
583 for (PandaDriver p : pandaBoards) {
584 p.toggle();
585 }
586 break;
587 case 'q':
588 if (!midiEngine.isQwertyEnabled()) {
589 diagnosticsOn = !diagnosticsOn;
590 }
591 break;
592 case 's':
593 if (!midiEngine.isQwertyEnabled()) {
594 simulationOn = !simulationOn;
595 }
596 break;
597 case 'u':
598 if (!midiEngine.isQwertyEnabled()) {
599 uiOn = !uiOn;
600 }
601 break;
602 }
603 }
604
605 /**
606 * Top-level mouse event handling
607 */
608 void mousePressed() {
609 if (debugMode) {
610 debugUI.mousePressed();
611 }
612 }
613