X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=shared%2Fmidi_mapper_util.h;h=5b66b83710f863e74bf5ab8f28cc0d37928a69ea;hb=2c9a83aeae44dae6a0bbfbae33719976e6d527af;hp=39ccd84e43f7d1ec1e707e2de730ea1d08ad59d9;hpb=8e88294902b4b6f43c8db83dbf04830b5b9d2bfd;p=nageru diff --git a/shared/midi_mapper_util.h b/shared/midi_mapper_util.h index 39ccd84..5b66b83 100644 --- a/shared/midi_mapper_util.h +++ b/shared/midi_mapper_util.h @@ -2,6 +2,7 @@ #define _MIDI_MAPPER_UTIL_H 1 #include "midi_mapping.pb.h" +#include "shared/midi_device.h" #include @@ -61,4 +62,31 @@ void activate_mapped_light(const Proto &msg, int field_number, std::setinsert(light_proto.note_number()); } +inline double map_controller_to_float(int controller, int val) +{ + if (controller == MIDIReceiver::PITCH_BEND_CONTROLLER) { + // We supposedly go from -8192 to 8191 (inclusive), but there are + // controllers that only have 10-bit precision and do the upconversion + // to 14-bit wrong (just padding with zeros), making 8176 the highest + // attainable value. We solve this by making the effective range + // -8176..8176 (inclusive). + if (val <= -8176) { + return 0.0; + } else if (val >= 8176) { + return 1.0; + } else { + return 0.5 * (double(val) / 8176.0) + 0.5; + } + } + + // Slightly hackish mapping so that we can represent exactly 0.0, 0.5 and 1.0. + if (val <= 0) { + return 0.0; + } else if (val >= 127) { + return 1.0; + } else { + return (val + 0.5) / 127.0; + } +} + #endif // !defined(_MIDI_MAPPER_UTIL_H)