13
0

Add enc to midi map for mcp style encoders.

This commit is contained in:
Len Ovens 2015-07-20 23:15:53 -07:00 committed by Paul Davis
parent ae3d4efce0
commit d3b4ef4eed
3 changed files with 45 additions and 22 deletions

View File

@ -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;

View File

@ -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 ();
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)
);
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 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() ));
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);

View File

@ -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;