]> git.sesse.net Git - nageru/blob - shared/midi_mapper_util.h
072494e07606dee1f576e2f1e6ef8574189535ff
[nageru] / shared / midi_mapper_util.h
1 #ifndef _MIDI_MAPPER_UTIL_H
2 #define _MIDI_MAPPER_UTIL_H 1
3
4 #include "midi_mapping.pb.h"
5 #include "shared/midi_device.h"
6
7 #include <google/protobuf/descriptor.h>
8
9 template <class Proto>
10 inline int get_controller_mapping_helper(const Proto &msg, int field_number, int default_value)
11 {
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)) {
16                 return default_value;
17         }
18         const MIDIControllerProto &controller_proto =
19                 static_cast<const MIDIControllerProto &>(reflection->GetMessage(msg, descriptor));
20         return controller_proto.controller_number();
21 }
22
23 template <class Proto>
24 inline bool match_controller_helper(const Proto &msg, int field_number, int controller)
25 {
26         return (get_controller_mapping_helper(msg, field_number, -1) == controller);
27 }
28
29 template <class Proto>
30 inline int get_button_mapping_helper(const Proto &msg, int field_number, int default_value)
31 {
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)) {
36                 return default_value;
37         }
38         const MIDIButtonProto &button_proto =
39                 static_cast<const MIDIButtonProto &>(reflection->GetMessage(msg, descriptor));
40         return button_proto.note_number();
41 }
42
43 template <class Proto>
44 inline bool match_button_helper(const Proto &msg, int field_number, int note)
45 {
46         return (get_button_mapping_helper(msg, field_number, -1) == note);
47 }
48
49 template <class Proto>
50 inline bool match_bank_helper(const Proto &msg, int bank_field_number, int bank)
51 {
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.
57                 return true;
58         }
59         return reflection->GetInt32(msg, bank_descriptor) == bank;
60 }
61
62 // Find what MIDI note the given light (as given by field_number) is mapped to, and enable it.
63 template <class Proto>
64 void activate_mapped_light(const Proto &msg, int field_number, std::map<MIDIDevice::LightKey, uint8_t> *active_lights)
65 {
66         using namespace google::protobuf;
67         const FieldDescriptor *descriptor = msg.GetDescriptor()->FindFieldByNumber(field_number);
68         const Reflection *reflection = msg.GetReflection();
69         if (!reflection->HasField(msg, descriptor)) {
70                 return;
71         }
72         const MIDILightProto &light_proto =
73                 static_cast<const MIDILightProto &>(reflection->GetMessage(msg, descriptor));
74         active_lights->emplace(MIDIDevice::LightKey{MIDIDevice::LightKey::NOTE, unsigned(light_proto.note_number())},
75                 light_proto.velocity());
76 }
77
78 inline double map_controller_to_float(int controller, int val)
79 {
80         if (controller == MIDIReceiver::PITCH_BEND_CONTROLLER) {
81                 // We supposedly go from -8192 to 8191 (inclusive), but there are
82                 // controllers that only have 10-bit precision and do the upconversion
83                 // to 14-bit wrong (just padding with zeros), making 8176 the highest
84                 // attainable value. We solve this by making the effective range
85                 // -8176..8176 (inclusive).
86                 if (val <= -8176) {
87                         return 0.0;
88                 } else if (val >= 8176) {
89                         return 1.0;
90                 } else {
91                         return 0.5 * (double(val) / 8176.0) + 0.5;
92                 }
93         }
94
95         // Slightly hackish mapping so that we can represent exactly 0.0, 0.5 and 1.0.
96         if (val <= 0) {
97                 return 0.0;
98         } else if (val >= 127) {
99                 return 1.0;
100         } else {
101                 return (val + 0.5) / 127.0;
102         }
103 }
104
105 #endif  // !defined(_MIDI_MAPPER_UTIL_H)