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.

367 lines
9.6KB

  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. EngineInternalTime::EngineInternalTime() noexcept
  50. : playing(false),
  51. frame(0) {}
  52. // -----------------------------------------------------------------------
  53. // NextAction
  54. EngineNextAction::EngineNextAction() noexcept
  55. : opcode(kEnginePostActionNull),
  56. pluginId(0),
  57. value(0),
  58. mutex() {}
  59. EngineNextAction::~EngineNextAction() noexcept
  60. {
  61. CARLA_SAFE_ASSERT(opcode == kEnginePostActionNull);
  62. }
  63. void EngineNextAction::ready() const noexcept
  64. {
  65. mutex.lock();
  66. mutex.unlock();
  67. }
  68. void EngineNextAction::clearAndReset() noexcept
  69. {
  70. mutex.lock();
  71. opcode = kEnginePostActionNull;
  72. pluginId = 0;
  73. value = 0;
  74. mutex.unlock();
  75. }
  76. // -----------------------------------------------------------------------
  77. // CarlaEngine::ProtectedData
  78. CarlaEngine::ProtectedData::ProtectedData(CarlaEngine* const engine) noexcept
  79. : osc(engine),
  80. thread(engine),
  81. oscData(nullptr),
  82. callback(nullptr),
  83. callbackPtr(nullptr),
  84. fileCallback(nullptr),
  85. fileCallbackPtr(nullptr),
  86. hints(0x0),
  87. bufferSize(0),
  88. sampleRate(0.0),
  89. aboutToClose(false),
  90. isIdling(0),
  91. curPluginCount(0),
  92. maxPluginNumber(0),
  93. nextPluginId(0),
  94. lastError(),
  95. name(),
  96. options(),
  97. timeInfo(),
  98. #ifndef BUILD_BRIDGE
  99. plugins(nullptr),
  100. #endif
  101. events(),
  102. #ifndef BUILD_BRIDGE
  103. graph(),
  104. #endif
  105. time(),
  106. nextAction()
  107. {
  108. #ifdef BUILD_BRIDGE
  109. carla_zeroStruct(plugins, 1);
  110. #endif
  111. }
  112. CarlaEngine::ProtectedData::~ProtectedData() noexcept
  113. {
  114. CARLA_SAFE_ASSERT(curPluginCount == 0);
  115. CARLA_SAFE_ASSERT(maxPluginNumber == 0);
  116. CARLA_SAFE_ASSERT(nextPluginId == 0);
  117. CARLA_SAFE_ASSERT(isIdling == 0);
  118. #ifndef BUILD_BRIDGE
  119. CARLA_SAFE_ASSERT(plugins == nullptr);
  120. #endif
  121. }
  122. // -----------------------------------------------------------------------
  123. bool CarlaEngine::ProtectedData::init(const char* const clientName)
  124. {
  125. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(name.isEmpty(), "Invalid engine internal data (err #1)");
  126. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(oscData == nullptr, "Invalid engine internal data (err #2)");
  127. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(events.in == nullptr, "Invalid engine internal data (err #4)");
  128. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(events.out == nullptr, "Invalid engine internal data (err #5)");
  129. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(clientName != nullptr && clientName[0] != '\0', "Invalid client name");
  130. #ifndef BUILD_BRIDGE
  131. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(plugins == nullptr, "Invalid engine internal data (err #3)");
  132. #endif
  133. aboutToClose = false;
  134. curPluginCount = 0;
  135. nextPluginId = 0;
  136. switch (options.processMode)
  137. {
  138. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  139. maxPluginNumber = MAX_RACK_PLUGINS;
  140. options.forceStereo = true; // just in case
  141. break;
  142. case ENGINE_PROCESS_MODE_PATCHBAY:
  143. maxPluginNumber = MAX_PATCHBAY_PLUGINS;
  144. break;
  145. case ENGINE_PROCESS_MODE_BRIDGE:
  146. maxPluginNumber = 1;
  147. break;
  148. default:
  149. maxPluginNumber = MAX_DEFAULT_PLUGINS;
  150. break;
  151. }
  152. switch (options.processMode)
  153. {
  154. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  155. case ENGINE_PROCESS_MODE_PATCHBAY:
  156. case ENGINE_PROCESS_MODE_BRIDGE:
  157. events.in = new EngineEvent[kMaxEngineEventInternalCount];
  158. events.out = new EngineEvent[kMaxEngineEventInternalCount];
  159. break;
  160. default:
  161. break;
  162. }
  163. nextPluginId = maxPluginNumber;
  164. name = clientName;
  165. name.toBasic();
  166. timeInfo.clear();
  167. osc.init(clientName);
  168. #ifndef BUILD_BRIDGE
  169. oscData = osc.getControlData();
  170. plugins = new EnginePluginData[maxPluginNumber];
  171. carla_zeroStruct(plugins, maxPluginNumber);
  172. #endif
  173. nextAction.ready();
  174. thread.startThread();
  175. return true;
  176. }
  177. void CarlaEngine::ProtectedData::close()
  178. {
  179. CARLA_SAFE_ASSERT(name.isNotEmpty());
  180. CARLA_SAFE_ASSERT(plugins != nullptr);
  181. CARLA_SAFE_ASSERT(nextPluginId == maxPluginNumber);
  182. CARLA_SAFE_ASSERT(nextAction.opcode == kEnginePostActionNull);
  183. aboutToClose = true;
  184. thread.stopThread(500);
  185. nextAction.ready();
  186. osc.close();
  187. oscData = nullptr;
  188. aboutToClose = false;
  189. curPluginCount = 0;
  190. maxPluginNumber = 0;
  191. nextPluginId = 0;
  192. #ifndef BUILD_BRIDGE
  193. if (plugins != nullptr)
  194. {
  195. delete[] plugins;
  196. plugins = nullptr;
  197. }
  198. #endif
  199. events.clear();
  200. name.clear();
  201. }
  202. // -----------------------------------------------------------------------
  203. #ifndef BUILD_BRIDGE
  204. void CarlaEngine::ProtectedData::doPluginRemove() noexcept
  205. {
  206. CARLA_SAFE_ASSERT_RETURN(curPluginCount > 0,);
  207. CARLA_SAFE_ASSERT_RETURN(nextAction.pluginId < curPluginCount,);
  208. --curPluginCount;
  209. // move all plugins 1 spot backwards
  210. for (uint i=nextAction.pluginId; i < curPluginCount; ++i)
  211. {
  212. CarlaPlugin* const plugin(plugins[i+1].plugin);
  213. CARLA_SAFE_ASSERT_BREAK(plugin != nullptr);
  214. plugin->setId(i);
  215. plugins[i].plugin = plugin;
  216. plugins[i].insPeak[0] = 0.0f;
  217. plugins[i].insPeak[1] = 0.0f;
  218. plugins[i].outsPeak[0] = 0.0f;
  219. plugins[i].outsPeak[1] = 0.0f;
  220. }
  221. const uint id(curPluginCount);
  222. // reset last plugin (now removed)
  223. plugins[id].plugin = nullptr;
  224. plugins[id].insPeak[0] = 0.0f;
  225. plugins[id].insPeak[1] = 0.0f;
  226. plugins[id].outsPeak[0] = 0.0f;
  227. plugins[id].outsPeak[1] = 0.0f;
  228. }
  229. void CarlaEngine::ProtectedData::doPluginsSwitch() noexcept
  230. {
  231. CARLA_SAFE_ASSERT_RETURN(curPluginCount >= 2,);
  232. const uint idA(nextAction.pluginId);
  233. const uint idB(nextAction.value);
  234. CARLA_SAFE_ASSERT_RETURN(idA < curPluginCount,);
  235. CARLA_SAFE_ASSERT_RETURN(idB < curPluginCount,);
  236. CARLA_SAFE_ASSERT_RETURN(plugins[idA].plugin != nullptr,);
  237. CARLA_SAFE_ASSERT_RETURN(plugins[idB].plugin != nullptr,);
  238. #if 0
  239. std::swap(plugins[idA].plugin, plugins[idB].plugin);
  240. #else
  241. CarlaPlugin* const tmp(plugins[idA].plugin);
  242. plugins[idA].plugin = plugins[idB].plugin;
  243. plugins[idB].plugin = tmp;
  244. #endif
  245. }
  246. #endif
  247. void CarlaEngine::ProtectedData::doNextPluginAction(const bool unlock) noexcept
  248. {
  249. switch (nextAction.opcode)
  250. {
  251. case kEnginePostActionNull:
  252. break;
  253. case kEnginePostActionZeroCount:
  254. curPluginCount = 0;
  255. break;
  256. #ifndef BUILD_BRIDGE
  257. case kEnginePostActionRemovePlugin:
  258. doPluginRemove();
  259. break;
  260. case kEnginePostActionSwitchPlugins:
  261. doPluginsSwitch();
  262. break;
  263. #endif
  264. }
  265. nextAction.opcode = kEnginePostActionNull;
  266. nextAction.pluginId = 0;
  267. nextAction.value = 0;
  268. if (unlock)
  269. {
  270. nextAction.mutex.tryLock();
  271. nextAction.mutex.unlock();
  272. }
  273. }
  274. // -----------------------------------------------------------------------
  275. // ScopedActionLock
  276. ScopedActionLock::ScopedActionLock(CarlaEngine::ProtectedData* const data, const EnginePostAction action, const uint pluginId, const uint value, const bool lockWait) noexcept
  277. : fData(data)
  278. {
  279. CARLA_SAFE_ASSERT_RETURN(action != kEnginePostActionNull,);
  280. fData->nextAction.mutex.lock();
  281. CARLA_SAFE_ASSERT_RETURN(fData->nextAction.opcode == kEnginePostActionNull,);
  282. fData->nextAction.opcode = action;
  283. fData->nextAction.pluginId = pluginId;
  284. fData->nextAction.value = value;
  285. if (lockWait)
  286. {
  287. // block wait for unlock on processing side
  288. carla_stdout("ScopedPluginAction(%i) - blocking START", pluginId);
  289. fData->nextAction.mutex.lock();
  290. carla_stdout("ScopedPluginAction(%i) - blocking DONE", pluginId);
  291. }
  292. else
  293. {
  294. fData->doNextPluginAction(false);
  295. }
  296. }
  297. ScopedActionLock::~ScopedActionLock() noexcept
  298. {
  299. CARLA_SAFE_ASSERT(fData->nextAction.opcode == kEnginePostActionNull);
  300. fData->nextAction.mutex.unlock();
  301. }
  302. // -----------------------------------------------------------------------
  303. CARLA_BACKEND_END_NAMESPACE