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.

367 lines
20KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - 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. namespace juce
  24. {
  25. // This file provides interfaces for managing the internal configuration of Blocks
  26. // and synchronises with the connected Block
  27. using namespace BlocksProtocol;
  28. using ConfigType = Block::ConfigMetaData::ConfigType;
  29. /** Manages the configuration of blocks
  30. @tags{Blocks}
  31. */
  32. struct BlockConfigManager
  33. {
  34. /** Structure describing a configuration */
  35. struct ConfigDescription
  36. {
  37. ConfigItemId item;
  38. int32 value;
  39. int32 min;
  40. int32 max;
  41. bool isActive;
  42. const char* name;
  43. ConfigType type;
  44. const char* optionNames[configMaxOptions];
  45. const char* group;
  46. static_assert (configMaxOptions == Block::ConfigMetaData::numOptionNames, "Config options size and config metadata size should be the same");
  47. Block::ConfigMetaData toConfigMetaData() const
  48. {
  49. return Block::ConfigMetaData ((uint32) item, value, { min, max }, isActive, name, type, (const char**) optionNames, group);
  50. }
  51. };
  52. BlockConfigManager (Array<ConfigDescription> defaultConfig)
  53. {
  54. for (auto c : defaultConfig)
  55. {
  56. uint32 itemIndex;
  57. if (getIndexForItem (c.item, itemIndex))
  58. configList[itemIndex] = c;
  59. }
  60. }
  61. void setDeviceIndex (TopologyIndex newDeviceIndex) { deviceIndex = newDeviceIndex; }
  62. void setDeviceComms (PhysicalTopologySource::DeviceConnection* newConn) { deviceConnection = newConn; }
  63. static constexpr uint32 numConfigItems = 69;
  64. static constexpr const char* midiSettingsGroup = "MIDI Settings";
  65. static constexpr const char* pitchGroup = "Pitch";
  66. static constexpr const char* playGroup = "Play mode";
  67. static constexpr const char* sensitivityGroup = "Sensitivity";
  68. static constexpr const char* rhythmGroup = "Rhythm";
  69. static constexpr const char* coloursGroup = "Colors";
  70. ConfigDescription configList[numConfigItems] =
  71. {
  72. { midiStartChannel, 2, 1, 16, false, "MIDI Start Channel", ConfigType::integer, {}, midiSettingsGroup },
  73. { midiEndChannel, 16, 1, 16, false, "MIDI End Channel", ConfigType::integer, {}, midiSettingsGroup },
  74. { midiUseMPE, 1, 0, 2, false, "MIDI Mode", ConfigType::options, { "Multi Channel",
  75. "MPE",
  76. "Single Channel" }, midiSettingsGroup },
  77. { pitchBendRange, 48, 1, 96, false, "Pitch Bend Range", ConfigType::integer, {}, midiSettingsGroup },
  78. { midiChannelRange, 15, 1, 15, false, "No. MIDI Channels", ConfigType::integer, {}, midiSettingsGroup },
  79. { MPEZone, 0, 0, 1, false, "MPE Zone", ConfigType::options, { "Lower Zone",
  80. "Upper Zone"}, midiSettingsGroup },
  81. { octave, 0, -4, 6, false, "Octave", ConfigType::integer, {}, pitchGroup },
  82. { transpose, 0, -11, 11, false, "Transpose", ConfigType::integer, {}, pitchGroup },
  83. { slideCC, 74, 0, 127, false, "Slide CC", ConfigType::integer, {}, playGroup },
  84. { slideMode, 0, 0, 2, false, "Slide Mode", ConfigType::options, { "Absolute",
  85. "Relative Unipolar",
  86. "Relative Bipolar" }, playGroup },
  87. { velocitySensitivity, 100, 0, 127, false, "Strike Sensitivity", ConfigType::integer, {}, sensitivityGroup },
  88. { glideSensitivity, 100, 0, 127, false, "Glide Sensitivity", ConfigType::integer, {}, sensitivityGroup },
  89. { slideSensitivity, 100, 0, 127, false, "Slide Sensitivity", ConfigType::integer, {}, sensitivityGroup },
  90. { pressureSensitivity, 100, 0, 127, false, "Pressure Sensitivity", ConfigType::integer, {}, sensitivityGroup },
  91. { liftSensitivity, 100, 0, 127, false, "Lift Sensitivity", ConfigType::integer, {}, sensitivityGroup },
  92. { fixedVelocity, 0, 0, 1, false, "Fixed Velocity", ConfigType::boolean, {}, sensitivityGroup },
  93. { fixedVelocityValue, 127, 1, 127, false, "Fixed Velocity Value", ConfigType::integer, {}, sensitivityGroup },
  94. { pianoMode, 0, 0, 1, false, "Piano Mode", ConfigType::boolean, {}, playGroup },
  95. { glideLock, 0, 0, 127, false, "Glide Rate", ConfigType::integer, {}, playGroup },
  96. { glideLockEnable, 0, 0, 1, false, "Glide Lock Enable", ConfigType::boolean, {}, playGroup },
  97. { mode, 4, 1, 5, false, "Mode", ConfigType::integer, {}, playGroup },
  98. { volume, 100, 0, 127, false, "Volume", ConfigType::integer, {}, playGroup },
  99. { scale, 0, 0, 18, false, "Scale", ConfigType::integer, {}, playGroup }, // NOTE: Should be options
  100. { hideMode, 0, 0, 1, false, "Hide Mode", ConfigType::boolean, {}, playGroup },
  101. { chord, 0, 0, 127, false, "Chord", ConfigType::integer, {}, playGroup }, // NOTE: Should be options
  102. { arpPattern, 0, 0, 127, false, "Arp Pattern", ConfigType::integer, {}, playGroup },
  103. { tempo, 120, 1, 300, false, "Tempo", ConfigType::integer, {}, rhythmGroup },
  104. { key, 0, 0, 11, false, "Key", ConfigType::options, { "C", "C#", "D", "D#",
  105. "E", "F", "F#", "G",
  106. "G#", "A", "A#", "B"}, playGroup },
  107. { autoTransposeToKey, 0, 0, 1, false, "Auto Transpose To Key",ConfigType::boolean, {}, pitchGroup },
  108. { xTrackingMode, 1, 1, 4, false, "Glide Tracking Mode", ConfigType::options, { "Multi-Channel",
  109. "Last Played",
  110. "Highest",
  111. "Lowest",
  112. "Disabled" }, playGroup },
  113. { yTrackingMode, 1, 1, 4, false, "Slide Tracking Mode", ConfigType::options, { "Multi-Channel",
  114. "Last Played",
  115. "Highest",
  116. "Lowest",
  117. "Disabled" }, playGroup },
  118. { zTrackingMode, 1, 0, 4, false, "Pressure Tracking Mode", ConfigType::options, { "Poly Aftertouch",
  119. "Last Played",
  120. "Highest",
  121. "Lowest",
  122. "Disabled",
  123. "Hardest" }, playGroup },
  124. { gammaCorrection, 0, 0, 1, false, "Gamma Correction", ConfigType::boolean, {}, coloursGroup },
  125. { globalKeyColour, INT32_MIN, INT32_MIN, INT32_MAX, false, "Global Key Color", ConfigType::colour, {}, coloursGroup },
  126. { rootKeyColour, INT32_MIN, INT32_MIN, INT32_MAX, false, "Root Key Color" , ConfigType::colour, {}, coloursGroup },
  127. { brightness, 100, 0, 100, false, "Brightness", ConfigType::integer, {}, coloursGroup },
  128. // These can be defined for unique usage for a given Littlefoot script
  129. { user0, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  130. { user1, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  131. { user2, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  132. { user3, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  133. { user4, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  134. { user5, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  135. { user6, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  136. { user7, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  137. { user8, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  138. { user9, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  139. { user10, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  140. { user11, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  141. { user12, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  142. { user13, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  143. { user14, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  144. { user15, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  145. { user16, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  146. { user17, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  147. { user18, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  148. { user19, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  149. { user20, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  150. { user21, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  151. { user22, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  152. { user23, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  153. { user24, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  154. { user25, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  155. { user26, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  156. { user27, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  157. { user28, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  158. { user29, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  159. { user30, 0, 0, 127, false, {}, ConfigType::integer, {}, {} },
  160. { user31, 0, 0, 127, false, {}, ConfigType::integer, {}, {} }
  161. };
  162. //==============================================================================
  163. int32 getItemValue (ConfigItemId item)
  164. {
  165. uint32 itemIndex;
  166. if (getIndexForItem (item, itemIndex))
  167. return configList[itemIndex].value;
  168. return 0;
  169. }
  170. void setItemValue (ConfigItemId item, int32 value)
  171. {
  172. uint32 itemIndex;
  173. if (getIndexForItem (item, itemIndex))
  174. configList[itemIndex].value = value;
  175. setBlockConfig (item, value);
  176. }
  177. int32 getItemMin (ConfigItemId item)
  178. {
  179. uint32 itemIndex;
  180. if (getIndexForItem (item, itemIndex))
  181. return configList[itemIndex].min;
  182. return 0;
  183. }
  184. void setItemMin (ConfigItemId item, int32 min)
  185. {
  186. uint32 itemIndex;
  187. if (getIndexForItem (item, itemIndex))
  188. configList[itemIndex].min = min;
  189. }
  190. int32 getItemMax (ConfigItemId item)
  191. {
  192. uint32 itemIndex;
  193. if (getIndexForItem (item, itemIndex))
  194. return configList[itemIndex].max;
  195. return 0;
  196. }
  197. void setItemMax (ConfigItemId item, int32 max)
  198. {
  199. uint32 itemIndex;
  200. if (getIndexForItem (item, itemIndex))
  201. configList[itemIndex].max = max;
  202. // Send updateConfig message to Block
  203. }
  204. bool getItemActive (ConfigItemId item)
  205. {
  206. uint32 itemIndex;
  207. if (getIndexForItem (item, itemIndex))
  208. return configList[itemIndex].isActive;
  209. return false;
  210. }
  211. void setItemActive (ConfigItemId item, bool isActive)
  212. {
  213. uint32 itemIndex;
  214. if (getIndexForItem (item, itemIndex))
  215. configList[itemIndex].isActive = isActive;
  216. // Send setConfigState message to Block
  217. }
  218. String getOptionName (ConfigItemId item, uint8 optionIndex)
  219. {
  220. uint32 itemIndex;
  221. if (getIndexForItem (item, itemIndex) && optionIndex < configMaxOptions)
  222. return configList[itemIndex].optionNames[optionIndex];
  223. return {};
  224. }
  225. Block::ConfigMetaData getMetaData (ConfigItemId item)
  226. {
  227. uint32 itemIndex;
  228. if (getIndexForItem (item, itemIndex))
  229. return configList[itemIndex].toConfigMetaData();
  230. return {};
  231. }
  232. void resetConfigListActiveStatus()
  233. {
  234. for (auto& i : configList)
  235. i.isActive = false;
  236. }
  237. //==============================================================================
  238. // Set Block Configuration
  239. void setBlockConfig (ConfigItemId item, int32 value)
  240. {
  241. buildAndSendPacket ([item, value] (HostPacketBuilder<32>& p) { p.addConfigSetMessage (item, value); });
  242. }
  243. void requestBlockConfig (ConfigItemId item)
  244. {
  245. buildAndSendPacket ([item] (HostPacketBuilder<32>& p) { p.addRequestMessage (item); });
  246. }
  247. void requestFactoryConfigSync()
  248. {
  249. buildAndSendPacket ([] (HostPacketBuilder<32>& p) { p.addRequestFactorySyncMessage(); });
  250. }
  251. void requestUserConfigSync()
  252. {
  253. buildAndSendPacket ([] (HostPacketBuilder<32>& p) { p.addRequestUserSyncMessage(); });
  254. }
  255. void handleConfigUpdateMessage (int32 item, int32 value, int32 min, int32 max)
  256. {
  257. uint32 index;
  258. if (getIndexForItem ((ConfigItemId) item, index))
  259. {
  260. configList[index].value = value;
  261. configList[index].min = min;
  262. configList[index].max = max;
  263. configList[index].isActive = true;
  264. }
  265. }
  266. void handleConfigSetMessage(int32 item, int32 value)
  267. {
  268. uint32 index;
  269. if (getIndexForItem ((ConfigItemId) item, index))
  270. configList[index].value = value;
  271. }
  272. private:
  273. bool getIndexForItem (ConfigItemId item, uint32& index)
  274. {
  275. for (uint32 i = 0; i < numConfigItems; ++i)
  276. {
  277. if (configList[i].item == item)
  278. {
  279. index = i;
  280. return true;
  281. }
  282. }
  283. return false;
  284. }
  285. template<typename PacketBuildFn>
  286. void buildAndSendPacket (PacketBuildFn buildFn)
  287. {
  288. if (deviceConnection == nullptr)
  289. return;
  290. HostPacketBuilder<32> packet;
  291. packet.writePacketSysexHeaderBytes (deviceIndex);
  292. buildFn (packet);
  293. packet.writePacketSysexFooter();
  294. deviceConnection->sendMessageToDevice (packet.getData(), (size_t) packet.size());
  295. }
  296. TopologyIndex deviceIndex {};
  297. PhysicalTopologySource::DeviceConnection* deviceConnection {};
  298. };
  299. } // namespace juce