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.

280 lines
8.5KB

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