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.

230 lines
7.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. /**
  24. Helper class for constructing a packet for sending to a BLOCKS device
  25. */
  26. template <int maxPacketBytes>
  27. struct HostPacketBuilder
  28. {
  29. HostPacketBuilder() noexcept {}
  30. HostPacketBuilder (const HostPacketBuilder&) = delete;
  31. HostPacketBuilder (HostPacketBuilder&&) = default;
  32. const void* getData() const noexcept { return data.getData(); }
  33. int size() const noexcept { return data.size(); }
  34. //==============================================================================
  35. void writePacketSysexHeaderBytes (TopologyIndex deviceIndex) noexcept
  36. {
  37. static_assert (maxPacketBytes > 10, "Not enough bytes for a sensible message!");
  38. jassert ((deviceIndex & 64) == 0);
  39. data.writeHeaderSysexBytes (deviceIndex);
  40. }
  41. void writePacketSysexFooter() noexcept
  42. {
  43. data.writePacketSysexFooter();
  44. }
  45. //==============================================================================
  46. bool deviceControlMessage (DeviceCommand command) noexcept
  47. {
  48. if (! data.hasCapacity (MessageType::bits + DeviceCommand::bits))
  49. return false;
  50. writeMessageType (MessageFromHost::deviceCommandMessage);
  51. data << command;
  52. return true;
  53. }
  54. //==============================================================================
  55. bool beginDataChanges (PacketIndex packetIndex) noexcept
  56. {
  57. if (! data.hasCapacity (MessageType::bits + PacketIndex::bits + DataChangeCommand::bits))
  58. return false;
  59. writeMessageType (MessageFromHost::sharedDataChange);
  60. data << packetIndex;
  61. return true;
  62. }
  63. bool endDataChanges (bool isLastChange) noexcept
  64. {
  65. if (! data.hasCapacity (DataChangeCommand::bits))
  66. return false;
  67. data << DataChangeCommand ((uint32) isLastChange ? endOfChanges : endOfPacket);
  68. return true;
  69. }
  70. bool skipBytes (int numToSkip) noexcept
  71. {
  72. if (numToSkip <= 0)
  73. return true;
  74. auto state = data.getState();
  75. while (numToSkip > ByteCountMany::maxValue)
  76. {
  77. if (! skipBytes (ByteCountMany::maxValue))
  78. {
  79. data.restore (state);
  80. return false;
  81. }
  82. numToSkip -= ByteCountMany::maxValue;
  83. }
  84. if (numToSkip > ByteCountFew::maxValue)
  85. {
  86. if (! data.hasCapacity (DataChangeCommand::bits * 2 + ByteCountMany::bits))
  87. {
  88. data.restore (state);
  89. return false;
  90. }
  91. data << DataChangeCommand ((uint32) skipBytesMany) << ByteCountMany ((uint32) numToSkip);
  92. return true;
  93. }
  94. if (! data.hasCapacity (DataChangeCommand::bits * 2 + ByteCountFew::bits))
  95. {
  96. data.restore (state);
  97. return false;
  98. }
  99. data << DataChangeCommand ((uint32) skipBytesFew) << ByteCountFew ((uint32) numToSkip);
  100. return true;
  101. }
  102. bool setMultipleBytes (const uint8* values, int num) noexcept
  103. {
  104. if (num <= 0)
  105. return true;
  106. if (! data.hasCapacity (DataChangeCommand::bits * 2 + num * (1 + ByteValue::bits)))
  107. return false;
  108. data << DataChangeCommand ((uint32) setSequenceOfBytes);
  109. for (int i = 0; i < num; ++i)
  110. data << ByteValue ((uint32) values[i])
  111. << ByteSequenceContinues (i < num - 1 ? 1 : 0);
  112. return true;
  113. }
  114. bool setMultipleBytes (uint8 value, uint8 lastValue, int num) noexcept
  115. {
  116. if (num <= 0)
  117. return true;
  118. if (num == 1)
  119. return setMultipleBytes (&value, 1); // (this is a more compact message)
  120. auto state = data.getState();
  121. if (num > ByteCountMany::maxValue)
  122. {
  123. if (! setMultipleBytes (value, lastValue, ByteCountMany::maxValue))
  124. {
  125. data.restore (state);
  126. return false;
  127. }
  128. return setMultipleBytes (value, lastValue, num - ByteCountMany::maxValue);
  129. }
  130. if (num > ByteCountFew::maxValue)
  131. {
  132. if (! data.hasCapacity (DataChangeCommand::bits * 2 + ByteCountMany::bits + ByteValue::bits))
  133. {
  134. data.restore (state);
  135. return false;
  136. }
  137. data << DataChangeCommand ((uint32) setManyBytesWithValue)
  138. << ByteCountMany ((uint32) num)
  139. << ByteValue ((uint32) value);
  140. return true;
  141. }
  142. if (value == lastValue)
  143. {
  144. if (! data.hasCapacity (DataChangeCommand::bits * 2 + ByteCountFew::bits))
  145. {
  146. data.restore (state);
  147. return false;
  148. }
  149. data << DataChangeCommand ((uint32) setFewBytesWithLastValue) << ByteCountFew ((uint32) num);
  150. return true;
  151. }
  152. if (! data.hasCapacity (DataChangeCommand::bits * 2 + ByteCountFew::bits + ByteValue::bits))
  153. {
  154. data.restore (state);
  155. return false;
  156. }
  157. data << DataChangeCommand ((uint32) setFewBytesWithValue) << ByteCountFew ((uint32) num)
  158. << ByteValue ((uint32) value);
  159. return true;
  160. }
  161. bool addProgramEventMessage (const int32* messageData)
  162. {
  163. if (! data.hasCapacity (BitSizes::programEventMessage))
  164. return false;
  165. writeMessageType (MessageFromHost::programEventMessage);
  166. for (uint32 i = 0; i < numProgramMessageInts; ++i)
  167. data << IntegerWithBitSize<32> ((uint32) messageData[i]);
  168. return true;
  169. }
  170. //==============================================================================
  171. private:
  172. Packed7BitArrayBuilder<maxPacketBytes> data;
  173. void writeMessageType (MessageFromHost type) noexcept
  174. {
  175. data << MessageType ((uint32) type);
  176. }
  177. };