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.

348 lines
9.4KB

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