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.

551 lines
14KB

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