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.

784 lines
21KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2018 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. #include "CarlaEngineInternal.hpp"
  18. #include "CarlaPlugin.hpp"
  19. #include "CarlaSemUtils.hpp"
  20. #include "jackbridge/JackBridge.hpp"
  21. CARLA_BACKEND_START_NAMESPACE
  22. // -----------------------------------------------------------------------
  23. // Engine Internal helper macro, sets lastError and returns false/NULL
  24. #define CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(cond, err) if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); lastError = err; return false; }
  25. #define CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERRN(cond, err) if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); lastError = err; return nullptr; }
  26. // -----------------------------------------------------------------------
  27. // InternalEvents
  28. EngineInternalEvents::EngineInternalEvents() noexcept
  29. : in(nullptr),
  30. out(nullptr) {}
  31. EngineInternalEvents::~EngineInternalEvents() noexcept
  32. {
  33. CARLA_SAFE_ASSERT(in == nullptr);
  34. CARLA_SAFE_ASSERT(out == nullptr);
  35. }
  36. void EngineInternalEvents::clear() noexcept
  37. {
  38. if (in != nullptr)
  39. {
  40. delete[] in;
  41. in = nullptr;
  42. }
  43. if (out != nullptr)
  44. {
  45. delete[] out;
  46. out = nullptr;
  47. }
  48. }
  49. // -----------------------------------------------------------------------
  50. // InternalTime
  51. static const double kTicksPerBeat = 1920.0;
  52. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  53. static uint32_t calculate_link_latency(const double bufferSize, const double sampleRate) noexcept
  54. {
  55. CARLA_SAFE_ASSERT_RETURN(carla_isNotZero(sampleRate), 0);
  56. const long long int latency = llround(1.0e6 * bufferSize / sampleRate);
  57. CARLA_SAFE_ASSERT_RETURN(latency >= 0 && latency < UINT32_MAX, 0);
  58. return static_cast<uint32_t>(latency);
  59. }
  60. #endif
  61. EngineInternalTime::EngineInternalTime(EngineTimeInfo& ti, const EngineTransportMode& tm) noexcept
  62. : beatsPerBar(4.0),
  63. beatsPerMinute(120.0),
  64. bufferSize(0.0),
  65. sampleRate(0.0),
  66. tick(0.0),
  67. needsReset(false),
  68. nextFrame(0),
  69. #ifndef BUILD_BRIDGE
  70. hylia(),
  71. #endif
  72. timeInfo(ti),
  73. transportMode(tm) {}
  74. void EngineInternalTime::init(const uint32_t bsize, const double srate)
  75. {
  76. bufferSize = bsize;
  77. sampleRate = srate;
  78. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  79. if (hylia.instance != nullptr)
  80. {
  81. hylia_set_beats_per_bar(hylia.instance, beatsPerBar);
  82. hylia_set_beats_per_minute(hylia.instance, beatsPerMinute);
  83. hylia_set_output_latency(hylia.instance, calculate_link_latency(bsize, srate));
  84. if (hylia.enabled)
  85. hylia_enable(hylia.instance, true);
  86. }
  87. #endif
  88. needsReset = true;
  89. }
  90. void EngineInternalTime::updateAudioValues(const uint32_t bsize, const double srate)
  91. {
  92. bufferSize = bsize;
  93. sampleRate = srate;
  94. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  95. if (hylia.instance != nullptr)
  96. hylia_set_output_latency(hylia.instance, calculate_link_latency(bsize, srate));
  97. #endif
  98. needsReset = true;
  99. }
  100. void EngineInternalTime::enableLink(const bool enable)
  101. {
  102. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  103. if (hylia.enabled == enable)
  104. return;
  105. if (hylia.instance != nullptr)
  106. {
  107. hylia.enabled = enable;
  108. hylia_enable(hylia.instance, enable);
  109. }
  110. #else
  111. // unused
  112. (void)enable;
  113. #endif
  114. needsReset = true;
  115. }
  116. void EngineInternalTime::setBPM(const double bpm)
  117. {
  118. beatsPerMinute = bpm;
  119. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  120. if (hylia.instance != nullptr)
  121. hylia_set_beats_per_minute(hylia.instance, bpm);
  122. #endif
  123. }
  124. void EngineInternalTime::setNeedsReset() noexcept
  125. {
  126. needsReset = true;
  127. }
  128. void EngineInternalTime::pause() noexcept
  129. {
  130. timeInfo.playing = false;
  131. nextFrame = timeInfo.frame;
  132. needsReset = true;
  133. }
  134. void EngineInternalTime::relocate(const uint64_t frame) noexcept
  135. {
  136. timeInfo.frame = frame;
  137. nextFrame = frame;
  138. needsReset = true;
  139. }
  140. void EngineInternalTime::fillEngineTimeInfo(const uint32_t newFrames) noexcept
  141. {
  142. CARLA_SAFE_ASSERT_RETURN(carla_isNotZero(sampleRate),);
  143. CARLA_SAFE_ASSERT_RETURN(newFrames > 0,);
  144. double ticktmp;
  145. if (transportMode == ENGINE_TRANSPORT_MODE_INTERNAL)
  146. {
  147. timeInfo.usecs = 0;
  148. timeInfo.frame = nextFrame;
  149. }
  150. if (needsReset)
  151. {
  152. timeInfo.bbt.valid = true;
  153. timeInfo.bbt.beatType = 4.0f;
  154. timeInfo.bbt.ticksPerBeat = kTicksPerBeat;
  155. double abs_beat, abs_tick;
  156. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  157. if (hylia.enabled)
  158. {
  159. if (hylia.timeInfo.beat >= 0.0)
  160. {
  161. abs_beat = hylia.timeInfo.beat;
  162. abs_tick = abs_beat * kTicksPerBeat;
  163. }
  164. else
  165. {
  166. abs_beat = 0.0;
  167. abs_tick = 0.0;
  168. timeInfo.playing = false;
  169. }
  170. }
  171. else
  172. #endif
  173. {
  174. const double min = static_cast<double>(timeInfo.frame) / (sampleRate * 60.0);
  175. abs_beat = min * beatsPerMinute;
  176. abs_tick = abs_beat * kTicksPerBeat;
  177. needsReset = false;
  178. }
  179. const double bar = std::floor(abs_beat / beatsPerBar);
  180. const double beat = std::floor(std::fmod(abs_beat, beatsPerBar));
  181. timeInfo.bbt.bar = static_cast<int32_t>(bar) + 1;
  182. timeInfo.bbt.beat = static_cast<int32_t>(beat) + 1;
  183. timeInfo.bbt.barStartTick = ((bar * beatsPerBar) + beat) * kTicksPerBeat;
  184. ticktmp = abs_tick - timeInfo.bbt.barStartTick;
  185. }
  186. else if (timeInfo.playing)
  187. {
  188. ticktmp = tick + (newFrames * kTicksPerBeat * beatsPerMinute / (sampleRate * 60));
  189. while (ticktmp >= kTicksPerBeat)
  190. {
  191. ticktmp -= kTicksPerBeat;
  192. if (++timeInfo.bbt.beat > beatsPerBar)
  193. {
  194. ++timeInfo.bbt.bar;
  195. timeInfo.bbt.beat = 1;
  196. timeInfo.bbt.barStartTick += beatsPerBar * kTicksPerBeat;
  197. }
  198. }
  199. }
  200. else
  201. {
  202. ticktmp = tick;
  203. }
  204. timeInfo.bbt.beatsPerBar = static_cast<float>(beatsPerBar);
  205. timeInfo.bbt.beatsPerMinute = beatsPerMinute;
  206. timeInfo.bbt.tick = ticktmp;
  207. tick = ticktmp;
  208. if (transportMode == ENGINE_TRANSPORT_MODE_INTERNAL && timeInfo.playing)
  209. nextFrame += newFrames;
  210. }
  211. void EngineInternalTime::fillJackTimeInfo(jack_position_t* const pos, const uint32_t newFrames) noexcept
  212. {
  213. CARLA_SAFE_ASSERT_RETURN(carla_isNotZero(sampleRate),);
  214. CARLA_SAFE_ASSERT_RETURN(newFrames > 0,);
  215. CARLA_SAFE_ASSERT(transportMode == ENGINE_TRANSPORT_MODE_JACK);
  216. fillEngineTimeInfo(newFrames);
  217. pos->valid = JackPositionBBT;
  218. pos->bar = timeInfo.bbt.bar;
  219. pos->beat = timeInfo.bbt.beat;
  220. pos->tick = static_cast<int32_t>(tick + 0.5);
  221. pos->bar_start_tick = timeInfo.bbt.barStartTick;
  222. pos->beats_per_bar = timeInfo.bbt.beatsPerBar;
  223. pos->beat_type = timeInfo.bbt.beatType;
  224. pos->ticks_per_beat = kTicksPerBeat;
  225. pos->beats_per_minute = beatsPerMinute;
  226. }
  227. void EngineInternalTime::preProcess(const uint32_t numFrames)
  228. {
  229. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  230. if (hylia.enabled)
  231. {
  232. hylia_process(hylia.instance, numFrames, &hylia.timeInfo);
  233. const double new_bpb = hylia.timeInfo.beatsPerBar;
  234. const double new_bpm = hylia.timeInfo.beatsPerMinute;
  235. if (new_bpb >= 1.0 && carla_isNotEqual(beatsPerBar, new_bpb))
  236. {
  237. beatsPerBar = new_bpb;
  238. needsReset = true;
  239. }
  240. if (new_bpm > 0.0 && carla_isNotEqual(beatsPerMinute, new_bpm))
  241. {
  242. beatsPerMinute = new_bpm;
  243. needsReset = true;
  244. }
  245. }
  246. #endif
  247. if (transportMode == ENGINE_TRANSPORT_MODE_INTERNAL)
  248. fillEngineTimeInfo(numFrames);
  249. }
  250. // -----------------------------------------------------------------------
  251. // EngineInternalTime::Hylia
  252. #ifndef BUILD_BRIDGE
  253. EngineInternalTime::Hylia::Hylia()
  254. : enabled(false),
  255. instance(nullptr),
  256. timeInfo()
  257. {
  258. carla_zeroStruct(timeInfo);
  259. # ifdef HAVE_HYLIA
  260. instance = hylia_create();
  261. # endif
  262. }
  263. EngineInternalTime::Hylia::~Hylia()
  264. {
  265. # ifdef HAVE_HYLIA
  266. hylia_cleanup(instance);
  267. # endif
  268. }
  269. #endif
  270. // -----------------------------------------------------------------------
  271. // NextAction
  272. EngineNextAction::EngineNextAction() noexcept
  273. : opcode(kEnginePostActionNull),
  274. pluginId(0),
  275. value(0),
  276. mutex(),
  277. needsPost(false),
  278. postDone(false),
  279. sem(carla_sem_create(false)) {}
  280. EngineNextAction::~EngineNextAction() noexcept
  281. {
  282. CARLA_SAFE_ASSERT(opcode == kEnginePostActionNull);
  283. if (sem != nullptr)
  284. {
  285. carla_sem_destroy(sem);
  286. sem = nullptr;
  287. }
  288. }
  289. void EngineNextAction::clearAndReset() noexcept
  290. {
  291. mutex.lock();
  292. CARLA_SAFE_ASSERT(opcode == kEnginePostActionNull);
  293. opcode = kEnginePostActionNull;
  294. pluginId = 0;
  295. value = 0;
  296. needsPost = false;
  297. postDone = false;
  298. mutex.unlock();
  299. }
  300. // -----------------------------------------------------------------------
  301. // CarlaEngine::ProtectedData
  302. CarlaEngine::ProtectedData::ProtectedData(CarlaEngine* const engine) noexcept
  303. : thread(engine),
  304. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  305. osc(engine),
  306. oscData(nullptr),
  307. #endif
  308. callback(nullptr),
  309. callbackPtr(nullptr),
  310. fileCallback(nullptr),
  311. fileCallbackPtr(nullptr),
  312. actionCanceled(false),
  313. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  314. loadingProject(false),
  315. currentProjectFilename(),
  316. #endif
  317. hints(0x0),
  318. bufferSize(0),
  319. sampleRate(0.0),
  320. aboutToClose(false),
  321. isIdling(0),
  322. curPluginCount(0),
  323. maxPluginNumber(0),
  324. nextPluginId(0),
  325. envMutex(),
  326. lastError(),
  327. name(),
  328. options(),
  329. timeInfo(),
  330. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  331. plugins(nullptr),
  332. xruns(0),
  333. #endif
  334. events(),
  335. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  336. graph(engine),
  337. #endif
  338. time(timeInfo, options.transportMode),
  339. nextAction()
  340. {
  341. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  342. carla_zeroStructs(plugins, 1);
  343. #endif
  344. }
  345. CarlaEngine::ProtectedData::~ProtectedData() noexcept
  346. {
  347. CARLA_SAFE_ASSERT(curPluginCount == 0);
  348. CARLA_SAFE_ASSERT(maxPluginNumber == 0);
  349. CARLA_SAFE_ASSERT(nextPluginId == 0);
  350. CARLA_SAFE_ASSERT(isIdling == 0);
  351. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  352. CARLA_SAFE_ASSERT(plugins == nullptr);
  353. #endif
  354. }
  355. // -----------------------------------------------------------------------
  356. bool CarlaEngine::ProtectedData::init(const char* const clientName)
  357. {
  358. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(name.isEmpty(), "Invalid engine internal data (err #1)");
  359. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  360. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(oscData == nullptr, "Invalid engine internal data (err #2)");
  361. #endif
  362. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(events.in == nullptr, "Invalid engine internal data (err #4)");
  363. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(events.out == nullptr, "Invalid engine internal data (err #5)");
  364. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(clientName != nullptr && clientName[0] != '\0', "Invalid client name");
  365. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  366. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(plugins == nullptr, "Invalid engine internal data (err #3)");
  367. #endif
  368. aboutToClose = false;
  369. curPluginCount = 0;
  370. nextPluginId = 0;
  371. switch (options.processMode)
  372. {
  373. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  374. maxPluginNumber = MAX_RACK_PLUGINS;
  375. options.forceStereo = true;
  376. break;
  377. case ENGINE_PROCESS_MODE_PATCHBAY:
  378. maxPluginNumber = MAX_PATCHBAY_PLUGINS;
  379. break;
  380. case ENGINE_PROCESS_MODE_BRIDGE:
  381. maxPluginNumber = 1;
  382. break;
  383. default:
  384. maxPluginNumber = MAX_DEFAULT_PLUGINS;
  385. break;
  386. }
  387. switch (options.processMode)
  388. {
  389. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  390. case ENGINE_PROCESS_MODE_PATCHBAY:
  391. case ENGINE_PROCESS_MODE_BRIDGE:
  392. events.in = new EngineEvent[kMaxEngineEventInternalCount];
  393. events.out = new EngineEvent[kMaxEngineEventInternalCount];
  394. carla_zeroStructs(events.in, kMaxEngineEventInternalCount);
  395. carla_zeroStructs(events.out, kMaxEngineEventInternalCount);
  396. break;
  397. default:
  398. break;
  399. }
  400. nextPluginId = maxPluginNumber;
  401. name = clientName;
  402. name.toBasic();
  403. timeInfo.clear();
  404. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  405. osc.init(clientName);
  406. oscData = osc.getControlData();
  407. #endif
  408. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  409. plugins = new EnginePluginData[maxPluginNumber];
  410. carla_zeroStructs(plugins, maxPluginNumber);
  411. xruns = 0;
  412. #endif
  413. nextAction.clearAndReset();
  414. thread.startThread();
  415. return true;
  416. }
  417. void CarlaEngine::ProtectedData::close()
  418. {
  419. CARLA_SAFE_ASSERT(name.isNotEmpty());
  420. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  421. CARLA_SAFE_ASSERT(plugins != nullptr);
  422. CARLA_SAFE_ASSERT(nextPluginId == maxPluginNumber);
  423. #endif
  424. aboutToClose = true;
  425. thread.stopThread(500);
  426. nextAction.clearAndReset();
  427. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  428. osc.close();
  429. oscData = nullptr;
  430. #endif
  431. aboutToClose = false;
  432. curPluginCount = 0;
  433. maxPluginNumber = 0;
  434. nextPluginId = 0;
  435. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  436. if (plugins != nullptr)
  437. {
  438. delete[] plugins;
  439. plugins = nullptr;
  440. }
  441. #endif
  442. events.clear();
  443. name.clear();
  444. }
  445. void CarlaEngine::ProtectedData::initTime(const char* const features)
  446. {
  447. time.init(bufferSize, sampleRate);
  448. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  449. const bool linkEnabled = features != nullptr && std::strstr(features, ":link:") != nullptr;
  450. time.enableLink(linkEnabled);
  451. #else
  452. return;
  453. // unused
  454. (void)features;
  455. #endif
  456. }
  457. // -----------------------------------------------------------------------
  458. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  459. void CarlaEngine::ProtectedData::doPluginRemove(const uint pluginId) noexcept
  460. {
  461. CARLA_SAFE_ASSERT_RETURN(curPluginCount > 0,);
  462. CARLA_SAFE_ASSERT_RETURN(pluginId < curPluginCount,);
  463. --curPluginCount;
  464. // move all plugins 1 spot backwards
  465. for (uint i=pluginId; i < curPluginCount; ++i)
  466. {
  467. CarlaPlugin* const plugin(plugins[i+1].plugin);
  468. CARLA_SAFE_ASSERT_BREAK(plugin != nullptr);
  469. plugin->setId(i);
  470. plugins[i].plugin = plugin;
  471. carla_zeroFloats(plugins[i].peaks, 4);
  472. }
  473. const uint id(curPluginCount);
  474. // reset last plugin (now removed)
  475. plugins[id].plugin = nullptr;
  476. carla_zeroFloats(plugins[id].peaks, 4);
  477. }
  478. void CarlaEngine::ProtectedData::doPluginsSwitch(const uint idA, const uint idB) noexcept
  479. {
  480. CARLA_SAFE_ASSERT_RETURN(curPluginCount >= 2,);
  481. CARLA_SAFE_ASSERT_RETURN(idA < curPluginCount,);
  482. CARLA_SAFE_ASSERT_RETURN(idB < curPluginCount,);
  483. CarlaPlugin* const pluginA(plugins[idA].plugin);
  484. CARLA_SAFE_ASSERT_RETURN(pluginA != nullptr,);
  485. CarlaPlugin* const pluginB(plugins[idB].plugin);
  486. CARLA_SAFE_ASSERT_RETURN(pluginB != nullptr,);
  487. pluginA->setId(idB);
  488. plugins[idA].plugin = pluginB;
  489. pluginB->setId(idA);
  490. plugins[idB].plugin = pluginA;
  491. }
  492. #endif
  493. void CarlaEngine::ProtectedData::doNextPluginAction() noexcept
  494. {
  495. if (! nextAction.mutex.tryLock())
  496. return;
  497. const EnginePostAction opcode = nextAction.opcode;
  498. const bool needsPost = nextAction.needsPost;
  499. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  500. const uint pluginId = nextAction.pluginId;
  501. const uint value = nextAction.value;
  502. #endif
  503. nextAction.opcode = kEnginePostActionNull;
  504. nextAction.pluginId = 0;
  505. nextAction.value = 0;
  506. nextAction.needsPost = false;
  507. nextAction.mutex.unlock();
  508. switch (opcode)
  509. {
  510. case kEnginePostActionNull:
  511. break;
  512. case kEnginePostActionZeroCount:
  513. curPluginCount = 0;
  514. break;
  515. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  516. case kEnginePostActionRemovePlugin:
  517. doPluginRemove(pluginId);
  518. break;
  519. case kEnginePostActionSwitchPlugins:
  520. doPluginsSwitch(pluginId, value);
  521. break;
  522. #endif
  523. }
  524. if (needsPost)
  525. {
  526. if (nextAction.sem != nullptr)
  527. carla_sem_post(*nextAction.sem);
  528. nextAction.postDone = true;
  529. }
  530. }
  531. // -----------------------------------------------------------------------
  532. // PendingRtEventsRunner
  533. PendingRtEventsRunner::PendingRtEventsRunner(CarlaEngine* const engine, const uint32_t frames) noexcept
  534. : pData(engine->pData)
  535. {
  536. pData->time.preProcess(frames);
  537. }
  538. PendingRtEventsRunner::~PendingRtEventsRunner() noexcept
  539. {
  540. pData->doNextPluginAction();
  541. }
  542. // -----------------------------------------------------------------------
  543. // ScopedActionLock
  544. ScopedActionLock::ScopedActionLock(CarlaEngine* const engine,
  545. const EnginePostAction action,
  546. const uint pluginId,
  547. const uint value) noexcept
  548. : pData(engine->pData)
  549. {
  550. CARLA_SAFE_ASSERT_RETURN(action != kEnginePostActionNull,);
  551. {
  552. const CarlaMutexLocker cml(pData->nextAction.mutex);
  553. CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull,);
  554. pData->nextAction.opcode = action;
  555. pData->nextAction.pluginId = pluginId;
  556. pData->nextAction.value = value;
  557. pData->nextAction.needsPost = engine->isRunning();
  558. pData->nextAction.postDone = false;
  559. }
  560. #ifdef BUILD_BRIDGE
  561. #define ACTION_MSG_PREFIX "Bridge: "
  562. #else
  563. #define ACTION_MSG_PREFIX ""
  564. #endif
  565. if (pData->nextAction.needsPost)
  566. {
  567. #if defined(DEBUG) || defined(BUILD_BRIDGE)
  568. // block wait for unlock on processing side
  569. carla_stdout(ACTION_MSG_PREFIX "ScopedPluginAction(%i) - blocking START", pluginId);
  570. #endif
  571. bool engineStoppedWhileWaiting = false;
  572. if (! pData->nextAction.postDone)
  573. {
  574. for (int i = 10; --i >= 0;)
  575. {
  576. if (pData->nextAction.sem != nullptr)
  577. {
  578. if (carla_sem_timedwait(*pData->nextAction.sem, 200))
  579. break;
  580. }
  581. else
  582. {
  583. carla_msleep(200);
  584. }
  585. if (! engine->isRunning())
  586. {
  587. engineStoppedWhileWaiting = true;
  588. break;
  589. }
  590. }
  591. }
  592. #if defined(DEBUG) || defined(BUILD_BRIDGE)
  593. carla_stdout(ACTION_MSG_PREFIX "ScopedPluginAction(%i) - blocking DONE", pluginId);
  594. #endif
  595. // check if anything went wrong...
  596. if (! pData->nextAction.postDone)
  597. {
  598. bool needsCorrection = false;
  599. {
  600. const CarlaMutexLocker cml(pData->nextAction.mutex);
  601. if (pData->nextAction.opcode != kEnginePostActionNull)
  602. {
  603. needsCorrection = true;
  604. pData->nextAction.needsPost = false;
  605. }
  606. }
  607. if (needsCorrection)
  608. {
  609. pData->doNextPluginAction();
  610. if (! engineStoppedWhileWaiting)
  611. carla_stderr2(ACTION_MSG_PREFIX "Failed to wait for engine, is audio not running?");
  612. }
  613. }
  614. }
  615. else
  616. {
  617. pData->doNextPluginAction();
  618. }
  619. }
  620. ScopedActionLock::~ScopedActionLock() noexcept
  621. {
  622. CARLA_SAFE_ASSERT(pData->nextAction.opcode == kEnginePostActionNull);
  623. }
  624. // -----------------------------------------------------------------------
  625. // ScopedThreadStopper
  626. ScopedThreadStopper::ScopedThreadStopper(CarlaEngine* const e) noexcept
  627. : engine(e),
  628. pData(e->pData)
  629. {
  630. pData->thread.stopThread(500);
  631. }
  632. ScopedThreadStopper::~ScopedThreadStopper() noexcept
  633. {
  634. if (engine->isRunning() && ! pData->aboutToClose)
  635. pData->thread.startThread();
  636. }
  637. // -----------------------------------------------------------------------
  638. // ScopedEngineEnvironmentLocker
  639. ScopedEngineEnvironmentLocker::ScopedEngineEnvironmentLocker(CarlaEngine* const engine) noexcept
  640. : pData(engine->pData)
  641. {
  642. pData->envMutex.lock();
  643. }
  644. ScopedEngineEnvironmentLocker::~ScopedEngineEnvironmentLocker() noexcept
  645. {
  646. pData->envMutex.unlock();
  647. }
  648. // -----------------------------------------------------------------------
  649. CARLA_BACKEND_END_NAMESPACE