Control Surface  1.1.0
MIDI Control Surface library for Arduino
Channel.hpp
Go to the documentation of this file.
1 /* ✔ */
2 
3 #pragma once
4 
5 #include <stdint.h> // int8_t
6 #include <Settings/NamespaceSettings.hpp>
7 
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  explicit constexpr Channel(int8_t 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  constexpr int8_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(int8_t oneBasedChannel) {
38  return Channel{int8_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  constexpr bool operator==(const Channel &rhs) const {
48  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  Channel &operator+=(const int8_t rhs) {
58  this->zeroBasedChannel += rhs;
59  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  Channel operator+(const int8_t rhs) const {
69  Channel copy = *this;
70  copy += rhs;
71  return copy;
72  }
73 
74  /**
75  * @brief Subtract an offset.
76  *
77  * @param rhs
78  * The offset to subtract from this channel.
79  */
80  Channel &operator-=(const int8_t rhs) {
81  this->zeroBasedChannel -= rhs;
82  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  Channel operator-(const int8_t rhs) const {
92  Channel copy = *this;
93  copy -= rhs;
94  return copy;
95  }
96 
97  private:
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 
CHANNEL_13
constexpr Channel CHANNEL_13
Definition: Channel.hpp:123
Channel
A type-safe class for MIDI channels.
Definition: Channel.hpp:13
Channel::operator+
Channel operator+(const int8_t rhs) const
Add an offset to a channel.
Definition: Channel.hpp:68
CHANNEL_3
constexpr Channel CHANNEL_3
Definition: Channel.hpp:113
Channel::createChannel
static constexpr Channel createChannel(int8_t oneBasedChannel)
Create a channel.
Definition: Channel.hpp:37
CHANNEL_6
constexpr Channel CHANNEL_6
Definition: Channel.hpp:116
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
CHANNEL_11
constexpr Channel CHANNEL_11
Definition: Channel.hpp:121
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
CHANNEL_8
constexpr Channel CHANNEL_8
Definition: Channel.hpp:118
CHANNEL_12
constexpr Channel CHANNEL_12
Definition: Channel.hpp:122
Channel::operator-
Channel operator-(const int8_t rhs) const
Subtract an offset from a channel.
Definition: Channel.hpp:91
CHANNEL_7
constexpr Channel CHANNEL_7
Definition: Channel.hpp:117
Channel::operator+=
Channel & operator+=(const int8_t rhs)
Add an offset.
Definition: Channel.hpp:57
CHANNEL_15
constexpr Channel CHANNEL_15
Definition: Channel.hpp:125
CHANNEL_16
constexpr Channel CHANNEL_16
Definition: Channel.hpp:126
Channel::getRaw
constexpr int8_t getRaw() const
Get the channel as an integer.
Definition: Channel.hpp:29
Channel::Channel
constexpr Channel(int8_t zeroBasedChannel)
Create a MIDI Channel object.
Definition: Channel.hpp:21
CHANNEL_5
constexpr Channel CHANNEL_5
Definition: Channel.hpp:115
CHANNEL_4
constexpr Channel CHANNEL_4
Definition: Channel.hpp:114
CHANNEL_10
constexpr Channel CHANNEL_10
Definition: Channel.hpp:120
Channel::operator==
constexpr bool operator==(const Channel &rhs) const
Check if two channels are the same.
Definition: Channel.hpp:47
Channel::operator-=
Channel & operator-=(const int8_t rhs)
Subtract an offset.
Definition: Channel.hpp:80
CHANNEL_9
constexpr Channel CHANNEL_9
Definition: Channel.hpp:119
CHANNEL_14
constexpr Channel CHANNEL_14
Definition: Channel.hpp:124
Channel::zeroBasedChannel
int8_t zeroBasedChannel
Definition: Channel.hpp:98
CHANNEL_2
constexpr Channel CHANNEL_2
Definition: Channel.hpp:112
CHANNEL_1
constexpr Channel CHANNEL_1
Definition: Channel.hpp:111