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.

429 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 juce::Thread
  117. {
  118. public:
  119. CarlaEngineBridge(const char* const audioBaseName, const char* const controlBaseName)
  120. : CarlaEngine(),
  121. juce::Thread("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. fBufferSize = fShmControl.readInt();
  170. carla_stderr("BufferSize: %i", fBufferSize);
  171. opcode = fShmControl.readOpcode();
  172. CARLA_ASSERT_INT(opcode == kPluginBridgeOpcodeSetSampleRate, opcode);
  173. fSampleRate = fShmControl.readFloat();
  174. carla_stderr("SampleRate: %f", fSampleRate);
  175. fIsRunning = true;
  176. startThread(10);
  177. CarlaEngine::init(clientName);
  178. return true;
  179. }
  180. bool close() override
  181. {
  182. carla_debug("CarlaEnginePlugin::close()");
  183. CarlaEngine::close();
  184. stopThread(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. // -------------------------------------
  202. // CarlaThread virtual calls
  203. void run() override
  204. {
  205. // TODO - set RT permissions
  206. carla_debug("CarlaEngineBridge::run()");
  207. while (! threadShouldExit())
  208. {
  209. if (! jackbridge_sem_timedwait(&fShmControl.data->runServer, 5))
  210. {
  211. if (errno == ETIMEDOUT)
  212. {
  213. fIsRunning = false;
  214. signalThreadShouldExit();
  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->isEnabled())
  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->isEnabled())
  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->isEnabled())
  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(pData->bufEvents.in != nullptr);
  288. if (pData->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->isEnabled() && plugin->tryLock())
  299. {
  300. const uint32_t inCount(plugin->getAudioInCount());
  301. const uint32_t outCount(plugin->getAudioOutCount());
  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. signalThreadShouldExit();
  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. volatile bool fIsRunning;
  328. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineBridge)
  329. };
  330. // -----------------------------------------
  331. CarlaEngine* CarlaEngine::newBridge(const char* const audioBaseName, const char* const controlBaseName)
  332. {
  333. return new CarlaEngineBridge(audioBaseName, controlBaseName);
  334. }
  335. CARLA_BACKEND_END_NAMESPACE