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.

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