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.

371 lines
10KB

  1. /*
  2. * Carla Engine
  3. * Copyright (C) 2012-2013 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. #ifndef CARLA_ENGINE_INTERNAL_HPP_INCLUDED
  18. #define CARLA_ENGINE_INTERNAL_HPP_INCLUDED
  19. #include "CarlaEngine.hpp"
  20. #include "CarlaEngineOsc.hpp"
  21. #include "CarlaEngineThread.hpp"
  22. #include "CarlaPlugin.hpp"
  23. #include "CarlaMutex.hpp"
  24. CARLA_BACKEND_START_NAMESPACE
  25. #if 0
  26. } // Fix editor indentation
  27. #endif
  28. // -----------------------------------------------------------------------
  29. // Engine helper macro, sets lastError and returns false/NULL
  30. #define CARLA_SAFE_ASSERT_RETURN_ERR(cond, err) if (cond) pass(); else { carla_safe_assert(#cond, __FILE__, __LINE__); setLastError(err); return false; }
  31. #define CARLA_SAFE_ASSERT_RETURN_ERRN(cond, err) if (cond) pass(); else { carla_safe_assert(#cond, __FILE__, __LINE__); setLastError(err); return nullptr; }
  32. // -----------------------------------------------------------------------
  33. static inline
  34. const char* EngineType2Str(const EngineType type)
  35. {
  36. switch (type)
  37. {
  38. case kEngineTypeNull:
  39. return "kEngineTypeNull";
  40. case kEngineTypeJack:
  41. return "kEngineTypeJack";
  42. case kEngineTypeJuce:
  43. return "kEngineTypeJuce";
  44. case kEngineTypeRtAudio:
  45. return "kEngineTypeRtAudio";
  46. case kEngineTypePlugin:
  47. return "kEngineTypePlugin";
  48. case kEngineTypeBridge:
  49. return "kEngineTypeBridge";
  50. }
  51. carla_stderr("CarlaBackend::EngineType2Str(%i) - invalid type", type);
  52. return nullptr;
  53. }
  54. static inline
  55. const char* EnginePortType2Str(const EnginePortType type)
  56. {
  57. switch (type)
  58. {
  59. case kEnginePortTypeNull:
  60. return "kEnginePortTypeNull";
  61. case kEnginePortTypeAudio:
  62. return "kEnginePortTypeAudio";
  63. case kEnginePortTypeCV:
  64. return "kEnginePortTypeCV";
  65. case kEnginePortTypeEvent:
  66. return "kEnginePortTypeEvent";
  67. }
  68. carla_stderr("CarlaBackend::EnginePortType2Str(%i) - invalid type", type);
  69. return nullptr;
  70. }
  71. static inline
  72. const char* EngineEventType2Str(const EngineEventType type)
  73. {
  74. switch (type)
  75. {
  76. case kEngineEventTypeNull:
  77. return "kEngineEventTypeNull";
  78. case kEngineEventTypeControl:
  79. return "kEngineEventTypeControl";
  80. case kEngineEventTypeMidi:
  81. return "kEngineEventTypeMidi";
  82. }
  83. carla_stderr("CarlaBackend::EngineEventType2Str(%i) - invalid type", type);
  84. return nullptr;
  85. }
  86. static inline
  87. const char* EngineControlEventType2Str(const EngineControlEventType type)
  88. {
  89. switch (type)
  90. {
  91. case kEngineControlEventTypeNull:
  92. return "kEngineNullEvent";
  93. case kEngineControlEventTypeParameter:
  94. return "kEngineControlEventTypeParameter";
  95. case kEngineControlEventTypeMidiBank:
  96. return "kEngineControlEventTypeMidiBank";
  97. case kEngineControlEventTypeMidiProgram:
  98. return "kEngineControlEventTypeMidiProgram";
  99. case kEngineControlEventTypeAllSoundOff:
  100. return "kEngineControlEventTypeAllSoundOff";
  101. case kEngineControlEventTypeAllNotesOff:
  102. return "kEngineControlEventTypeAllNotesOff";
  103. }
  104. carla_stderr("CarlaBackend::EngineControlEventType2Str(%i) - invalid type", type);
  105. return nullptr;
  106. }
  107. // -----------------------------------------------------------------------
  108. const unsigned short kEngineMaxInternalEventCount = 512;
  109. enum EnginePostAction {
  110. kEnginePostActionNull,
  111. kEnginePostActionZeroCount,
  112. kEnginePostActionRemovePlugin,
  113. kEnginePostActionSwitchPlugins
  114. };
  115. struct EnginePluginData {
  116. CarlaPlugin* plugin;
  117. float insPeak[2];
  118. float outsPeak[2];
  119. void clear()
  120. {
  121. plugin = nullptr;
  122. insPeak[0] = insPeak[1] = 0.0f;
  123. outsPeak[0] = outsPeak[1] = 0.0f;
  124. }
  125. };
  126. // -----------------------------------------------------------------------
  127. struct CarlaEngineProtectedData {
  128. CarlaEngineOsc osc;
  129. CarlaEngineThread thread;
  130. const CarlaOscData* oscData;
  131. EngineCallbackFunc callback;
  132. void* callbackPtr;
  133. unsigned int hints;
  134. uint32_t bufferSize;
  135. double sampleRate;
  136. bool aboutToClose; // don't re-activate thread if true
  137. unsigned int curPluginCount; // number of plugins loaded (0...max)
  138. unsigned int maxPluginNumber; // number of plugins allowed (0, 16, 99 or 255)
  139. unsigned int nextPluginId; // invalid if == maxPluginNumber
  140. CarlaString lastError;
  141. CarlaString name;
  142. EngineOptions options;
  143. EngineTimeInfo timeInfo;
  144. EnginePluginData* plugins;
  145. struct InternalEvents {
  146. EngineEvent* in;
  147. EngineEvent* out;
  148. InternalEvents() noexcept
  149. : in(nullptr),
  150. out(nullptr) {}
  151. ~InternalEvents()
  152. {
  153. CARLA_ASSERT(in == nullptr);
  154. CARLA_ASSERT(out == nullptr);
  155. }
  156. } bufEvents;
  157. struct InternalTime {
  158. bool playing;
  159. uint64_t frame;
  160. InternalTime() noexcept
  161. : playing(false),
  162. frame(0) {}
  163. } time;
  164. struct NextAction {
  165. EnginePostAction opcode;
  166. unsigned int pluginId;
  167. unsigned int value;
  168. CarlaMutex mutex;
  169. NextAction() noexcept
  170. : opcode(kEnginePostActionNull),
  171. pluginId(0),
  172. value(0) {}
  173. ~NextAction()
  174. {
  175. CARLA_ASSERT(opcode == kEnginePostActionNull);
  176. }
  177. void ready() noexcept
  178. {
  179. mutex.lock();
  180. mutex.unlock();
  181. }
  182. } nextAction;
  183. CarlaEngineProtectedData(CarlaEngine* const engine)
  184. : osc(engine),
  185. thread(engine),
  186. oscData(nullptr),
  187. callback(nullptr),
  188. callbackPtr(nullptr),
  189. hints(0x0),
  190. bufferSize(0),
  191. sampleRate(0.0),
  192. aboutToClose(false),
  193. curPluginCount(0),
  194. maxPluginNumber(0),
  195. nextPluginId(0),
  196. plugins(nullptr) {}
  197. #ifdef CARLA_PROPER_CPP11_SUPPORT
  198. CarlaEngineProtectedData() = delete;
  199. CARLA_DECLARE_NON_COPY_STRUCT(CarlaEngineProtectedData)
  200. #endif
  201. ~CarlaEngineProtectedData()
  202. {
  203. CARLA_ASSERT(curPluginCount == 0);
  204. CARLA_ASSERT(maxPluginNumber == 0);
  205. CARLA_ASSERT(nextPluginId == 0);
  206. CARLA_ASSERT(plugins == nullptr);
  207. }
  208. void doPluginRemove()
  209. {
  210. CARLA_ASSERT(curPluginCount > 0);
  211. CARLA_ASSERT(nextAction.pluginId < curPluginCount);
  212. --curPluginCount;
  213. // move all plugins 1 spot backwards
  214. for (unsigned int i=nextAction.pluginId; i < curPluginCount; ++i)
  215. {
  216. CarlaPlugin* const plugin(plugins[i+1].plugin);
  217. CARLA_SAFE_ASSERT_BREAK(plugin != nullptr);
  218. plugin->setId(i);
  219. plugins[i].plugin = plugin;
  220. plugins[i].insPeak[0] = 0.0f;
  221. plugins[i].insPeak[1] = 0.0f;
  222. plugins[i].outsPeak[0] = 0.0f;
  223. plugins[i].outsPeak[1] = 0.0f;
  224. }
  225. const unsigned int id(curPluginCount);
  226. // reset last plugin (now removed)
  227. plugins[id].plugin = nullptr;
  228. plugins[id].insPeak[0] = 0.0f;
  229. plugins[id].insPeak[1] = 0.0f;
  230. plugins[id].outsPeak[0] = 0.0f;
  231. plugins[id].outsPeak[1] = 0.0f;
  232. }
  233. void doPluginsSwitch()
  234. {
  235. CARLA_ASSERT(curPluginCount >= 2);
  236. const unsigned int idA(nextAction.pluginId);
  237. const unsigned int idB(nextAction.value);
  238. CARLA_ASSERT(idA < curPluginCount);
  239. CARLA_ASSERT(idB < curPluginCount);
  240. CARLA_ASSERT(plugins[idA].plugin != nullptr);
  241. CARLA_ASSERT(plugins[idB].plugin != nullptr);
  242. #if 0
  243. std::swap(plugins[idA].plugin, plugins[idB].plugin);
  244. #else
  245. CarlaPlugin* const tmp(plugins[idA].plugin);
  246. plugins[idA].plugin = plugins[idB].plugin;
  247. plugins[idB].plugin = tmp;
  248. #endif
  249. }
  250. void doNextPluginAction(const bool unlock) noexcept
  251. {
  252. switch (nextAction.opcode)
  253. {
  254. case kEnginePostActionNull:
  255. break;
  256. case kEnginePostActionZeroCount:
  257. curPluginCount = 0;
  258. break;
  259. case kEnginePostActionRemovePlugin:
  260. doPluginRemove();
  261. break;
  262. case kEnginePostActionSwitchPlugins:
  263. doPluginsSwitch();
  264. break;
  265. }
  266. nextAction.opcode = kEnginePostActionNull;
  267. nextAction.pluginId = 0;
  268. nextAction.value = 0;
  269. if (unlock)
  270. nextAction.mutex.unlock();
  271. }
  272. class ScopedActionLock
  273. {
  274. public:
  275. ScopedActionLock(CarlaEngineProtectedData* const data, const EnginePostAction action, const unsigned int pluginId, const unsigned int value, const bool lockWait)
  276. : fData(data)
  277. {
  278. fData->nextAction.mutex.lock();
  279. CARLA_ASSERT(fData->nextAction.opcode == kEnginePostActionNull);
  280. fData->nextAction.opcode = action;
  281. fData->nextAction.pluginId = pluginId;
  282. fData->nextAction.value = value;
  283. if (lockWait)
  284. {
  285. // block wait for unlock on processing side
  286. carla_stdout("ScopedPluginAction(%i) - blocking START", pluginId);
  287. fData->nextAction.mutex.lock();
  288. carla_stdout("ScopedPluginAction(%i) - blocking DONE", pluginId);
  289. }
  290. else
  291. {
  292. fData->doNextPluginAction(false);
  293. }
  294. }
  295. ~ScopedActionLock() noexcept
  296. {
  297. fData->nextAction.mutex.unlock();
  298. }
  299. private:
  300. CarlaEngineProtectedData* const fData;
  301. };
  302. };
  303. // -----------------------------------------------------------------------
  304. CARLA_BACKEND_END_NAMESPACE
  305. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED