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.

476 lines
14KB

  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.readUInt();
  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. const int64_t poolSize(fShmControl.readLong());
  236. CARLA_SAFE_ASSERT_BREAK(poolSize > 0);
  237. fShmAudioPool.data = (float*)carla_shm_map(fShmAudioPool.shm, size_t(poolSize));
  238. break;
  239. }
  240. case kPluginBridgeOpcodeSetBufferSize: {
  241. const uint32_t bufferSize(fShmControl.readUInt());
  242. bufferSizeChanged(bufferSize);
  243. break;
  244. }
  245. case kPluginBridgeOpcodeSetSampleRate: {
  246. const float sampleRate(fShmControl.readFloat());
  247. sampleRateChanged(sampleRate);
  248. break;
  249. }
  250. case kPluginBridgeOpcodeSetParameter: {
  251. const int32_t 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. //if (index >= 0)
  258. // plugin->postponeRtEvent(kPluginPostRtEventParameterChange, index, 0, value);
  259. }
  260. break;
  261. }
  262. case kPluginBridgeOpcodeSetProgram: {
  263. const int32_t index(fShmControl.readInt());
  264. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  265. CarlaPlugin* const plugin(getPluginUnchecked(0));
  266. if (plugin != nullptr && plugin->isEnabled())
  267. {
  268. plugin->setProgram(index, false, false, false);
  269. //plugin->postponeRtEvent(kPluginPostRtEventProgramChange, index, 0, 0.0f);
  270. }
  271. break;
  272. }
  273. case kPluginBridgeOpcodeSetMidiProgram: {
  274. const int32_t index(fShmControl.readInt());
  275. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  276. CarlaPlugin* const plugin(getPluginUnchecked(0));
  277. if (plugin != nullptr && plugin->isEnabled())
  278. {
  279. plugin->setMidiProgram(index, false, false, false);
  280. //plugin->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  281. }
  282. break;
  283. }
  284. case kPluginBridgeOpcodeMidiEvent: {
  285. const int64_t time(fShmControl.readLong());
  286. const int32_t size(fShmControl.readInt());
  287. CARLA_SAFE_ASSERT_BREAK(time >= 0);
  288. CARLA_SAFE_ASSERT_BREAK(size > 0 && size <= 4);
  289. uint8_t data[size];
  290. for (int32_t i=0; i < size; ++i)
  291. data[i] = static_cast<uint8_t>(fShmControl.readChar());
  292. CARLA_SAFE_ASSERT_BREAK(pData->bufEvents.in != nullptr);
  293. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  294. {
  295. EngineEvent& event(pData->bufEvents.in[i]);
  296. if (event.type != kEngineEventTypeNull)
  297. continue;
  298. event.fillFromMidiData(static_cast<uint8_t>(size), data);
  299. }
  300. break;
  301. }
  302. case kPluginBridgeOpcodeProcess: {
  303. CARLA_SAFE_ASSERT_BREAK(fShmAudioPool.data != nullptr);
  304. CarlaPlugin* const plugin(getPluginUnchecked(0));
  305. if (plugin != nullptr && plugin->isEnabled() && plugin->tryLock(true)) // FIXME - always lock?
  306. {
  307. const uint32_t inCount(plugin->getAudioInCount());
  308. const uint32_t outCount(plugin->getAudioOutCount());
  309. float* inBuffer[inCount];
  310. float* outBuffer[outCount];
  311. for (uint32_t i=0; i < inCount; ++i)
  312. inBuffer[i] = fShmAudioPool.data + i*pData->bufferSize;
  313. for (uint32_t i=0; i < outCount; ++i)
  314. outBuffer[i] = fShmAudioPool.data + (i+inCount)*pData->bufferSize;
  315. plugin->initBuffers();
  316. plugin->process(inBuffer, outBuffer, pData->bufferSize);
  317. plugin->unlock();
  318. }
  319. // clear buffer
  320. CARLA_SAFE_ASSERT_BREAK(pData->bufEvents.in != nullptr);
  321. if (pData->bufEvents.in[0].type != kEngineEventTypeNull)
  322. carla_zeroStruct<EngineEvent>(pData->bufEvents.in, kMaxEngineEventInternalCount);
  323. break;
  324. }
  325. case kPluginBridgeOpcodeQuit:
  326. signalShouldExit();
  327. break;
  328. }
  329. }
  330. if (jackbridge_sem_post(&fShmControl.data->runClient) != 0)
  331. pass(); //carla_stderr2("Could not post to semaphore");
  332. }
  333. fIsRunning = false;
  334. }
  335. private:
  336. BridgeAudioPool fShmAudioPool;
  337. BridgeControl fShmControl;
  338. volatile bool fIsRunning;
  339. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineBridge)
  340. };
  341. // -----------------------------------------
  342. CarlaEngine* CarlaEngine::newBridge(const char* const audioBaseName, const char* const controlBaseName)
  343. {
  344. return new CarlaEngineBridge(audioBaseName, controlBaseName);
  345. }
  346. CARLA_BACKEND_END_NAMESPACE
  347. // -----------------------------------------------------------------------
  348. // Extra stuff for linking purposes
  349. CARLA_BACKEND_START_NAMESPACE
  350. CarlaEngine* CarlaEngine::newRtAudio(const AudioApi) { return nullptr; }
  351. unsigned int CarlaEngine::getRtAudioApiCount() { return 0; }
  352. const char* CarlaEngine::getRtAudioApiName(const unsigned int) { return nullptr; }
  353. const char* const* CarlaEngine::getRtAudioApiDeviceNames(const unsigned int) { return nullptr; }
  354. const EngineDriverDeviceInfo* CarlaEngine::getRtAudioDeviceInfo(const unsigned int, const char* const) { return nullptr; }
  355. #ifdef HAVE_JUCE
  356. CarlaEngine* CarlaEngine::newJuce(const AudioApi) { return nullptr; }
  357. unsigned int CarlaEngine::getJuceApiCount() { return 0; }
  358. const char* CarlaEngine::getJuceApiName(const unsigned int) { return nullptr; }
  359. const char* const* CarlaEngine::getJuceApiDeviceNames(const unsigned int) { return nullptr; }
  360. const EngineDriverDeviceInfo* CarlaEngine::getJuceDeviceInfo(const unsigned int, const char* const) { return nullptr; }
  361. #endif
  362. CARLA_BACKEND_END_NAMESPACE
  363. // -----------------------------------------------------------------------
  364. #if defined(CARLA_OS_WIN) && ! defined(__WINE__)
  365. extern "C" __declspec (dllexport)
  366. #else
  367. extern "C" __attribute__ ((visibility("default")))
  368. #endif
  369. void carla_register_native_plugin_carla();
  370. void carla_register_native_plugin_carla(){}
  371. // -----------------------------------------------------------------------