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.

824 lines
22KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2019 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. #include <ctime>
  22. #include <sys/time.h>
  23. CARLA_BACKEND_START_NAMESPACE
  24. // -----------------------------------------------------------------------
  25. // Engine Internal helper macro, sets lastError and returns false/NULL
  26. #define CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(cond, err) if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); lastError = err; return false; }
  27. #define CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERRN(cond, err) if (! (cond)) { carla_safe_assert(#cond, __FILE__, __LINE__); lastError = err; return nullptr; }
  28. // -----------------------------------------------------------------------
  29. // InternalEvents
  30. EngineInternalEvents::EngineInternalEvents() noexcept
  31. : in(nullptr),
  32. out(nullptr) {}
  33. EngineInternalEvents::~EngineInternalEvents() noexcept
  34. {
  35. CARLA_SAFE_ASSERT(in == nullptr);
  36. CARLA_SAFE_ASSERT(out == nullptr);
  37. }
  38. void EngineInternalEvents::clear() noexcept
  39. {
  40. if (in != nullptr)
  41. {
  42. delete[] in;
  43. in = nullptr;
  44. }
  45. if (out != nullptr)
  46. {
  47. delete[] out;
  48. out = nullptr;
  49. }
  50. }
  51. // -----------------------------------------------------------------------
  52. // InternalTime
  53. static const double kTicksPerBeat = 1920.0;
  54. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  55. static uint32_t calculate_link_latency(const double bufferSize, const double sampleRate) noexcept
  56. {
  57. CARLA_SAFE_ASSERT_RETURN(carla_isNotZero(sampleRate), 0);
  58. const long long int latency = llround(1.0e6 * bufferSize / sampleRate);
  59. CARLA_SAFE_ASSERT_RETURN(latency >= 0 && latency < UINT32_MAX, 0);
  60. return static_cast<uint32_t>(latency);
  61. }
  62. #endif
  63. EngineInternalTime::EngineInternalTime(EngineTimeInfo& ti, const EngineTransportMode& tm) noexcept
  64. : beatsPerBar(4.0),
  65. beatsPerMinute(120.0),
  66. bufferSize(0.0),
  67. sampleRate(0.0),
  68. tick(0.0),
  69. needsReset(false),
  70. nextFrame(0),
  71. #ifndef BUILD_BRIDGE
  72. hylia(),
  73. #endif
  74. timeInfo(ti),
  75. transportMode(tm) {}
  76. void EngineInternalTime::init(const uint32_t bsize, const double srate)
  77. {
  78. bufferSize = bsize;
  79. sampleRate = srate;
  80. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  81. if (hylia.instance != nullptr)
  82. {
  83. hylia_set_beats_per_bar(hylia.instance, beatsPerBar);
  84. hylia_set_beats_per_minute(hylia.instance, beatsPerMinute);
  85. hylia_set_output_latency(hylia.instance, calculate_link_latency(bsize, srate));
  86. if (hylia.enabled)
  87. hylia_enable(hylia.instance, true);
  88. }
  89. #endif
  90. needsReset = true;
  91. }
  92. void EngineInternalTime::updateAudioValues(const uint32_t bsize, const double srate)
  93. {
  94. bufferSize = bsize;
  95. sampleRate = srate;
  96. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  97. if (hylia.instance != nullptr)
  98. hylia_set_output_latency(hylia.instance, calculate_link_latency(bsize, srate));
  99. #endif
  100. needsReset = true;
  101. }
  102. void EngineInternalTime::enableLink(const bool enable)
  103. {
  104. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  105. if (hylia.enabled == enable)
  106. return;
  107. if (hylia.instance != nullptr)
  108. {
  109. hylia.enabled = enable;
  110. hylia_enable(hylia.instance, enable);
  111. }
  112. #else
  113. // unused
  114. (void)enable;
  115. #endif
  116. needsReset = true;
  117. }
  118. void EngineInternalTime::setBPM(const double bpm)
  119. {
  120. beatsPerMinute = bpm;
  121. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  122. if (hylia.instance != nullptr)
  123. hylia_set_beats_per_minute(hylia.instance, bpm);
  124. #endif
  125. }
  126. void EngineInternalTime::setNeedsReset() noexcept
  127. {
  128. needsReset = true;
  129. }
  130. void EngineInternalTime::pause() noexcept
  131. {
  132. timeInfo.playing = false;
  133. nextFrame = timeInfo.frame;
  134. needsReset = true;
  135. }
  136. void EngineInternalTime::relocate(const uint64_t frame) noexcept
  137. {
  138. timeInfo.frame = frame;
  139. nextFrame = frame;
  140. needsReset = true;
  141. }
  142. void EngineInternalTime::fillEngineTimeInfo(const uint32_t newFrames) noexcept
  143. {
  144. CARLA_SAFE_ASSERT_RETURN(carla_isNotZero(sampleRate),);
  145. CARLA_SAFE_ASSERT_RETURN(newFrames > 0,);
  146. double ticktmp;
  147. if (transportMode == ENGINE_TRANSPORT_MODE_INTERNAL)
  148. {
  149. timeInfo.usecs = 0;
  150. timeInfo.frame = nextFrame;
  151. }
  152. if (needsReset)
  153. {
  154. timeInfo.bbt.valid = true;
  155. timeInfo.bbt.beatType = 4.0f;
  156. timeInfo.bbt.ticksPerBeat = kTicksPerBeat;
  157. double abs_beat, abs_tick;
  158. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  159. if (hylia.enabled)
  160. {
  161. if (hylia.timeInfo.beat >= 0.0)
  162. {
  163. abs_beat = hylia.timeInfo.beat;
  164. abs_tick = abs_beat * kTicksPerBeat;
  165. }
  166. else
  167. {
  168. abs_beat = 0.0;
  169. abs_tick = 0.0;
  170. timeInfo.playing = false;
  171. }
  172. }
  173. else
  174. #endif
  175. {
  176. const double min = static_cast<double>(timeInfo.frame) / (sampleRate * 60.0);
  177. abs_beat = min * beatsPerMinute;
  178. abs_tick = abs_beat * kTicksPerBeat;
  179. needsReset = false;
  180. }
  181. const double bar = std::floor(abs_beat / beatsPerBar);
  182. const double beat = std::floor(std::fmod(abs_beat, beatsPerBar));
  183. timeInfo.bbt.bar = static_cast<int32_t>(bar) + 1;
  184. timeInfo.bbt.beat = static_cast<int32_t>(beat) + 1;
  185. timeInfo.bbt.barStartTick = ((bar * beatsPerBar) + beat) * kTicksPerBeat;
  186. ticktmp = abs_tick - timeInfo.bbt.barStartTick;
  187. }
  188. else if (timeInfo.playing)
  189. {
  190. ticktmp = tick + (newFrames * kTicksPerBeat * beatsPerMinute / (sampleRate * 60));
  191. while (ticktmp >= kTicksPerBeat)
  192. {
  193. ticktmp -= kTicksPerBeat;
  194. if (++timeInfo.bbt.beat > beatsPerBar)
  195. {
  196. ++timeInfo.bbt.bar;
  197. timeInfo.bbt.beat = 1;
  198. timeInfo.bbt.barStartTick += beatsPerBar * kTicksPerBeat;
  199. }
  200. }
  201. }
  202. else
  203. {
  204. ticktmp = tick;
  205. }
  206. timeInfo.bbt.beatsPerBar = static_cast<float>(beatsPerBar);
  207. timeInfo.bbt.beatsPerMinute = beatsPerMinute;
  208. timeInfo.bbt.tick = ticktmp;
  209. tick = ticktmp;
  210. if (transportMode == ENGINE_TRANSPORT_MODE_INTERNAL && timeInfo.playing)
  211. nextFrame += newFrames;
  212. }
  213. void EngineInternalTime::fillJackTimeInfo(jack_position_t* const pos, const uint32_t newFrames) noexcept
  214. {
  215. CARLA_SAFE_ASSERT_RETURN(carla_isNotZero(sampleRate),);
  216. CARLA_SAFE_ASSERT_RETURN(newFrames > 0,);
  217. CARLA_SAFE_ASSERT(transportMode == ENGINE_TRANSPORT_MODE_JACK);
  218. fillEngineTimeInfo(newFrames);
  219. pos->valid = JackPositionBBT;
  220. pos->bar = timeInfo.bbt.bar;
  221. pos->beat = timeInfo.bbt.beat;
  222. pos->tick = static_cast<int32_t>(tick + 0.5);
  223. pos->bar_start_tick = timeInfo.bbt.barStartTick;
  224. pos->beats_per_bar = timeInfo.bbt.beatsPerBar;
  225. pos->beat_type = timeInfo.bbt.beatType;
  226. pos->ticks_per_beat = kTicksPerBeat;
  227. pos->beats_per_minute = beatsPerMinute;
  228. }
  229. void EngineInternalTime::preProcess(const uint32_t numFrames)
  230. {
  231. #if defined(HAVE_HYLIA) && !defined(BUILD_BRIDGE)
  232. if (hylia.enabled)
  233. {
  234. hylia_process(hylia.instance, numFrames, &hylia.timeInfo);
  235. const double new_bpb = hylia.timeInfo.beatsPerBar;
  236. const double new_bpm = hylia.timeInfo.beatsPerMinute;
  237. if (new_bpb >= 1.0 && carla_isNotEqual(beatsPerBar, new_bpb))
  238. {
  239. beatsPerBar = new_bpb;
  240. needsReset = true;
  241. }
  242. if (new_bpm > 0.0 && carla_isNotEqual(beatsPerMinute, new_bpm))
  243. {
  244. beatsPerMinute = new_bpm;
  245. needsReset = true;
  246. }
  247. }
  248. #endif
  249. if (transportMode == ENGINE_TRANSPORT_MODE_INTERNAL)
  250. fillEngineTimeInfo(numFrames);
  251. }
  252. // -----------------------------------------------------------------------
  253. // EngineInternalTime::Hylia
  254. #ifndef BUILD_BRIDGE
  255. EngineInternalTime::Hylia::Hylia()
  256. : enabled(false),
  257. instance(nullptr),
  258. timeInfo()
  259. {
  260. carla_zeroStruct(timeInfo);
  261. # ifdef HAVE_HYLIA
  262. instance = hylia_create();
  263. # endif
  264. }
  265. EngineInternalTime::Hylia::~Hylia()
  266. {
  267. # ifdef HAVE_HYLIA
  268. hylia_cleanup(instance);
  269. # endif
  270. }
  271. #endif
  272. // -----------------------------------------------------------------------
  273. // NextAction
  274. EngineNextAction::EngineNextAction() noexcept
  275. : opcode(kEnginePostActionNull),
  276. pluginId(0),
  277. value(0),
  278. mutex(),
  279. needsPost(false),
  280. postDone(false),
  281. sem(carla_sem_create(false)) {}
  282. EngineNextAction::~EngineNextAction() noexcept
  283. {
  284. CARLA_SAFE_ASSERT(opcode == kEnginePostActionNull);
  285. if (sem != nullptr)
  286. {
  287. carla_sem_destroy(sem);
  288. sem = nullptr;
  289. }
  290. }
  291. void EngineNextAction::clearAndReset() noexcept
  292. {
  293. mutex.lock();
  294. CARLA_SAFE_ASSERT(opcode == kEnginePostActionNull);
  295. opcode = kEnginePostActionNull;
  296. pluginId = 0;
  297. value = 0;
  298. needsPost = false;
  299. postDone = false;
  300. mutex.unlock();
  301. }
  302. // -----------------------------------------------------------------------
  303. // CarlaEngine::ProtectedData
  304. CarlaEngine::ProtectedData::ProtectedData(CarlaEngine* const engine) noexcept
  305. : thread(engine),
  306. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  307. osc(engine),
  308. #endif
  309. callback(nullptr),
  310. callbackPtr(nullptr),
  311. fileCallback(nullptr),
  312. fileCallbackPtr(nullptr),
  313. actionCanceled(false),
  314. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  315. loadingProject(false),
  316. currentProjectFilename(),
  317. #endif
  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. dspLoad(0.0f),
  334. #endif
  335. events(),
  336. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  337. graph(engine),
  338. #endif
  339. time(timeInfo, options.transportMode),
  340. nextAction()
  341. {
  342. #ifdef BUILD_BRIDGE_ALTERNATIVE_ARCH
  343. carla_zeroStructs(plugins, 1);
  344. #endif
  345. }
  346. CarlaEngine::ProtectedData::~ProtectedData() noexcept
  347. {
  348. CARLA_SAFE_ASSERT(curPluginCount == 0);
  349. CARLA_SAFE_ASSERT(maxPluginNumber == 0);
  350. CARLA_SAFE_ASSERT(nextPluginId == 0);
  351. CARLA_SAFE_ASSERT(isIdling == 0);
  352. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  353. CARLA_SAFE_ASSERT(plugins == nullptr);
  354. #endif
  355. }
  356. // -----------------------------------------------------------------------
  357. bool CarlaEngine::ProtectedData::init(const char* const clientName)
  358. {
  359. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(name.isEmpty(), "Invalid engine internal data (err #1)");
  360. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(events.in == nullptr, "Invalid engine internal data (err #4)");
  361. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(events.out == nullptr, "Invalid engine internal data (err #5)");
  362. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(clientName != nullptr && clientName[0] != '\0', "Invalid client name");
  363. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  364. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(plugins == nullptr, "Invalid engine internal data (err #3)");
  365. #endif
  366. aboutToClose = false;
  367. curPluginCount = 0;
  368. nextPluginId = 0;
  369. switch (options.processMode)
  370. {
  371. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  372. maxPluginNumber = MAX_RACK_PLUGINS;
  373. options.forceStereo = true;
  374. break;
  375. case ENGINE_PROCESS_MODE_PATCHBAY:
  376. maxPluginNumber = MAX_PATCHBAY_PLUGINS;
  377. break;
  378. case ENGINE_PROCESS_MODE_BRIDGE:
  379. maxPluginNumber = 1;
  380. break;
  381. default:
  382. maxPluginNumber = MAX_DEFAULT_PLUGINS;
  383. break;
  384. }
  385. switch (options.processMode)
  386. {
  387. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  388. case ENGINE_PROCESS_MODE_PATCHBAY:
  389. case ENGINE_PROCESS_MODE_BRIDGE:
  390. events.in = new EngineEvent[kMaxEngineEventInternalCount];
  391. events.out = new EngineEvent[kMaxEngineEventInternalCount];
  392. carla_zeroStructs(events.in, kMaxEngineEventInternalCount);
  393. carla_zeroStructs(events.out, kMaxEngineEventInternalCount);
  394. break;
  395. default:
  396. break;
  397. }
  398. nextPluginId = maxPluginNumber;
  399. name = clientName;
  400. name.toBasic();
  401. timeInfo.clear();
  402. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  403. if (options.oscEnabled)
  404. osc.init(clientName, options.oscPortTCP, options.oscPortUDP);
  405. #endif
  406. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  407. plugins = new EnginePluginData[maxPluginNumber];
  408. carla_zeroStructs(plugins, maxPluginNumber);
  409. xruns = 0;
  410. dspLoad = 0.0f;
  411. #endif
  412. nextAction.clearAndReset();
  413. thread.startThread();
  414. return true;
  415. }
  416. void CarlaEngine::ProtectedData::close()
  417. {
  418. CARLA_SAFE_ASSERT(name.isNotEmpty());
  419. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  420. CARLA_SAFE_ASSERT(plugins != nullptr);
  421. CARLA_SAFE_ASSERT(nextPluginId == maxPluginNumber);
  422. #endif
  423. aboutToClose = true;
  424. thread.stopThread(500);
  425. nextAction.clearAndReset();
  426. #if defined(HAVE_LIBLO) && !defined(BUILD_BRIDGE)
  427. osc.close();
  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. static int64_t getTimeInMicroseconds() noexcept
  532. {
  533. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  534. struct timeval tv;
  535. gettimeofday(&tv, nullptr);
  536. return (tv.tv_sec * 1000000) + tv.tv_usec;
  537. #else
  538. struct timespec ts;
  539. # ifdef CLOCK_MONOTONIC_RAW
  540. clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
  541. # else
  542. clock_gettime(CLOCK_MONOTONIC, &ts);
  543. # endif
  544. return (ts.tv_sec * 1000000) + (ts.tv_nsec / 1000);
  545. #endif
  546. }
  547. PendingRtEventsRunner::PendingRtEventsRunner(CarlaEngine* const engine,
  548. const uint32_t frames,
  549. const bool calcDSPLoad) noexcept
  550. : pData(engine->pData),
  551. prevTime(calcDSPLoad ? getTimeInMicroseconds() : 0)
  552. {
  553. pData->time.preProcess(frames);
  554. }
  555. PendingRtEventsRunner::~PendingRtEventsRunner() noexcept
  556. {
  557. pData->doNextPluginAction();
  558. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  559. if (prevTime > 0)
  560. {
  561. const int64_t newTime = getTimeInMicroseconds();
  562. if (newTime < prevTime)
  563. return;
  564. const double timeDiff = static_cast<double>(newTime - prevTime) / 1000000.0;
  565. const double maxTime = pData->bufferSize / pData->sampleRate;
  566. const float dspLoad = static_cast<float>(timeDiff / maxTime) * 100.0f;
  567. if (dspLoad > pData->dspLoad)
  568. pData->dspLoad = std::min(100.0f, dspLoad);
  569. else
  570. pData->dspLoad *= static_cast<float>(1.0 - maxTime) + 1e-12f;
  571. }
  572. #endif
  573. }
  574. // -----------------------------------------------------------------------
  575. // ScopedActionLock
  576. ScopedActionLock::ScopedActionLock(CarlaEngine* const engine,
  577. const EnginePostAction action,
  578. const uint pluginId,
  579. const uint value) noexcept
  580. : pData(engine->pData)
  581. {
  582. CARLA_SAFE_ASSERT_RETURN(action != kEnginePostActionNull,);
  583. {
  584. const CarlaMutexLocker cml(pData->nextAction.mutex);
  585. CARLA_SAFE_ASSERT_RETURN(pData->nextAction.opcode == kEnginePostActionNull,);
  586. pData->nextAction.opcode = action;
  587. pData->nextAction.pluginId = pluginId;
  588. pData->nextAction.value = value;
  589. pData->nextAction.needsPost = engine->isRunning();
  590. pData->nextAction.postDone = false;
  591. }
  592. #ifdef BUILD_BRIDGE
  593. #define ACTION_MSG_PREFIX "Bridge: "
  594. #else
  595. #define ACTION_MSG_PREFIX ""
  596. #endif
  597. if (pData->nextAction.needsPost)
  598. {
  599. #if defined(DEBUG) || defined(BUILD_BRIDGE)
  600. // block wait for unlock on processing side
  601. carla_stdout(ACTION_MSG_PREFIX "ScopedPluginAction(%i) - blocking START", pluginId);
  602. #endif
  603. bool engineStoppedWhileWaiting = false;
  604. if (! pData->nextAction.postDone)
  605. {
  606. for (int i = 10; --i >= 0;)
  607. {
  608. if (pData->nextAction.sem != nullptr)
  609. {
  610. if (carla_sem_timedwait(*pData->nextAction.sem, 200))
  611. break;
  612. }
  613. else
  614. {
  615. carla_msleep(200);
  616. }
  617. if (! engine->isRunning())
  618. {
  619. engineStoppedWhileWaiting = true;
  620. break;
  621. }
  622. }
  623. }
  624. #if defined(DEBUG) || defined(BUILD_BRIDGE)
  625. carla_stdout(ACTION_MSG_PREFIX "ScopedPluginAction(%i) - blocking DONE", pluginId);
  626. #endif
  627. // check if anything went wrong...
  628. if (! pData->nextAction.postDone)
  629. {
  630. bool needsCorrection = false;
  631. {
  632. const CarlaMutexLocker cml(pData->nextAction.mutex);
  633. if (pData->nextAction.opcode != kEnginePostActionNull)
  634. {
  635. needsCorrection = true;
  636. pData->nextAction.needsPost = false;
  637. }
  638. }
  639. if (needsCorrection)
  640. {
  641. pData->doNextPluginAction();
  642. if (! engineStoppedWhileWaiting)
  643. carla_stderr2(ACTION_MSG_PREFIX "Failed to wait for engine, is audio not running?");
  644. }
  645. }
  646. }
  647. else
  648. {
  649. pData->doNextPluginAction();
  650. }
  651. }
  652. ScopedActionLock::~ScopedActionLock() noexcept
  653. {
  654. CARLA_SAFE_ASSERT(pData->nextAction.opcode == kEnginePostActionNull);
  655. }
  656. // -----------------------------------------------------------------------
  657. // ScopedThreadStopper
  658. ScopedThreadStopper::ScopedThreadStopper(CarlaEngine* const e) noexcept
  659. : engine(e),
  660. pData(e->pData)
  661. {
  662. pData->thread.stopThread(500);
  663. }
  664. ScopedThreadStopper::~ScopedThreadStopper() noexcept
  665. {
  666. if (engine->isRunning() && ! pData->aboutToClose)
  667. pData->thread.startThread();
  668. }
  669. // -----------------------------------------------------------------------
  670. // ScopedEngineEnvironmentLocker
  671. ScopedEngineEnvironmentLocker::ScopedEngineEnvironmentLocker(CarlaEngine* const engine) noexcept
  672. : pData(engine->pData)
  673. {
  674. pData->envMutex.lock();
  675. }
  676. ScopedEngineEnvironmentLocker::~ScopedEngineEnvironmentLocker() noexcept
  677. {
  678. pData->envMutex.unlock();
  679. }
  680. // -----------------------------------------------------------------------
  681. CARLA_BACKEND_END_NAMESPACE