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.

455 lines
11KB

  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. curPluginCount(0),
  175. maxPluginNumber(0),
  176. nextPluginId(0)
  177. #ifndef BUILD_BRIDGE
  178. , plugins(nullptr)
  179. #endif
  180. {
  181. #ifdef BUILD_BRIDGE
  182. carla_zeroStruct(plugins, 1);
  183. #endif
  184. }
  185. CarlaEngine::ProtectedData::~ProtectedData() noexcept
  186. {
  187. CARLA_SAFE_ASSERT(curPluginCount == 0);
  188. CARLA_SAFE_ASSERT(maxPluginNumber == 0);
  189. CARLA_SAFE_ASSERT(nextPluginId == 0);
  190. CARLA_SAFE_ASSERT(plugins == nullptr);
  191. }
  192. // -----------------------------------------------------------------------
  193. bool CarlaEngine::ProtectedData::init(const char* const clientName)
  194. {
  195. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(name.isEmpty(), "Invalid engine internal data (err #1)");
  196. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(oscData == nullptr, "Invalid engine internal data (err #2)");
  197. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(plugins == nullptr, "Invalid engine internal data (err #3)");
  198. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(events.in == nullptr, "Invalid engine internal data (err #4)");
  199. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(events.out == nullptr, "Invalid engine internal data (err #5)");
  200. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(clientName != nullptr && clientName[0] != '\0', "Invalid client name");
  201. aboutToClose = false;
  202. curPluginCount = 0;
  203. maxPluginNumber = 0;
  204. nextPluginId = 0;
  205. switch (options.processMode)
  206. {
  207. case ENGINE_PROCESS_MODE_SINGLE_CLIENT:
  208. case ENGINE_PROCESS_MODE_MULTIPLE_CLIENTS:
  209. maxPluginNumber = MAX_DEFAULT_PLUGINS;
  210. break;
  211. case ENGINE_PROCESS_MODE_CONTINUOUS_RACK:
  212. maxPluginNumber = MAX_RACK_PLUGINS;
  213. events.in = new EngineEvent[kMaxEngineEventInternalCount];
  214. events.out = new EngineEvent[kMaxEngineEventInternalCount];
  215. break;
  216. case ENGINE_PROCESS_MODE_PATCHBAY:
  217. maxPluginNumber = MAX_PATCHBAY_PLUGINS;
  218. break;
  219. case ENGINE_PROCESS_MODE_BRIDGE:
  220. maxPluginNumber = 1;
  221. events.in = new EngineEvent[kMaxEngineEventInternalCount];
  222. events.out = new EngineEvent[kMaxEngineEventInternalCount];
  223. break;
  224. }
  225. CARLA_SAFE_ASSERT_RETURN_INTERNAL_ERR(maxPluginNumber != 0, "Invalid engine process mode");
  226. nextPluginId = maxPluginNumber;
  227. name = clientName;
  228. name.toBasic();
  229. timeInfo.clear();
  230. #ifndef BUILD_BRIDGE
  231. plugins = new EnginePluginData[maxPluginNumber];
  232. carla_zeroStruct(plugins, maxPluginNumber);
  233. #endif
  234. osc.init(clientName);
  235. #ifndef BUILD_BRIDGE
  236. oscData = osc.getControlData();
  237. #endif
  238. nextAction.ready();
  239. thread.startThread();
  240. return true;
  241. }
  242. void CarlaEngine::ProtectedData::close()
  243. {
  244. CARLA_SAFE_ASSERT(name.isNotEmpty());
  245. CARLA_SAFE_ASSERT(plugins != nullptr);
  246. CARLA_SAFE_ASSERT(nextPluginId == maxPluginNumber);
  247. CARLA_SAFE_ASSERT(nextAction.opcode == kEnginePostActionNull);
  248. aboutToClose = true;
  249. thread.stopThread(500);
  250. nextAction.ready();
  251. osc.close();
  252. oscData = nullptr;
  253. aboutToClose = false;
  254. curPluginCount = 0;
  255. maxPluginNumber = 0;
  256. nextPluginId = 0;
  257. #ifndef BUILD_BRIDGE
  258. if (plugins != nullptr)
  259. {
  260. delete[] plugins;
  261. plugins = nullptr;
  262. }
  263. graph.clear();
  264. #endif
  265. events.clear();
  266. name.clear();
  267. }
  268. // -----------------------------------------------------------------------
  269. #ifndef BUILD_BRIDGE
  270. void CarlaEngine::ProtectedData::doPluginRemove() noexcept
  271. {
  272. CARLA_SAFE_ASSERT_RETURN(curPluginCount > 0,);
  273. CARLA_SAFE_ASSERT_RETURN(nextAction.pluginId < curPluginCount,);
  274. --curPluginCount;
  275. // move all plugins 1 spot backwards
  276. for (uint i=nextAction.pluginId; i < curPluginCount; ++i)
  277. {
  278. CarlaPlugin* const plugin(plugins[i+1].plugin);
  279. CARLA_SAFE_ASSERT_BREAK(plugin != nullptr);
  280. plugin->setId(i);
  281. plugins[i].plugin = plugin;
  282. plugins[i].insPeak[0] = 0.0f;
  283. plugins[i].insPeak[1] = 0.0f;
  284. plugins[i].outsPeak[0] = 0.0f;
  285. plugins[i].outsPeak[1] = 0.0f;
  286. }
  287. const uint id(curPluginCount);
  288. // reset last plugin (now removed)
  289. plugins[id].plugin = nullptr;
  290. plugins[id].insPeak[0] = 0.0f;
  291. plugins[id].insPeak[1] = 0.0f;
  292. plugins[id].outsPeak[0] = 0.0f;
  293. plugins[id].outsPeak[1] = 0.0f;
  294. }
  295. void CarlaEngine::ProtectedData::doPluginsSwitch() noexcept
  296. {
  297. CARLA_SAFE_ASSERT_RETURN(curPluginCount >= 2,);
  298. const uint idA(nextAction.pluginId);
  299. const uint idB(nextAction.value);
  300. CARLA_SAFE_ASSERT_RETURN(idA < curPluginCount,);
  301. CARLA_SAFE_ASSERT_RETURN(idB < curPluginCount,);
  302. CARLA_SAFE_ASSERT_RETURN(plugins[idA].plugin != nullptr,);
  303. CARLA_SAFE_ASSERT_RETURN(plugins[idB].plugin != nullptr,);
  304. #if 0
  305. std::swap(plugins[idA].plugin, plugins[idB].plugin);
  306. #else
  307. CarlaPlugin* const tmp(plugins[idA].plugin);
  308. plugins[idA].plugin = plugins[idB].plugin;
  309. plugins[idB].plugin = tmp;
  310. #endif
  311. }
  312. #endif
  313. void CarlaEngine::ProtectedData::doNextPluginAction(const bool unlock) noexcept
  314. {
  315. switch (nextAction.opcode)
  316. {
  317. case kEnginePostActionNull:
  318. break;
  319. case kEnginePostActionZeroCount:
  320. curPluginCount = 0;
  321. break;
  322. #ifndef BUILD_BRIDGE
  323. case kEnginePostActionRemovePlugin:
  324. doPluginRemove();
  325. break;
  326. case kEnginePostActionSwitchPlugins:
  327. doPluginsSwitch();
  328. break;
  329. #endif
  330. }
  331. nextAction.opcode = kEnginePostActionNull;
  332. nextAction.pluginId = 0;
  333. nextAction.value = 0;
  334. if (unlock)
  335. {
  336. nextAction.mutex.tryLock();
  337. nextAction.mutex.unlock();
  338. }
  339. }
  340. // -----------------------------------------------------------------------
  341. // ScopedActionLock
  342. ScopedActionLock::ScopedActionLock(CarlaEngine::ProtectedData* const data, const EnginePostAction action, const uint pluginId, const uint value, const bool lockWait) noexcept
  343. : fData(data)
  344. {
  345. fData->nextAction.mutex.lock();
  346. CARLA_SAFE_ASSERT_RETURN(fData->nextAction.opcode == kEnginePostActionNull,);
  347. fData->nextAction.opcode = action;
  348. fData->nextAction.pluginId = pluginId;
  349. fData->nextAction.value = value;
  350. if (lockWait)
  351. {
  352. // block wait for unlock on processing side
  353. carla_stdout("ScopedPluginAction(%i) - blocking START", pluginId);
  354. fData->nextAction.mutex.lock();
  355. carla_stdout("ScopedPluginAction(%i) - blocking DONE", pluginId);
  356. }
  357. else
  358. {
  359. fData->doNextPluginAction(false);
  360. }
  361. }
  362. ScopedActionLock::~ScopedActionLock() noexcept
  363. {
  364. fData->nextAction.mutex.unlock();
  365. }
  366. // -----------------------------------------------------------------------
  367. CARLA_BACKEND_END_NAMESPACE