Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. #ifndef DOXYGEN
  18. namespace juce
  19. {
  20. namespace universal_midi_packets
  21. {
  22. /**
  23. Holds a single Universal MIDI Packet.
  24. @tags{Audio}
  25. */
  26. template <size_t numWords>
  27. class Packet
  28. {
  29. public:
  30. Packet() = default;
  31. template <size_t w = numWords, typename std::enable_if<w == 1, int>::type = 0>
  32. Packet (uint32_t a)
  33. : contents { { a } }
  34. {
  35. jassert (Utils::getNumWordsForMessageType (a) == 1);
  36. }
  37. template <size_t w = numWords, typename std::enable_if<w == 2, int>::type = 0>
  38. Packet (uint32_t a, uint32_t b)
  39. : contents { { a, b } }
  40. {
  41. jassert (Utils::getNumWordsForMessageType (a) == 2);
  42. }
  43. template <size_t w = numWords, typename std::enable_if<w == 3, int>::type = 0>
  44. Packet (uint32_t a, uint32_t b, uint32_t c)
  45. : contents { { a, b, c } }
  46. {
  47. jassert (Utils::getNumWordsForMessageType (a) == 3);
  48. }
  49. template <size_t w = numWords, typename std::enable_if<w == 4, int>::type = 0>
  50. Packet (uint32_t a, uint32_t b, uint32_t c, uint32_t d)
  51. : contents { { a, b, c, d } }
  52. {
  53. jassert (Utils::getNumWordsForMessageType (a) == 4);
  54. }
  55. template <size_t w, typename std::enable_if<w == numWords, int>::type = 0>
  56. explicit Packet (const std::array<uint32_t, w>& fullPacket)
  57. : contents (fullPacket)
  58. {
  59. jassert (Utils::getNumWordsForMessageType (fullPacket.front()) == numWords);
  60. }
  61. Packet withMessageType (uint8_t type) const noexcept
  62. {
  63. return withU4<0> (type);
  64. }
  65. Packet withGroup (uint8_t group) const noexcept
  66. {
  67. return withU4<1> (group);
  68. }
  69. Packet withStatus (uint8_t status) const noexcept
  70. {
  71. return withU4<2> (status);
  72. }
  73. Packet withChannel (uint8_t channel) const noexcept
  74. {
  75. return withU4<3> (channel);
  76. }
  77. uint8_t getMessageType() const noexcept { return getU4<0>(); }
  78. uint8_t getGroup() const noexcept { return getU4<1>(); }
  79. uint8_t getStatus() const noexcept { return getU4<2>(); }
  80. uint8_t getChannel() const noexcept { return getU4<3>(); }
  81. template <size_t index>
  82. Packet withU4 (uint8_t value) const noexcept
  83. {
  84. constexpr auto word = index / 8;
  85. auto copy = *this;
  86. std::get<word> (copy.contents) = Utils::U4<index % 8>::set (copy.template getU32<word>(), value);
  87. return copy;
  88. }
  89. template <size_t index>
  90. Packet withU8 (uint8_t value) const noexcept
  91. {
  92. constexpr auto word = index / 4;
  93. auto copy = *this;
  94. std::get<word> (copy.contents) = Utils::U8<index % 4>::set (copy.template getU32<word>(), value);
  95. return copy;
  96. }
  97. template <size_t index>
  98. Packet withU16 (uint16_t value) const noexcept
  99. {
  100. constexpr auto word = index / 2;
  101. auto copy = *this;
  102. std::get<word> (copy.contents) = Utils::U16<index % 2>::set (copy.template getU32<word>(), value);
  103. return copy;
  104. }
  105. template <size_t index>
  106. Packet withU32 (uint32_t value) const noexcept
  107. {
  108. auto copy = *this;
  109. std::get<index> (copy.contents) = value;
  110. return copy;
  111. }
  112. template <size_t index>
  113. uint8_t getU4() const noexcept
  114. {
  115. return Utils::U4<index % 8>::get (this->template getU32<index / 8>());
  116. }
  117. template <size_t index>
  118. uint8_t getU8() const noexcept
  119. {
  120. return Utils::U8<index % 4>::get (this->template getU32<index / 4>());
  121. }
  122. template <size_t index>
  123. uint16_t getU16() const noexcept
  124. {
  125. return Utils::U16<index % 2>::get (this->template getU32<index / 2>());
  126. }
  127. template <size_t index>
  128. uint32_t getU32() const noexcept
  129. {
  130. return std::get<index> (contents);
  131. }
  132. //==============================================================================
  133. using Contents = std::array<uint32_t, numWords>;
  134. using const_iterator = typename Contents::const_iterator;
  135. const_iterator begin() const noexcept { return contents.begin(); }
  136. const_iterator cbegin() const noexcept { return contents.begin(); }
  137. const_iterator end() const noexcept { return contents.end(); }
  138. const_iterator cend() const noexcept { return contents.end(); }
  139. const uint32_t* data() const noexcept { return contents.data(); }
  140. const uint32_t& front() const noexcept { return contents.front(); }
  141. const uint32_t& back() const noexcept { return contents.back(); }
  142. const uint32_t& operator[] (size_t index) const noexcept { return contents[index]; }
  143. private:
  144. Contents contents { {} };
  145. };
  146. using PacketX1 = Packet<1>;
  147. using PacketX2 = Packet<2>;
  148. using PacketX3 = Packet<3>;
  149. using PacketX4 = Packet<4>;
  150. }
  151. }
  152. #endif