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.

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