| 1 | /** |
| 2 | * DOUBLE BLACK DIAMOND DOUBLE BLACK DIAMOND |
| 3 | * |
| 4 | * //\\ //\\ //\\ //\\ |
| 5 | * ///\\\ ///\\\ ///\\\ ///\\\ |
| 6 | * \\\/// \\\/// \\\/// \\\/// |
| 7 | * \\// \\// \\// \\// |
| 8 | * |
| 9 | * EXPERTS ONLY!! EXPERTS ONLY!! |
| 10 | * |
| 11 | * This file defines the MIDI mapping interfaces. This shouldn't |
| 12 | * need editing unless you're adding top level support for a |
| 13 | * specific MIDI device of some sort. Generally, all MIDI devices |
| 14 | * will just work with the default configuration, and you can |
| 15 | * set your SCPattern class to respond to the controllers that you |
| 16 | * care about. |
| 17 | */ |
| 18 | |
| 19 | interface MidiEngineListener { |
| 20 | public void onFocusedDeck(int deckIndex); |
| 21 | } |
| 22 | |
| 23 | class MidiEngine { |
| 24 | |
| 25 | public final GridController grid; |
| 26 | private final List<MidiEngineListener> listeners = new ArrayList<MidiEngineListener>(); |
| 27 | private final List<SCMidiInput> midiControllers = new ArrayList<SCMidiInput>(); |
| 28 | |
| 29 | public MidiEngine addListener(MidiEngineListener l) { |
| 30 | listeners.add(l); |
| 31 | return this; |
| 32 | } |
| 33 | |
| 34 | public MidiEngine removeListener(MidiEngineListener l) { |
| 35 | listeners.remove(l); |
| 36 | return this; |
| 37 | } |
| 38 | |
| 39 | private SCMidiInput midiQwertyKeys; |
| 40 | private SCMidiInput midiQwertyAPC; |
| 41 | |
| 42 | private int activeDeckIndex = 0; |
| 43 | |
| 44 | public MidiEngine() { |
| 45 | grid = new GridController(this); |
| 46 | midiControllers.add(midiQwertyKeys = new SCMidiInput(this, SCMidiInput.KEYS)); |
| 47 | midiControllers.add(midiQwertyAPC = new SCMidiInput(this, SCMidiInput.APC)); |
| 48 | for (MidiInputDevice device : RWMidi.getInputDevices()) { |
| 49 | if (device.getName().contains("APC")) { |
| 50 | midiControllers.add(new APC40MidiInput(this, device).setEnabled(true)); |
| 51 | } else if (device.getName().contains("SLIDER/KNOB KORG")) { |
| 52 | midiControllers.add(new KorgNanoKontrolMidiInput(this, device).setEnabled(true)); |
| 53 | } else { |
| 54 | boolean enabled = device.getName().contains("KEYBOARD KORG") || device.getName().contains("Bus 1 Apple"); |
| 55 | midiControllers.add(new SCMidiInput(this, device).setEnabled(enabled)); |
| 56 | } |
| 57 | } |
| 58 | for (MidiOutputDevice device : RWMidi.getOutputDevices()) { |
| 59 | if (device.getName().contains("APC")) { |
| 60 | new APC40MidiOutput(this, device); |
| 61 | } |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public List<SCMidiInput> getControllers() { |
| 66 | return this.midiControllers; |
| 67 | } |
| 68 | |
| 69 | public Engine.Deck getFocusedDeck() { |
| 70 | return lx.engine.getDeck(activeDeckIndex); |
| 71 | } |
| 72 | |
| 73 | public SCPattern getFocusedPattern() { |
| 74 | return (SCPattern) getFocusedDeck().getActivePattern(); |
| 75 | } |
| 76 | |
| 77 | public MidiEngine setFocusedDeck(int deckIndex) { |
| 78 | if (this.activeDeckIndex != deckIndex) { |
| 79 | this.activeDeckIndex = deckIndex; |
| 80 | for (MidiEngineListener listener : listeners) { |
| 81 | listener.onFocusedDeck(deckIndex); |
| 82 | } |
| 83 | } |
| 84 | return this; |
| 85 | } |
| 86 | |
| 87 | public boolean isQwertyEnabled() { |
| 88 | return midiQwertyKeys.isEnabled() || midiQwertyAPC.isEnabled(); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | public interface SCMidiInputListener { |
| 93 | public void onEnabled(SCMidiInput controller, boolean enabled); |
| 94 | } |
| 95 | |
| 96 | public class SCMidiInput extends AbstractScrollItem { |
| 97 | |
| 98 | public static final int MIDI = 0; |
| 99 | public static final int KEYS = 1; |
| 100 | public static final int APC = 2; |
| 101 | |
| 102 | private boolean enabled = false; |
| 103 | private final String name; |
| 104 | private final int mode; |
| 105 | private int octaveShift = 0; |
| 106 | |
| 107 | protected final MidiEngine midiEngine; |
| 108 | |
| 109 | class NoteMeta { |
| 110 | int channel; |
| 111 | int number; |
| 112 | NoteMeta(int channel, int number) { |
| 113 | this.channel = channel; |
| 114 | this.number = number; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | final Map<Character, NoteMeta> keyToNote = new HashMap<Character, NoteMeta>(); |
| 119 | |
| 120 | final List<SCMidiInputListener> listeners = new ArrayList<SCMidiInputListener>(); |
| 121 | |
| 122 | public SCMidiInput addListener(SCMidiInputListener l) { |
| 123 | listeners.add(l); |
| 124 | return this; |
| 125 | } |
| 126 | |
| 127 | public SCMidiInput removeListener(SCMidiInputListener l) { |
| 128 | listeners.remove(l); |
| 129 | return this; |
| 130 | } |
| 131 | |
| 132 | SCMidiInput(MidiEngine midiEngine, MidiInputDevice d) { |
| 133 | this.midiEngine = midiEngine; |
| 134 | mode = MIDI; |
| 135 | d.createInput(this); |
| 136 | name = d.getName().replace("Unknown vendor",""); |
| 137 | } |
| 138 | |
| 139 | SCMidiInput(MidiEngine midiEngine, int mode) { |
| 140 | this.midiEngine = midiEngine; |
| 141 | this.mode = mode; |
| 142 | switch (mode) { |
| 143 | case APC: |
| 144 | name = "QWERTY (APC Mode)"; |
| 145 | mapAPC(); |
| 146 | break; |
| 147 | default: |
| 148 | case KEYS: |
| 149 | name = "QWERTY (Key Mode)"; |
| 150 | mapKeys(); |
| 151 | break; |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | private void mapAPC() { |
| 156 | mapNote('1', 0, 53); |
| 157 | mapNote('2', 1, 53); |
| 158 | mapNote('3', 2, 53); |
| 159 | mapNote('4', 3, 53); |
| 160 | mapNote('5', 4, 53); |
| 161 | mapNote('6', 5, 53); |
| 162 | mapNote('q', 0, 54); |
| 163 | mapNote('w', 1, 54); |
| 164 | mapNote('e', 2, 54); |
| 165 | mapNote('r', 3, 54); |
| 166 | mapNote('t', 4, 54); |
| 167 | mapNote('y', 5, 54); |
| 168 | mapNote('a', 0, 55); |
| 169 | mapNote('s', 1, 55); |
| 170 | mapNote('d', 2, 55); |
| 171 | mapNote('f', 3, 55); |
| 172 | mapNote('g', 4, 55); |
| 173 | mapNote('h', 5, 55); |
| 174 | mapNote('z', 0, 56); |
| 175 | mapNote('x', 1, 56); |
| 176 | mapNote('c', 2, 56); |
| 177 | mapNote('v', 3, 56); |
| 178 | mapNote('b', 4, 56); |
| 179 | mapNote('n', 5, 56); |
| 180 | registerKeyEvent(this); |
| 181 | } |
| 182 | |
| 183 | private void mapKeys() { |
| 184 | int note = 48; |
| 185 | mapNote('a', 1, note++); |
| 186 | mapNote('w', 1, note++); |
| 187 | mapNote('s', 1, note++); |
| 188 | mapNote('e', 1, note++); |
| 189 | mapNote('d', 1, note++); |
| 190 | mapNote('f', 1, note++); |
| 191 | mapNote('t', 1, note++); |
| 192 | mapNote('g', 1, note++); |
| 193 | mapNote('y', 1, note++); |
| 194 | mapNote('h', 1, note++); |
| 195 | mapNote('u', 1, note++); |
| 196 | mapNote('j', 1, note++); |
| 197 | mapNote('k', 1, note++); |
| 198 | mapNote('o', 1, note++); |
| 199 | mapNote('l', 1, note++); |
| 200 | registerKeyEvent(this); |
| 201 | } |
| 202 | |
| 203 | void mapNote(char ch, int channel, int number) { |
| 204 | keyToNote.put(ch, new NoteMeta(channel, number)); |
| 205 | } |
| 206 | |
| 207 | public String getLabel() { |
| 208 | return name; |
| 209 | } |
| 210 | |
| 211 | public void keyEvent(KeyEvent e) { |
| 212 | if (!enabled) { |
| 213 | return; |
| 214 | } |
| 215 | char c = Character.toLowerCase(e.getKeyChar()); |
| 216 | NoteMeta nm = keyToNote.get(c); |
| 217 | if (nm != null) { |
| 218 | switch (e.getID()) { |
| 219 | case KeyEvent.KEY_PRESSED: |
| 220 | noteOnReceived(new Note(Note.NOTE_ON, nm.channel, nm.number + octaveShift*12, 127)); |
| 221 | break; |
| 222 | case KeyEvent.KEY_RELEASED: |
| 223 | noteOffReceived(new Note(Note.NOTE_OFF, nm.channel, nm.number + octaveShift*12, 0)); |
| 224 | break; |
| 225 | } |
| 226 | } |
| 227 | if ((mode == KEYS) && (e.getID() == KeyEvent.KEY_PRESSED)) { |
| 228 | switch (c) { |
| 229 | case 'z': |
| 230 | octaveShift = constrain(octaveShift-1, -4, 4); |
| 231 | break; |
| 232 | case 'x': |
| 233 | octaveShift = constrain(octaveShift+1, -4, 4); |
| 234 | break; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | public boolean isEnabled() { |
| 240 | return enabled; |
| 241 | } |
| 242 | |
| 243 | public boolean isSelected() { |
| 244 | return enabled; |
| 245 | } |
| 246 | |
| 247 | public void onMousePressed() { |
| 248 | setEnabled(!enabled); |
| 249 | } |
| 250 | |
| 251 | public SCMidiInput setEnabled(boolean enabled) { |
| 252 | if (enabled != this.enabled) { |
| 253 | this.enabled = enabled; |
| 254 | for (SCMidiInputListener l : listeners) { |
| 255 | l.onEnabled(this, enabled); |
| 256 | } |
| 257 | } |
| 258 | return this; |
| 259 | } |
| 260 | |
| 261 | private boolean logMidi() { |
| 262 | return (uiMidi != null) && uiMidi.logMidi(); |
| 263 | } |
| 264 | |
| 265 | final void programChangeReceived(ProgramChange pc) { |
| 266 | if (!enabled) { |
| 267 | return; |
| 268 | } |
| 269 | if (logMidi()) { |
| 270 | println(getLabel() + " :: Program Change :: " + pc.getNumber()); |
| 271 | } |
| 272 | handleProgramChange(pc); |
| 273 | } |
| 274 | |
| 275 | final void controllerChangeReceived(rwmidi.Controller cc) { |
| 276 | if (!enabled) { |
| 277 | return; |
| 278 | } |
| 279 | if (logMidi()) { |
| 280 | println(getLabel() + " :: Controller :: " + cc.getChannel() + " :: " + cc.getCC() + ":" + cc.getValue()); |
| 281 | } |
| 282 | if (!handleGridControllerChange(cc)) { |
| 283 | if (!midiEngine.getFocusedPattern().controllerChange(cc)) { |
| 284 | handleControllerChange(cc); |
| 285 | } |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | final void noteOnReceived(Note note) { |
| 290 | if (!enabled) { |
| 291 | return; |
| 292 | } |
| 293 | if (logMidi()) { |
| 294 | println(getLabel() + " :: Note On :: " + note.getChannel() + ":" + note.getPitch() + ":" + note.getVelocity()); |
| 295 | } |
| 296 | if (!handleGridNoteOn(note)) { |
| 297 | if (!midiEngine.getFocusedPattern().noteOn(note)) { |
| 298 | handleNoteOn(note); |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | final void noteOffReceived(Note note) { |
| 304 | if (!enabled) { |
| 305 | return; |
| 306 | } |
| 307 | if (logMidi()) { |
| 308 | println(getLabel() + " :: Note Off :: " + note.getChannel() + ":" + note.getPitch() + ":" + note.getVelocity()); |
| 309 | } |
| 310 | if (!handleGridNoteOff(note)) { |
| 311 | if (!midiEngine.getFocusedPattern().noteOff(note)) { |
| 312 | handleNoteOff(note); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | // Subclasses may implement these to map top-level functionality |
| 318 | protected boolean handleGridNoteOn(Note note) { return false; } |
| 319 | protected boolean handleGridNoteOff(Note note) { return false; } |
| 320 | protected boolean handleGridControllerChange(rwmidi.Controller cc) { return false; } |
| 321 | protected void handleProgramChange(ProgramChange pc) {} |
| 322 | protected void handleControllerChange(rwmidi.Controller cc) {} |
| 323 | protected void handleNoteOn(Note note) {} |
| 324 | protected void handleNoteOff(Note note) {} |
| 325 | } |
| 326 | |
| 327 | public class APC40MidiInput extends SCMidiInput { |
| 328 | |
| 329 | private boolean shiftOn = false; |
| 330 | private LXEffect releaseEffect = null; |
| 331 | |
| 332 | APC40MidiInput(MidiEngine midiEngine, MidiInputDevice d) { |
| 333 | super(midiEngine, d); |
| 334 | } |
| 335 | |
| 336 | private class GridPosition { |
| 337 | public final int row, col; |
| 338 | GridPosition(int r, int c) { |
| 339 | row = r; |
| 340 | col = c; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | private GridPosition getGridPosition(Note note) { |
| 345 | int channel = note.getChannel(); |
| 346 | int pitch = note.getPitch(); |
| 347 | if (channel < 8) { |
| 348 | if (pitch >= 53 && pitch <=57) return new GridPosition(pitch-53, channel); |
| 349 | else if (pitch == 52) return new GridPosition(5, channel); |
| 350 | } |
| 351 | return null; |
| 352 | } |
| 353 | |
| 354 | protected boolean handleGridNoteOn(Note note) { |
| 355 | GridPosition p = getGridPosition(note); |
| 356 | if (p != null) { |
| 357 | return midiEngine.grid.gridPressed(p.row, p.col); |
| 358 | } |
| 359 | return false; |
| 360 | } |
| 361 | |
| 362 | protected boolean handleGridNoteOff(Note note) { |
| 363 | GridPosition p = getGridPosition(note); |
| 364 | if (p != null) { |
| 365 | return midiEngine.grid.gridReleased(p.row, p.col); |
| 366 | } |
| 367 | return false; |
| 368 | } |
| 369 | |
| 370 | protected void handleControllerChange(rwmidi.Controller cc) { |
| 371 | int number = cc.getCC(); |
| 372 | switch (number) { |
| 373 | // Crossfader |
| 374 | case 15: |
| 375 | lx.engine.getDeck(1).getCrossfader().setValue(cc.getValue() / 127.); |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | int parameterIndex = -1; |
| 380 | if (number >= 48 && number <= 55) { |
| 381 | parameterIndex = number - 48; |
| 382 | } else if (number >= 16 && number <= 19) { |
| 383 | parameterIndex = 8 + (number-16); |
| 384 | } |
| 385 | if (parameterIndex >= 0) { |
| 386 | List<LXParameter> parameters = midiEngine.getFocusedPattern().getParameters(); |
| 387 | if (parameterIndex < parameters.size()) { |
| 388 | parameters.get(parameterIndex).setValue(cc.getValue() / 127.); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | if (number >= 20 && number <= 23) { |
| 393 | int effectIndex = number - 20; |
| 394 | List<LXParameter> parameters = glucose.getSelectedEffect().getParameters(); |
| 395 | if (effectIndex < parameters.size()) { |
| 396 | parameters.get(effectIndex).setValue(cc.getValue() / 127.); |
| 397 | } |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | private long tap1 = 0; |
| 402 | |
| 403 | private boolean lbtwn(long a, long b, long c) { |
| 404 | return a >= b && a <= c; |
| 405 | } |
| 406 | |
| 407 | protected void handleNoteOn(Note note) { |
| 408 | int nPitch = note.getPitch(), nChan = note.getChannel(); |
| 409 | switch (nPitch) { |
| 410 | |
| 411 | case 82: // scene 1 |
| 412 | EFF_boom.trigger(); |
| 413 | break; |
| 414 | |
| 415 | case 83: // scene 2 |
| 416 | EFF_flash.trigger(); |
| 417 | break; |
| 418 | |
| 419 | case 90: |
| 420 | // dan's dirty tapping mechanism |
| 421 | lx.tempo.trigger(); |
| 422 | tap1 = millis(); |
| 423 | break; |
| 424 | |
| 425 | case 91: // play |
| 426 | case 97: // left bank |
| 427 | midiEngine.setFocusedDeck(0); |
| 428 | break; |
| 429 | |
| 430 | case 93: // rec |
| 431 | case 96: // right bank |
| 432 | midiEngine.setFocusedDeck(1); |
| 433 | break; |
| 434 | |
| 435 | case 94: // up bank |
| 436 | if (shiftOn) { |
| 437 | glucose.incrementSelectedEffectBy(-1); |
| 438 | } else { |
| 439 | midiEngine.getFocusedDeck().goPrev(); |
| 440 | } |
| 441 | break; |
| 442 | case 95: // down bank |
| 443 | if (shiftOn) { |
| 444 | glucose.incrementSelectedEffectBy(1); |
| 445 | } else { |
| 446 | midiEngine.getFocusedDeck().goNext(); |
| 447 | } |
| 448 | break; |
| 449 | |
| 450 | case 98: // shift |
| 451 | shiftOn = true; |
| 452 | break; |
| 453 | |
| 454 | case 99: // tap tempo |
| 455 | lx.tempo.tap(); |
| 456 | break; |
| 457 | case 100: // nudge+ |
| 458 | lx.tempo.setBpm(lx.tempo.bpm() + (shiftOn ? 1 : .1)); |
| 459 | break; |
| 460 | case 101: // nudge- |
| 461 | lx.tempo.setBpm(lx.tempo.bpm() - (shiftOn ? 1 : .1)); |
| 462 | break; |
| 463 | |
| 464 | case 62: // Detail View / red 5 |
| 465 | releaseEffect = glucose.getSelectedEffect(); |
| 466 | if (releaseEffect.isMomentary()) { |
| 467 | releaseEffect.enable(); |
| 468 | } else { |
| 469 | releaseEffect.toggle(); |
| 470 | } |
| 471 | break; |
| 472 | |
| 473 | case 63: // rec quantize / red 6 |
| 474 | glucose.getSelectedEffect().disable(); |
| 475 | break; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | protected void handleNoteOff(Note note) { |
| 480 | int nPitch = note.getPitch(), nChan = note.getChannel(); |
| 481 | switch (nPitch) { |
| 482 | case 90: // SEND C |
| 483 | long tapDelta = millis() - tap1; |
| 484 | if (lbtwn(tapDelta,5000,300*1000)) { // hackish tapping mechanism |
| 485 | double bpm = 32.*60000./(tapDelta); |
| 486 | while (bpm < 20) bpm*=2; |
| 487 | while (bpm > 40) bpm/=2; |
| 488 | lx.tempo.setBpm(bpm); |
| 489 | lx.tempo.trigger(); |
| 490 | tap1 = 0; |
| 491 | println("Tap Set - " + bpm + " bpm"); |
| 492 | } |
| 493 | break; |
| 494 | |
| 495 | case 63: // rec quantize / RED 6 |
| 496 | if (releaseEffect != null) { |
| 497 | if (releaseEffect.isMomentary()) { |
| 498 | releaseEffect.disable(); |
| 499 | } |
| 500 | } |
| 501 | break; |
| 502 | |
| 503 | case 98: // shift |
| 504 | shiftOn = false; |
| 505 | break; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | class KorgNanoKontrolMidiInput extends SCMidiInput { |
| 511 | |
| 512 | KorgNanoKontrolMidiInput(MidiEngine midiEngine, MidiInputDevice d) { |
| 513 | super(midiEngine, d); |
| 514 | } |
| 515 | |
| 516 | protected void handleControllerChange(rwmidi.Controller cc) { |
| 517 | int number = cc.getCC(); |
| 518 | if (number >= 16 && number <= 23) { |
| 519 | int parameterIndex = number - 16; |
| 520 | List<LXParameter> parameters = midiEngine.getFocusedPattern().getParameters(); |
| 521 | if (parameterIndex < parameters.size()) { |
| 522 | parameters.get(parameterIndex).setValue(cc.getValue() / 127.); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (cc.getValue() == 127) { |
| 527 | switch (number) { |
| 528 | // Left track |
| 529 | case 58: |
| 530 | midiEngine.setFocusedDeck(0); |
| 531 | break; |
| 532 | // Right track |
| 533 | case 59: |
| 534 | midiEngine.setFocusedDeck(1); |
| 535 | break; |
| 536 | // Left chevron |
| 537 | case 43: |
| 538 | midiEngine.getFocusedDeck().goPrev(); |
| 539 | break; |
| 540 | // Right chevron |
| 541 | case 44: |
| 542 | midiEngine.getFocusedDeck().goNext(); |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | } |
| 548 | |
| 549 | class APC40MidiOutput implements LXParameter.Listener, GridOutput { |
| 550 | |
| 551 | private final MidiEngine midiEngine; |
| 552 | private final MidiOutput output; |
| 553 | private LXPattern focusedPattern = null; |
| 554 | private LXEffect focusedEffect = null; |
| 555 | |
| 556 | APC40MidiOutput(MidiEngine midiEngine, MidiOutputDevice device) { |
| 557 | this.midiEngine = midiEngine; |
| 558 | output = device.createOutput(); |
| 559 | midiEngine.addListener(new MidiEngineListener() { |
| 560 | public void onFocusedDeck(int deckIndex) { |
| 561 | resetPatternParameters(); |
| 562 | } |
| 563 | }); |
| 564 | glucose.addEffectListener(new GLucose.EffectListener() { |
| 565 | public void effectSelected(LXEffect effect) { |
| 566 | resetEffectParameters(); |
| 567 | } |
| 568 | }); |
| 569 | Engine.Listener deckListener = new Engine.AbstractListener() { |
| 570 | public void patternDidChange(Engine.Deck deck, LXPattern pattern) { |
| 571 | resetPatternParameters(); |
| 572 | } |
| 573 | }; |
| 574 | for (Engine.Deck d : lx.engine.getDecks()) { |
| 575 | d.addListener(deckListener); |
| 576 | } |
| 577 | resetParameters(); |
| 578 | midiEngine.grid.addOutput(this); |
| 579 | } |
| 580 | |
| 581 | private void resetParameters() { |
| 582 | resetPatternParameters(); |
| 583 | resetEffectParameters(); |
| 584 | } |
| 585 | |
| 586 | private void resetPatternParameters() { |
| 587 | LXPattern newPattern = midiEngine.getFocusedDeck().getActivePattern(); |
| 588 | if (newPattern == focusedPattern) { |
| 589 | return; |
| 590 | } |
| 591 | if (focusedPattern != null) { |
| 592 | for (LXParameter p : focusedPattern.getParameters()) { |
| 593 | ((LXListenableParameter) p).removeListener(this); |
| 594 | } |
| 595 | } |
| 596 | focusedPattern = newPattern; |
| 597 | int i = 0; |
| 598 | for (LXParameter p : focusedPattern.getParameters()) { |
| 599 | ((LXListenableParameter) p).addListener(this); |
| 600 | sendKnob(i++, p); |
| 601 | } |
| 602 | while (i < 12) { |
| 603 | sendKnob(i++, 0); |
| 604 | } |
| 605 | for (int row = 0; row < 7; ++row) { |
| 606 | for (int col = 0; col < 8; ++col) { |
| 607 | setGridState(row, col, 0); |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | private void resetEffectParameters() { |
| 613 | LXEffect newEffect = glucose.getSelectedEffect(); |
| 614 | if (newEffect == focusedEffect) { |
| 615 | return; |
| 616 | } |
| 617 | if (focusedEffect != null) { |
| 618 | for (LXParameter p : focusedPattern.getParameters()) { |
| 619 | ((LXListenableParameter) p).removeListener(this); |
| 620 | } |
| 621 | } |
| 622 | focusedEffect = newEffect; |
| 623 | int i = 0; |
| 624 | for (LXParameter p : focusedEffect.getParameters()) { |
| 625 | ((LXListenableParameter) p).addListener(this); |
| 626 | sendKnob(12 + i++, p); |
| 627 | } |
| 628 | while (i < 4) { |
| 629 | sendKnob(12 + i++, 0); |
| 630 | } |
| 631 | } |
| 632 | |
| 633 | private void sendKnob(int i, LXParameter p) { |
| 634 | sendKnob(i, (int) (p.getValuef() * 127.)); |
| 635 | } |
| 636 | |
| 637 | private void sendKnob(int i, int value) { |
| 638 | if (i < 8) { |
| 639 | output.sendController(0, 48+i, value); |
| 640 | } else if (i < 16) { |
| 641 | output.sendController(0, 8+i, value); |
| 642 | } |
| 643 | } |
| 644 | |
| 645 | public void onParameterChanged(LXParameter parameter) { |
| 646 | int i = 0; |
| 647 | for (LXParameter p : focusedPattern.getParameters()) { |
| 648 | if (p == parameter) { |
| 649 | sendKnob(i, p); |
| 650 | break; |
| 651 | } |
| 652 | ++i; |
| 653 | } |
| 654 | i = 12; |
| 655 | for (LXParameter p : focusedEffect.getParameters()) { |
| 656 | if (p == parameter) { |
| 657 | sendKnob(i, p); |
| 658 | break; |
| 659 | } |
| 660 | ++i; |
| 661 | } |
| 662 | } |
| 663 | |
| 664 | public void setGridState(int row, int col, int state) { |
| 665 | if (col < 8) { |
| 666 | if (row < 5) output.sendNoteOn(col, 53+row, state); |
| 667 | else if (row == 6) output.sendNoteOn(col, 52, state); |
| 668 | } |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | interface GridOutput { |
| 673 | public static final int OFF = 0; |
| 674 | public static final int GREEN = 1; |
| 675 | public static final int GREEN_BLINK = 2; |
| 676 | public static final int RED = 3; |
| 677 | public static final int RED_BLINK = 4; |
| 678 | public static final int YELLOW = 5; |
| 679 | public static final int YELLOW_BLINK = 6; |
| 680 | public static final int ON = 127; |
| 681 | |
| 682 | public void setGridState(int row, int col, int state); |
| 683 | } |
| 684 | |
| 685 | class GridController { |
| 686 | private final List<GridOutput> outputs = new ArrayList<GridOutput>(); |
| 687 | |
| 688 | private final MidiEngine midiEngine; |
| 689 | |
| 690 | GridController(MidiEngine midiEngine) { |
| 691 | this.midiEngine = midiEngine; |
| 692 | } |
| 693 | |
| 694 | public void addOutput(GridOutput output) { |
| 695 | outputs.add(output); |
| 696 | } |
| 697 | |
| 698 | public boolean gridPressed(int row, int col) { |
| 699 | return midiEngine.getFocusedPattern().gridPressed(row, col); |
| 700 | } |
| 701 | |
| 702 | public boolean gridReleased(int row, int col) { |
| 703 | return midiEngine.getFocusedPattern().gridReleased(row, col); |
| 704 | } |
| 705 | |
| 706 | public void setState(int row, int col, int state) { |
| 707 | for (GridOutput g : outputs) { |
| 708 | g.setGridState(row, col, state); |
| 709 | } |
| 710 | } |
| 711 | } |
| 712 | |