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