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.

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