From: Mark Slee Date: Sat, 19 Oct 2013 19:54:11 +0000 (-0700) Subject: Basic preset stuff and onTransitionEnd message X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=e0794d3a20f8c2c301f6c7541925f3d403277fab;p=SugarCubes.git Basic preset stuff and onTransitionEnd message --- diff --git a/_Internals.pde b/_Internals.pde index f1ebdc0..9d6b118 100644 --- a/_Internals.pde +++ b/_Internals.pde @@ -52,6 +52,7 @@ LXPattern[] patterns; MappingTool mappingTool; PandaDriver[] pandaBoards; MidiEngine midiEngine; +PresetManager presetManager; // Display configuration mode boolean mappingMode = false; @@ -136,6 +137,10 @@ void setup() { // MIDI devices midiEngine = new MidiEngine(); logTime("Setup MIDI devices"); + + // Preset manager + presetManager = new PresetManager(); + logTime("Loaded presets"); // Build output driver PandaMapping[] pandaMappings = buildPandaList(); diff --git a/_MIDI.pde b/_MIDI.pde index 9a27f8c..5f25dda 100644 --- a/_MIDI.pde +++ b/_MIDI.pde @@ -348,7 +348,6 @@ public class APC40MidiInput extends GenericDeviceMidiInput { int pitch = note.getPitch(); if (channel < 8) { if (pitch >= 53 && pitch <=57) return new GridPosition(pitch-53, channel); - else if (pitch == 52) return new GridPosition(5, channel); } return null; } @@ -453,6 +452,16 @@ public class APC40MidiInput extends GenericDeviceMidiInput { } break; + case 52: // CLIP STOP + if (nChan < PresetManager.NUM_PRESETS) { + if (shiftOn) { + presetManager.store(nChan); + } else { + presetManager.select(nChan); + } + } + break; + case 82: // scene 1 EFF_boom.trigger(); break; @@ -733,9 +742,8 @@ class APC40MidiOutput implements LXParameter.Listener, GridOutput { } public void setGridState(int row, int col, int state) { - if (col < 8) { - if (row < 5) output.sendNoteOn(col, 53+row, state); - else if (row == 6) output.sendNoteOn(col, 52, state); + if (col < 8 && row < 5) { + output.sendNoteOn(col, 53+row, state); } } } diff --git a/_Presets.pde b/_Presets.pde new file mode 100644 index 0000000..ada8bb1 --- /dev/null +++ b/_Presets.pde @@ -0,0 +1,133 @@ +interface PresetListener { + public void onPresetLoaded(Preset preset); + public void onPresetDirty(Preset preset); + public void onPresetStored(Preset preset); +} + +class PresetManager { + + public static final int NUM_PRESETS = 8; + public static final String FILENAME = "data/presets.txt"; + public static final String DELIMITER = "\t"; + + private final Preset[] presets = new Preset[NUM_PRESETS]; + private final List listeners = new ArrayList(); + + private LXPattern loadedPreset = null; + + PresetManager() { + for (int i = 0; i < presets.length; ++i) { + presets[i] = new Preset(this, i); + } + String[] values = loadStrings(FILENAME); + if (values == null) { + write(); + } else { + int i = 0; + for (String serialized : values) { + presets[i++].load(serialized); + if (i >= NUM_PRESETS) { + break; + } + } + } + } + + public void addListener(PresetListener listener) { + listeners.add(listener); + } + + public void select(int index) { + presets[index].select(); + } + + public void store(int index) { + presets[index].store(midiEngine.getFocusedPattern()); + } + + public void onPresetLoaded(Preset preset, LXPattern pattern) { + for (PresetListener listener : listeners) { + listener.onPresetLoaded(preset); + } + } + + public void write() { + String[] lines = new String[NUM_PRESETS]; + int i = 0; + for (Preset preset : presets) { + lines[i++] = preset.serialize(); + } + saveStrings(FILENAME, lines); + } +} + +class Preset { + + final PresetManager manager; + final int index; + + String className; + final Map parameters = new HashMap(); + + Preset(PresetManager manager, int index) { + this.manager = manager; + this.index = index; + } + + public void load(String serialized) { + className = null; + parameters.clear(); + try { + String[] parts = serialized.split(PresetManager.DELIMITER); + className = parts[0]; + int i = 1; + while (i < parts.length - 1) { + parameters.put(parts[i], Float.parseFloat(parts[i+1])); + i += 2; + } + } catch (Exception x) { + className = null; + parameters.clear(); + } + } + + public String serialize() { + if (className == null) { + return "null"; + } + String val = className + PresetManager.DELIMITER; + for (String pKey : parameters.keySet()) { + val += pKey + PresetManager.DELIMITER + parameters.get(pKey) + PresetManager.DELIMITER; + } + return val; + } + + public void store(LXPattern pattern) { + className = null; + parameters.clear(); + className = pattern.getClass().getName(); + for (LXParameter p : pattern.getParameters()) { + parameters.put(p.getLabel(), p.getValuef()); + } + manager.write(); + } + + public void select() { + Engine.Deck deck = midiEngine.getFocusedDeck(); + for (LXPattern pattern : deck.getPatterns()) { + if (pattern.getClass().getName().equals(className)) { + for (String pLabel : parameters.keySet()) { + for (LXParameter p : pattern.getParameters()) { + if (p.getLabel().equals(pLabel)) { + p.setValue(parameters.get(pLabel)); + } + } + } + deck.goPattern(pattern); + manager.onPresetLoaded(this, pattern); + break; + } + } + } +} + diff --git a/code/HeronLX.jar b/code/HeronLX.jar index d5352fc..3a64906 100755 Binary files a/code/HeronLX.jar and b/code/HeronLX.jar differ