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