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.

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