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.

460 lines
12KB

  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. #include "CarlaMIDI.h"
  20. #include "CarlaMathUtils.hpp"
  21. #include "juce_audio_basics.h"
  22. using juce::FloatVectorOperations;
  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. #if 0
  29. // -----------------------------------------------------------------------
  30. // InternalAudio
  31. EngineInternalAudio::EngineInternalAudio() noexcept
  32. : isReady(false),
  33. inCount(0),
  34. outCount(0),
  35. inBuf(nullptr),
  36. outBuf(nullptr) {}
  37. EngineInternalAudio::~EngineInternalAudio() noexcept
  38. {
  39. CARLA_SAFE_ASSERT(! isReady);
  40. CARLA_SAFE_ASSERT(inCount == 0);
  41. CARLA_SAFE_ASSERT(outCount == 0);
  42. CARLA_SAFE_ASSERT(inBuf == nullptr);
  43. CARLA_SAFE_ASSERT(outBuf == nullptr);
  44. }
  45. void EngineInternalAudio::clearBuffers() noexcept
  46. {
  47. for (uint32_t i=0; i < inCount; ++i)
  48. {
  49. if (inBuf[i] != nullptr)
  50. {
  51. delete[] inBuf[i];
  52. inBuf[i] = nullptr;
  53. }
  54. }
  55. for (uint32_t i=0; i < outCount; ++i)
  56. {
  57. if (outBuf[i] != nullptr)
  58. {
  59. delete[] outBuf[i];
  60. outBuf[i] = nullptr;
  61. }
  62. }
  63. }
  64. void EngineInternalAudio::clear() noexcept
  65. {
  66. isReady = false;
  67. clearBuffers();
  68. inCount = 0;
  69. outCount = 0;
  70. if (inBuf != nullptr)
  71. {
  72. delete[] inBuf;
  73. inBuf = nullptr;
  74. }
  75. if (outBuf != nullptr)
  76. {
  77. delete[] outBuf;
  78. outBuf = nullptr;
  79. }
  80. }
  81. void EngineInternalAudio::create(const uint32_t bufferSize)
  82. {
  83. CARLA_SAFE_ASSERT(! isReady);
  84. CARLA_SAFE_ASSERT(inBuf == nullptr);
  85. CARLA_SAFE_ASSERT(outBuf == nullptr);
  86. if (inCount > 0)
  87. {
  88. inBuf = new float*[inCount];
  89. for (uint32_t i=0; i < inCount; ++i)
  90. inBuf[i] = nullptr;
  91. }
  92. if (outCount > 0)
  93. {
  94. outBuf = new float*[outCount];
  95. for (uint32_t i=0; i < outCount; ++i)
  96. outBuf[i] = nullptr;
  97. }
  98. resize(bufferSize, false);
  99. }
  100. void EngineInternalAudio::resize(const uint32_t bufferSize, const bool doClear = true)
  101. {
  102. if (doClear)
  103. clearBuffers();
  104. CARLA_SAFE_ASSERT_RETURN(bufferSize != 0,);
  105. for (uint32_t i=0; i < inCount; ++i)
  106. {
  107. inBuf[i] = new float[bufferSize];
  108. FloatVectorOperations::clear(inBuf[i], bufferSize);
  109. }
  110. for (uint32_t i=0; i < outCount; ++i)
  111. {
  112. outBuf[i] = new float[bufferSize];
  113. FloatVectorOperations::clear(outBuf[i], bufferSize);
  114. }
  115. }
  116. #endif
  117. // -----------------------------------------------------------------------
  118. // InternalEvents
  119. EngineInternalEvents::EngineInternalEvents() noexcept
  120. : in(nullptr),
  121. out(nullptr) {}
  122. EngineInternalEvents::~EngineInternalEvents() noexcept
  123. {
  124. CARLA_SAFE_ASSERT(in == nullptr);
  125. CARLA_SAFE_ASSERT(out == nullptr);
  126. }
  127. void EngineInternalEvents::clear() noexcept
  128. {
  129. if (in != nullptr)
  130. {
  131. delete[] in;
  132. in = nullptr;
  133. }
  134. if (out != nullptr)
  135. {
  136. delete[] out;
  137. out = nullptr;
  138. }
  139. }
  140. // -----------------------------------------------------------------------
  141. // InternalTime
  142. EngineInternalTime::EngineInternalTime() noexcept
  143. : playing(false),
  144. frame(0) {}
  145. // -----------------------------------------------------------------------
  146. // NextAction
  147. EngineNextAction::EngineNextAction() noexcept
  148. : opcode(kEnginePostActionNull),
  149. pluginId(0),
  150. value(0) {}
  151. EngineNextAction::~EngineNextAction() noexcept
  152. {
  153. CARLA_SAFE_ASSERT(opcode == kEnginePostActionNull);
  154. }
  155. void EngineNextAction::ready() const noexcept
  156. {
  157. mutex.lock();
  158. mutex.unlock();
  159. }
  160. // -----------------------------------------------------------------------
  161. // CarlaEngine::ProtectedData
  162. CarlaEngine::ProtectedData::ProtectedData(CarlaEngine* const engine) noexcept
  163. : osc(engine),
  164. thread(engine),
  165. oscData(nullptr),
  166. callback(nullptr),
  167. callbackPtr(nullptr),
  168. fileCallback(nullptr),
  169. fileCallbackPtr(nullptr),
  170. hints(0x0),
  171. bufferSize(0),
  172. sampleRate(0.0),
  173. aboutToClose(false),
  174. isIdling(false),
  175. curPluginCount(0),
  176. maxPluginNumber(0),
  177. nextPluginId(0)
  178. #ifndef BUILD_BRIDGE
  179. , plugins(nullptr)
  180. #endif
  181. {
  182. #ifdef BUILD_BRIDGE
  183. carla_zeroStruct(plugins, 1);
  184. #endif
  185. }
  186. CarlaEngine::ProtectedData::~ProtectedData() noexcept
  187. {
  188. CARLA_SAFE_ASSERT(curPluginCount == 0);
  189. CARLA_SAFE_ASSERT(maxPluginNumber == 0);
  190. CARLA_SAFE_ASSERT(nextPluginId == 0);
  191. #ifndef BUILD_BRIDGE
  192. CARLA_SAFE_ASSERT(plugins == nullptr);
  193. #endif
  194. }
  195. // -----------------------------------------------------------------------
  196. bool CarlaEngine::ProtectedData::init(const char* const clientName)
  197. {
  198. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(name.isEmpty(), "Invalid engine internal data (err #1)");
  199. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(oscData == nullptr, "Invalid engine internal data (err #2)");
  200. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(events.in == nullptr, "Invalid engine internal data (err #4)");
  201. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(events.out == nullptr, "Invalid engine internal data (err #5)");
  202. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(clientName != nullptr && clientName[0] != '\0', "Invalid client name");
  203. #ifndef BUILD_BRIDGE
  204. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(plugins == nullptr, "Invalid engine internal data (err #3)");
  205. #endif
  206. aboutToClose = false;
  207. curPluginCount = 0;
  208. maxPluginNumber = 0;
  209. nextPluginId = 0;
  210. switch (options.processMode)
  211. {
  212. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  213. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  214. maxPluginNumber = MAX_DEFAULT_PLUGINS;
  215. break;
  216. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  217. maxPluginNumber = MAX_RACK_PLUGINS;
  218. events.in = new EngineEvent[kMaxEngineEventInternalCount];
  219. events.out = new EngineEvent[kMaxEngineEventInternalCount];
  220. break;
  221. case ENGINE_PROCESS_MODE_PATCHBAY:
  222. maxPluginNumber = MAX_PATCHBAY_PLUGINS;
  223. break;
  224. case ENGINE_PROCESS_MODE_BRIDGE:
  225. maxPluginNumber = 1;
  226. events.in = new EngineEvent[kMaxEngineEventInternalCount];
  227. events.out = new EngineEvent[kMaxEngineEventInternalCount];
  228. break;
  229. }
  230. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(maxPluginNumber != 0, "Invalid engine process mode");
  231. nextPluginId = maxPluginNumber;
  232. name = clientName;
  233. name.toBasic();
  234. timeInfo.clear();
  235. #ifndef BUILD_BRIDGE
  236. plugins = new EnginePluginData[maxPluginNumber];
  237. carla_zeroStruct(plugins, maxPluginNumber);
  238. #endif
  239. osc.init(clientName);
  240. #ifndef BUILD_BRIDGE
  241. oscData = osc.getControlData();
  242. #endif
  243. nextAction.ready();
  244. thread.startThread();
  245. return true;
  246. }
  247. void CarlaEngine::ProtectedData::close()
  248. {
  249. CARLA_SAFE_ASSERT(name.isNotEmpty());
  250. CARLA_SAFE_ASSERT(plugins != nullptr);
  251. CARLA_SAFE_ASSERT(nextPluginId == maxPluginNumber);
  252. CARLA_SAFE_ASSERT(nextAction.opcode == kEnginePostActionNull);
  253. aboutToClose = true;
  254. thread.stopThread(500);
  255. nextAction.ready();
  256. osc.close();
  257. oscData = nullptr;
  258. aboutToClose = false;
  259. curPluginCount = 0;
  260. maxPluginNumber = 0;
  261. nextPluginId = 0;
  262. #ifndef BUILD_BRIDGE
  263. if (plugins != nullptr)
  264. {
  265. delete[] plugins;
  266. plugins = nullptr;
  267. }
  268. graph.clear();
  269. #endif
  270. events.clear();
  271. name.clear();
  272. }
  273. // -----------------------------------------------------------------------
  274. #ifndef BUILD_BRIDGE
  275. void CarlaEngine::ProtectedData::doPluginRemove() noexcept
  276. {
  277. CARLA_SAFE_ASSERT_RETURN(curPluginCount > 0,);
  278. CARLA_SAFE_ASSERT_RETURN(nextAction.pluginId < curPluginCount,);
  279. --curPluginCount;
  280. // move all plugins 1 spot backwards
  281. for (uint i=nextAction.pluginId; i < curPluginCount; ++i)
  282. {
  283. CarlaPlugin* const plugin(plugins[i+1].plugin);
  284. CARLA_SAFE_ASSERT_BREAK(plugin != nullptr);
  285. plugin->setId(i);
  286. plugins[i].plugin = plugin;
  287. plugins[i].insPeak[0] = 0.0f;
  288. plugins[i].insPeak[1] = 0.0f;
  289. plugins[i].outsPeak[0] = 0.0f;
  290. plugins[i].outsPeak[1] = 0.0f;
  291. }
  292. const uint id(curPluginCount);
  293. // reset last plugin (now removed)
  294. plugins[id].plugin = nullptr;
  295. plugins[id].insPeak[0] = 0.0f;
  296. plugins[id].insPeak[1] = 0.0f;
  297. plugins[id].outsPeak[0] = 0.0f;
  298. plugins[id].outsPeak[1] = 0.0f;
  299. }
  300. void CarlaEngine::ProtectedData::doPluginsSwitch() noexcept
  301. {
  302. CARLA_SAFE_ASSERT_RETURN(curPluginCount >= 2,);
  303. const uint idA(nextAction.pluginId);
  304. const uint idB(nextAction.value);
  305. CARLA_SAFE_ASSERT_RETURN(idA < curPluginCount,);
  306. CARLA_SAFE_ASSERT_RETURN(idB < curPluginCount,);
  307. CARLA_SAFE_ASSERT_RETURN(plugins[idA].plugin != nullptr,);
  308. CARLA_SAFE_ASSERT_RETURN(plugins[idB].plugin != nullptr,);
  309. #if 0
  310. std::swap(plugins[idA].plugin, plugins[idB].plugin);
  311. #else
  312. CarlaPlugin* const tmp(plugins[idA].plugin);
  313. plugins[idA].plugin = plugins[idB].plugin;
  314. plugins[idB].plugin = tmp;
  315. #endif
  316. }
  317. #endif
  318. void CarlaEngine::ProtectedData::doNextPluginAction(const bool unlock) noexcept
  319. {
  320. switch (nextAction.opcode)
  321. {
  322. case kEnginePostActionNull:
  323. break;
  324. case kEnginePostActionZeroCount:
  325. curPluginCount = 0;
  326. break;
  327. #ifndef BUILD_BRIDGE
  328. case kEnginePostActionRemovePlugin:
  329. doPluginRemove();
  330. break;
  331. case kEnginePostActionSwitchPlugins:
  332. doPluginsSwitch();
  333. break;
  334. #endif
  335. }
  336. nextAction.opcode = kEnginePostActionNull;
  337. nextAction.pluginId = 0;
  338. nextAction.value = 0;
  339. if (unlock)
  340. {
  341. nextAction.mutex.tryLock();
  342. nextAction.mutex.unlock();
  343. }
  344. }
  345. // -----------------------------------------------------------------------
  346. // ScopedActionLock
  347. ScopedActionLock::ScopedActionLock(CarlaEngine::ProtectedData* const data, const EnginePostAction action, const uint pluginId, const uint value, const bool lockWait) noexcept
  348. : fData(data)
  349. {
  350. fData->nextAction.mutex.lock();
  351. CARLA_SAFE_ASSERT_RETURN(fData->nextAction.opcode == kEnginePostActionNull,);
  352. fData->nextAction.opcode = action;
  353. fData->nextAction.pluginId = pluginId;
  354. fData->nextAction.value = value;
  355. if (lockWait)
  356. {
  357. // block wait for unlock on processing side
  358. carla_stdout("ScopedPluginAction(%i) - blocking START", pluginId);
  359. fData->nextAction.mutex.lock();
  360. carla_stdout("ScopedPluginAction(%i) - blocking DONE", pluginId);
  361. }
  362. else
  363. {
  364. fData->doNextPluginAction(false);
  365. }
  366. }
  367. ScopedActionLock::~ScopedActionLock() noexcept
  368. {
  369. fData->nextAction.mutex.unlock();
  370. }
  371. // -----------------------------------------------------------------------
  372. CARLA_BACKEND_END_NAMESPACE