1 #ifndef _MIDI_MAPPER_UTIL_H
2 #define _MIDI_MAPPER_UTIL_H 1
4 #include "midi_mapping.pb.h"
5 #include "shared/midi_device.h"
7 #include <google/protobuf/descriptor.h>
10 inline int get_controller_mapping_helper(const Proto &msg, int field_number, int default_value)
12 using namespace google::protobuf;
13 const FieldDescriptor *descriptor = msg.GetDescriptor()->FindFieldByNumber(field_number);
14 const Reflection *reflection = msg.GetReflection();
15 if (!reflection->HasField(msg, descriptor)) {
18 const MIDIControllerProto &controller_proto =
19 static_cast<const MIDIControllerProto &>(reflection->GetMessage(msg, descriptor));
20 return controller_proto.controller_number();
23 template <class Proto>
24 inline bool match_controller_helper(const Proto &msg, int field_number, int controller)
26 return (get_controller_mapping_helper(msg, field_number, -1) == controller);
29 template <class Proto>
30 inline int get_button_mapping_helper(const Proto &msg, int field_number, int default_value)
32 using namespace google::protobuf;
33 const FieldDescriptor *descriptor = msg.GetDescriptor()->FindFieldByNumber(field_number);
34 const Reflection *reflection = msg.GetReflection();
35 if (!reflection->HasField(msg, descriptor)) {
38 const MIDIButtonProto &button_proto =
39 static_cast<const MIDIButtonProto &>(reflection->GetMessage(msg, descriptor));
40 return button_proto.note_number();
43 template <class Proto>
44 inline bool match_button_helper(const Proto &msg, int field_number, int note)
46 return (get_button_mapping_helper(msg, field_number, -1) == note);
49 template <class Proto>
50 inline bool match_bank_helper(const Proto &msg, int bank_field_number, int bank)
52 using namespace google::protobuf;
53 const FieldDescriptor *bank_descriptor = msg.GetDescriptor()->FindFieldByNumber(bank_field_number);
54 const Reflection *reflection = msg.GetReflection();
55 if (!reflection->HasField(msg, bank_descriptor)) {
56 // No bank set => in all banks.
59 return reflection->GetInt32(msg, bank_descriptor) == bank;
62 template <class Proto>
63 inline MIDILightProto get_light_mapping_helper(const Proto &msg, int field_number)
65 using namespace google::protobuf;
66 const FieldDescriptor *descriptor = msg.GetDescriptor()->FindFieldByNumber(field_number);
67 const Reflection *reflection = msg.GetReflection();
68 if (!reflection->HasField(msg, descriptor)) {
69 return MIDILightProto();
71 return static_cast<const MIDILightProto &>(reflection->GetMessage(msg, descriptor));
74 // Find what MIDI note the given light (as given by field_number) is mapped to, and enable it.
75 template <class Proto>
76 void activate_mapped_light(const Proto &msg, int field_number, std::map<MIDIDevice::LightKey, uint8_t> *active_lights)
78 MIDILightProto light_proto = get_light_mapping_helper(msg, field_number);
79 if (!light_proto.has_note_number()) {
82 active_lights->emplace(MIDIDevice::LightKey{MIDIDevice::LightKey::NOTE, unsigned(light_proto.note_number())},
83 light_proto.velocity());
86 inline double map_controller_to_float(int controller, int val)
88 if (controller == MIDIReceiver::PITCH_BEND_CONTROLLER) {
89 // We supposedly go from -8192 to 8191 (inclusive), but there are
90 // controllers that only have 10-bit precision and do the upconversion
91 // to 14-bit wrong (just padding with zeros), making 8176 the highest
92 // attainable value. We solve this by making the effective range
93 // -8176..8176 (inclusive).
96 } else if (val >= 8176) {
99 return 0.5 * (double(val) / 8176.0) + 0.5;
103 // Slightly hackish mapping so that we can represent exactly 0.0, 0.5 and 1.0.
106 } else if (val >= 127) {
109 return (val + 0.5) / 127.0;
113 #endif // !defined(_MIDI_MAPPER_UTIL_H)