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.

586 lines
17KB

  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 "CarlaMIDI.h"
  25. #include "jackbridge/JackBridge.hpp"
  26. #include <cerrno>
  27. #include <ctime>
  28. #ifdef JACKBRIDGE_EXPORT
  29. // -------------------------------------------------------------------
  30. bool jackbridge_is_ok() noexcept
  31. {
  32. return true;
  33. }
  34. #endif
  35. // -------------------------------------------------------------------
  36. CARLA_BACKEND_START_NAMESPACE
  37. #if 0
  38. } // Fix editor indentation
  39. #endif
  40. // -------------------------------------------------------------------
  41. template<typename T>
  42. bool jackbridge_shm_map2(char* shm, T*& value)
  43. {
  44. value = (T*)jackbridge_shm_map(shm, sizeof(T));
  45. return (value != nullptr);
  46. }
  47. struct BridgeAudioPool {
  48. CarlaString filename;
  49. float* data;
  50. char shm[32];
  51. BridgeAudioPool()
  52. : data(nullptr)
  53. {
  54. carla_zeroChar(shm, 32);
  55. jackbridge_shm_init(shm);
  56. }
  57. ~BridgeAudioPool()
  58. {
  59. // should be cleared by now
  60. CARLA_ASSERT(data == nullptr);
  61. clear();
  62. }
  63. bool attach()
  64. {
  65. jackbridge_shm_attach(shm, filename);
  66. return jackbridge_shm_is_valid(shm);
  67. }
  68. void clear()
  69. {
  70. filename.clear();
  71. data = nullptr;
  72. if (jackbridge_shm_is_valid(shm))
  73. jackbridge_shm_close(shm);
  74. }
  75. CARLA_DECLARE_NON_COPY_STRUCT(BridgeAudioPool)
  76. };
  77. struct BridgeControl : public CarlaRingBuffer<StackBuffer> {
  78. CarlaString filename;
  79. BridgeShmControl* data;
  80. char shm[32];
  81. BridgeControl()
  82. : CarlaRingBuffer<StackBuffer>(),
  83. data(nullptr)
  84. {
  85. carla_zeroChar(shm, 32);
  86. jackbridge_shm_init(shm);
  87. }
  88. ~BridgeControl()
  89. {
  90. // should be cleared by now
  91. CARLA_ASSERT(data == nullptr);
  92. clear();
  93. }
  94. bool attach()
  95. {
  96. jackbridge_shm_attach(shm, filename);
  97. return jackbridge_shm_is_valid(shm);
  98. }
  99. void clear()
  100. {
  101. filename.clear();
  102. data = nullptr;
  103. if (jackbridge_shm_is_valid(shm))
  104. jackbridge_shm_close(shm);
  105. }
  106. bool mapData()
  107. {
  108. CARLA_ASSERT(data == nullptr);
  109. if (jackbridge_shm_map2<BridgeShmControl>(shm, data))
  110. {
  111. setRingBuffer(&data->buffer, false);
  112. return true;
  113. }
  114. return false;
  115. }
  116. PluginBridgeOpcode readOpcode()
  117. {
  118. return static_cast<PluginBridgeOpcode>(readInt());
  119. }
  120. CARLA_DECLARE_NON_COPY_STRUCT(BridgeControl)
  121. };
  122. struct BridgeTime {
  123. CarlaString filename;
  124. BridgeTimeInfo* info;
  125. char shm[32];
  126. BridgeTime()
  127. : info(nullptr)
  128. {
  129. carla_zeroChar(shm, 32);
  130. jackbridge_shm_init(shm);
  131. }
  132. ~BridgeTime()
  133. {
  134. // should be cleared by now
  135. CARLA_ASSERT(info == nullptr);
  136. clear();
  137. }
  138. bool attach()
  139. {
  140. jackbridge_shm_attach(shm, filename);
  141. return jackbridge_shm_is_valid(shm);
  142. }
  143. void clear()
  144. {
  145. filename.clear();
  146. info = nullptr;
  147. if (jackbridge_shm_is_valid(shm))
  148. jackbridge_shm_close(shm);
  149. }
  150. bool mapData()
  151. {
  152. CARLA_ASSERT(info == nullptr);
  153. return jackbridge_shm_map2<BridgeTimeInfo>(shm, info);
  154. }
  155. CARLA_DECLARE_NON_COPY_STRUCT(BridgeTime)
  156. };
  157. // -------------------------------------------------------------------
  158. class CarlaEngineBridge : public CarlaEngine,
  159. public CarlaThread
  160. {
  161. public:
  162. CarlaEngineBridge(const char* const audioBaseName, const char* const controlBaseName, const char* const timeBaseName)
  163. : CarlaEngine(),
  164. CarlaThread("CarlaEngineBridge"),
  165. fIsRunning(false)
  166. {
  167. carla_debug("CarlaEngineBridge::CarlaEngineBridge()");
  168. fShmAudioPool.filename = "/carla-bridge_shm_";
  169. fShmAudioPool.filename += audioBaseName;
  170. fShmControl.filename = "/carla-bridge_shc_";
  171. fShmControl.filename += controlBaseName;
  172. fShmTime.filename = "/carla-bridge_sht_";
  173. fShmTime.filename += timeBaseName;
  174. }
  175. ~CarlaEngineBridge() noexcept override
  176. {
  177. carla_debug("CarlaEngineBridge::~CarlaEngineBridge()");
  178. }
  179. // -------------------------------------
  180. // CarlaEngine virtual calls
  181. bool init(const char* const clientName) override
  182. {
  183. carla_debug("CarlaEngineBridge::init(\"%s\")", clientName);
  184. // SHM Audio Pool
  185. {
  186. if (! fShmAudioPool.attach())
  187. {
  188. carla_stdout("Failed to open or create shared memory file #1");
  189. return false;
  190. }
  191. }
  192. // SHM Control
  193. {
  194. if (! fShmControl.attach())
  195. {
  196. carla_stdout("Failed to open or create shared memory file #2");
  197. // clear
  198. fShmAudioPool.clear();
  199. return false;
  200. }
  201. if (! fShmControl.mapData())
  202. {
  203. carla_stdout("Failed to map shared memory file #2");
  204. // clear
  205. fShmControl.clear();
  206. fShmAudioPool.clear();
  207. return false;
  208. }
  209. }
  210. // SHM Transport
  211. {
  212. if (! fShmTime.attach())
  213. {
  214. carla_stdout("Failed to open or create shared memory file #3");
  215. // clear
  216. fShmControl.clear();
  217. fShmAudioPool.clear();
  218. return false;
  219. }
  220. if (! fShmTime.mapData())
  221. {
  222. carla_stdout("Failed to map shared memory file #3");
  223. // clear
  224. fShmTime.clear();
  225. fShmControl.clear();
  226. fShmAudioPool.clear();
  227. return false;
  228. }
  229. }
  230. // Read values from memory
  231. PluginBridgeOpcode opcode;
  232. opcode = fShmControl.readOpcode();
  233. CARLA_SAFE_ASSERT_INT(opcode == kPluginBridgeOpcodeNull, opcode);
  234. const uint32_t shmStructSize = fShmControl.readUInt();
  235. CARLA_SAFE_ASSERT_INT2(shmStructSize == sizeof(BridgeShmControl), shmStructSize, sizeof(BridgeShmControl));
  236. const uint32_t timeStructSize = fShmControl.readUInt();
  237. CARLA_SAFE_ASSERT_INT2(timeStructSize == sizeof(BridgeTimeInfo), timeStructSize, sizeof(BridgeTimeInfo));
  238. opcode = fShmControl.readOpcode();
  239. CARLA_SAFE_ASSERT_INT(opcode == kPluginBridgeOpcodeSetBufferSize, opcode);
  240. pData->bufferSize = fShmControl.readUInt();
  241. opcode = fShmControl.readOpcode();
  242. CARLA_SAFE_ASSERT_INT(opcode == kPluginBridgeOpcodeSetSampleRate, opcode);
  243. pData->sampleRate = fShmControl.readFloat();
  244. carla_stdout("Carla Client Info:");
  245. carla_stdout(" BufferSize: %i", pData->bufferSize);
  246. carla_stdout(" SampleRate: %f", pData->sampleRate);
  247. carla_stdout(" sizeof(BridgeShmControl): %i/" P_SIZE, shmStructSize, sizeof(BridgeShmControl));
  248. carla_stdout(" sizeof(BridgeTimeInfo): %i/" P_SIZE, timeStructSize, sizeof(BridgeTimeInfo));
  249. CarlaThread::startThread();
  250. CarlaEngine::init(clientName);
  251. return true;
  252. }
  253. bool close() override
  254. {
  255. carla_debug("CarlaEnginePlugin::close()");
  256. CarlaEngine::close();
  257. CarlaThread::stopThread(6000);
  258. fShmTime.clear();
  259. fShmControl.clear();
  260. fShmAudioPool.clear();
  261. return true;
  262. }
  263. bool isRunning() const noexcept override
  264. {
  265. return fIsRunning;
  266. }
  267. bool isOffline() const noexcept override
  268. {
  269. return false;
  270. }
  271. EngineType getType() const noexcept override
  272. {
  273. return kEngineTypeBridge;
  274. }
  275. const char* getCurrentDriverName() const noexcept
  276. {
  277. return "Bridge";
  278. }
  279. // -------------------------------------
  280. // CarlaThread virtual calls
  281. void run() override
  282. {
  283. fIsRunning = true;
  284. // TODO - set RT permissions
  285. carla_debug("CarlaEngineBridge::run()");
  286. for (; ! shouldThreadExit();)
  287. {
  288. if (! jackbridge_sem_timedwait(&fShmControl.data->runServer, 5))
  289. {
  290. if (errno == ETIMEDOUT)
  291. {
  292. fIsRunning = false;
  293. signalThreadShouldExit();
  294. return;
  295. }
  296. }
  297. for (; fShmControl.isDataAvailableForReading();)
  298. {
  299. const PluginBridgeOpcode opcode(fShmControl.readOpcode());
  300. if (opcode != kPluginBridgeOpcodeProcess) {
  301. carla_debug("CarlaEngineBridge::run() - got opcode: %s", PluginBridgeOpcode2str(opcode));
  302. }
  303. switch (opcode)
  304. {
  305. case kPluginBridgeOpcodeNull:
  306. break;
  307. case kPluginBridgeOpcodeSetAudioPool: {
  308. const int64_t poolSize(fShmControl.readLong());
  309. CARLA_SAFE_ASSERT_BREAK(poolSize > 0);
  310. fShmAudioPool.data = (float*)jackbridge_shm_map(fShmAudioPool.shm, static_cast<size_t>(poolSize));
  311. break;
  312. }
  313. case kPluginBridgeOpcodeSetBufferSize: {
  314. const uint32_t bufferSize(fShmControl.readUInt());
  315. bufferSizeChanged(bufferSize);
  316. break;
  317. }
  318. case kPluginBridgeOpcodeSetSampleRate: {
  319. const float sampleRate(fShmControl.readFloat());
  320. sampleRateChanged(sampleRate);
  321. break;
  322. }
  323. case kPluginBridgeOpcodeSetParameterRt:
  324. case kPluginBridgeOpcodeSetParameterNonRt:{
  325. const int32_t index(fShmControl.readInt());
  326. const float value(fShmControl.readFloat());
  327. CarlaPlugin* const plugin(getPluginUnchecked(0));
  328. if (plugin != nullptr && plugin->isEnabled())
  329. {
  330. if (index == PARAMETER_ACTIVE)
  331. {
  332. plugin->setActive((value > 0.0f), false, false);
  333. break;
  334. }
  335. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  336. plugin->setParameterValue(static_cast<uint32_t>(index), value, (opcode == kPluginBridgeOpcodeSetParameterNonRt), false, false);
  337. }
  338. break;
  339. }
  340. case kPluginBridgeOpcodeSetProgram: {
  341. const int32_t index(fShmControl.readInt());
  342. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  343. CarlaPlugin* const plugin(getPluginUnchecked(0));
  344. if (plugin != nullptr && plugin->isEnabled())
  345. {
  346. plugin->setProgram(index, false, false, false);
  347. //plugin->postponeRtEvent(kPluginPostRtEventProgramChange, index, 0, 0.0f);
  348. }
  349. break;
  350. }
  351. case kPluginBridgeOpcodeSetMidiProgram: {
  352. const int32_t index(fShmControl.readInt());
  353. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  354. CarlaPlugin* const plugin(getPluginUnchecked(0));
  355. if (plugin != nullptr && plugin->isEnabled())
  356. {
  357. plugin->setMidiProgram(index, false, false, false);
  358. //plugin->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  359. }
  360. break;
  361. }
  362. case kPluginBridgeOpcodeMidiEvent: {
  363. const int64_t time(fShmControl.readLong());
  364. const int32_t size(fShmControl.readInt());
  365. CARLA_SAFE_ASSERT_BREAK(time >= 0);
  366. CARLA_SAFE_ASSERT_BREAK(size > 0 && size <= 4);
  367. uint8_t data[size];
  368. for (int32_t i=0; i < size; ++i)
  369. data[i] = fShmControl.readUByte();
  370. CARLA_SAFE_ASSERT_BREAK(pData->events.in != nullptr);
  371. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  372. {
  373. EngineEvent& event(pData->events.in[i]);
  374. if (event.type != kEngineEventTypeNull)
  375. continue;
  376. event.fillFromMidiData(static_cast<uint8_t>(size), data);
  377. break;
  378. }
  379. break;
  380. }
  381. case kPluginBridgeOpcodeProcess: {
  382. CARLA_SAFE_ASSERT_BREAK(fShmAudioPool.data != nullptr);
  383. CarlaPlugin* const plugin(getPluginUnchecked(0));
  384. BridgeTimeInfo* const bridgeInfo(fShmTime.info);
  385. CARLA_SAFE_ASSERT_BREAK(bridgeInfo != nullptr);
  386. if (plugin != nullptr && plugin->isEnabled() && plugin->tryLock(true)) // FIXME - always lock?
  387. {
  388. const uint32_t inCount(plugin->getAudioInCount());
  389. const uint32_t outCount(plugin->getAudioOutCount());
  390. float* inBuffer[inCount];
  391. float* outBuffer[outCount];
  392. for (uint32_t i=0; i < inCount; ++i)
  393. inBuffer[i] = fShmAudioPool.data + i*pData->bufferSize;
  394. for (uint32_t i=0; i < outCount; ++i)
  395. outBuffer[i] = fShmAudioPool.data + (i+inCount)*pData->bufferSize;
  396. EngineTimeInfo& timeInfo(pData->timeInfo);
  397. timeInfo.playing = bridgeInfo->playing;
  398. timeInfo.frame = bridgeInfo->frame;
  399. timeInfo.usecs = bridgeInfo->usecs;
  400. timeInfo.valid = bridgeInfo->valid;
  401. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  402. {
  403. timeInfo.bbt.bar = bridgeInfo->bar;
  404. timeInfo.bbt.beat = bridgeInfo->beat;
  405. timeInfo.bbt.tick = bridgeInfo->tick;
  406. timeInfo.bbt.beatsPerBar = bridgeInfo->beatsPerBar;
  407. timeInfo.bbt.beatType = bridgeInfo->beatType;
  408. timeInfo.bbt.ticksPerBeat = bridgeInfo->ticksPerBeat;
  409. timeInfo.bbt.beatsPerMinute = bridgeInfo->beatsPerMinute;
  410. timeInfo.bbt.barStartTick = bridgeInfo->barStartTick;
  411. }
  412. plugin->initBuffers();
  413. plugin->process(inBuffer, outBuffer, pData->bufferSize);
  414. plugin->unlock();
  415. }
  416. // clear buffer
  417. CARLA_SAFE_ASSERT_BREAK(pData->events.in != nullptr);
  418. if (pData->events.in[0].type != kEngineEventTypeNull)
  419. carla_zeroStruct<EngineEvent>(pData->events.in, kMaxEngineEventInternalCount);
  420. break;
  421. }
  422. case kPluginBridgeOpcodeQuit:
  423. signalThreadShouldExit();
  424. fIsRunning = false;
  425. break;
  426. }
  427. }
  428. if (! jackbridge_sem_post(&fShmControl.data->runClient))
  429. carla_stderr2("Could not post to semaphore");
  430. }
  431. fIsRunning = false;
  432. }
  433. private:
  434. BridgeAudioPool fShmAudioPool;
  435. BridgeControl fShmControl;
  436. BridgeTime fShmTime;
  437. volatile bool fIsRunning;
  438. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineBridge)
  439. };
  440. // -----------------------------------------
  441. CarlaEngine* CarlaEngine::newBridge(const char* const audioBaseName, const char* const controlBaseName, const char* const timeBaseName)
  442. {
  443. return new CarlaEngineBridge(audioBaseName, controlBaseName, timeBaseName);
  444. }
  445. CARLA_BACKEND_END_NAMESPACE
  446. // -----------------------------------------------------------------------
  447. #if defined(CARLA_OS_WIN) && ! defined(__WINE__)
  448. extern "C" __declspec (dllexport)
  449. #else
  450. extern "C" __attribute__ ((visibility("default")))
  451. #endif
  452. void carla_register_native_plugin_carla();
  453. void carla_register_native_plugin_carla(){}
  454. // -----------------------------------------------------------------------