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.

CarlaEngineBridge.cpp 13KB

10 years ago
10 years ago
10 years ago
10 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
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
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2014 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 BUILD_BRIDGE
  18. # error This file should not be compiled if not building bridge
  19. #endif
  20. #include "CarlaEngineInternal.hpp"
  21. #include "CarlaPlugin.hpp"
  22. #include "CarlaBackendUtils.hpp"
  23. #include "CarlaBridgeUtils.hpp"
  24. #include "CarlaShmUtils.hpp"
  25. #include "CarlaMIDI.h"
  26. #include "jackbridge/JackBridge.hpp"
  27. #include <cerrno>
  28. #include <ctime>
  29. CARLA_BACKEND_START_NAMESPACE
  30. #if 0
  31. } // Fix editor indentation
  32. #endif
  33. // -------------------------------------------------------------------
  34. struct BridgeAudioPool {
  35. CarlaString filename;
  36. float* data;
  37. shm_t shm;
  38. BridgeAudioPool()
  39. : data(nullptr)
  40. {
  41. carla_shm_init(shm);
  42. }
  43. ~BridgeAudioPool()
  44. {
  45. // should be cleared by now
  46. CARLA_ASSERT(data == nullptr);
  47. clear();
  48. }
  49. bool attach()
  50. {
  51. #ifdef CARLA_OS_WIN
  52. // TESTING!
  53. shm = carla_shm_attach_linux((const char*)filename);
  54. #else
  55. shm = carla_shm_attach((const char*)filename);
  56. #endif
  57. return carla_is_shm_valid(shm);
  58. }
  59. void clear()
  60. {
  61. filename.clear();
  62. data = nullptr;
  63. if (carla_is_shm_valid(shm))
  64. carla_shm_close(shm);
  65. }
  66. };
  67. struct BridgeControl : public RingBufferControl<StackPackedRingBuffer> {
  68. CarlaString filename;
  69. BridgeShmControl* data;
  70. shm_t shm;
  71. BridgeControl()
  72. : RingBufferControl(nullptr),
  73. data(nullptr)
  74. {
  75. carla_shm_init(shm);
  76. }
  77. ~BridgeControl()
  78. {
  79. // should be cleared by now
  80. CARLA_ASSERT(data == nullptr);
  81. clear();
  82. }
  83. bool attach()
  84. {
  85. #ifdef CARLA_OS_WIN
  86. // TESTING!
  87. shm = carla_shm_attach_linux((const char*)filename);
  88. #else
  89. shm = carla_shm_attach((const char*)filename);
  90. #endif
  91. return carla_is_shm_valid(shm);
  92. }
  93. void clear()
  94. {
  95. filename.clear();
  96. data = nullptr;
  97. if (carla_is_shm_valid(shm))
  98. carla_shm_close(shm);
  99. }
  100. bool mapData()
  101. {
  102. CARLA_ASSERT(data == nullptr);
  103. if (carla_shm_map<BridgeShmControl>(shm, data))
  104. {
  105. setRingBuffer(&data->ringBuffer, false);
  106. return true;
  107. }
  108. return false;
  109. }
  110. PluginBridgeOpcode readOpcode()
  111. {
  112. return static_cast<PluginBridgeOpcode>(readInt());
  113. }
  114. };
  115. // -------------------------------------------------------------------
  116. class CarlaEngineBridge : public CarlaEngine,
  117. public CarlaThread
  118. {
  119. public:
  120. CarlaEngineBridge(const char* const audioBaseName, const char* const controlBaseName)
  121. : CarlaEngine(),
  122. CarlaThread("CarlaEngineBridge"),
  123. fIsRunning(false)
  124. {
  125. carla_debug("CarlaEngineBridge::CarlaEngineBridge()");
  126. fShmAudioPool.filename = "/carla-bridge_shm_";
  127. fShmAudioPool.filename += audioBaseName;
  128. fShmControl.filename = "/carla-bridge_shc_";
  129. fShmControl.filename += controlBaseName;
  130. }
  131. ~CarlaEngineBridge() override
  132. {
  133. carla_debug("CarlaEngineBridge::~CarlaEngineBridge()");
  134. }
  135. // -------------------------------------
  136. // CarlaEngine virtual calls
  137. bool init(const char* const clientName) override
  138. {
  139. carla_debug("CarlaEngineBridge::init(\"%s\")", clientName);
  140. // SHM Audio Pool
  141. {
  142. if (! fShmAudioPool.attach())
  143. {
  144. carla_stdout("Failed to open or create shared memory file #1");
  145. return false;
  146. }
  147. }
  148. // SHM Control
  149. {
  150. if (! fShmControl.attach())
  151. {
  152. carla_stdout("Failed to open or create shared memory file #2");
  153. // clear
  154. fShmAudioPool.clear();
  155. return false;
  156. }
  157. if (! fShmControl.mapData())
  158. {
  159. carla_stdout("Failed to mmap shared memory file");
  160. // clear
  161. fShmControl.clear();
  162. fShmAudioPool.clear();
  163. return false;
  164. }
  165. }
  166. // Read values from memory
  167. PluginBridgeOpcode opcode;
  168. opcode = fShmControl.readOpcode();
  169. CARLA_ASSERT_INT(opcode == kPluginBridgeOpcodeSetBufferSize, opcode);
  170. pData->bufferSize = fShmControl.readInt();
  171. carla_stderr("BufferSize: %i", pData->bufferSize);
  172. opcode = fShmControl.readOpcode();
  173. CARLA_ASSERT_INT(opcode == kPluginBridgeOpcodeSetSampleRate, opcode);
  174. pData->sampleRate = fShmControl.readFloat();
  175. carla_stderr("SampleRate: %f", pData->sampleRate);
  176. CarlaThread::start();
  177. CarlaEngine::init(clientName);
  178. return true;
  179. }
  180. bool close() override
  181. {
  182. carla_debug("CarlaEnginePlugin::close()");
  183. CarlaEngine::close();
  184. CarlaThread::stop(6000);
  185. fShmControl.clear();
  186. fShmAudioPool.clear();
  187. return true;
  188. }
  189. bool isRunning() const noexcept override
  190. {
  191. return fIsRunning;
  192. }
  193. bool isOffline() const noexcept override
  194. {
  195. return false;
  196. }
  197. EngineType getType() const noexcept override
  198. {
  199. return kEngineTypeBridge;
  200. }
  201. const char* getCurrentDriverName() const noexcept
  202. {
  203. return "Bridge";
  204. }
  205. // -------------------------------------
  206. // CarlaThread virtual calls
  207. void run() override
  208. {
  209. fIsRunning = true;
  210. // TODO - set RT permissions
  211. carla_debug("CarlaEngineBridge::run()");
  212. while (! shouldExit())
  213. {
  214. if (! jackbridge_sem_timedwait(&fShmControl.data->runServer, 5))
  215. {
  216. if (errno == ETIMEDOUT)
  217. {
  218. fIsRunning = false;
  219. signalShouldExit();
  220. return;
  221. }
  222. }
  223. while (fShmControl.isDataAvailable())
  224. {
  225. const PluginBridgeOpcode opcode(fShmControl.readOpcode());
  226. if (opcode != kPluginBridgeOpcodeProcess)
  227. {
  228. carla_debug("CarlaEngineBridge::run() - got opcode: %s", PluginBridgeOpcode2str(opcode));
  229. }
  230. switch (opcode)
  231. {
  232. case kPluginBridgeOpcodeNull:
  233. break;
  234. case kPluginBridgeOpcodeSetAudioPool:
  235. {
  236. const int64_t poolSize(fShmControl.readLong());
  237. CARLA_SAFE_ASSERT_BREAK(poolSize > 0);
  238. fShmAudioPool.data = (float*)carla_shm_map(fShmAudioPool.shm, size_t(poolSize));
  239. break;
  240. }
  241. case kPluginBridgeOpcodeSetBufferSize:
  242. {
  243. const int bufferSize(fShmControl.readInt());
  244. bufferSizeChanged(bufferSize);
  245. break;
  246. }
  247. case kPluginBridgeOpcodeSetSampleRate:
  248. {
  249. const float sampleRate(fShmControl.readFloat());
  250. sampleRateChanged(sampleRate);
  251. break;
  252. }
  253. case kPluginBridgeOpcodeSetParameter:
  254. {
  255. //const int index(fShmControl.readInt());
  256. //const float value(fShmControl.readFloat());
  257. CarlaPlugin* const plugin(getPluginUnchecked(0));
  258. if (plugin != nullptr && plugin->isEnabled())
  259. {
  260. //plugin->setParameterValueByRealIndex(index, value, false, false, false);
  261. //plugin->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, value);
  262. }
  263. break;
  264. }
  265. case kPluginBridgeOpcodeSetProgram:
  266. {
  267. //const int index(fShmControl.readInt());
  268. CarlaPlugin* const plugin(getPluginUnchecked(0));
  269. if (plugin != nullptr && plugin->isEnabled())
  270. {
  271. //plugin->setProgram(index, false, false, false);
  272. //plugin->postponeRtEvent(kPluginPostRtEventProgramChange, index, 0, 0.0f);
  273. }
  274. break;
  275. }
  276. case kPluginBridgeOpcodeSetMidiProgram:
  277. {
  278. //const int index(fShmControl.readInt());
  279. CarlaPlugin* const plugin(getPluginUnchecked(0));
  280. if (plugin != nullptr && plugin->isEnabled())
  281. {
  282. //plugin->setMidiProgram(index, false, false, false);
  283. //plugin->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  284. }
  285. break;
  286. }
  287. case kPluginBridgeOpcodeMidiEvent:
  288. {
  289. //uint8_t data[4] = { 0 };
  290. //const long time(fShmControl.readLong());
  291. //const int dataSize(fShmControl.readInt());
  292. //CARLA_ASSERT_INT(dataSize >= 1 && dataSize <= 4, dataSize);
  293. //for (int i=0; i < dataSize && i < 4; ++i)
  294. // data[i] = fShmControl.readChar();
  295. //CARLA_ASSERT(pData->bufEvents.in != nullptr);
  296. //if (pData->bufEvents.in != nullptr)
  297. {
  298. // TODO
  299. }
  300. break;
  301. }
  302. case kPluginBridgeOpcodeProcess:
  303. {
  304. CARLA_ASSERT(fShmAudioPool.data != nullptr);
  305. CarlaPlugin* const plugin(getPluginUnchecked(0));
  306. if (plugin != nullptr && plugin->isEnabled() && plugin->tryLock(true)) // FIXME - always lock?
  307. {
  308. const uint32_t inCount(plugin->getAudioInCount());
  309. const uint32_t outCount(plugin->getAudioOutCount());
  310. float* inBuffer[inCount];
  311. float* outBuffer[outCount];
  312. for (uint32_t i=0; i < inCount; ++i)
  313. inBuffer[i] = fShmAudioPool.data + i*pData->bufferSize;
  314. for (uint32_t i=0; i < outCount; ++i)
  315. outBuffer[i] = fShmAudioPool.data + (i+inCount)*pData->bufferSize;
  316. plugin->initBuffers();
  317. plugin->process(inBuffer, outBuffer, pData->bufferSize);
  318. plugin->unlock();
  319. }
  320. break;
  321. }
  322. case kPluginBridgeOpcodeQuit:
  323. signalShouldExit();
  324. break;
  325. }
  326. }
  327. if (jackbridge_sem_post(&fShmControl.data->runClient) != 0)
  328. pass(); //carla_stderr2("Could not post to semaphore");
  329. }
  330. fIsRunning = false;
  331. }
  332. private:
  333. BridgeAudioPool fShmAudioPool;
  334. BridgeControl fShmControl;
  335. volatile bool fIsRunning;
  336. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineBridge)
  337. };
  338. // -----------------------------------------
  339. CarlaEngine* CarlaEngine::newBridge(const char* const audioBaseName, const char* const controlBaseName)
  340. {
  341. return new CarlaEngineBridge(audioBaseName, controlBaseName);
  342. }
  343. CARLA_BACKEND_END_NAMESPACE
  344. // -----------------------------------------------------------------------
  345. // Extra stuff for linking purposes
  346. CARLA_BACKEND_START_NAMESPACE
  347. CarlaEngine* CarlaEngine::newRtAudio(const AudioApi) { return nullptr; }
  348. unsigned int CarlaEngine::getRtAudioApiCount() { return 0; }
  349. const char* CarlaEngine::getRtAudioApiName(const unsigned int) { return nullptr; }
  350. const char* const* CarlaEngine::getRtAudioApiDeviceNames(const unsigned int) { return nullptr; }
  351. const EngineDriverDeviceInfo* CarlaEngine::getRtAudioDeviceInfo(const unsigned int, const char* const) { return nullptr; }
  352. #ifdef HAVE_JUCE
  353. CarlaEngine* CarlaEngine::newJuce(const AudioApi) { return nullptr; }
  354. unsigned int CarlaEngine::getJuceApiCount() { return 0; }
  355. const char* CarlaEngine::getJuceApiName(const unsigned int) { return nullptr; }
  356. const char* const* CarlaEngine::getJuceApiDeviceNames(const unsigned int) { return nullptr; }
  357. const EngineDriverDeviceInfo* CarlaEngine::getJuceDeviceInfo(const unsigned int, const char* const) { return nullptr; }
  358. #endif
  359. CARLA_BACKEND_END_NAMESPACE
  360. // -----------------------------------------------------------------------