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.

330 lines
13KB

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