Audio plugin host https://kx.studio/carla
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.

363 lines
14KB

  1. /*
  2. * Carla Bridge utils
  3. * Copyright (C) 2013-2022 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_BRIDGE_UTILS_HPP_INCLUDED
  18. #define CARLA_BRIDGE_UTILS_HPP_INCLUDED
  19. #include "CarlaBridgeDefines.hpp"
  20. #include "CarlaMutex.hpp"
  21. #include "CarlaRingBuffer.hpp"
  22. #include "CarlaString.hpp"
  23. // -------------------------------------------------------------------------------------------------------------------
  24. static inline
  25. const char* PluginBridgeRtClientOpcode2str(const PluginBridgeRtClientOpcode opcode) noexcept
  26. {
  27. switch (opcode)
  28. {
  29. case kPluginBridgeRtClientNull:
  30. return "kPluginBridgeRtClientNull";
  31. case kPluginBridgeRtClientSetAudioPool:
  32. return "kPluginBridgeRtClientSetAudioPool";
  33. case kPluginBridgeRtClientSetBufferSize:
  34. return "kPluginBridgeRtClientSetBufferSize";
  35. case kPluginBridgeRtClientSetSampleRate:
  36. return "kPluginBridgeRtClientSetSampleRate";
  37. case kPluginBridgeRtClientSetOnline:
  38. return "kPluginBridgeRtClientSetOnline";
  39. case kPluginBridgeRtClientControlEventParameter:
  40. return "kPluginBridgeRtClientControlEventParameter";
  41. case kPluginBridgeRtClientControlEventMidiBank:
  42. return "kPluginBridgeRtClientControlEventMidiBank";
  43. case kPluginBridgeRtClientControlEventMidiProgram:
  44. return "kPluginBridgeRtClientControlEventMidiProgram";
  45. case kPluginBridgeRtClientControlEventAllSoundOff:
  46. return "kPluginBridgeRtClientControlEventAllSoundOff";
  47. case kPluginBridgeRtClientControlEventAllNotesOff:
  48. return "kPluginBridgeRtClientControlEventAllNotesOff";
  49. case kPluginBridgeRtClientMidiEvent:
  50. return "kPluginBridgeRtClientMidiEvent";
  51. case kPluginBridgeRtClientProcess:
  52. return "kPluginBridgeRtClientProcess";
  53. case kPluginBridgeRtClientQuit:
  54. return "kPluginBridgeRtClientQuit";
  55. }
  56. carla_stderr("CarlaBackend::PluginBridgeRtClientOpcode2str(%i) - invalid opcode", opcode);
  57. return nullptr;
  58. }
  59. static inline
  60. const char* PluginBridgeNonRtClientOpcode2str(const PluginBridgeNonRtClientOpcode opcode) noexcept
  61. {
  62. switch (opcode)
  63. {
  64. case kPluginBridgeNonRtClientNull:
  65. return "kPluginBridgeNonRtClientNull";
  66. case kPluginBridgeNonRtClientVersion:
  67. return "kPluginBridgeNonRtClientVersion";
  68. case kPluginBridgeNonRtClientPing:
  69. return "kPluginBridgeNonRtClientPing";
  70. case kPluginBridgeNonRtClientPingOnOff:
  71. return "kPluginBridgeNonRtClientPingOnOff";
  72. case kPluginBridgeNonRtClientActivate:
  73. return "kPluginBridgeNonRtClientActivate";
  74. case kPluginBridgeNonRtClientDeactivate:
  75. return "kPluginBridgeNonRtClientDeactivate";
  76. case kPluginBridgeNonRtClientInitialSetup:
  77. return "kPluginBridgeNonRtClientInitialSetup";
  78. case kPluginBridgeNonRtClientSetParameterValue:
  79. return "kPluginBridgeNonRtClientSetParameterValue";
  80. case kPluginBridgeNonRtClientSetParameterMidiChannel:
  81. return "kPluginBridgeNonRtClientSetParameterMidiChannel";
  82. case kPluginBridgeNonRtClientSetParameterMappedControlIndex:
  83. return "kPluginBridgeNonRtClientSetParameterMappedControlIndex";
  84. case kPluginBridgeNonRtClientSetProgram:
  85. return "kPluginBridgeNonRtClientSetProgram";
  86. case kPluginBridgeNonRtClientSetMidiProgram:
  87. return "kPluginBridgeNonRtClientSetMidiProgram";
  88. case kPluginBridgeNonRtClientSetCustomData:
  89. return "kPluginBridgeNonRtClientSetCustomData";
  90. case kPluginBridgeNonRtClientSetChunkDataFile:
  91. return "kPluginBridgeNonRtClientSetChunkDataFile";
  92. case kPluginBridgeNonRtClientSetCtrlChannel:
  93. return "kPluginBridgeNonRtClientSetCtrlChannel";
  94. case kPluginBridgeNonRtClientSetOption:
  95. return "kPluginBridgeNonRtClientSetOption";
  96. case kPluginBridgeNonRtClientGetParameterText:
  97. return "kPluginBridgeNonRtClientGetParameterText";
  98. case kPluginBridgeNonRtClientPrepareForSave:
  99. return "kPluginBridgeNonRtClientPrepareForSave";
  100. case kPluginBridgeNonRtClientRestoreLV2State:
  101. return "kPluginBridgeNonRtClientRestoreLV2State";
  102. case kPluginBridgeNonRtClientShowUI:
  103. return "kPluginBridgeNonRtClientShowUI";
  104. case kPluginBridgeNonRtClientHideUI:
  105. return "kPluginBridgeNonRtClientHideUI";
  106. case kPluginBridgeNonRtClientUiParameterChange:
  107. return "kPluginBridgeNonRtClientUiParameterChange";
  108. case kPluginBridgeNonRtClientUiProgramChange:
  109. return "kPluginBridgeNonRtClientUiProgramChange";
  110. case kPluginBridgeNonRtClientUiMidiProgramChange:
  111. return "kPluginBridgeNonRtClientUiMidiProgramChange";
  112. case kPluginBridgeNonRtClientUiNoteOn:
  113. return "kPluginBridgeNonRtClientUiNoteOn";
  114. case kPluginBridgeNonRtClientUiNoteOff:
  115. return "kPluginBridgeNonRtClientUiNoteOff";
  116. case kPluginBridgeNonRtClientQuit:
  117. return "kPluginBridgeNonRtClientQuit";
  118. case kPluginBridgeNonRtClientSetParameterMappedRange:
  119. return "kPluginBridgeNonRtClientSetParameterMappedRange";
  120. case kPluginBridgeNonRtClientSetOptions:
  121. return "kPluginBridgeNonRtClientSetOptions";
  122. case kPluginBridgeNonRtClientSetWindowTitle:
  123. return "kPluginBridgeNonRtClientSetWindowTitle";
  124. case kPluginBridgeNonRtClientEmbedUI:
  125. return "kPluginBridgeNonRtClientEmbedUI";
  126. }
  127. carla_stderr("CarlaBackend::PluginBridgeNonRtClientOpcode2str(%i) - invalid opcode", opcode);
  128. return nullptr;
  129. }
  130. static inline
  131. const char* PluginBridgeNonRtServerOpcode2str(const PluginBridgeNonRtServerOpcode opcode) noexcept
  132. {
  133. switch (opcode)
  134. {
  135. case kPluginBridgeNonRtServerNull:
  136. return "kPluginBridgeNonRtServerNull";
  137. case kPluginBridgeNonRtServerPong:
  138. return "kPluginBridgeNonRtServerPong";
  139. case kPluginBridgeNonRtServerPluginInfo1:
  140. return "kPluginBridgeNonRtServerPluginInfo1";
  141. case kPluginBridgeNonRtServerPluginInfo2:
  142. return "kPluginBridgeNonRtServerPluginInfo2";
  143. case kPluginBridgeNonRtServerAudioCount:
  144. return "kPluginBridgeNonRtServerAudioCount";
  145. case kPluginBridgeNonRtServerMidiCount:
  146. return "kPluginBridgeNonRtServerMidiCount";
  147. case kPluginBridgeNonRtServerCvCount:
  148. return "kPluginBridgeNonRtServerCvCount";
  149. case kPluginBridgeNonRtServerParameterCount:
  150. return "kPluginBridgeNonRtServerParameterCount";
  151. case kPluginBridgeNonRtServerProgramCount:
  152. return "kPluginBridgeNonRtServerProgramCount";
  153. case kPluginBridgeNonRtServerMidiProgramCount:
  154. return "kPluginBridgeNonRtServerMidiProgramCount";
  155. case kPluginBridgeNonRtServerPortName:
  156. return "kPluginBridgeNonRtServerPortName";
  157. case kPluginBridgeNonRtServerParameterData1:
  158. return "kPluginBridgeNonRtServerParameterData1";
  159. case kPluginBridgeNonRtServerParameterData2:
  160. return "kPluginBridgeNonRtServerParameterData2";
  161. case kPluginBridgeNonRtServerParameterRanges:
  162. return "kPluginBridgeNonRtServerParameterRanges";
  163. case kPluginBridgeNonRtServerParameterValue:
  164. return "kPluginBridgeNonRtServerParameterValue";
  165. case kPluginBridgeNonRtServerParameterValue2:
  166. return "kPluginBridgeNonRtServerParameterValue2";
  167. case kPluginBridgeNonRtServerParameterTouch:
  168. return "kPluginBridgeNonRtServerParameterTouch";
  169. case kPluginBridgeNonRtServerDefaultValue:
  170. return "kPluginBridgeNonRtServerDefaultValue";
  171. case kPluginBridgeNonRtServerCurrentProgram:
  172. return "kPluginBridgeNonRtServerCurrentProgram";
  173. case kPluginBridgeNonRtServerCurrentMidiProgram:
  174. return "kPluginBridgeNonRtServerCurrentMidiProgram";
  175. case kPluginBridgeNonRtServerProgramName:
  176. return "kPluginBridgeNonRtServerProgramName";
  177. case kPluginBridgeNonRtServerMidiProgramData:
  178. return "kPluginBridgeNonRtServerMidiProgramData";
  179. case kPluginBridgeNonRtServerSetCustomData:
  180. return "kPluginBridgeNonRtServerSetCustomData";
  181. case kPluginBridgeNonRtServerSetChunkDataFile:
  182. return "kPluginBridgeNonRtServerSetChunkDataFile";
  183. case kPluginBridgeNonRtServerSetLatency:
  184. return "kPluginBridgeNonRtServerSetLatency";
  185. case kPluginBridgeNonRtServerSetParameterText:
  186. return "kPluginBridgeNonRtServerSetParameterText";
  187. case kPluginBridgeNonRtServerReady:
  188. return "kPluginBridgeNonRtServerReady";
  189. case kPluginBridgeNonRtServerSaved:
  190. return "kPluginBridgeNonRtServerSaved";
  191. case kPluginBridgeNonRtServerUiClosed:
  192. return "kPluginBridgeNonRtServerUiClosed";
  193. case kPluginBridgeNonRtServerError:
  194. return "kPluginBridgeNonRtServerError";
  195. case kPluginBridgeNonRtServerVersion:
  196. return "kPluginBridgeNonRtServerVersion";
  197. case kPluginBridgeNonRtServerRespEmbedUI:
  198. return "kPluginBridgeNonRtServerRespEmbedUI";
  199. case kPluginBridgeNonRtServerResizeEmbedUI:
  200. return "kPluginBridgeNonRtServerResizeEmbedUI";
  201. }
  202. carla_stderr("CarlaBackend::PluginBridgeNonRtServerOpcode2str%i) - invalid opcode", opcode);
  203. return nullptr;
  204. }
  205. // -------------------------------------------------------------------------------------------------------------------
  206. static const std::size_t kBridgeRtClientDataMidiOutSize = 511*4;
  207. static const std::size_t kBridgeBaseMidiOutHeaderSize = 6U /* time, port and size */;
  208. // Server => Client RT
  209. struct BridgeRtClientData {
  210. BridgeSemaphore sem;
  211. BridgeTimeInfo timeInfo;
  212. SmallStackBuffer ringBuffer;
  213. uint8_t midiOut[kBridgeRtClientDataMidiOutSize];
  214. uint32_t procFlags;
  215. };
  216. // Server => Client Non-RT
  217. struct BridgeNonRtClientData {
  218. BigStackBuffer ringBuffer;
  219. };
  220. // Client => Server Non-RT
  221. struct BridgeNonRtServerData {
  222. HugeStackBuffer ringBuffer;
  223. };
  224. // -------------------------------------------------------------------------------------------------------------------
  225. struct BridgeAudioPool {
  226. float* data;
  227. std::size_t dataSize;
  228. CarlaString filename;
  229. char shm[64];
  230. bool isServer;
  231. BridgeAudioPool() noexcept;
  232. ~BridgeAudioPool() noexcept;
  233. bool initializeServer() noexcept;
  234. bool attachClient(const char* const fname) noexcept;
  235. void clear() noexcept;
  236. void resize(const uint32_t bufferSize, const uint32_t audioPortCount, const uint32_t cvPortCount) noexcept;
  237. const char* getFilenameSuffix() const noexcept;
  238. CARLA_DECLARE_NON_COPYABLE(BridgeAudioPool)
  239. };
  240. // -------------------------------------------------------------------------------------------------------------------
  241. struct BridgeRtClientControl : public CarlaRingBufferControl<SmallStackBuffer> {
  242. BridgeRtClientData* data;
  243. CarlaString filename;
  244. bool needsSemDestroy; // client only
  245. char shm[64];
  246. bool isServer;
  247. BridgeRtClientControl() noexcept;
  248. ~BridgeRtClientControl() noexcept override;
  249. bool initializeServer() noexcept;
  250. bool attachClient(const char* const basename) noexcept;
  251. void clear() noexcept;
  252. bool mapData() noexcept;
  253. void unmapData() noexcept;
  254. // non-bridge, server
  255. bool waitForClient(const uint msecs) noexcept;
  256. bool writeOpcode(const PluginBridgeRtClientOpcode opcode) noexcept;
  257. // bridge, client
  258. PluginBridgeRtClientOpcode readOpcode() noexcept;
  259. // helper class that automatically posts semaphore on destructor
  260. struct WaitHelper {
  261. BridgeRtClientData* const data;
  262. const bool ok;
  263. WaitHelper(BridgeRtClientControl& c) noexcept;
  264. ~WaitHelper() noexcept;
  265. CARLA_DECLARE_NON_COPYABLE(WaitHelper)
  266. };
  267. CARLA_DECLARE_NON_COPYABLE(BridgeRtClientControl)
  268. };
  269. // -------------------------------------------------------------------------------------------------------------------
  270. struct BridgeNonRtClientControl : public CarlaRingBufferControl<BigStackBuffer> {
  271. BridgeNonRtClientData* data;
  272. CarlaString filename;
  273. CarlaMutex mutex;
  274. char shm[64];
  275. bool isServer;
  276. BridgeNonRtClientControl() noexcept;
  277. ~BridgeNonRtClientControl() noexcept override;
  278. bool initializeServer() noexcept;
  279. bool attachClient(const char* const basename) noexcept;
  280. void clear() noexcept;
  281. bool mapData() noexcept;
  282. void unmapData() noexcept;
  283. // non-bridge, server
  284. void waitIfDataIsReachingLimit() noexcept;
  285. bool writeOpcode(const PluginBridgeNonRtClientOpcode opcode) noexcept;
  286. // bridge, client
  287. PluginBridgeNonRtClientOpcode readOpcode() noexcept;
  288. CARLA_DECLARE_NON_COPYABLE(BridgeNonRtClientControl)
  289. };
  290. // -------------------------------------------------------------------------------------------------------------------
  291. struct BridgeNonRtServerControl : public CarlaRingBufferControl<HugeStackBuffer> {
  292. BridgeNonRtServerData* data;
  293. CarlaString filename;
  294. CarlaMutex mutex;
  295. char shm[64];
  296. bool isServer;
  297. BridgeNonRtServerControl() noexcept;
  298. ~BridgeNonRtServerControl() noexcept override;
  299. bool initializeServer() noexcept;
  300. bool attachClient(const char* const basename) noexcept;
  301. void clear() noexcept;
  302. bool mapData() noexcept;
  303. void unmapData() noexcept;
  304. // non-bridge, server
  305. PluginBridgeNonRtServerOpcode readOpcode() noexcept;
  306. // bridge, client
  307. void waitIfDataIsReachingLimit() noexcept;
  308. bool writeOpcode(const PluginBridgeNonRtServerOpcode opcode) noexcept;
  309. CARLA_DECLARE_NON_COPYABLE(BridgeNonRtServerControl)
  310. };
  311. // -------------------------------------------------------------------------------------------------------------------
  312. #endif // CARLA_BRIDGE_UTILS_HPP_INCLUDED