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.

369 lines
9.7KB

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