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.

281 lines
8.9KB

  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. All sysex messages to or from a BLOCKS device begin with these header bytes.
  25. The next byte that follows indicates the device index within the topology, where
  26. the 0x40 bit is set for device->host messages, and clear for host->device messages.
  27. The lower 6 bits contain the topology index of the destination or source device.
  28. */
  29. static const uint8 roliSysexHeader[] = { 0xf0, 0x00, 0x21, 0x10, 0x77 };
  30. static uint8 calculatePacketChecksum (const uint8* data, uint32 size) noexcept
  31. {
  32. uint8 checksum = (uint8) size;
  33. for (uint32 i = 0; i < size; ++i)
  34. checksum += checksum * 2 + data[i];
  35. return checksum & 0x7f;
  36. }
  37. //==============================================================================
  38. template <int numBits>
  39. struct IntegerWithBitSize
  40. {
  41. IntegerWithBitSize() noexcept = default;
  42. IntegerWithBitSize (const IntegerWithBitSize&) noexcept = default;
  43. IntegerWithBitSize& operator= (const IntegerWithBitSize&) noexcept = default;
  44. IntegerWithBitSize (uint32 v) noexcept : value (v)
  45. {
  46. static_assert (numBits <= 32, "numBits must be <= 32");
  47. jassert (v >= 0 && v <= maxValue);
  48. }
  49. enum
  50. {
  51. bits = numBits,
  52. maxValue = static_cast<uint32> ((1ULL << numBits) - 1ULL)
  53. };
  54. operator uint32() const noexcept { return value; }
  55. uint32 get() const noexcept { return value; }
  56. uint8 getScaledToByte() const noexcept
  57. {
  58. return (uint8) (numBits < 8 ? (uint32) (value << (8 - numBits))
  59. : (uint32) (value >> (numBits - 8)));
  60. }
  61. float toUnipolarFloat() const noexcept { return value / (float) maxValue; }
  62. float toBipolarFloat() const noexcept { return static_cast<int32> (value << (32 - numBits)) / (float) 0x80000000u; }
  63. static IntegerWithBitSize fromUnipolarFloat (float value) noexcept
  64. {
  65. static_assert (numBits <= 31, "numBits must be <= 31");
  66. return IntegerWithBitSize ((uint32) jlimit (0, (int) maxValue, (int) (value * maxValue)));
  67. }
  68. static IntegerWithBitSize fromBipolarFloat (float value) noexcept
  69. {
  70. static_assert (numBits <= 31, "numBits must be <= 31");
  71. return IntegerWithBitSize (maxValue & (uint32) jlimit ((int) -(maxValue / 2), (int) (maxValue / 2), (int) (value * (maxValue / 2))));
  72. }
  73. uint32 value = 0;
  74. };
  75. //==============================================================================
  76. /**
  77. This helper class allocates a block of 7-bit bytes and can push sequences of bits into it.
  78. @see Packed7BitArrayReader
  79. */
  80. template <int allocatedBytes>
  81. struct Packed7BitArrayBuilder
  82. {
  83. const void* getData() const noexcept { return data; }
  84. int size() const noexcept { return bytesWritten + (bitsInCurrentByte > 0 ? 1 : 0); }
  85. bool hasCapacity (int bitsNeeded) const noexcept
  86. {
  87. return ((bytesWritten + 2) * 7 + bitsInCurrentByte + bitsNeeded) <= allocatedBytes * 7;
  88. }
  89. void writeHeaderSysexBytes (uint8 deviceIndex) noexcept
  90. {
  91. jassert (bytesWritten + bitsInCurrentByte == 0);
  92. for (int i = 0; i < (int) sizeof (roliSysexHeader); ++i)
  93. data[bytesWritten++] = roliSysexHeader[i];
  94. jassert (deviceIndex < 128);
  95. data[bytesWritten++] = deviceIndex & 0x7f;
  96. }
  97. void writePacketSysexFooter() noexcept
  98. {
  99. if (bitsInCurrentByte != 0)
  100. {
  101. bitsInCurrentByte = 0;
  102. ++bytesWritten;
  103. }
  104. jassert (hasCapacity (0));
  105. uint32 headerBytes = (uint32) sizeof (roliSysexHeader) + 1;
  106. data[bytesWritten] = calculatePacketChecksum (data + headerBytes, (uint32) bytesWritten - headerBytes);
  107. ++bytesWritten;
  108. data[bytesWritten++] = 0xf7;
  109. }
  110. template <int numBits>
  111. Packed7BitArrayBuilder& operator<< (IntegerWithBitSize<numBits> value) noexcept
  112. {
  113. writeBits (value.value, numBits);
  114. return *this;
  115. }
  116. void writeBits (uint32 value, int numBits) noexcept
  117. {
  118. jassert (numBits <= 32);
  119. jassert (hasCapacity (numBits));
  120. jassert (numBits == 32 || (value >> numBits) == 0);
  121. while (numBits > 0)
  122. {
  123. if (bitsInCurrentByte == 0)
  124. {
  125. if (numBits < 7)
  126. {
  127. data[bytesWritten] = (uint8) value;
  128. bitsInCurrentByte = numBits;
  129. return;
  130. }
  131. if (numBits == 7)
  132. {
  133. data[bytesWritten++] = (uint8) value;
  134. return;
  135. }
  136. data[bytesWritten++] = (uint8) (value & 0x7f);
  137. value >>= 7;
  138. numBits -= 7;
  139. }
  140. else
  141. {
  142. const int bitsToDo = jmin (7 - bitsInCurrentByte, numBits);
  143. data[bytesWritten] |= ((value & ((1 << bitsToDo) - 1)) << bitsInCurrentByte);
  144. value >>= bitsToDo;
  145. numBits -= bitsToDo;
  146. bitsInCurrentByte += bitsToDo;
  147. if (bitsInCurrentByte == 7)
  148. {
  149. bitsInCurrentByte = 0;
  150. ++bytesWritten;
  151. }
  152. }
  153. }
  154. }
  155. struct State
  156. {
  157. int bytesWritten, bitsInCurrentByte;
  158. };
  159. State getState() const noexcept
  160. {
  161. return { bytesWritten, bitsInCurrentByte };
  162. }
  163. void restore (State state) noexcept
  164. {
  165. bytesWritten = state.bytesWritten;
  166. bitsInCurrentByte = state.bitsInCurrentByte;
  167. }
  168. private:
  169. uint8 data[allocatedBytes];
  170. int bytesWritten = 0, bitsInCurrentByte = 0;
  171. };
  172. //==============================================================================
  173. /**
  174. This helper class reads from a block of 7-bit bytes as sequences of bits.
  175. @see Packed7BitArrayBuilder
  176. */
  177. struct Packed7BitArrayReader
  178. {
  179. Packed7BitArrayReader (const void* sourceData, int numBytes) noexcept
  180. : data (static_cast<const uint8*> (sourceData)), totalBits (numBytes * 7)
  181. {
  182. }
  183. int getRemainingBits() const noexcept
  184. {
  185. return totalBits - bitsReadInCurrentByte;
  186. }
  187. template <typename Target>
  188. Target read() noexcept
  189. {
  190. return Target (readBits (Target::bits));
  191. }
  192. uint32 readBits (int numBits) noexcept
  193. {
  194. jassert (numBits <= 32);
  195. jassert (getRemainingBits() >= numBits);
  196. uint32 value = 0;
  197. int bitsSoFar = 0;
  198. while (numBits > 0)
  199. {
  200. const uint32 valueInCurrentByte = (*data >> bitsReadInCurrentByte);
  201. const int bitsAvailable = 7 - bitsReadInCurrentByte;
  202. if (bitsAvailable > numBits)
  203. {
  204. value |= ((valueInCurrentByte & ((1 << numBits) - 1)) << bitsSoFar);
  205. bitsReadInCurrentByte += numBits;
  206. break;
  207. }
  208. value |= (valueInCurrentByte << bitsSoFar);
  209. numBits -= bitsAvailable;
  210. bitsSoFar += bitsAvailable;
  211. bitsReadInCurrentByte = 0;
  212. ++data;
  213. totalBits -= 7;
  214. }
  215. return value;
  216. }
  217. static bool checksumIsOK (const uint8* data, uint32 size) noexcept
  218. {
  219. return size > 1 && calculatePacketChecksum (data, size - 1) == data[size - 1];
  220. }
  221. private:
  222. const uint8* data;
  223. int totalBits, bitsReadInCurrentByte = 0;
  224. };