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.

732 lines
19KB

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