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.

590 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_stdout("CarlaEngineBridge::CarlaEngineBridge(%s, %s, %s)", audioBaseName, controlBaseName, timeBaseName);
  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 stackBufferSize = fShmControl.readUInt();
  235. CARLA_SAFE_ASSERT_INT2(stackBufferSize == sizeof(StackBuffer), stackBufferSize, sizeof(StackBuffer));
  236. const uint32_t shmStructSize = fShmControl.readUInt();
  237. CARLA_SAFE_ASSERT_INT2(shmStructSize == sizeof(BridgeShmControl), shmStructSize, sizeof(BridgeShmControl));
  238. const uint32_t timeStructSize = fShmControl.readUInt();
  239. CARLA_SAFE_ASSERT_INT2(timeStructSize == sizeof(BridgeTimeInfo), timeStructSize, sizeof(BridgeTimeInfo));
  240. opcode = fShmControl.readOpcode();
  241. CARLA_SAFE_ASSERT_INT(opcode == kPluginBridgeOpcodeSetBufferSize, opcode);
  242. pData->bufferSize = fShmControl.readUInt();
  243. opcode = fShmControl.readOpcode();
  244. CARLA_SAFE_ASSERT_INT(opcode == kPluginBridgeOpcodeSetSampleRate, opcode);
  245. pData->sampleRate = fShmControl.readFloat();
  246. carla_stdout("Carla Client Info:");
  247. carla_stdout(" BufferSize: %i", pData->bufferSize);
  248. carla_stdout(" SampleRate: %f", pData->sampleRate);
  249. carla_stdout(" sizeof(StackBuffer): %i/" P_SIZE, stackBufferSize, sizeof(StackBuffer));
  250. carla_stdout(" sizeof(BridgeShmControl): %i/" P_SIZE, shmStructSize, sizeof(BridgeShmControl));
  251. carla_stdout(" sizeof(BridgeTimeInfo): %i/" P_SIZE, timeStructSize, sizeof(BridgeTimeInfo));
  252. CarlaThread::startThread();
  253. CarlaEngine::init(clientName);
  254. return true;
  255. }
  256. bool close() override
  257. {
  258. carla_debug("CarlaEnginePlugin::close()");
  259. CarlaEngine::close();
  260. CarlaThread::stopThread(6000);
  261. fShmTime.clear();
  262. fShmControl.clear();
  263. fShmAudioPool.clear();
  264. return true;
  265. }
  266. bool isRunning() const noexcept override
  267. {
  268. return fIsRunning;
  269. }
  270. bool isOffline() const noexcept override
  271. {
  272. return false;
  273. }
  274. EngineType getType() const noexcept override
  275. {
  276. return kEngineTypeBridge;
  277. }
  278. const char* getCurrentDriverName() const noexcept
  279. {
  280. return "Bridge";
  281. }
  282. // -------------------------------------
  283. // CarlaThread virtual calls
  284. void run() override
  285. {
  286. fIsRunning = true;
  287. // TODO - set RT permissions
  288. carla_debug("CarlaEngineBridge::run()");
  289. for (; ! shouldThreadExit();)
  290. {
  291. if (! jackbridge_sem_timedwait(&fShmControl.data->runServer, 5))
  292. {
  293. if (errno == ETIMEDOUT)
  294. {
  295. fIsRunning = false;
  296. signalThreadShouldExit();
  297. return;
  298. }
  299. }
  300. for (; fShmControl.isDataAvailableForReading();)
  301. {
  302. const PluginBridgeOpcode opcode(fShmControl.readOpcode());
  303. if (opcode != kPluginBridgeOpcodeProcess) {
  304. carla_debug("CarlaEngineBridge::run() - got opcode: %s", PluginBridgeOpcode2str(opcode));
  305. }
  306. switch (opcode)
  307. {
  308. case kPluginBridgeOpcodeNull:
  309. break;
  310. case kPluginBridgeOpcodeSetAudioPool: {
  311. const int64_t poolSize(fShmControl.readLong());
  312. CARLA_SAFE_ASSERT_BREAK(poolSize > 0);
  313. fShmAudioPool.data = (float*)jackbridge_shm_map(fShmAudioPool.shm, static_cast<size_t>(poolSize));
  314. break;
  315. }
  316. case kPluginBridgeOpcodeSetBufferSize: {
  317. const uint32_t bufferSize(fShmControl.readUInt());
  318. bufferSizeChanged(bufferSize);
  319. break;
  320. }
  321. case kPluginBridgeOpcodeSetSampleRate: {
  322. const float sampleRate(fShmControl.readFloat());
  323. sampleRateChanged(sampleRate);
  324. break;
  325. }
  326. case kPluginBridgeOpcodeSetParameterRt:
  327. case kPluginBridgeOpcodeSetParameterNonRt:{
  328. const int32_t index(fShmControl.readInt());
  329. const float value(fShmControl.readFloat());
  330. CarlaPlugin* const plugin(getPluginUnchecked(0));
  331. if (plugin != nullptr && plugin->isEnabled())
  332. {
  333. if (index == PARAMETER_ACTIVE)
  334. {
  335. plugin->setActive((value > 0.0f), false, false);
  336. break;
  337. }
  338. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  339. plugin->setParameterValue(static_cast<uint32_t>(index), value, (opcode == kPluginBridgeOpcodeSetParameterNonRt), false, false);
  340. }
  341. break;
  342. }
  343. case kPluginBridgeOpcodeSetProgram: {
  344. const int32_t index(fShmControl.readInt());
  345. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  346. CarlaPlugin* const plugin(getPluginUnchecked(0));
  347. if (plugin != nullptr && plugin->isEnabled())
  348. {
  349. plugin->setProgram(index, false, false, false);
  350. //plugin->postponeRtEvent(kPluginPostRtEventProgramChange, index, 0, 0.0f);
  351. }
  352. break;
  353. }
  354. case kPluginBridgeOpcodeSetMidiProgram: {
  355. const int32_t index(fShmControl.readInt());
  356. CARLA_SAFE_ASSERT_BREAK(index >= 0);
  357. CarlaPlugin* const plugin(getPluginUnchecked(0));
  358. if (plugin != nullptr && plugin->isEnabled())
  359. {
  360. plugin->setMidiProgram(index, false, false, false);
  361. //plugin->postponeRtEvent(kPluginPostRtEventMidiProgramChange, index, 0, 0.0f);
  362. }
  363. break;
  364. }
  365. case kPluginBridgeOpcodeMidiEvent: {
  366. const int64_t time(fShmControl.readLong());
  367. const int32_t size(fShmControl.readInt());
  368. CARLA_SAFE_ASSERT_BREAK(time >= 0);
  369. CARLA_SAFE_ASSERT_BREAK(size > 0 && size <= 4);
  370. uint8_t data[size];
  371. for (int32_t i=0; i < size; ++i)
  372. data[i] = fShmControl.readByte();
  373. CARLA_SAFE_ASSERT_BREAK(pData->events.in != nullptr);
  374. for (ushort i=0; i < kMaxEngineEventInternalCount; ++i)
  375. {
  376. EngineEvent& event(pData->events.in[i]);
  377. if (event.type != kEngineEventTypeNull)
  378. continue;
  379. event.fillFromMidiData(static_cast<uint8_t>(size), data);
  380. break;
  381. }
  382. break;
  383. }
  384. case kPluginBridgeOpcodeProcess: {
  385. CARLA_SAFE_ASSERT_BREAK(fShmAudioPool.data != nullptr);
  386. CarlaPlugin* const plugin(getPluginUnchecked(0));
  387. BridgeTimeInfo* const bridgeInfo(fShmTime.info);
  388. CARLA_SAFE_ASSERT_BREAK(bridgeInfo != nullptr);
  389. if (plugin != nullptr && plugin->isEnabled() && plugin->tryLock(true)) // FIXME - always lock?
  390. {
  391. const uint32_t inCount(plugin->getAudioInCount());
  392. const uint32_t outCount(plugin->getAudioOutCount());
  393. float* inBuffer[inCount];
  394. float* outBuffer[outCount];
  395. for (uint32_t i=0; i < inCount; ++i)
  396. inBuffer[i] = fShmAudioPool.data + i*pData->bufferSize;
  397. for (uint32_t i=0; i < outCount; ++i)
  398. outBuffer[i] = fShmAudioPool.data + (i+inCount)*pData->bufferSize;
  399. EngineTimeInfo& timeInfo(pData->timeInfo);
  400. timeInfo.playing = bridgeInfo->playing;
  401. timeInfo.frame = bridgeInfo->frame;
  402. timeInfo.usecs = bridgeInfo->usecs;
  403. timeInfo.valid = bridgeInfo->valid;
  404. if (timeInfo.valid & EngineTimeInfo::kValidBBT)
  405. {
  406. timeInfo.bbt.bar = bridgeInfo->bar;
  407. timeInfo.bbt.beat = bridgeInfo->beat;
  408. timeInfo.bbt.tick = bridgeInfo->tick;
  409. timeInfo.bbt.beatsPerBar = bridgeInfo->beatsPerBar;
  410. timeInfo.bbt.beatType = bridgeInfo->beatType;
  411. timeInfo.bbt.ticksPerBeat = bridgeInfo->ticksPerBeat;
  412. timeInfo.bbt.beatsPerMinute = bridgeInfo->beatsPerMinute;
  413. timeInfo.bbt.barStartTick = bridgeInfo->barStartTick;
  414. }
  415. plugin->initBuffers();
  416. plugin->process(inBuffer, outBuffer, pData->bufferSize);
  417. plugin->unlock();
  418. }
  419. // clear buffer
  420. CARLA_SAFE_ASSERT_BREAK(pData->events.in != nullptr);
  421. if (pData->events.in[0].type != kEngineEventTypeNull)
  422. carla_zeroStruct<EngineEvent>(pData->events.in, kMaxEngineEventInternalCount);
  423. break;
  424. }
  425. case kPluginBridgeOpcodeQuit:
  426. signalThreadShouldExit();
  427. fIsRunning = false;
  428. break;
  429. }
  430. }
  431. if (! jackbridge_sem_post(&fShmControl.data->runClient))
  432. carla_stderr2("Could not post to semaphore");
  433. }
  434. fIsRunning = false;
  435. }
  436. private:
  437. BridgeAudioPool fShmAudioPool;
  438. BridgeControl fShmControl;
  439. BridgeTime fShmTime;
  440. volatile bool fIsRunning;
  441. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaEngineBridge)
  442. };
  443. // -----------------------------------------
  444. CarlaEngine* CarlaEngine::newBridge(const char* const audioBaseName, const char* const controlBaseName, const char* const timeBaseName)
  445. {
  446. return new CarlaEngineBridge(audioBaseName, controlBaseName, timeBaseName);
  447. }
  448. CARLA_BACKEND_END_NAMESPACE
  449. // -----------------------------------------------------------------------
  450. #if defined(CARLA_OS_WIN) && ! defined(__WINE__)
  451. extern "C" __declspec (dllexport)
  452. #else
  453. extern "C" __attribute__ ((visibility("default")))
  454. #endif
  455. void carla_register_native_plugin_carla();
  456. void carla_register_native_plugin_carla(){}
  457. // -----------------------------------------------------------------------