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.

CarlaBridgeUtils.hpp 12KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. const char* getFilenameSuffix() const noexcept;
  205. CARLA_DECLARE_NON_COPY_STRUCT(BridgeAudioPool)
  206. };
  207. // -------------------------------------------------------------------------------------------------------------------
  208. struct BridgeRtClientControl : public CarlaRingBufferControl<SmallStackBuffer> {
  209. BridgeRtClientData* data;
  210. CarlaString filename;
  211. bool needsSemDestroy; // client only
  212. char shm[64];
  213. bool isServer;
  214. BridgeRtClientControl() noexcept;
  215. ~BridgeRtClientControl() noexcept override;
  216. bool initializeServer() noexcept;
  217. bool attachClient(const char* const basename) noexcept;
  218. void clear() noexcept;
  219. bool mapData() noexcept;
  220. void unmapData() noexcept;
  221. // non-bridge, server
  222. bool waitForClient(const uint msecs) noexcept;
  223. void writeOpcode(const PluginBridgeRtClientOpcode opcode) noexcept;
  224. // bridge, client
  225. PluginBridgeRtClientOpcode readOpcode() noexcept;
  226. // helper class that automatically posts semaphore on destructor
  227. struct WaitHelper {
  228. BridgeRtClientData* const data;
  229. const bool ok;
  230. WaitHelper(BridgeRtClientControl& c) noexcept;
  231. ~WaitHelper() noexcept;
  232. CARLA_DECLARE_NON_COPY_STRUCT(WaitHelper)
  233. };
  234. CARLA_DECLARE_NON_COPY_STRUCT(BridgeRtClientControl)
  235. };
  236. // -------------------------------------------------------------------------------------------------------------------
  237. struct BridgeNonRtClientControl : public CarlaRingBufferControl<BigStackBuffer> {
  238. BridgeNonRtClientData* data;
  239. CarlaString filename;
  240. CarlaMutex mutex;
  241. char shm[64];
  242. bool isServer;
  243. BridgeNonRtClientControl() noexcept;
  244. ~BridgeNonRtClientControl() noexcept override;
  245. bool initializeServer() noexcept;
  246. bool attachClient(const char* const basename) noexcept;
  247. void clear() noexcept;
  248. bool mapData() noexcept;
  249. void unmapData() noexcept;
  250. // non-bridge, server
  251. void waitIfDataIsReachingLimit() noexcept;
  252. void writeOpcode(const PluginBridgeNonRtClientOpcode opcode) noexcept;
  253. // bridge, client
  254. PluginBridgeNonRtClientOpcode readOpcode() noexcept;
  255. CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtClientControl)
  256. };
  257. // -------------------------------------------------------------------------------------------------------------------
  258. struct BridgeNonRtServerControl : public CarlaRingBufferControl<HugeStackBuffer> {
  259. BridgeNonRtServerData* data;
  260. CarlaString filename;
  261. CarlaMutex mutex;
  262. char shm[64];
  263. bool isServer;
  264. BridgeNonRtServerControl() noexcept;
  265. ~BridgeNonRtServerControl() noexcept override;
  266. bool initializeServer() noexcept;
  267. bool attachClient(const char* const basename) noexcept;
  268. void clear() noexcept;
  269. bool mapData() noexcept;
  270. void unmapData() noexcept;
  271. // non-bridge, server
  272. PluginBridgeNonRtServerOpcode readOpcode() noexcept;
  273. // bridge, client
  274. void waitIfDataIsReachingLimit() noexcept;
  275. void writeOpcode(const PluginBridgeNonRtServerOpcode opcode) noexcept;
  276. CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtServerControl)
  277. };
  278. // -------------------------------------------------------------------------------------------------------------------
  279. #endif // CARLA_BRIDGE_UTILS_HPP_INCLUDED