The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

191 lines
5.7KB

  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::universal_midi_packets
  19. {
  20. /**
  21. Holds a single Universal MIDI Packet.
  22. @tags{Audio}
  23. */
  24. template <size_t numWords>
  25. class Packet
  26. {
  27. public:
  28. Packet() = default;
  29. template <size_t w = numWords, std::enable_if_t<w == 1, int> = 0>
  30. Packet (uint32_t a)
  31. : contents { { a } }
  32. {
  33. jassert (Utils::getNumWordsForMessageType (a) == 1);
  34. }
  35. template <size_t w = numWords, std::enable_if_t<w == 2, int> = 0>
  36. Packet (uint32_t a, uint32_t b)
  37. : contents { { a, b } }
  38. {
  39. jassert (Utils::getNumWordsForMessageType (a) == 2);
  40. }
  41. template <size_t w = numWords, std::enable_if_t<w == 3, int> = 0>
  42. Packet (uint32_t a, uint32_t b, uint32_t c)
  43. : contents { { a, b, c } }
  44. {
  45. jassert (Utils::getNumWordsForMessageType (a) == 3);
  46. }
  47. template <size_t w = numWords, std::enable_if_t<w == 4, int> = 0>
  48. Packet (uint32_t a, uint32_t b, uint32_t c, uint32_t d)
  49. : contents { { a, b, c, d } }
  50. {
  51. jassert (Utils::getNumWordsForMessageType (a) == 4);
  52. }
  53. template <size_t w, std::enable_if_t<w == numWords, int> = 0>
  54. explicit Packet (const std::array<uint32_t, w>& fullPacket)
  55. : contents (fullPacket)
  56. {
  57. jassert (Utils::getNumWordsForMessageType (fullPacket.front()) == numWords);
  58. }
  59. Packet withMessageType (uint8_t type) const noexcept
  60. {
  61. return withU4<0> (type);
  62. }
  63. Packet withGroup (uint8_t group) const noexcept
  64. {
  65. return withU4<1> (group);
  66. }
  67. Packet withStatus (uint8_t status) const noexcept
  68. {
  69. return withU4<2> (status);
  70. }
  71. Packet withChannel (uint8_t channel) const noexcept
  72. {
  73. return withU4<3> (channel);
  74. }
  75. uint8_t getMessageType() const noexcept { return getU4<0>(); }
  76. uint8_t getGroup() const noexcept { return getU4<1>(); }
  77. uint8_t getStatus() const noexcept { return getU4<2>(); }
  78. uint8_t getChannel() const noexcept { return getU4<3>(); }
  79. template <size_t index>
  80. Packet withU4 (uint8_t value) const noexcept
  81. {
  82. constexpr auto word = index / 8;
  83. auto copy = *this;
  84. std::get<word> (copy.contents) = Utils::U4<index % 8>::set (copy.template getU32<word>(), value);
  85. return copy;
  86. }
  87. template <size_t index>
  88. Packet withU8 (uint8_t value) const noexcept
  89. {
  90. constexpr auto word = index / 4;
  91. auto copy = *this;
  92. std::get<word> (copy.contents) = Utils::U8<index % 4>::set (copy.template getU32<word>(), value);
  93. return copy;
  94. }
  95. template <size_t index>
  96. Packet withU16 (uint16_t value) const noexcept
  97. {
  98. constexpr auto word = index / 2;
  99. auto copy = *this;
  100. std::get<word> (copy.contents) = Utils::U16<index % 2>::set (copy.template getU32<word>(), value);
  101. return copy;
  102. }
  103. template <size_t index>
  104. Packet withU32 (uint32_t value) const noexcept
  105. {
  106. auto copy = *this;
  107. std::get<index> (copy.contents) = value;
  108. return copy;
  109. }
  110. template <size_t index>
  111. uint8_t getU4() const noexcept
  112. {
  113. return Utils::U4<index % 8>::get (this->template getU32<index / 8>());
  114. }
  115. template <size_t index>
  116. uint8_t getU8() const noexcept
  117. {
  118. return Utils::U8<index % 4>::get (this->template getU32<index / 4>());
  119. }
  120. template <size_t index>
  121. uint16_t getU16() const noexcept
  122. {
  123. return Utils::U16<index % 2>::get (this->template getU32<index / 2>());
  124. }
  125. template <size_t index>
  126. uint32_t getU32() const noexcept
  127. {
  128. return std::get<index> (contents);
  129. }
  130. //==============================================================================
  131. using Contents = std::array<uint32_t, numWords>;
  132. using const_iterator = typename Contents::const_iterator;
  133. const_iterator begin() const noexcept { return contents.begin(); }
  134. const_iterator cbegin() const noexcept { return contents.begin(); }
  135. const_iterator end() const noexcept { return contents.end(); }
  136. const_iterator cend() const noexcept { return contents.end(); }
  137. const uint32_t* data() const noexcept { return contents.data(); }
  138. const uint32_t& front() const noexcept { return contents.front(); }
  139. const uint32_t& back() const noexcept { return contents.back(); }
  140. const uint32_t& operator[] (size_t index) const noexcept { return contents[index]; }
  141. private:
  142. Contents contents { {} };
  143. };
  144. using PacketX1 = Packet<1>;
  145. using PacketX2 = Packet<2>;
  146. using PacketX3 = Packet<3>;
  147. using PacketX4 = Packet<4>;
  148. } // namespace juce::universal_midi_packets
  149. #endif