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.

782 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. #endif
  333. events(),
  334. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  335. graph(engine),
  336. #endif
  337. time(timeInfo, options.transportMode),
  338. nextAction()
  339. {
  340. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  341. carla_zeroStructs(plugins, 1);
  342. #endif
  343. }
  344. CarlaEngine::ProtectedData::~ProtectedData() noexcept
  345. {
  346. CARLA_SAFE_ASSERT(curPluginCount == 0);
  347. CARLA_SAFE_ASSERT(maxPluginNumber == 0);
  348. CARLA_SAFE_ASSERT(nextPluginId == 0);
  349. CARLA_SAFE_ASSERT(isIdling == 0);
  350. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  351. CARLA_SAFE_ASSERT(plugins == nullptr);
  352. #endif
  353. }
  354. // -----------------------------------------------------------------------
  355. bool CarlaEngine::ProtectedData::init(const char* const clientName)
  356. {
  357. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(name.isEmpty(), "Invalid engine internal data (err #1)");
  358. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  359. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(oscData == nullptr, "Invalid engine internal data (err #2)");
  360. #endif
  361. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(events.in == nullptr, "Invalid engine internal data (err #4)");
  362. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(events.out == nullptr, "Invalid engine internal data (err #5)");
  363. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(clientName != nullptr && clientName[0] != '\0', "Invalid client name");
  364. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  365. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(plugins == nullptr, "Invalid engine internal data (err #3)");
  366. #endif
  367. aboutToClose = false;
  368. curPluginCount = 0;
  369. nextPluginId = 0;
  370. switch (options.processMode)
  371. {
  372. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  373. maxPluginNumber = MAX_RACK_PLUGINS;
  374. options.forceStereo = true;
  375. break;
  376. case ENGINE_PROCESS_MODE_PATCHBAY:
  377. maxPluginNumber = MAX_PATCHBAY_PLUGINS;
  378. break;
  379. case ENGINE_PROCESS_MODE_BRIDGE:
  380. maxPluginNumber = 1;
  381. break;
  382. default:
  383. maxPluginNumber = MAX_DEFAULT_PLUGINS;
  384. break;
  385. }
  386. switch (options.processMode)
  387. {
  388. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  389. case ENGINE_PROCESS_MODE_PATCHBAY:
  390. case ENGINE_PROCESS_MODE_BRIDGE:
  391. events.in = new EngineEvent[kMaxEngineEventInternalCount];
  392. events.out = new EngineEvent[kMaxEngineEventInternalCount];
  393. carla_zeroStructs(events.in, kMaxEngineEventInternalCount);
  394. carla_zeroStructs(events.out, kMaxEngineEventInternalCount);
  395. break;
  396. default:
  397. break;
  398. }
  399. nextPluginId = maxPluginNumber;
  400. name = clientName;
  401. name.toBasic();
  402. timeInfo.clear();
  403. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  404. osc.init(clientName);
  405. oscData = osc.getControlData();
  406. #endif
  407. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  408. plugins = new EnginePluginData[maxPluginNumber];
  409. carla_zeroStructs(plugins, maxPluginNumber);
  410. #endif
  411. nextAction.clearAndReset();
  412. thread.startThread();
  413. return true;
  414. }
  415. void CarlaEngine::ProtectedData::close()
  416. {
  417. CARLA_SAFE_ASSERT(name.isNotEmpty());
  418. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  419. CARLA_SAFE_ASSERT(plugins != nullptr);
  420. CARLA_SAFE_ASSERT(nextPluginId == maxPluginNumber);
  421. #endif
  422. aboutToClose = true;
  423. thread.stopThread(500);
  424. nextAction.clearAndReset();
  425. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  426. osc.close();
  427. oscData = nullptr;
  428. #endif
  429. aboutToClose = false;
  430. curPluginCount = 0;
  431. maxPluginNumber = 0;
  432. nextPluginId = 0;
  433. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  434. if (plugins != nullptr)
  435. {
  436. delete[] plugins;
  437. plugins = nullptr;
  438. }
  439. #endif
  440. events.clear();
  441. name.clear();
  442. }
  443. void CarlaEngine::ProtectedData::initTime(const char* const features)
  444. {
  445. time.init(bufferSize, sampleRate);
  446. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  447. const bool linkEnabled = features != nullptr && std::strstr(features, ":link:") != nullptr;
  448. time.enableLink(linkEnabled);
  449. #else
  450. return;
  451. // unused
  452. (void)features;
  453. #endif
  454. }
  455. // -----------------------------------------------------------------------
  456. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  457. void CarlaEngine::ProtectedData::doPluginRemove(const uint pluginId) noexcept
  458. {
  459. CARLA_SAFE_ASSERT_RETURN(curPluginCount > 0,);
  460. CARLA_SAFE_ASSERT_RETURN(pluginId < curPluginCount,);
  461. --curPluginCount;
  462. // move all plugins 1 spot backwards
  463. for (uint i=pluginId; i < curPluginCount; ++i)
  464. {
  465. CarlaPlugin* const plugin(plugins[i+1].plugin);
  466. CARLA_SAFE_ASSERT_BREAK(plugin != nullptr);
  467. plugin->setId(i);
  468. plugins[i].plugin = plugin;
  469. carla_zeroFloats(plugins[i].peaks, 4);
  470. }
  471. const uint id(curPluginCount);
  472. // reset last plugin (now removed)
  473. plugins[id].plugin = nullptr;
  474. carla_zeroFloats(plugins[id].peaks, 4);
  475. }
  476. void CarlaEngine::ProtectedData::doPluginsSwitch(const uint idA, const uint idB) noexcept
  477. {
  478. CARLA_SAFE_ASSERT_RETURN(curPluginCount >= 2,);
  479. CARLA_SAFE_ASSERT_RETURN(idA < curPluginCount,);
  480. CARLA_SAFE_ASSERT_RETURN(idB < curPluginCount,);
  481. CarlaPlugin* const pluginA(plugins[idA].plugin);
  482. CARLA_SAFE_ASSERT_RETURN(pluginA != nullptr,);
  483. CarlaPlugin* const pluginB(plugins[idB].plugin);
  484. CARLA_SAFE_ASSERT_RETURN(pluginB != nullptr,);
  485. pluginA->setId(idB);
  486. plugins[idA].plugin = pluginB;
  487. pluginB->setId(idA);
  488. plugins[idB].plugin = pluginA;
  489. }
  490. #endif
  491. void CarlaEngine::ProtectedData::doNextPluginAction() noexcept
  492. {
  493. if (! nextAction.mutex.tryLock())
  494. return;
  495. const EnginePostAction opcode = nextAction.opcode;
  496. const bool needsPost = nextAction.needsPost;
  497. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  498. const uint pluginId = nextAction.pluginId;
  499. const uint value = nextAction.value;
  500. #endif
  501. nextAction.opcode = kEnginePostActionNull;
  502. nextAction.pluginId = 0;
  503. nextAction.value = 0;
  504. nextAction.needsPost = false;
  505. nextAction.mutex.unlock();
  506. switch (opcode)
  507. {
  508. case kEnginePostActionNull:
  509. break;
  510. case kEnginePostActionZeroCount:
  511. curPluginCount = 0;
  512. break;
  513. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  514. case kEnginePostActionRemovePlugin:
  515. doPluginRemove(pluginId);
  516. break;
  517. case kEnginePostActionSwitchPlugins:
  518. doPluginsSwitch(pluginId, value);
  519. break;
  520. #endif
  521. }
  522. if (needsPost)
  523. {
  524. if (nextAction.sem != nullptr)
  525. carla_sem_post(*nextAction.sem);
  526. nextAction.postDone = true;
  527. }
  528. }
  529. // -----------------------------------------------------------------------
  530. // PendingRtEventsRunner
  531. PendingRtEventsRunner::PendingRtEventsRunner(CarlaEngine* const engine, const uint32_t frames) noexcept
  532. : pData(engine->pData)
  533. {
  534. pData->time.preProcess(frames);
  535. }
  536. PendingRtEventsRunner::~PendingRtEventsRunner() noexcept
  537. {
  538. pData->doNextPluginAction();
  539. }
  540. // -----------------------------------------------------------------------
  541. // ScopedActionLock
  542. ScopedActionLock::ScopedActionLock(CarlaEngine* const engine,
  543. const EnginePostAction action,
  544. const uint pluginId,
  545. const uint value) noexcept
  546. : pData(engine->pData)
  547. {
  548. CARLA_SAFE_ASSERT_RETURN(action != kEnginePostActionNull,);
  549. {
  550. const CarlaMutexLocker cml(pData->nextAction.mutex);
  551. CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull,);
  552. pData->nextAction.opcode = action;
  553. pData->nextAction.pluginId = pluginId;
  554. pData->nextAction.value = value;
  555. pData->nextAction.needsPost = engine->isRunning();
  556. pData->nextAction.postDone = false;
  557. }
  558. #ifdef BUILD_BRIDGE
  559. #define ACTION_MSG_PREFIX "Bridge: "
  560. #else
  561. #define ACTION_MSG_PREFIX ""
  562. #endif
  563. if (pData->nextAction.needsPost)
  564. {
  565. #if defined(DEBUG) || defined(BUILD_BRIDGE)
  566. // block wait for unlock on processing side
  567. carla_stdout(ACTION_MSG_PREFIX "ScopedPluginAction(%i) - blocking START", pluginId);
  568. #endif
  569. bool engineStoppedWhileWaiting = false;
  570. if (! pData->nextAction.postDone)
  571. {
  572. for (int i = 10; --i >= 0;)
  573. {
  574. if (pData->nextAction.sem != nullptr)
  575. {
  576. if (carla_sem_timedwait(*pData->nextAction.sem, 200))
  577. break;
  578. }
  579. else
  580. {
  581. carla_msleep(200);
  582. }
  583. if (! engine->isRunning())
  584. {
  585. engineStoppedWhileWaiting = true;
  586. break;
  587. }
  588. }
  589. }
  590. #if defined(DEBUG) || defined(BUILD_BRIDGE)
  591. carla_stdout(ACTION_MSG_PREFIX "ScopedPluginAction(%i) - blocking DONE", pluginId);
  592. #endif
  593. // check if anything went wrong...
  594. if (! pData->nextAction.postDone)
  595. {
  596. bool needsCorrection = false;
  597. {
  598. const CarlaMutexLocker cml(pData->nextAction.mutex);
  599. if (pData->nextAction.opcode != kEnginePostActionNull)
  600. {
  601. needsCorrection = true;
  602. pData->nextAction.needsPost = false;
  603. }
  604. }
  605. if (needsCorrection)
  606. {
  607. pData->doNextPluginAction();
  608. if (! engineStoppedWhileWaiting)
  609. carla_stderr2(ACTION_MSG_PREFIX "Failed to wait for engine, is audio not running?");
  610. }
  611. }
  612. }
  613. else
  614. {
  615. pData->doNextPluginAction();
  616. }
  617. }
  618. ScopedActionLock::~ScopedActionLock() noexcept
  619. {
  620. CARLA_SAFE_ASSERT(pData->nextAction.opcode == kEnginePostActionNull);
  621. }
  622. // -----------------------------------------------------------------------
  623. // ScopedThreadStopper
  624. ScopedThreadStopper::ScopedThreadStopper(CarlaEngine* const e) noexcept
  625. : engine(e),
  626. pData(e->pData)
  627. {
  628. pData->thread.stopThread(500);
  629. }
  630. ScopedThreadStopper::~ScopedThreadStopper() noexcept
  631. {
  632. if (engine->isRunning() && ! pData->aboutToClose)
  633. pData->thread.startThread();
  634. }
  635. // -----------------------------------------------------------------------
  636. // ScopedEngineEnvironmentLocker
  637. ScopedEngineEnvironmentLocker::ScopedEngineEnvironmentLocker(CarlaEngine* const engine) noexcept
  638. : pData(engine->pData)
  639. {
  640. pData->envMutex.lock();
  641. }
  642. ScopedEngineEnvironmentLocker::~ScopedEngineEnvironmentLocker() noexcept
  643. {
  644. pData->envMutex.unlock();
  645. }
  646. // -----------------------------------------------------------------------
  647. CARLA_BACKEND_END_NAMESPACE