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