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.

355 lines
13KB

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