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.

365 lines
9.6KB

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