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.

432 lines
11KB

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