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.

604 lines
16KB

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