Line data Source code
1 : /* ✔ */ 2 : 3 : #pragma once 4 : 5 : #include <stdint.h> // uint8_t 6 : #include <Settings/NamespaceSettings.hpp> 7 : 8 : BEGIN_CS_NAMESPACE 9 : 10 : /** 11 : * A type-safe class for MIDI channels. 12 : */ 13 : class Channel { 14 : public: 15 : /** 16 : * @brief Create a MIDI Channel object. 17 : * 18 : * @param zeroBasedChannel 19 : * The zero-based channel (0 is the first channel). 20 : */ 21 182 : explicit constexpr Channel(uint8_t zeroBasedChannel) 22 182 : : zeroBasedChannel(zeroBasedChannel) {} 23 : 24 : /** 25 : * @brief Get the channel as an integer. 26 : * 27 : * @return The zero-based channel (0 is the first channel). 28 : */ 29 387 : constexpr uint8_t getRaw() const { return zeroBasedChannel; } 30 : 31 : /** 32 : * @brief Create a channel. 33 : * 34 : * @param oneBasedChannel 35 : * The channel number (1 is the first channel). 36 : */ 37 : static constexpr Channel createChannel(uint8_t oneBasedChannel) { 38 : return Channel{uint8_t(oneBasedChannel - 1)}; 39 : } 40 : 41 : /** 42 : * @brief Check if two channels are the same. 43 : * 44 : * @param rhs 45 : * The other channel to compare this channel to. 46 : */ 47 31 : constexpr bool operator==(const Channel &rhs) const { 48 31 : return this->zeroBasedChannel == rhs.zeroBasedChannel; 49 : } 50 : 51 : /** 52 : * @brief Add an offset. 53 : * 54 : * @param rhs 55 : * The offset to add to this channel. 56 : */ 57 12 : Channel &operator+=(uint8_t rhs) { 58 12 : this->zeroBasedChannel += rhs; 59 12 : return *this; 60 : } 61 : 62 : /** 63 : * @brief Add an offset to a channel. 64 : * 65 : * @param rhs 66 : * The offset to add to the channel. 67 : */ 68 12 : Channel operator+(uint8_t rhs) const { 69 12 : Channel copy = *this; 70 12 : copy += rhs; 71 12 : return copy; 72 : } 73 : 74 : /** 75 : * @brief Subtract an offset. 76 : * 77 : * @param rhs 78 : * The offset to subtract from this channel. 79 : */ 80 4 : Channel &operator-=(uint8_t rhs) { 81 4 : this->zeroBasedChannel -= rhs; 82 4 : return *this; 83 : } 84 : 85 : /** 86 : * @brief Subtract an offset from a channel. 87 : * 88 : * @param rhs 89 : * The offset to subtract from the channel. 90 : */ 91 4 : Channel operator-(uint8_t rhs) const { 92 4 : Channel copy = *this; 93 4 : copy -= rhs; 94 4 : return copy; 95 : } 96 : 97 : private: 98 : uint8_t zeroBasedChannel : 4; 99 : }; 100 : 101 : /** 102 : * @brief A literal operator to create MIDI Channels. 103 : * 104 : * @param ch 105 : * The (one-based) MIDI channel (1 is the first channel). 106 : */ 107 : constexpr Channel operator"" _ch(unsigned long long ch) { 108 : return Channel::createChannel(ch); 109 : } 110 : 111 : constexpr Channel CHANNEL_1 = 1_ch; 112 : constexpr Channel CHANNEL_2 = 2_ch; 113 : constexpr Channel CHANNEL_3 = 3_ch; 114 : constexpr Channel CHANNEL_4 = 4_ch; 115 : constexpr Channel CHANNEL_5 = 5_ch; 116 : constexpr Channel CHANNEL_6 = 6_ch; 117 : constexpr Channel CHANNEL_7 = 7_ch; 118 : constexpr Channel CHANNEL_8 = 8_ch; 119 : constexpr Channel CHANNEL_9 = 9_ch; 120 : constexpr Channel CHANNEL_10 = 10_ch; 121 : constexpr Channel CHANNEL_11 = 11_ch; 122 : constexpr Channel CHANNEL_12 = 12_ch; 123 : constexpr Channel CHANNEL_13 = 13_ch; 124 : constexpr Channel CHANNEL_14 = 14_ch; 125 : constexpr Channel CHANNEL_15 = 15_ch; 126 : constexpr Channel CHANNEL_16 = 16_ch; 127 : 128 : END_CS_NAMESPACE