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.

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