VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_audio_basics / midi / ump / juce_UMPacket.h
blobc0e855d3c834035b1bae00d9afa38c46648b99d9
1 /*
2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
10 The code included in this file is provided under the terms of the ISC license
11 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12 To use, copy, modify, and/or distribute this software for any purpose with or
13 without fee is hereby granted provided that the above copyright notice and
14 this permission notice appear in all copies.
16 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18 DISCLAIMED.
20 ==============================================================================
23 #ifndef DOXYGEN
25 namespace juce
27 namespace universal_midi_packets
30 /**
31 Holds a single Universal MIDI Packet.
33 @tags{Audio}
35 template <size_t numWords>
36 class Packet
38 public:
39 Packet() = default;
41 template <size_t w = numWords, typename std::enable_if<w == 1, int>::type = 0>
42 Packet (uint32_t a)
43 : contents { { a } }
45 jassert (Utils::getNumWordsForMessageType (a) == 1);
48 template <size_t w = numWords, typename std::enable_if<w == 2, int>::type = 0>
49 Packet (uint32_t a, uint32_t b)
50 : contents { { a, b } }
52 jassert (Utils::getNumWordsForMessageType (a) == 2);
55 template <size_t w = numWords, typename std::enable_if<w == 3, int>::type = 0>
56 Packet (uint32_t a, uint32_t b, uint32_t c)
57 : contents { { a, b, c } }
59 jassert (Utils::getNumWordsForMessageType (a) == 3);
62 template <size_t w = numWords, typename std::enable_if<w == 4, int>::type = 0>
63 Packet (uint32_t a, uint32_t b, uint32_t c, uint32_t d)
64 : contents { { a, b, c, d } }
66 jassert (Utils::getNumWordsForMessageType (a) == 4);
69 template <size_t w, typename std::enable_if<w == numWords, int>::type = 0>
70 explicit Packet (const std::array<uint32_t, w>& fullPacket)
71 : contents (fullPacket)
73 jassert (Utils::getNumWordsForMessageType (fullPacket.front()) == numWords);
76 Packet withMessageType (uint8_t type) const noexcept
78 return withU4<0> (type);
81 Packet withGroup (uint8_t group) const noexcept
83 return withU4<1> (group);
86 Packet withStatus (uint8_t status) const noexcept
88 return withU4<2> (status);
91 Packet withChannel (uint8_t channel) const noexcept
93 return withU4<3> (channel);
96 uint8_t getMessageType() const noexcept { return getU4<0>(); }
98 uint8_t getGroup() const noexcept { return getU4<1>(); }
100 uint8_t getStatus() const noexcept { return getU4<2>(); }
102 uint8_t getChannel() const noexcept { return getU4<3>(); }
104 template <size_t index>
105 Packet withU4 (uint8_t value) const noexcept
107 constexpr auto word = index / 8;
108 auto copy = *this;
109 std::get<word> (copy.contents) = Utils::U4<index % 8>::set (copy.template getU32<word>(), value);
110 return copy;
113 template <size_t index>
114 Packet withU8 (uint8_t value) const noexcept
116 constexpr auto word = index / 4;
117 auto copy = *this;
118 std::get<word> (copy.contents) = Utils::U8<index % 4>::set (copy.template getU32<word>(), value);
119 return copy;
122 template <size_t index>
123 Packet withU16 (uint16_t value) const noexcept
125 constexpr auto word = index / 2;
126 auto copy = *this;
127 std::get<word> (copy.contents) = Utils::U16<index % 2>::set (copy.template getU32<word>(), value);
128 return copy;
131 template <size_t index>
132 Packet withU32 (uint32_t value) const noexcept
134 auto copy = *this;
135 std::get<index> (copy.contents) = value;
136 return copy;
139 template <size_t index>
140 uint8_t getU4() const noexcept
142 return Utils::U4<index % 8>::get (this->template getU32<index / 8>());
145 template <size_t index>
146 uint8_t getU8() const noexcept
148 return Utils::U8<index % 4>::get (this->template getU32<index / 4>());
151 template <size_t index>
152 uint16_t getU16() const noexcept
154 return Utils::U16<index % 2>::get (this->template getU32<index / 2>());
157 template <size_t index>
158 uint32_t getU32() const noexcept
160 return std::get<index> (contents);
163 //==============================================================================
164 using Contents = std::array<uint32_t, numWords>;
166 using const_iterator = typename Contents::const_iterator;
168 const_iterator begin() const noexcept { return contents.begin(); }
169 const_iterator cbegin() const noexcept { return contents.begin(); }
171 const_iterator end() const noexcept { return contents.end(); }
172 const_iterator cend() const noexcept { return contents.end(); }
174 const uint32_t* data() const noexcept { return contents.data(); }
176 const uint32_t& front() const noexcept { return contents.front(); }
177 const uint32_t& back() const noexcept { return contents.back(); }
179 const uint32_t& operator[] (size_t index) const noexcept { return contents[index]; }
181 private:
182 Contents contents { {} };
185 using PacketX1 = Packet<1>;
186 using PacketX2 = Packet<2>;
187 using PacketX3 = Packet<3>;
188 using PacketX4 = Packet<4>;
193 #endif