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.

431 lines
13KB

  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 littlefoot
  18. {
  19. //==============================================================================
  20. /**
  21. This class manages the synchronisation of a remote block of heap memory used
  22. by a littlefoot program running on a block.
  23. Data in the block can be changed by calling setByte, setBytes, setBits etc, and
  24. these changes will be flushed to the device when sendChanges is called.
  25. @tags{Blocks}
  26. */
  27. template <typename ImplementationClass>
  28. struct LittleFootRemoteHeap
  29. {
  30. LittleFootRemoteHeap (uint32 blockSizeToUse) noexcept : blockSize (blockSizeToUse)
  31. {
  32. resetDeviceStateToUnknown();
  33. }
  34. void clear() noexcept
  35. {
  36. zeromem (targetData, sizeof (targetData));
  37. invalidateData();
  38. }
  39. void resetDeviceStateToUnknown()
  40. {
  41. invalidateData();
  42. messagesSent.clear();
  43. resetDataRangeToUnknown (0, ImplementationClass::maxBlockSize);
  44. }
  45. void resetDataRangeToUnknown (size_t offset, size_t size) noexcept
  46. {
  47. auto* latestState = getLatestExpectedDataState();
  48. for (size_t i = 0; i < size; ++i)
  49. latestState[offset + i] = unknownByte;
  50. }
  51. void setByte (size_t offset, uint8 value) noexcept
  52. {
  53. if (offset < blockSize)
  54. {
  55. if (targetData[offset] != value)
  56. {
  57. targetData[offset] = value;
  58. needsSyncing = true;
  59. if (programStateKnown && offset < programSize)
  60. programStateKnown = false;
  61. }
  62. }
  63. else
  64. {
  65. jassertfalse;
  66. }
  67. }
  68. void setBytes (size_t offset, const uint8* data, size_t num) noexcept
  69. {
  70. for (size_t i = 0; i < num; ++i)
  71. setByte (offset + i, data[i]);
  72. }
  73. void setBits (uint32 startBit, uint32 numBits, uint32 value) noexcept
  74. {
  75. if (startBit + numBits > 8 * blockSize)
  76. {
  77. jassertfalse;
  78. return;
  79. }
  80. if (readLittleEndianBitsInBuffer (targetData, startBit, numBits) != value)
  81. {
  82. writeLittleEndianBitsInBuffer (targetData, startBit, numBits, value);
  83. invalidateData();
  84. }
  85. }
  86. uint8 getByte (size_t offset) noexcept
  87. {
  88. if (offset < blockSize)
  89. return targetData [offset];
  90. jassertfalse;
  91. return 0;
  92. }
  93. void invalidateData() noexcept
  94. {
  95. needsSyncing = true;
  96. programStateKnown = false;
  97. }
  98. bool isFullySynced() const noexcept
  99. {
  100. return ! needsSyncing;
  101. }
  102. static bool isAllZero (const uint8* data, size_t size) noexcept
  103. {
  104. for (size_t i = 0; i < size; ++i)
  105. if (data[i] != 0)
  106. return false;
  107. return true;
  108. }
  109. void sendChanges (ImplementationClass& bi, bool forceSend)
  110. {
  111. if ((needsSyncing && messagesSent.isEmpty()) || forceSend)
  112. {
  113. for (int maxChanges = 30; --maxChanges >= 0;)
  114. {
  115. if (isAllZero (targetData, blockSize))
  116. break;
  117. uint16 data[ImplementationClass::maxBlockSize];
  118. auto* latestState = getLatestExpectedDataState();
  119. for (uint32 i = 0; i < blockSize; ++i)
  120. data[i] = latestState[i];
  121. uint32 packetIndex = messagesSent.isEmpty() ? lastPacketIndexReceived
  122. : messagesSent.getLast()->packetIndex;
  123. packetIndex = (packetIndex + 1) & ImplementationClass::maxPacketCounter;
  124. if (! Diff (data, targetData, blockSize).createChangeMessage (bi, data, messagesSent, packetIndex))
  125. break;
  126. dumpStatus();
  127. }
  128. }
  129. for (auto* m : messagesSent)
  130. {
  131. if (m->dispatchTime >= Time::getCurrentTime() - RelativeTime::milliseconds (250))
  132. break;
  133. m->dispatchTime = Time::getCurrentTime();
  134. bi.sendMessageToDevice (m->packet);
  135. //DBG ("Sending packet " << (int) m->packetIndex << " - " << m->packet.size() << " bytes, device " << bi.getDeviceIndex());
  136. if (getTotalSizeOfMessagesSent() > 200)
  137. break;
  138. }
  139. }
  140. void handleACKFromDevice (ImplementationClass& bi, uint32 packetIndex) noexcept
  141. {
  142. //DBG ("ACK " << (int) packetIndex << " device " << (int) bi.getDeviceIndex());
  143. if (lastPacketIndexReceived != packetIndex)
  144. {
  145. lastPacketIndexReceived = packetIndex;
  146. for (int i = messagesSent.size(); --i >= 0;)
  147. {
  148. auto& m = *messagesSent.getUnchecked(i);
  149. if (m.packetIndex == packetIndex)
  150. {
  151. for (uint32 j = 0; j < blockSize; ++j)
  152. deviceState[j] = m.resultDataState[j];
  153. programStateKnown = false;
  154. messagesSent.removeRange (0, i + 1);
  155. dumpStatus();
  156. sendChanges (bi, false);
  157. if (messagesSent.isEmpty())
  158. needsSyncing = false;
  159. return;
  160. }
  161. }
  162. resetDeviceStateToUnknown();
  163. }
  164. }
  165. bool isProgramLoaded() noexcept
  166. {
  167. if (! programStateKnown)
  168. {
  169. programStateKnown = true;
  170. uint8 deviceMemory[ImplementationClass::maxBlockSize];
  171. for (size_t i = 0; i < blockSize; ++i)
  172. deviceMemory[i] = (uint8) deviceState[i];
  173. littlefoot::Program prog (deviceMemory, (uint32) blockSize);
  174. programLoaded = prog.checksumMatches();
  175. programSize = prog.getProgramSize();
  176. }
  177. return programLoaded;
  178. }
  179. const size_t blockSize;
  180. static constexpr uint16 unknownByte = 0x100;
  181. private:
  182. uint16 deviceState[ImplementationClass::maxBlockSize] = { 0 };
  183. uint8 targetData[ImplementationClass::maxBlockSize] = { 0 };
  184. uint32 programSize = 0;
  185. bool needsSyncing = true, programStateKnown = true, programLoaded = false;
  186. uint16* getLatestExpectedDataState() noexcept
  187. {
  188. return messagesSent.isEmpty() ? deviceState
  189. : messagesSent.getLast()->resultDataState;
  190. }
  191. struct ChangeMessage
  192. {
  193. typename ImplementationClass::PacketBuilder packet;
  194. Time dispatchTime;
  195. uint32 packetIndex;
  196. uint16 resultDataState[ImplementationClass::maxBlockSize];
  197. };
  198. OwnedArray<ChangeMessage> messagesSent;
  199. uint32 lastPacketIndexReceived = 0;
  200. int getTotalSizeOfMessagesSent() const noexcept
  201. {
  202. int total = 0;
  203. for (auto* m : messagesSent)
  204. if (m->dispatchTime != Time())
  205. total += m->packet.size();
  206. return total;
  207. }
  208. void dumpStatus()
  209. {
  210. #if DUMP_LITTLEFOOT_HEAP_STATUS
  211. int differences = 0;
  212. constexpr int diffLen = 50;
  213. char areas[diffLen + 1] = { 0 };
  214. for (int i = 0; i < diffLen; ++i)
  215. areas[i] = '.';
  216. for (int i = 0; i < (int) blockSize; ++i)
  217. {
  218. if (targetData[i] != deviceState[i])
  219. {
  220. ++differences;
  221. areas[i * diffLen / (int) blockSize] = 'X';
  222. }
  223. }
  224. double proportionOK = ((int) blockSize - differences) / (double) blockSize;
  225. juce::ignoreUnused (proportionOK);
  226. DBG ("Heap: " << areas << " " << String (roundToInt (100 * proportionOK)) << "% "
  227. << (isProgramLoaded() ? "Ready" : "Loading"));
  228. #endif
  229. }
  230. struct Diff
  231. {
  232. Diff (uint16* current, const uint8* target, size_t blockSizeToUse)
  233. : newData (target), blockSize (blockSizeToUse)
  234. {
  235. ranges.ensureStorageAllocated ((int) blockSize);
  236. for (int i = 0; i < (int) blockSize; ++i)
  237. ranges.add ({ i, 1, newData[i] == current[i], false });
  238. coalesceUniformRegions();
  239. coalesceSequences();
  240. trim();
  241. }
  242. bool createChangeMessage (const ImplementationClass& bi,
  243. const uint16* currentState,
  244. OwnedArray<ChangeMessage>& messagesCreated,
  245. uint32 nextPacketIndex)
  246. {
  247. if (ranges.isEmpty())
  248. return false;
  249. auto deviceIndex = bi.getDeviceIndex();
  250. if (deviceIndex < 0)
  251. return false;
  252. auto& message = *messagesCreated.add (new ChangeMessage());
  253. message.packetIndex = nextPacketIndex;
  254. for (uint32 i = 0; i < blockSize; ++i)
  255. message.resultDataState[i] = currentState[i];
  256. auto& p = message.packet;
  257. p.writePacketSysexHeaderBytes ((uint8) deviceIndex);
  258. p.beginDataChanges (nextPacketIndex);
  259. uint8 lastValue = 0;
  260. bool packetOverflow = false;
  261. for (auto& r : ranges)
  262. {
  263. if (r.isSkipped)
  264. {
  265. packetOverflow = ! p.skipBytes (r.length);
  266. }
  267. else if (r.isMixed)
  268. {
  269. jassert (r.length > 1);
  270. packetOverflow = ! p.setMultipleBytes (newData + r.index, r.length);
  271. if (! packetOverflow)
  272. lastValue = newData[r.index + r.length - 1];
  273. }
  274. else
  275. {
  276. auto value = newData[r.index];
  277. packetOverflow = ! p.setMultipleBytes (value, lastValue, r.length);
  278. if (! packetOverflow)
  279. lastValue = value;
  280. }
  281. if (packetOverflow)
  282. break;
  283. if (! r.isSkipped)
  284. for (int i = r.index; i < r.index + r.length; ++i)
  285. message.resultDataState[i] = newData[i];
  286. }
  287. p.endDataChanges (! packetOverflow);
  288. p.writePacketSysexFooter();
  289. return packetOverflow;
  290. }
  291. private:
  292. struct ByteSequence
  293. {
  294. int index, length;
  295. bool isSkipped, isMixed;
  296. };
  297. const uint8* const newData;
  298. const size_t blockSize;
  299. Array<ByteSequence> ranges;
  300. void coalesceUniformRegions()
  301. {
  302. for (int i = ranges.size(); --i > 0;)
  303. {
  304. auto& r1 = ranges.getReference (i - 1);
  305. auto r2 = ranges.getReference (i);
  306. if (r1.isSkipped == r2.isSkipped
  307. && (r1.isSkipped || newData[r1.index] == newData[r2.index]))
  308. {
  309. r1.length += r2.length;
  310. ranges.remove (i);
  311. i = jmin (ranges.size() - 1, i + 1);
  312. }
  313. }
  314. }
  315. void coalesceSequences()
  316. {
  317. for (int i = ranges.size(); --i > 0;)
  318. {
  319. auto& r1 = ranges.getReference (i - 1);
  320. auto r2 = ranges.getReference (i);
  321. if (! (r1.isSkipped || r2.isSkipped)
  322. && (r1.isMixed || r1.length == 1)
  323. && (r2.isMixed || r2.length == 1))
  324. {
  325. if (r1.length + r2.length < 32)
  326. {
  327. r1.length += r2.length;
  328. r1.isMixed = true;
  329. ranges.remove (i);
  330. i = jmin (ranges.size() - 1, i + 1);
  331. }
  332. }
  333. }
  334. }
  335. void trim()
  336. {
  337. while (ranges.size() > 0 && ranges.getLast().isSkipped)
  338. ranges.removeLast();
  339. }
  340. };
  341. };
  342. }