]> git.sesse.net Git - nageru/blob - shared/midi_mapper_util.h
Make Futatabi fades apply white balance.
[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 template <class Proto>
63 inline MIDILightProto get_light_mapping_helper(const Proto &msg, int field_number)
64 {
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();
70         }
71         return static_cast<const MIDILightProto &>(reflection->GetMessage(msg, descriptor));
72 }
73
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)
77 {
78         MIDILightProto light_proto = get_light_mapping_helper(msg, field_number);
79         if (!light_proto.has_note_number()) {
80                 return;
81         }
82         active_lights->emplace(MIDIDevice::LightKey{MIDIDevice::LightKey::NOTE, unsigned(light_proto.note_number())},
83                 light_proto.velocity());
84 }
85
86 inline double map_controller_to_float(int controller, int val)
87 {
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).
94                 if (val <= -8176) {
95                         return 0.0;
96                 } else if (val >= 8176) {
97                         return 1.0;
98                 } else {
99                         return 0.5 * (double(val) / 8176.0) + 0.5;
100                 }
101         }
102
103         // Slightly hackish mapping so that we can represent exactly 0.0, 0.5 and 1.0.
104         if (val <= 0) {
105                 return 0.0;
106         } else if (val >= 127) {
107                 return 1.0;
108         } else {
109                 return (val + 0.5) / 127.0;
110         }
111 }
112
113 #endif  // !defined(_MIDI_MAPPER_UTIL_H)