]> git.sesse.net Git - nageru/blobdiff - nageru/midi_mapper.cpp
Turn off guessing if the source bus contains the pitch bend controller.
[nageru] / nageru / midi_mapper.cpp
index 30e934b856396f3e450274912c835dfe52f1436c..fe62546569c4defca910c6a06f917244bcaf7b37 100644 (file)
@@ -19,7 +19,9 @@
 #include <thread>
 
 #include "audio_mixer.h"
-#include "midi_mapping.pb.h"
+#include "nageru_midi_mapping.pb.h"
+#include "shared/midi_device.h"
+#include "shared/midi_mapper_util.h"
 #include "shared/text_proto.h"
 
 using namespace google::protobuf;
@@ -28,8 +30,23 @@ using namespace std::placeholders;
 
 namespace {
 
-double map_controller_to_float(int val)
+double map_controller_to_float(int controller, int val)
 {
+       if (controller == MIDIReceiver::PITCH_BEND_CONTROLLER) {
+               // We supposedly go from -8192 to 8191 (inclusive), but there are
+               // controllers that only have 10-bit precision and do the upconversion
+               // to 14-bit wrong (just padding with zeros), making 8176 the highest
+               // attainable value. We solve this by making the effective range
+               // -8176..8176 (inclusive).
+               if (val <= -8176) {
+                       return 0.0;
+               } else if (val >= 8176) {
+                       return 1.0;
+               } else {
+                       return 0.5 * (double(val) / 8176.0) + 0.5;
+               }
+       }
+
        // Slightly hackish mapping so that we can represent exactly 0.0, 0.5 and 1.0.
        if (val <= 0) {
                return 0.0;
@@ -95,7 +112,7 @@ ControllerReceiver *MIDIMapper::set_receiver(ControllerReceiver *new_receiver)
 
 void MIDIMapper::controller_received(int controller, int value_int)
 {
-       const float value = map_controller_to_float(value_int);
+       const float value = map_controller_to_float(controller, value_int);
 
        receiver->controller_changed(controller);
 
@@ -209,15 +226,7 @@ void MIDIMapper::match_controller(int controller, int field_number, int bank_fie
 
        for (size_t bus_idx = 0; bus_idx < size_t(mapping_proto->bus_mapping_size()); ++bus_idx) {
                const MIDIMappingBusProto &bus_mapping = mapping_proto->bus_mapping(bus_idx);
-
-               const FieldDescriptor *descriptor = bus_mapping.GetDescriptor()->FindFieldByNumber(field_number);
-               const Reflection *bus_reflection = bus_mapping.GetReflection();
-               if (!bus_reflection->HasField(bus_mapping, descriptor)) {
-                       continue;
-               }
-               const MIDIControllerProto &controller_proto =
-                       static_cast<const MIDIControllerProto &>(bus_reflection->GetMessage(bus_mapping, descriptor));
-               if (controller_proto.controller_number() == controller) {
+               if (match_controller_helper(bus_mapping, field_number, controller)) {
                        func(bus_idx, value);
                }
        }
@@ -231,15 +240,7 @@ void MIDIMapper::match_button(int note, int field_number, int bank_field_number,
 
        for (size_t bus_idx = 0; bus_idx < size_t(mapping_proto->bus_mapping_size()); ++bus_idx) {
                const MIDIMappingBusProto &bus_mapping = mapping_proto->bus_mapping(bus_idx);
-
-               const FieldDescriptor *descriptor = bus_mapping.GetDescriptor()->FindFieldByNumber(field_number);
-               const Reflection *bus_reflection = bus_mapping.GetReflection();
-               if (!bus_reflection->HasField(bus_mapping, descriptor)) {
-                       continue;
-               }
-               const MIDIButtonProto &button_proto =
-                       static_cast<const MIDIButtonProto &>(bus_reflection->GetMessage(bus_mapping, descriptor));
-               if (button_proto.note_number() == note) {
+               if (match_button_helper(bus_mapping, field_number, note)) {
                        func(bus_idx);
                }
        }
@@ -259,10 +260,7 @@ bool MIDIMapper::has_active_controller(unsigned bus_idx, int field_number, int b
 
 bool MIDIMapper::bank_mismatch(int bank_field_number)
 {
-       const FieldDescriptor *bank_descriptor = mapping_proto->GetDescriptor()->FindFieldByNumber(bank_field_number);
-       const Reflection *reflection = mapping_proto->GetReflection();
-       return (reflection->HasField(*mapping_proto, bank_descriptor) &&
-               reflection->GetInt32(*mapping_proto, bank_descriptor) != current_controller_bank);
+       return !match_bank_helper(*mapping_proto, bank_field_number, current_controller_bank);
 }
 
 void MIDIMapper::refresh_highlights()
@@ -345,19 +343,6 @@ void MIDIMapper::update_highlights()
        }
 }
 
-// Find what MIDI note the given light (as given by field_number) is mapped to, and enable it.
-void activate_mapped_light(const MIDIMappingBusProto &bus_mapping, int field_number, set<unsigned> *active_lights)
-{
-       const FieldDescriptor *descriptor = bus_mapping.GetDescriptor()->FindFieldByNumber(field_number);
-       const Reflection *bus_reflection = bus_mapping.GetReflection();
-       if (!bus_reflection->HasField(bus_mapping, descriptor)) {
-               return;
-       }
-       const MIDILightProto &light_proto =
-               static_cast<const MIDILightProto &>(bus_reflection->GetMessage(bus_mapping, descriptor));
-       active_lights->insert(light_proto.note_number());
-}
-
 void MIDIMapper::update_lights_lock_held()
 {
        if (global_audio_mixer == nullptr) {