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.

290 lines
8.6KB

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