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.

434 lines
12KB

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