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 13KB

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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /*
  2. * Carla Bridge utils
  3. * Copyright (C) 2013-2019 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. }
  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 kPluginBridgeNonRtServerParameterTouch:
  166. return "kPluginBridgeNonRtServerParameterTouch";
  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. case kPluginBridgeNonRtServerVersion:
  194. return "kPluginBridgeNonRtServerVersion";
  195. }
  196. carla_stderr("CarlaBackend::PluginBridgeNonRtServerOpcode2str%i) - invalid opcode", opcode);
  197. return nullptr;
  198. }
  199. // -------------------------------------------------------------------------------------------------------------------
  200. static const std::size_t kBridgeRtClientDataMidiOutSize = 511*4;
  201. static const std::size_t kBridgeBaseMidiOutHeaderSize = 6U /* time, port and size */;
  202. // Server => Client RT
  203. struct BridgeRtClientData {
  204. BridgeSemaphore sem;
  205. BridgeTimeInfo timeInfo;
  206. SmallStackBuffer ringBuffer;
  207. uint8_t midiOut[kBridgeRtClientDataMidiOutSize];
  208. uint32_t procFlags;
  209. };
  210. // Server => Client Non-RT
  211. struct BridgeNonRtClientData {
  212. BigStackBuffer ringBuffer;
  213. };
  214. // Client => Server Non-RT
  215. struct BridgeNonRtServerData {
  216. HugeStackBuffer ringBuffer;
  217. };
  218. // -------------------------------------------------------------------------------------------------------------------
  219. struct BridgeAudioPool {
  220. float* data;
  221. std::size_t dataSize;
  222. CarlaString filename;
  223. char shm[64];
  224. bool isServer;
  225. BridgeAudioPool() noexcept;
  226. ~BridgeAudioPool() noexcept;
  227. bool initializeServer() noexcept;
  228. bool attachClient(const char* const fname) noexcept;
  229. void clear() noexcept;
  230. void resize(const uint32_t bufferSize, const uint32_t audioPortCount, const uint32_t cvPortCount) noexcept;
  231. const char* getFilenameSuffix() const noexcept;
  232. CARLA_DECLARE_NON_COPY_STRUCT(BridgeAudioPool)
  233. };
  234. // -------------------------------------------------------------------------------------------------------------------
  235. struct BridgeRtClientControl : public CarlaRingBufferControl<SmallStackBuffer> {
  236. BridgeRtClientData* data;
  237. CarlaString filename;
  238. bool needsSemDestroy; // client only
  239. char shm[64];
  240. bool isServer;
  241. BridgeRtClientControl() noexcept;
  242. ~BridgeRtClientControl() noexcept override;
  243. bool initializeServer() noexcept;
  244. bool attachClient(const char* const basename) noexcept;
  245. void clear() noexcept;
  246. bool mapData() noexcept;
  247. void unmapData() noexcept;
  248. // non-bridge, server
  249. bool waitForClient(const uint msecs) noexcept;
  250. bool writeOpcode(const PluginBridgeRtClientOpcode opcode) noexcept;
  251. // bridge, client
  252. PluginBridgeRtClientOpcode readOpcode() noexcept;
  253. // helper class that automatically posts semaphore on destructor
  254. struct WaitHelper {
  255. BridgeRtClientData* const data;
  256. const bool ok;
  257. WaitHelper(BridgeRtClientControl& c) noexcept;
  258. ~WaitHelper() noexcept;
  259. CARLA_DECLARE_NON_COPY_STRUCT(WaitHelper)
  260. };
  261. CARLA_DECLARE_NON_COPY_STRUCT(BridgeRtClientControl)
  262. };
  263. // -------------------------------------------------------------------------------------------------------------------
  264. struct BridgeNonRtClientControl : public CarlaRingBufferControl<BigStackBuffer> {
  265. BridgeNonRtClientData* data;
  266. CarlaString filename;
  267. CarlaMutex mutex;
  268. char shm[64];
  269. bool isServer;
  270. BridgeNonRtClientControl() noexcept;
  271. ~BridgeNonRtClientControl() 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. void waitIfDataIsReachingLimit() noexcept;
  279. bool writeOpcode(const PluginBridgeNonRtClientOpcode opcode) noexcept;
  280. // bridge, client
  281. PluginBridgeNonRtClientOpcode readOpcode() noexcept;
  282. CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtClientControl)
  283. };
  284. // -------------------------------------------------------------------------------------------------------------------
  285. struct BridgeNonRtServerControl : public CarlaRingBufferControl<HugeStackBuffer> {
  286. BridgeNonRtServerData* data;
  287. CarlaString filename;
  288. CarlaMutex mutex;
  289. char shm[64];
  290. bool isServer;
  291. BridgeNonRtServerControl() noexcept;
  292. ~BridgeNonRtServerControl() noexcept override;
  293. bool initializeServer() noexcept;
  294. bool attachClient(const char* const basename) noexcept;
  295. void clear() noexcept;
  296. bool mapData() noexcept;
  297. void unmapData() noexcept;
  298. // non-bridge, server
  299. PluginBridgeNonRtServerOpcode readOpcode() noexcept;
  300. // bridge, client
  301. void waitIfDataIsReachingLimit() noexcept;
  302. bool writeOpcode(const PluginBridgeNonRtServerOpcode opcode) noexcept;
  303. CARLA_DECLARE_NON_COPY_STRUCT(BridgeNonRtServerControl)
  304. };
  305. // -------------------------------------------------------------------------------------------------------------------
  306. #endif // CARLA_BRIDGE_UTILS_HPP_INCLUDED