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.

361 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. };
  125. // -----------------------------------------------------------------------
  126. struct CarlaEngineProtectedData {
  127. CarlaEngineOsc osc;
  128. CarlaEngineThread thread;
  129. const CarlaOscData* oscData;
  130. EngineCallbackFunc 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 InternalEvents {
  139. EngineEvent* in;
  140. EngineEvent* out;
  141. InternalEvents()
  142. : in(nullptr),
  143. out(nullptr) {}
  144. ~InternalEvents()
  145. {
  146. CARLA_ASSERT(in == nullptr);
  147. CARLA_ASSERT(out == nullptr);
  148. }
  149. } bufEvents;
  150. struct InternalTime {
  151. bool playing;
  152. uint64_t frame;
  153. InternalTime()
  154. : playing(false),
  155. frame(0) {}
  156. } time;
  157. struct NextAction {
  158. EnginePostAction opcode;
  159. unsigned int pluginId;
  160. unsigned int value;
  161. CarlaMutex mutex;
  162. NextAction()
  163. : opcode(kEnginePostActionNull),
  164. pluginId(0),
  165. value(0) {}
  166. ~NextAction()
  167. {
  168. CARLA_ASSERT(opcode == kEnginePostActionNull);
  169. }
  170. void ready()
  171. {
  172. mutex.lock();
  173. mutex.unlock();
  174. }
  175. } nextAction;
  176. CarlaEngineProtectedData(CarlaEngine* const engine)
  177. : osc(engine),
  178. thread(engine),
  179. oscData(nullptr),
  180. callback(nullptr),
  181. callbackPtr(nullptr),
  182. aboutToClose(false),
  183. curPluginCount(0),
  184. maxPluginNumber(0),
  185. nextPluginId(0),
  186. plugins(nullptr) {}
  187. #ifdef CARLA_PROPER_CPP11_SUPPORT
  188. CarlaEngineProtectedData() = delete;
  189. CARLA_DECLARE_NON_COPY_STRUCT(CarlaEngineProtectedData)
  190. #endif
  191. ~CarlaEngineProtectedData()
  192. {
  193. CARLA_ASSERT(curPluginCount == 0);
  194. CARLA_ASSERT(maxPluginNumber == 0);
  195. CARLA_ASSERT(nextPluginId == 0);
  196. CARLA_ASSERT(plugins == nullptr);
  197. }
  198. void doPluginRemove()
  199. {
  200. CARLA_ASSERT(curPluginCount > 0);
  201. CARLA_ASSERT(nextAction.pluginId < curPluginCount);
  202. --curPluginCount;
  203. // move all plugins 1 spot backwards
  204. for (unsigned int i=nextAction.pluginId; i < curPluginCount; ++i)
  205. {
  206. CarlaPlugin* const plugin(plugins[i+1].plugin);
  207. CARLA_ASSERT(plugin != nullptr);
  208. if (plugin == nullptr)
  209. break;
  210. plugin->setId(i);
  211. plugins[i].plugin = plugin;
  212. plugins[i].insPeak[0] = 0.0f;
  213. plugins[i].insPeak[1] = 0.0f;
  214. plugins[i].outsPeak[0] = 0.0f;
  215. plugins[i].outsPeak[1] = 0.0f;
  216. }
  217. const unsigned int id(curPluginCount);
  218. // reset last plugin (now removed)
  219. plugins[id].plugin = nullptr;
  220. plugins[id].insPeak[0] = 0.0f;
  221. plugins[id].insPeak[1] = 0.0f;
  222. plugins[id].outsPeak[0] = 0.0f;
  223. plugins[id].outsPeak[1] = 0.0f;
  224. }
  225. void doPluginsSwitch()
  226. {
  227. CARLA_ASSERT(curPluginCount >= 2);
  228. const unsigned int idA(nextAction.pluginId);
  229. const unsigned int idB(nextAction.value);
  230. CARLA_ASSERT(idA < curPluginCount);
  231. CARLA_ASSERT(idB < curPluginCount);
  232. CARLA_ASSERT(plugins[idA].plugin != nullptr);
  233. CARLA_ASSERT(plugins[idB].plugin != nullptr);
  234. #if 0
  235. std::swap(plugins[idA].plugin, plugins[idB].plugin);
  236. #else
  237. CarlaPlugin* const tmp(plugins[idA].plugin);
  238. plugins[idA].plugin = plugins[idB].plugin;
  239. plugins[idB].plugin = tmp;
  240. #endif
  241. }
  242. void doNextPluginAction(const bool unlock)
  243. {
  244. switch (nextAction.opcode)
  245. {
  246. case kEnginePostActionNull:
  247. break;
  248. case kEnginePostActionZeroCount:
  249. curPluginCount = 0;
  250. break;
  251. case kEnginePostActionRemovePlugin:
  252. doPluginRemove();
  253. break;
  254. case kEnginePostActionSwitchPlugins:
  255. doPluginsSwitch();
  256. break;
  257. }
  258. nextAction.opcode = kEnginePostActionNull;
  259. nextAction.pluginId = 0;
  260. nextAction.value = 0;
  261. if (unlock)
  262. nextAction.mutex.unlock();
  263. }
  264. class ScopedActionLock
  265. {
  266. public:
  267. ScopedActionLock(CarlaEngineProtectedData* const data, const EnginePostAction action, const unsigned int pluginId, const unsigned int value, const bool lockWait)
  268. : fData(data)
  269. {
  270. fData->nextAction.mutex.lock();
  271. CARLA_ASSERT(fData->nextAction.opcode == kEnginePostActionNull);
  272. fData->nextAction.opcode = action;
  273. fData->nextAction.pluginId = pluginId;
  274. fData->nextAction.value = value;
  275. if (lockWait)
  276. {
  277. // block wait for unlock on processing side
  278. carla_stdout("ScopedPluginAction(%i) - blocking START", pluginId);
  279. fData->nextAction.mutex.lock();
  280. carla_stdout("ScopedPluginAction(%i) - blocking DONE", pluginId);
  281. }
  282. else
  283. {
  284. fData->doNextPluginAction(false);
  285. }
  286. }
  287. ~ScopedActionLock()
  288. {
  289. fData->nextAction.mutex.unlock();
  290. }
  291. private:
  292. CarlaEngineProtectedData* const fData;
  293. };
  294. };
  295. // -----------------------------------------------------------------------
  296. CARLA_BACKEND_END_NAMESPACE
  297. #endif // CARLA_ENGINE_INTERNAL_HPP_INCLUDED