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.

332 lines
13KB

  1. /*
  2. * Carla Bridge utils
  3. * Copyright (C) 2013-2018 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 "CarlaString.hpp"
  22. #if defined(CARLA_OS_WIN) && defined(BUILDING_CARLA_FOR_WINDOWS)
  23. # define PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT "Global\\carla-bridge_shm_rtC_"
  24. # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "Global\\carla-bridge_shm_nonrtC_"
  25. # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "Global\\carla-bridge_shm_nonrtS_"
  26. #else
  27. # define PLUGIN_BRIDGE_NAMEPREFIX_RT_CLIENT "/crlbrdg_shm_rtC_"
  28. # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_CLIENT "/crlbrdg_shm_nonrtC_"
  29. # define PLUGIN_BRIDGE_NAMEPREFIX_NON_RT_SERVER "/crlbrdg_shm_nonrtS_"
  30. #endif
  31. // -------------------------------------------------------------------------------------------------------------------
  32. static inline
  33. const char* PluginBridgeRtClientOpcode2str(const PluginBridgeRtClientOpcode opcode) noexcept
  34. {
  35. switch (opcode)
  36. {
  37. case kPluginBridgeRtClientNull:
  38. return "kPluginBridgeRtClientNull";
  39. case kPluginBridgeRtClientSetAudioPool:
  40. return "kPluginBridgeRtClientSetAudioPool";
  41. case kPluginBridgeRtClientSetBufferSize:
  42. return "kPluginBridgeRtClientSetBufferSize";
  43. case kPluginBridgeRtClientSetSampleRate:
  44. return "kPluginBridgeRtClientSetSampleRate";
  45. case kPluginBridgeRtClientSetOnline:
  46. return "kPluginBridgeRtClientSetOnline";
  47. case kPluginBridgeRtClientControlEventParameter:
  48. return "kPluginBridgeRtClientControlEventParameter";
  49. case kPluginBridgeRtClientControlEventMidiBank:
  50. return "kPluginBridgeRtClientControlEventMidiBank";
  51. case kPluginBridgeRtClientControlEventMidiProgram:
  52. return "kPluginBridgeRtClientControlEventMidiProgram";
  53. case kPluginBridgeRtClientControlEventAllSoundOff:
  54. return "kPluginBridgeRtClientControlEventAllSoundOff";
  55. case kPluginBridgeRtClientControlEventAllNotesOff:
  56. return "kPluginBridgeRtClientControlEventAllNotesOff";
  57. case kPluginBridgeRtClientMidiEvent:
  58. return "kPluginBridgeRtClientMidiEvent";
  59. case kPluginBridgeRtClientProcess:
  60. return "kPluginBridgeRtClientProcess";
  61. case kPluginBridgeRtClientQuit:
  62. return "kPluginBridgeRtClientQuit";
  63. }
  64. carla_stderr("CarlaBackend::PluginBridgeRtClientOpcode2str(%i) - invalid opcode", opcode);
  65. return nullptr;
  66. }
  67. static inline
  68. const char* PluginBridgeNonRtClientOpcode2str(const PluginBridgeNonRtClientOpcode opcode) noexcept
  69. {
  70. switch (opcode)
  71. {
  72. case kPluginBridgeNonRtClientNull:
  73. return "kPluginBridgeNonRtClientNull";
  74. case kPluginBridgeNonRtClientVersion:
  75. return "kPluginBridgeNonRtClientVersion";
  76. case kPluginBridgeNonRtClientPing:
  77. return "kPluginBridgeNonRtClientPing";
  78. case kPluginBridgeNonRtClientPingOnOff:
  79. return "kPluginBridgeNonRtClientPingOnOff";
  80. case kPluginBridgeNonRtClientActivate:
  81. return "kPluginBridgeNonRtClientActivate";
  82. case kPluginBridgeNonRtClientDeactivate:
  83. return "kPluginBridgeNonRtClientDeactivate";
  84. case kPluginBridgeNonRtClientInitialSetup:
  85. return "kPluginBridgeNonRtClientInitialSetup";
  86. case kPluginBridgeNonRtClientSetParameterValue:
  87. return "kPluginBridgeNonRtClientSetParameterValue";
  88. case kPluginBridgeNonRtClientSetParameterMidiChannel:
  89. return "kPluginBridgeNonRtClientSetParameterMidiChannel";
  90. case kPluginBridgeNonRtClientSetParameterMidiCC:
  91. return "kPluginBridgeNonRtClientSetParameterMidiCC";
  92. case kPluginBridgeNonRtClientSetProgram:
  93. return "kPluginBridgeNonRtClientSetProgram";
  94. case kPluginBridgeNonRtClientSetMidiProgram:
  95. return "kPluginBridgeNonRtClientSetMidiProgram";
  96. case kPluginBridgeNonRtClientSetCustomData:
  97. return "kPluginBridgeNonRtClientSetCustomData";
  98. case kPluginBridgeNonRtClientSetChunkDataFile:
  99. return "kPluginBridgeNonRtClientSetChunkDataFile";
  100. case kPluginBridgeNonRtClientSetCtrlChannel:
  101. return "kPluginBridgeNonRtClientSetCtrlChannel";
  102. case kPluginBridgeNonRtClientSetOption:
  103. return "kPluginBridgeNonRtClientSetOption";
  104. case kPluginBridgeNonRtClientGetParameterText:
  105. return "kPluginBridgeNonRtClientGetParameterText";
  106. case kPluginBridgeNonRtClientPrepareForSave:
  107. return "kPluginBridgeNonRtClientPrepareForSave";
  108. case kPluginBridgeNonRtClientRestoreLV2State:
  109. return "kPluginBridgeNonRtClientRestoreLV2State";
  110. case kPluginBridgeNonRtClientShowUI:
  111. return "kPluginBridgeNonRtClientShowUI";
  112. case kPluginBridgeNonRtClientHideUI:
  113. return "kPluginBridgeNonRtClientHideUI";
  114. case kPluginBridgeNonRtClientUiParameterChange:
  115. return "kPluginBridgeNonRtClientUiParameterChange";
  116. case kPluginBridgeNonRtClientUiProgramChange:
  117. return "kPluginBridgeNonRtClientUiProgramChange";
  118. case kPluginBridgeNonRtClientUiMidiProgramChange:
  119. return "kPluginBridgeNonRtClientUiMidiProgramChange";
  120. case kPluginBridgeNonRtClientUiNoteOn:
  121. return "kPluginBridgeNonRtClientUiNoteOn";
  122. case kPluginBridgeNonRtClientUiNoteOff:
  123. return "kPluginBridgeNonRtClientUiNoteOff";
  124. case kPluginBridgeNonRtClientQuit:
  125. return "kPluginBridgeNonRtClientQuit";
  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 kPluginBridgeNonRtServerDefaultValue:
  168. return "kPluginBridgeNonRtServerDefaultValue";
  169. case kPluginBridgeNonRtServerCurrentProgram:
  170. return "kPluginBridgeNonRtServerCurrentProgram";
  171. case kPluginBridgeNonRtServerCurrentMidiProgram:
  172. return "kPluginBridgeNonRtServerCurrentMidiProgram";
  173. case kPluginBridgeNonRtServerProgramName:
  174. return "kPluginBridgeNonRtServerProgramName";
  175. case kPluginBridgeNonRtServerMidiProgramData:
  176. return "kPluginBridgeNonRtServerMidiProgramData";
  177. case kPluginBridgeNonRtServerSetCustomData:
  178. return "kPluginBridgeNonRtServerSetCustomData";
  179. case kPluginBridgeNonRtServerSetChunkDataFile:
  180. return "kPluginBridgeNonRtServerSetChunkDataFile";
  181. case kPluginBridgeNonRtServerSetLatency:
  182. return "kPluginBridgeNonRtServerSetLatency";
  183. case kPluginBridgeNonRtServerSetParameterText:
  184. return "kPluginBridgeNonRtServerSetParameterText";
  185. case kPluginBridgeNonRtServerReady:
  186. return "kPluginBridgeNonRtServerReady";
  187. case kPluginBridgeNonRtServerSaved:
  188. return "kPluginBridgeNonRtServerSaved";
  189. case kPluginBridgeNonRtServerUiClosed:
  190. return "kPluginBridgeNonRtServerUiClosed";
  191. case kPluginBridgeNonRtServerError:
  192. return "kPluginBridgeNonRtServerError";
  193. }
  194. carla_stderr("CarlaBackend::PluginBridgeNonRtServerOpcode2str%i) - invalid opcode", opcode);
  195. return nullptr;
  196. }
  197. // -------------------------------------------------------------------------------------------------------------------
  198. struct BridgeAudioPool {
  199. float* data;
  200. std::size_t dataSize;
  201. CarlaString filename;
  202. char shm[64];
  203. bool isServer;
  204. BridgeAudioPool() noexcept;
  205. ~BridgeAudioPool() noexcept;
  206. bool initializeServer() noexcept;
  207. bool attachClient(const char* const fname) noexcept;
  208. void clear() noexcept;
  209. void resize(const uint32_t bufferSize, const uint32_t audioPortCount, const uint32_t cvPortCount) noexcept;
  210. const char* getFilenameSuffix() const noexcept;
  211. CARLA_DECLARE_NON_COPY_STRUCT(BridgeAudioPool)
  212. };
  213. // -------------------------------------------------------------------------------------------------------------------
  214. struct BridgeRtClientControl : public CarlaRingBufferControl<SmallStackBuffer> {
  215. BridgeRtClientData* data;
  216. CarlaString filename;
  217. bool needsSemDestroy; // client only
  218. char shm[64];
  219. bool isServer;
  220. BridgeRtClientControl() noexcept;
  221. ~BridgeRtClientControl() noexcept override;
  222. bool initializeServer() noexcept;
  223. bool attachClient(const char* const basename) noexcept;
  224. void clear() noexcept;
  225. bool mapData() noexcept;
  226. void unmapData() noexcept;
  227. // non-bridge, server
  228. bool waitForClient(const uint msecs) noexcept;
  229. bool writeOpcode(const PluginBridgeRtClientOpcode opcode) noexcept;
  230. // bridge, client
  231. PluginBridgeRtClientOpcode readOpcode() noexcept;
  232. // helper class that automatically posts semaphore on destructor
  233. struct WaitHelper {
  234. BridgeRtClientData* const data;
  235. const bool ok;
  236. WaitHelper(BridgeRtClientControl& c) noexcept;
  237. ~WaitHelper() noexcept;
  238. CARLA_DECLARE_NON_COPY_STRUCT(WaitHelper)
  239. };
  240. CARLA_DECLARE_NON_COPY_STRUCT(BridgeRtClientControl)
  241. };
  242. // -------------------------------------------------------------------------------------------------------------------
  243. struct BridgeNonRtClientControl : public CarlaRingBufferControl<BigStackBuffer> {
  244. BridgeNonRtClientData* data;
  245. CarlaString filename;
  246. CarlaMutex mutex;
  247. char shm[64];
  248. bool isServer;
  249. BridgeNonRtClientControl() noexcept;
  250. ~BridgeNonRtClientControl() noexcept override;
  251. bool initializeServer() noexcept;
  252. bool attachClient(const char* const basename) noexcept;
  253. void clear() noexcept;
  254. bool mapData() noexcept;
  255. void unmapData() noexcept;
  256. // non-bridge, server
  257. void waitIfDataIsReachingLimit() noexcept;
  258. bool writeOpcode(const PluginBridgeNonRtClientOpcode opcode) noexcept;
  259. // bridge, client
  260. PluginBridgeNonRtClientOpcode readOpcode() noexcept;
  261. CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtClientControl)
  262. };
  263. // -------------------------------------------------------------------------------------------------------------------
  264. struct BridgeNonRtServerControl : public CarlaRingBufferControl<HugeStackBuffer> {
  265. BridgeNonRtServerData* data;
  266. CarlaString filename;
  267. CarlaMutex mutex;
  268. char shm[64];
  269. bool isServer;
  270. BridgeNonRtServerControl() noexcept;
  271. ~BridgeNonRtServerControl() noexcept override;
  272. bool initializeServer() noexcept;
  273. bool attachClient(const char* const basename) noexcept;
  274. void clear() noexcept;
  275. bool mapData() noexcept;
  276. void unmapData() noexcept;
  277. // non-bridge, server
  278. PluginBridgeNonRtServerOpcode readOpcode() noexcept;
  279. // bridge, client
  280. void waitIfDataIsReachingLimit() noexcept;
  281. bool writeOpcode(const PluginBridgeNonRtServerOpcode opcode) noexcept;
  282. CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtServerControl)
  283. };
  284. // -------------------------------------------------------------------------------------------------------------------
  285. #endif // CARLA_BRIDGE_UTILS_HPP_INCLUDED