| 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.net.*; |
| 33 | import java.util.ArrayList; |
| 34 | import java.util.Collections; |
| 35 | import java.util.List; |
| 36 | |
| 37 | static final int VIEWPORT_WIDTH = 900; |
| 38 | static final int VIEWPORT_HEIGHT = 700; |
| 39 | |
| 40 | static final int LEFT_DECK = 0; |
| 41 | static final int RIGHT_DECK = 1; |
| 42 | |
| 43 | // The trailer is measured from the outside of the black metal (but not including the higher welded part on the front) |
| 44 | static final float TRAILER_WIDTH = 192; |
| 45 | static final float TRAILER_DEPTH = 192; |
| 46 | static final float TRAILER_HEIGHT = 33; |
| 47 | |
| 48 | int targetFramerate = 60; |
| 49 | int startMillis, lastMillis; |
| 50 | |
| 51 | // Core engine variables |
| 52 | LX lx; |
| 53 | Model model; |
| 54 | LXPattern[] patterns; |
| 55 | LXTransition[] transitions; |
| 56 | Effects effects; |
| 57 | LXEffect[] effectsArr; |
| 58 | DiscreteParameter selectedEffect; |
| 59 | MappingTool mappingTool; |
| 60 | IPOutput[] outputs = {}; |
| 61 | PresetManager presetManager; |
| 62 | MidiEngine midiEngine; |
| 63 | |
| 64 | // Display configuration mode |
| 65 | boolean mappingMode = false; |
| 66 | boolean debugMode = false; |
| 67 | boolean simulationOn = true; |
| 68 | boolean structureOn = false; |
| 69 | boolean diagnosticsOn = false; |
| 70 | LXPattern restoreToPattern = null; |
| 71 | PImage logo; |
| 72 | float[] hsb = new float[3]; |
| 73 | |
| 74 | // Handles to UI objects |
| 75 | UIPatternDeck uiPatternA; |
| 76 | UICrossfader uiCrossfader; |
| 77 | UIMidi uiMidi; |
| 78 | UIMapping uiMapping; |
| 79 | UIDebugText uiDebugText; |
| 80 | UISpeed uiSpeed; |
| 81 | |
| 82 | /** |
| 83 | * Engine construction and initialization. |
| 84 | */ |
| 85 | |
| 86 | LXTransition _transition(LX lx) { |
| 87 | return new DissolveTransition(lx).setDuration(1000); |
| 88 | } |
| 89 | |
| 90 | LXPattern[] _leftPatterns(LX lx) { |
| 91 | LXPattern[] patterns = patterns(lx); |
| 92 | for (LXPattern p : patterns) { |
| 93 | p.setTransition(_transition(lx)); |
| 94 | } |
| 95 | return patterns; |
| 96 | } |
| 97 | |
| 98 | LXPattern[] _rightPatterns(LX lx) { |
| 99 | LXPattern[] patterns = _leftPatterns(lx); |
| 100 | LXPattern[] rightPatterns = new LXPattern[patterns.length+1]; |
| 101 | int i = 0; |
| 102 | rightPatterns[i++] = new BlankPattern(lx).setTransition(_transition(lx)); |
| 103 | for (LXPattern p : patterns) { |
| 104 | rightPatterns[i++] = p; |
| 105 | } |
| 106 | return rightPatterns; |
| 107 | } |
| 108 | |
| 109 | LXEffect[] _effectsArray(Effects effects) { |
| 110 | List<LXEffect> effectList = new ArrayList<LXEffect>(); |
| 111 | for (Field f : effects.getClass().getDeclaredFields()) { |
| 112 | try { |
| 113 | Object val = f.get(effects); |
| 114 | if (val instanceof LXEffect) { |
| 115 | effectList.add((LXEffect)val); |
| 116 | } |
| 117 | } catch (IllegalAccessException iax) {} |
| 118 | } |
| 119 | return effectList.toArray(new LXEffect[]{}); |
| 120 | } |
| 121 | |
| 122 | LXEffect getSelectedEffect() { |
| 123 | return effectsArr[selectedEffect.getValuei()]; |
| 124 | } |
| 125 | |
| 126 | void logTime(String evt) { |
| 127 | int now = millis(); |
| 128 | println(evt + ": " + (now - lastMillis) + "ms"); |
| 129 | lastMillis = now; |
| 130 | } |
| 131 | |
| 132 | void setup() { |
| 133 | startMillis = lastMillis = millis(); |
| 134 | |
| 135 | // Initialize the Processing graphics environment |
| 136 | size(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, OPENGL); |
| 137 | frameRate(targetFramerate); |
| 138 | noSmooth(); |
| 139 | logTime("Created viewport"); |
| 140 | |
| 141 | // Create the model |
| 142 | model = buildModel(); |
| 143 | logTime("Built Model"); |
| 144 | |
| 145 | // LX engine |
| 146 | lx = new LX(this, model); |
| 147 | lx.enableKeyboardTempo(); |
| 148 | logTime("Built LX engine"); |
| 149 | |
| 150 | // Set the patterns |
| 151 | LXEngine engine = lx.engine; |
| 152 | engine.setPatterns(patterns = _leftPatterns(lx)); |
| 153 | engine.addDeck(_rightPatterns(lx)); |
| 154 | logTime("Built patterns"); |
| 155 | |
| 156 | // Transitions |
| 157 | transitions = transitions(lx); |
| 158 | lx.engine.getDeck(RIGHT_DECK).setFaderTransition(transitions[0]); |
| 159 | logTime("Built transitions"); |
| 160 | |
| 161 | // Effects |
| 162 | lx.addEffects(effectsArr = _effectsArray(effects = new Effects())); |
| 163 | selectedEffect = new DiscreteParameter("EFFECT", effectsArr.length); |
| 164 | logTime("Built effects"); |
| 165 | |
| 166 | // Preset manager |
| 167 | presetManager = new PresetManager(); |
| 168 | logTime("Loaded presets"); |
| 169 | |
| 170 | // MIDI devices |
| 171 | midiEngine = new MidiEngine(); |
| 172 | logTime("Setup MIDI devices"); |
| 173 | |
| 174 | // Build output driver |
| 175 | try { |
| 176 | outputs = buildOutputs(); |
| 177 | for (LXOutput output : outputs) { |
| 178 | lx.addOutput(output); |
| 179 | } |
| 180 | } catch (Exception x) { |
| 181 | x.printStackTrace(); |
| 182 | } |
| 183 | logTime("Built Grizzly + Panda Outputs"); |
| 184 | |
| 185 | // Mapping tool |
| 186 | mappingTool = new MappingTool(lx); |
| 187 | logTime("Built Mapping Tool"); |
| 188 | |
| 189 | // Build overlay UI |
| 190 | UILayer[] layers = new UILayer[] { |
| 191 | // Camera layer |
| 192 | new UICameraLayer(lx.ui) |
| 193 | .setCenter(model.cx, model.cy, model.cz) |
| 194 | .setRadius(290).addComponent(new UICubesLayer()), |
| 195 | |
| 196 | // Left controls |
| 197 | uiPatternA = new UIPatternDeck(lx.ui, lx.engine.getDeck(LEFT_DECK), "PATTERN A", 4, 4, 140, 324), |
| 198 | new UIBlendMode(4, 332, 140, 86), |
| 199 | new UIEffects(4, 422, 140, 144), |
| 200 | new UITempo(4, 570, 140, 50), |
| 201 | uiSpeed = new UISpeed(4, 624, 140, 50), |
| 202 | |
| 203 | // Right controls |
| 204 | new UIPatternDeck(lx.ui, lx.engine.getDeck(RIGHT_DECK), "PATTERN B", width-144, 4, 140, 324), |
| 205 | uiMidi = new UIMidi(midiEngine, width-144, 332, 140, 158), |
| 206 | new UIOutput(outputs, width-144, 494, 140, 106), |
| 207 | |
| 208 | // Crossfader |
| 209 | uiCrossfader = new UICrossfader(width/2-90, height-90, 180, 86), |
| 210 | |
| 211 | // Overlays |
| 212 | uiDebugText = new UIDebugText(148, height-138, width-304, 44), |
| 213 | uiMapping = new UIMapping(mappingTool, 4, 4, 140, 324) |
| 214 | }; |
| 215 | uiMapping.setVisible(false); |
| 216 | for (UILayer layer : layers) { |
| 217 | lx.ui.addLayer(layer); |
| 218 | } |
| 219 | logTime("Built UI"); |
| 220 | |
| 221 | // Load logo image |
| 222 | logo = loadImage("data/logo.png"); |
| 223 | logTime("Loaded logo image"); |
| 224 | |
| 225 | println("Total setup: " + (millis() - startMillis) + "ms"); |
| 226 | println("Hit the 'o' key to toggle live output"); |
| 227 | |
| 228 | lx.engine.framesPerSecond.setValue(60); |
| 229 | lx.engine.setThreaded(true); |
| 230 | } |
| 231 | |
| 232 | public SCPattern getPattern() { |
| 233 | return (SCPattern) lx.getPattern(); |
| 234 | } |
| 235 | |
| 236 | /** |
| 237 | * Subclass of LXPattern specific to sugar cubes. These patterns |
| 238 | * get access to the state and geometry, and have some |
| 239 | * little helpers for interacting with the model. |
| 240 | */ |
| 241 | public static abstract class SCPattern extends LXPattern { |
| 242 | |
| 243 | protected SCPattern(LX lx) { |
| 244 | super(lx); |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Reset this pattern to its default state. |
| 249 | */ |
| 250 | public final void reset() { |
| 251 | for (LXParameter parameter : getParameters()) { |
| 252 | parameter.reset(); |
| 253 | } |
| 254 | onReset(); |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Subclasses may override to add additional reset functionality. |
| 259 | */ |
| 260 | protected /*abstract*/ void onReset() {} |
| 261 | |
| 262 | /** |
| 263 | * Invoked by the engine when a grid controller button press occurs |
| 264 | * |
| 265 | * @param row Row index on the gird |
| 266 | * @param col Column index on the grid |
| 267 | * @return True if the event was consumed, false otherwise |
| 268 | */ |
| 269 | public boolean gridPressed(int row, int col) { |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Invoked by the engine when a grid controller button release occurs |
| 275 | * |
| 276 | * @param row Row index on the gird |
| 277 | * @param col Column index on the grid |
| 278 | * @return True if the event was consumed, false otherwise |
| 279 | */ |
| 280 | public boolean gridReleased(int row, int col) { |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Invoked by engine when this pattern is focused an a midi note is received. |
| 286 | * |
| 287 | * @param note |
| 288 | * @return True if the pattern has consumed this note, false if the top-level |
| 289 | * may handle it |
| 290 | */ |
| 291 | public boolean noteOn(rwmidi.Note note) { |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Invoked by engine when this pattern is focused an a midi note off is received. |
| 297 | * |
| 298 | * @param note |
| 299 | * @return True if the pattern has consumed this note, false if the top-level |
| 300 | * may handle it |
| 301 | */ |
| 302 | public boolean noteOff(rwmidi.Note note) { |
| 303 | return false; |
| 304 | } |
| 305 | |
| 306 | /** |
| 307 | * Invoked by engine when this pattern is focused an a controller is received |
| 308 | * |
| 309 | * @param note |
| 310 | * @return True if the pattern has consumed this controller, false if the top-level |
| 311 | * may handle it |
| 312 | */ |
| 313 | public boolean controllerChange(rwmidi.Controller controller) { |
| 314 | return false; |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | long simulationNanos = 0; |
| 319 | |
| 320 | /** |
| 321 | * Core render loop and drawing functionality. |
| 322 | */ |
| 323 | void draw() { |
| 324 | long drawStart = System.nanoTime(); |
| 325 | |
| 326 | // Set background |
| 327 | background(40); |
| 328 | |
| 329 | // Send colors |
| 330 | color[] sendColors = lx.getColors(); |
| 331 | long gammaStart = System.nanoTime(); |
| 332 | // Gamma correction here. Apply a cubic to the brightness |
| 333 | // for better representation of dynamic range |
| 334 | for (int i = 0; i < sendColors.length; ++i) { |
| 335 | lx.RGBtoHSB(sendColors[i], hsb); |
| 336 | float b = hsb[2]; |
| 337 | sendColors[i] = lx.hsb(360.*hsb[0], 100.*hsb[1], 100.*(b*b*b)); |
| 338 | } |
| 339 | long gammaNanos = System.nanoTime() - gammaStart; |
| 340 | |
| 341 | // Always draw FPS meter |
| 342 | drawFPS(); |
| 343 | |
| 344 | // TODO(mcslee): fix |
| 345 | long drawNanos = System.nanoTime() - drawStart; |
| 346 | long uiNanos = 0; |
| 347 | |
| 348 | if (diagnosticsOn) { |
| 349 | drawDiagnostics(drawNanos, simulationNanos, uiNanos, gammaNanos); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | void drawDiagnostics(long drawNanos, long simulationNanos, long uiNanos, long gammaNanos) { |
| 354 | float ws = 4 / 1000000.; |
| 355 | int thirtyfps = 1000000000 / 30; |
| 356 | int sixtyfps = 1000000000 / 60; |
| 357 | int x = width - 138; |
| 358 | int y = height - 14; |
| 359 | int h = 10; |
| 360 | noFill(); |
| 361 | stroke(#999999); |
| 362 | rect(x, y, thirtyfps * ws, h); |
| 363 | noStroke(); |
| 364 | int xp = x; |
| 365 | float hv = 0; |
| 366 | for (long val : new long[] {lx.timer.drawNanos, simulationNanos, uiNanos, gammaNanos, lx.engine.timer.outputNanos }) { |
| 367 | fill(lx.hsb(hv % 360, 100, 80)); |
| 368 | rect(xp, y, val * ws, h-1); |
| 369 | hv += 140; |
| 370 | xp += val * ws; |
| 371 | } |
| 372 | noFill(); |
| 373 | stroke(#333333); |
| 374 | line(x+sixtyfps*ws, y+1, x+sixtyfps*ws, y+h-1); |
| 375 | |
| 376 | y = y - 14; |
| 377 | xp = x; |
| 378 | float tw = thirtyfps * ws; |
| 379 | noFill(); |
| 380 | stroke(#999999); |
| 381 | rect(x, y, tw, h); |
| 382 | h = 5; |
| 383 | noStroke(); |
| 384 | for (long val : new long[] { |
| 385 | lx.engine.timer.deckNanos, |
| 386 | lx.engine.timer.copyNanos, |
| 387 | lx.engine.timer.fxNanos}) { |
| 388 | float amt = val / (float) lx.timer.drawNanos; |
| 389 | fill(lx.hsb(hv % 360, 100, 80)); |
| 390 | rect(xp, y, amt * tw, h-1); |
| 391 | hv += 140; |
| 392 | xp += amt * tw; |
| 393 | } |
| 394 | |
| 395 | xp = x; |
| 396 | y += h; |
| 397 | hv = 120; |
| 398 | for (long val : new long[] { |
| 399 | lx.engine.getDeck(0).timer.runNanos, |
| 400 | lx.engine.getDeck(1).timer.runNanos, |
| 401 | lx.engine.getDeck(1).getFaderTransition().timer.blendNanos}) { |
| 402 | float amt = val / (float) lx.timer.drawNanos; |
| 403 | fill(lx.hsb(hv % 360, 100, 80)); |
| 404 | rect(xp, y, amt * tw, h-1); |
| 405 | hv += 140; |
| 406 | xp += amt * tw; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | void drawFPS() { |
| 411 | // Always draw FPS meter |
| 412 | fill(#555555); |
| 413 | textSize(9); |
| 414 | textAlign(LEFT, BASELINE); |
| 415 | text("FPS: " + ((int) (frameRate*10)) / 10. + " / " + targetFramerate + " (-/+)", 4, height-4); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | /** |
| 420 | * Top-level keyboard event handling |
| 421 | */ |
| 422 | void keyPressed() { |
| 423 | if (mappingMode) { |
| 424 | mappingTool.keyPressed(uiMapping); |
| 425 | } |
| 426 | switch (key) { |
| 427 | case '1': |
| 428 | case '2': |
| 429 | case '3': |
| 430 | case '4': |
| 431 | case '5': |
| 432 | case '6': |
| 433 | case '7': |
| 434 | case '8': |
| 435 | if (!midiEngine.isQwertyEnabled()) { |
| 436 | presetManager.select(midiEngine.getFocusedDeck(), key - '1'); |
| 437 | } |
| 438 | break; |
| 439 | |
| 440 | case '!': |
| 441 | if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 0); |
| 442 | break; |
| 443 | case '@': |
| 444 | if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 1); |
| 445 | break; |
| 446 | case '#': |
| 447 | if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 2); |
| 448 | break; |
| 449 | case '$': |
| 450 | if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 3); |
| 451 | break; |
| 452 | case '%': |
| 453 | if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 4); |
| 454 | break; |
| 455 | case '^': |
| 456 | if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 5); |
| 457 | break; |
| 458 | case '&': |
| 459 | if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 6); |
| 460 | break; |
| 461 | case '*': |
| 462 | if (!midiEngine.isQwertyEnabled()) presetManager.store(midiEngine.getFocusedDeck(), 7); |
| 463 | break; |
| 464 | |
| 465 | case '-': |
| 466 | case '_': |
| 467 | frameRate(--targetFramerate); |
| 468 | break; |
| 469 | case '=': |
| 470 | case '+': |
| 471 | frameRate(++targetFramerate); |
| 472 | break; |
| 473 | case 'b': |
| 474 | effects.boom.trigger(); |
| 475 | break; |
| 476 | case 'd': |
| 477 | if (!midiEngine.isQwertyEnabled()) { |
| 478 | debugMode = !debugMode; |
| 479 | println("Debug output: " + (debugMode ? "ON" : "OFF")); |
| 480 | } |
| 481 | break; |
| 482 | case 'm': |
| 483 | if (!midiEngine.isQwertyEnabled()) { |
| 484 | mappingMode = !mappingMode; |
| 485 | uiPatternA.setVisible(!mappingMode); |
| 486 | uiMapping.setVisible(mappingMode); |
| 487 | if (mappingMode) { |
| 488 | restoreToPattern = lx.getPattern(); |
| 489 | lx.setPatterns(new LXPattern[] { mappingTool }); |
| 490 | } else { |
| 491 | lx.setPatterns(patterns); |
| 492 | LXTransition pop = restoreToPattern.getTransition(); |
| 493 | restoreToPattern.setTransition(null); |
| 494 | lx.goPattern(restoreToPattern); |
| 495 | restoreToPattern.setTransition(pop); |
| 496 | } |
| 497 | } |
| 498 | break; |
| 499 | case 't': |
| 500 | if (!midiEngine.isQwertyEnabled()) { |
| 501 | lx.engine.setThreaded(!lx.engine.isThreaded()); |
| 502 | } |
| 503 | break; |
| 504 | case 'o': |
| 505 | case 'p': |
| 506 | for (LXOutput output : outputs) { |
| 507 | output.enabled.toggle(); |
| 508 | } |
| 509 | break; |
| 510 | case 'q': |
| 511 | if (!midiEngine.isQwertyEnabled()) { |
| 512 | diagnosticsOn = !diagnosticsOn; |
| 513 | } |
| 514 | break; |
| 515 | case 's': |
| 516 | if (!midiEngine.isQwertyEnabled()) { |
| 517 | simulationOn = !simulationOn; |
| 518 | } |
| 519 | break; |
| 520 | case 'S': |
| 521 | if (!midiEngine.isQwertyEnabled()) { |
| 522 | structureOn = !structureOn; |
| 523 | } |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | |