]> git.sesse.net Git - nageru/blob - shared/midi_mapper_util.h
Support changing overall playing speed at runtime, using a slider or a MIDI controller.
[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 bool match_controller_helper(const Proto &msg, int field_number, int controller)
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 false;
17         }
18         const MIDIControllerProto &controller_proto =
19                 static_cast<const MIDIControllerProto &>(reflection->GetMessage(msg, descriptor));
20         return (controller_proto.controller_number() == controller);
21 }
22
23 template <class Proto>
24 inline bool match_button_helper(const Proto &msg, int field_number, int note)
25 {
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)) {
30                 return false;
31         }
32         const MIDIButtonProto &button_proto =
33                 static_cast<const MIDIButtonProto &>(reflection->GetMessage(msg, descriptor));
34         return (button_proto.note_number() == note);
35 }
36
37 template <class Proto>
38 inline bool match_bank_helper(const Proto &msg, int bank_field_number, int bank)
39 {
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.
45                 return true;
46         }
47         return reflection->GetInt32(msg, bank_descriptor) == bank;
48 }
49
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::set<unsigned> *active_lights)
53 {
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)) {
58                 return;
59         }
60         const MIDILightProto &light_proto =
61                 static_cast<const MIDILightProto &>(reflection->GetMessage(msg, descriptor));
62         active_lights->insert(light_proto.note_number());
63 }
64
65 inline double map_controller_to_float(int controller, int val)
66 {
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).
73                 if (val <= -8176) {
74                         return 0.0;
75                 } else if (val >= 8176) {
76                         return 1.0;
77                 } else {
78                         return 0.5 * (double(val) / 8176.0) + 0.5;
79                 }
80         }
81
82         // Slightly hackish mapping so that we can represent exactly 0.0, 0.5 and 1.0.
83         if (val <= 0) {
84                 return 0.0;
85         } else if (val >= 127) {
86                 return 1.0;
87         } else {
88                 return (val + 0.5) / 127.0;
89         }
90 }
91
92 #endif  // !defined(_MIDI_MAPPER_UTIL_H)