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.

324 lines
12KB

  1. /*
  2. * Carla Bridge utils
  3. * Copyright (C) 2013-2017 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 kPluginBridgeNonRtClientPrepareForSave:
  105. return "kPluginBridgeNonRtClientPrepareForSave";
  106. case kPluginBridgeNonRtClientShowUI:
  107. return "kPluginBridgeNonRtClientShowUI";
  108. case kPluginBridgeNonRtClientHideUI:
  109. return "kPluginBridgeNonRtClientHideUI";
  110. case kPluginBridgeNonRtClientUiParameterChange:
  111. return "kPluginBridgeNonRtClientUiParameterChange";
  112. case kPluginBridgeNonRtClientUiProgramChange:
  113. return "kPluginBridgeNonRtClientUiProgramChange";
  114. case kPluginBridgeNonRtClientUiMidiProgramChange:
  115. return "kPluginBridgeNonRtClientUiMidiProgramChange";
  116. case kPluginBridgeNonRtClientUiNoteOn:
  117. return "kPluginBridgeNonRtClientUiNoteOn";
  118. case kPluginBridgeNonRtClientUiNoteOff:
  119. return "kPluginBridgeNonRtClientUiNoteOff";
  120. case kPluginBridgeNonRtClientQuit:
  121. return "kPluginBridgeNonRtClientQuit";
  122. }
  123. carla_stderr("CarlaBackend::PluginBridgeNonRtClientOpcode2str(%i) - invalid opcode", opcode);
  124. return nullptr;
  125. }
  126. static inline
  127. const char* PluginBridgeNonRtServerOpcode2str(const PluginBridgeNonRtServerOpcode opcode) noexcept
  128. {
  129. switch (opcode)
  130. {
  131. case kPluginBridgeNonRtServerNull:
  132. return "kPluginBridgeNonRtServerNull";
  133. case kPluginBridgeNonRtServerPong:
  134. return "kPluginBridgeNonRtServerPong";
  135. case kPluginBridgeNonRtServerPluginInfo1:
  136. return "kPluginBridgeNonRtServerPluginInfo1";
  137. case kPluginBridgeNonRtServerPluginInfo2:
  138. return "kPluginBridgeNonRtServerPluginInfo2";
  139. case kPluginBridgeNonRtServerAudioCount:
  140. return "kPluginBridgeNonRtServerAudioCount";
  141. case kPluginBridgeNonRtServerMidiCount:
  142. return "kPluginBridgeNonRtServerMidiCount";
  143. case kPluginBridgeNonRtServerCvCount:
  144. return "kPluginBridgeNonRtServerCvCount";
  145. case kPluginBridgeNonRtServerParameterCount:
  146. return "kPluginBridgeNonRtServerParameterCount";
  147. case kPluginBridgeNonRtServerProgramCount:
  148. return "kPluginBridgeNonRtServerProgramCount";
  149. case kPluginBridgeNonRtServerMidiProgramCount:
  150. return "kPluginBridgeNonRtServerMidiProgramCount";
  151. case kPluginBridgeNonRtServerPortName:
  152. return "kPluginBridgeNonRtServerPortName";
  153. case kPluginBridgeNonRtServerParameterData1:
  154. return "kPluginBridgeNonRtServerParameterData1";
  155. case kPluginBridgeNonRtServerParameterData2:
  156. return "kPluginBridgeNonRtServerParameterData2";
  157. case kPluginBridgeNonRtServerParameterRanges:
  158. return "kPluginBridgeNonRtServerParameterRanges";
  159. case kPluginBridgeNonRtServerParameterValue:
  160. return "kPluginBridgeNonRtServerParameterValue";
  161. case kPluginBridgeNonRtServerParameterValue2:
  162. return "kPluginBridgeNonRtServerParameterValue2";
  163. case kPluginBridgeNonRtServerDefaultValue:
  164. return "kPluginBridgeNonRtServerDefaultValue";
  165. case kPluginBridgeNonRtServerCurrentProgram:
  166. return "kPluginBridgeNonRtServerCurrentProgram";
  167. case kPluginBridgeNonRtServerCurrentMidiProgram:
  168. return "kPluginBridgeNonRtServerCurrentMidiProgram";
  169. case kPluginBridgeNonRtServerProgramName:
  170. return "kPluginBridgeNonRtServerProgramName";
  171. case kPluginBridgeNonRtServerMidiProgramData:
  172. return "kPluginBridgeNonRtServerMidiProgramData";
  173. case kPluginBridgeNonRtServerSetCustomData:
  174. return "kPluginBridgeNonRtServerSetCustomData";
  175. case kPluginBridgeNonRtServerSetChunkDataFile:
  176. return "kPluginBridgeNonRtServerSetChunkDataFile";
  177. case kPluginBridgeNonRtServerSetLatency:
  178. return "kPluginBridgeNonRtServerSetLatency";
  179. case kPluginBridgeNonRtServerReady:
  180. return "kPluginBridgeNonRtServerReady";
  181. case kPluginBridgeNonRtServerSaved:
  182. return "kPluginBridgeNonRtServerSaved";
  183. case kPluginBridgeNonRtServerUiClosed:
  184. return "kPluginBridgeNonRtServerUiClosed";
  185. case kPluginBridgeNonRtServerError:
  186. return "kPluginBridgeNonRtServerError";
  187. }
  188. carla_stderr("CarlaBackend::PluginBridgeNonRtServerOpcode2str%i) - invalid opcode", opcode);
  189. return nullptr;
  190. }
  191. // -------------------------------------------------------------------------------------------------------------------
  192. struct BridgeAudioPool {
  193. float* data;
  194. std::size_t dataSize;
  195. CarlaString filename;
  196. char shm[64];
  197. bool isServer;
  198. BridgeAudioPool() noexcept;
  199. ~BridgeAudioPool() noexcept;
  200. bool initializeServer() noexcept;
  201. bool attachClient(const char* const fname) noexcept;
  202. void clear() noexcept;
  203. void resize(const uint32_t bufferSize, const uint32_t audioPortCount, const uint32_t cvPortCount) noexcept;
  204. CARLA_DECLARE_NON_COPY_STRUCT(BridgeAudioPool)
  205. };
  206. // -------------------------------------------------------------------------------------------------------------------
  207. struct BridgeRtClientControl : public CarlaRingBufferControl<SmallStackBuffer> {
  208. BridgeRtClientData* data;
  209. CarlaString filename;
  210. bool needsSemDestroy; // client only
  211. char shm[64];
  212. bool isServer;
  213. BridgeRtClientControl() noexcept;
  214. ~BridgeRtClientControl() noexcept override;
  215. bool initializeServer() noexcept;
  216. bool attachClient(const char* const basename) noexcept;
  217. void clear() noexcept;
  218. bool mapData() noexcept;
  219. void unmapData() noexcept;
  220. // non-bridge, server
  221. bool waitForClient(const uint msecs) noexcept;
  222. void writeOpcode(const PluginBridgeRtClientOpcode opcode) noexcept;
  223. // bridge, client
  224. PluginBridgeRtClientOpcode readOpcode() noexcept;
  225. // helper class that automatically posts semaphore on destructor
  226. struct WaitHelper {
  227. BridgeRtClientData* const data;
  228. const bool ok;
  229. WaitHelper(BridgeRtClientControl& c) noexcept;
  230. ~WaitHelper() noexcept;
  231. CARLA_DECLARE_NON_COPY_STRUCT(WaitHelper)
  232. };
  233. CARLA_DECLARE_NON_COPY_STRUCT(BridgeRtClientControl)
  234. };
  235. // -------------------------------------------------------------------------------------------------------------------
  236. struct BridgeNonRtClientControl : public CarlaRingBufferControl<BigStackBuffer> {
  237. BridgeNonRtClientData* data;
  238. CarlaString filename;
  239. CarlaMutex mutex;
  240. char shm[64];
  241. bool isServer;
  242. BridgeNonRtClientControl() noexcept;
  243. ~BridgeNonRtClientControl() noexcept override;
  244. bool initializeServer() noexcept;
  245. bool attachClient(const char* const basename) noexcept;
  246. void clear() noexcept;
  247. bool mapData() noexcept;
  248. void unmapData() noexcept;
  249. // non-bridge, server
  250. void waitIfDataIsReachingLimit() noexcept;
  251. void writeOpcode(const PluginBridgeNonRtClientOpcode opcode) noexcept;
  252. // bridge, client
  253. PluginBridgeNonRtClientOpcode readOpcode() noexcept;
  254. CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtClientControl)
  255. };
  256. // -------------------------------------------------------------------------------------------------------------------
  257. struct BridgeNonRtServerControl : public CarlaRingBufferControl<HugeStackBuffer> {
  258. BridgeNonRtServerData* data;
  259. CarlaString filename;
  260. CarlaMutex mutex;
  261. char shm[64];
  262. bool isServer;
  263. BridgeNonRtServerControl() noexcept;
  264. ~BridgeNonRtServerControl() noexcept override;
  265. bool initializeServer() noexcept;
  266. bool attachClient(const char* const basename) noexcept;
  267. void clear() noexcept;
  268. bool mapData() noexcept;
  269. void unmapData() noexcept;
  270. // non-bridge, server
  271. PluginBridgeNonRtServerOpcode readOpcode() noexcept;
  272. // bridge, client
  273. void waitIfDataIsReachingLimit() noexcept;
  274. void writeOpcode(const PluginBridgeNonRtServerOpcode opcode) noexcept;
  275. CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtServerControl)
  276. };
  277. // -------------------------------------------------------------------------------------------------------------------
  278. #endif // CARLA_BRIDGE_UTILS_HPP_INCLUDED