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.

359 lines
9.4KB

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