From d3b4ef4eeda501cd5e5e954bdd8393504362ec44 Mon Sep 17 00:00:00 2001 From: Len Ovens Date: Mon, 20 Jul 2015 23:15:53 -0700 Subject: Add enc to midi map for mcp style encoders. --- .../generic_midi/generic_midi_control_protocol.cc | 5 ++ libs/surfaces/generic_midi/midicontrollable.cc | 64 +++++++++++++--------- libs/surfaces/generic_midi/midicontrollable.h | 4 ++ 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/libs/surfaces/generic_midi/generic_midi_control_protocol.cc b/libs/surfaces/generic_midi/generic_midi_control_protocol.cc index 8d1952d5f2..e171cfbcc3 100644 --- a/libs/surfaces/generic_midi/generic_midi_control_protocol.cc +++ b/libs/surfaces/generic_midi/generic_midi_control_protocol.cc @@ -772,6 +772,7 @@ GenericMidiControlProtocol::create_binding (const XMLNode& node) MIDI::eventType ev; int intval; bool momentary; + bool encoder = false; if ((prop = node.property (X_("ctl"))) != 0) { ev = MIDI::controller; @@ -781,6 +782,9 @@ GenericMidiControlProtocol::create_binding (const XMLNode& node) ev = MIDI::program; } else if ((prop = node.property (X_("pb"))) != 0) { ev = MIDI::pitchbend; + } else if ((prop = node.property (X_("enc"))) != 0) { + encoder = true; + ev = MIDI::controller; } else { return 0; } @@ -820,6 +824,7 @@ GenericMidiControlProtocol::create_binding (const XMLNode& node) return 0; } + mc->set_encoder (encoder); mc->bind_midi (channel, ev, detail); return mc; diff --git a/libs/surfaces/generic_midi/midicontrollable.cc b/libs/surfaces/generic_midi/midicontrollable.cc index a5c2e9c017..9ac8c794e8 100644 --- a/libs/surfaces/generic_midi/midicontrollable.cc +++ b/libs/surfaces/generic_midi/midicontrollable.cc @@ -54,6 +54,7 @@ MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, MIDI::Parser& , _momentary (m) { _learned = false; /* from URI */ + _encoder = false; setting = false; last_value = 0; // got a better idea ? last_controllable_value = 0.0f; @@ -72,6 +73,7 @@ MIDIControllable::MIDIControllable (GenericMidiControlProtocol* s, MIDI::Parser& set_controllable (&c); _learned = true; /* from controllable */ + _encoder = false; setting = false; last_value = 0; // got a better idea ? last_controllable_value = 0.0f; @@ -188,8 +190,9 @@ MIDIControllable::control_to_midi (float val) val = actl->internal_to_interface(val); } } - - return (val - control_min) / control_range * max_value_for_type (); + // fiddle value of max so value doesn't jump from 125 to 127 for 1.0 + // otherwise decrement won't work. + return (val - control_min) / control_range * (max_value_for_type () - 1); } float @@ -197,7 +200,7 @@ MIDIControllable::midi_to_control (int val) { /* fiddle with MIDI value so that we get an odd number of integer steps and can thus represent "middle" precisely as 0.5. this maps to - the range 0..+1.0 + the range 0..+1.0 (0 to 126) */ float fv = (val == 0 ? 0 : float (val - 1) / (max_value_for_type() - 1)); @@ -301,30 +304,41 @@ MIDIControllable::midi_sense_controller (Parser &, EventTwoBytes *msg) if (control_additional == msg->controller_number) { if (!controllable->is_toggle()) { + if (!is_encoder()) { + float new_value = msg->value; + float max_value = max(last_controllable_value, new_value); + float min_value = min(last_controllable_value, new_value); + float range = max_value - min_value; + float threshold = (float) _surface->threshold (); + + bool const in_sync = ( + range < threshold && + controllable->get_value() <= midi_to_control(max_value) && + controllable->get_value() >= midi_to_control(min_value) + ); + + /* If the surface is not motorised, we try to prevent jumps when + the MIDI controller and controllable are out of sync. + There might be a better way of doing this. + */ + + if (in_sync || _surface->motorised ()) { + controllable->set_value (midi_to_control (new_value)); + } + DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI CC %1 value %2 %3\n", (int) msg->controller_number, (float) midi_to_control(new_value), current_uri() )); + + last_controllable_value = new_value; + } else { + // add or subtract ticks from last value + int offset = (msg->value & 0x3f); + if (msg->value > 0x40) { + controllable->set_value (midi_to_control (last_value - offset + 1)); + } else { + controllable->set_value (midi_to_control (last_value + offset + 1)); + } + DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI CC %1 value %2 %3\n", (int) msg->controller_number, (int) last_value, current_uri() )); - float new_value = msg->value; - float max_value = max(last_controllable_value, new_value); - float min_value = min(last_controllable_value, new_value); - float range = max_value - min_value; - float threshold = (float) _surface->threshold (); - - bool const in_sync = ( - range < threshold && - controllable->get_value() <= midi_to_control(max_value) && - controllable->get_value() >= midi_to_control(min_value) - ); - - /* If the surface is not motorised, we try to prevent jumps when - the MIDI controller and controllable are out of sync. - There might be a better way of doing this. - */ - - if (in_sync || _surface->motorised ()) { - controllable->set_value (midi_to_control (new_value)); } - DEBUG_TRACE (DEBUG::GenericMidi, string_compose ("MIDI CC %1 value %2 %3\n", (int) msg->controller_number, (float) midi_to_control(new_value), current_uri() )); - - last_controllable_value = new_value; } else { if (msg->value > 64.0f) { controllable->set_value (1); diff --git a/libs/surfaces/generic_midi/midicontrollable.h b/libs/surfaces/generic_midi/midicontrollable.h index aeed05db4a..51ee60bb1e 100644 --- a/libs/surfaces/generic_midi/midicontrollable.h +++ b/libs/surfaces/generic_midi/midicontrollable.h @@ -75,6 +75,9 @@ class MIDIControllable : public PBD::Stateful bool learned() const { return _learned; } + bool is_encoder() const { return _encoder; } + void set_encoder(bool val) { _encoder = val; } + MIDI::Parser& get_parser() { return _parser; } PBD::Controllable* get_controllable() const { return controllable; } void set_controllable (PBD::Controllable*); @@ -109,6 +112,7 @@ class MIDIControllable : public PBD::Stateful bool _momentary; bool _is_gain_controller; bool _learned; + bool _encoder; int midi_msg_id; /* controller ID or note number */ PBD::ScopedConnection midi_sense_connection[2]; PBD::ScopedConnection midi_learn_connection; -- cgit v1.2.3