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.

355 lines
9.5KB

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