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 bool match_controller_helper(const Proto &msg, int field_number, int controller)
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() == controller);
23 template <class Proto>
24 inline bool match_button_helper(const Proto &msg, int field_number, int note)
26 using namespace google::protobuf;
27 const FieldDescriptor *descriptor = msg.GetDescriptor()->FindFieldByNumber(field_number);
28 const Reflection *reflection = msg.GetReflection();
29 if (!reflection->HasField(msg, descriptor)) {
32 const MIDIButtonProto &button_proto =
33 static_cast<const MIDIButtonProto &>(reflection->GetMessage(msg, descriptor));
34 return (button_proto.note_number() == note);
37 template <class Proto>
38 inline bool match_bank_helper(const Proto &msg, int bank_field_number, int bank)
40 using namespace google::protobuf;
41 const FieldDescriptor *bank_descriptor = msg.GetDescriptor()->FindFieldByNumber(bank_field_number);
42 const Reflection *reflection = msg.GetReflection();
43 if (!reflection->HasField(msg, bank_descriptor)) {
44 // No bank set => in all banks.
47 return reflection->GetInt32(msg, bank_descriptor) == bank;
50 // Find what MIDI note the given light (as given by field_number) is mapped to, and enable it.
51 template <class Proto>
52 void activate_mapped_light(const Proto &msg, int field_number, std::map<unsigned, uint8_t> *active_lights)
54 using namespace google::protobuf;
55 const FieldDescriptor *descriptor = msg.GetDescriptor()->FindFieldByNumber(field_number);
56 const Reflection *reflection = msg.GetReflection();
57 if (!reflection->HasField(msg, descriptor)) {
60 const MIDILightProto &light_proto =
61 static_cast<const MIDILightProto &>(reflection->GetMessage(msg, descriptor));
62 active_lights->emplace(light_proto.note_number(), light_proto.velocity());
65 inline double map_controller_to_float(int controller, int val)
67 if (controller == MIDIReceiver::PITCH_BEND_CONTROLLER) {
68 // We supposedly go from -8192 to 8191 (inclusive), but there are
69 // controllers that only have 10-bit precision and do the upconversion
70 // to 14-bit wrong (just padding with zeros), making 8176 the highest
71 // attainable value. We solve this by making the effective range
72 // -8176..8176 (inclusive).
75 } else if (val >= 8176) {
78 return 0.5 * (double(val) / 8176.0) + 0.5;
82 // Slightly hackish mapping so that we can represent exactly 0.0, 0.5 and 1.0.
85 } else if (val >= 127) {
88 return (val + 0.5) / 127.0;
92 #endif // !defined(_MIDI_MAPPER_UTIL_H)