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.

592 lines
14KB

  1. /*
  2. * Carla Plugin
  3. * Copyright (C) 2011-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 GPL.txt file
  16. */
  17. #ifndef __CARLA_PLUGIN_INTERNAL_HPP__
  18. #define __CARLA_PLUGIN_INTERNAL_HPP__
  19. #include "CarlaPluginThread.hpp"
  20. #include "CarlaPlugin.hpp"
  21. #include "CarlaEngine.hpp"
  22. #include "CarlaOscUtils.hpp"
  23. #include "CarlaStateUtils.hpp"
  24. #include "CarlaMutex.hpp"
  25. #include "CarlaMIDI.h"
  26. #include "RtList.hpp"
  27. #define CARLA_PROCESS_CONTINUE_CHECK if (! fEnabled) { kData->engine->callback(CALLBACK_DEBUG, fId, 0, 0, 0.0, nullptr); return; }
  28. CARLA_BACKEND_START_NAMESPACE
  29. // -----------------------------------------------------------------------
  30. const unsigned short MAX_RT_EVENTS = 128;
  31. const unsigned short MAX_MIDI_EVENTS = 512;
  32. const unsigned int PLUGIN_HINT_HAS_MIDI_IN = 0x1;
  33. const unsigned int PLUGIN_HINT_HAS_MIDI_OUT = 0x2;
  34. const unsigned int PLUGIN_HINT_CAN_RUN_RACK = 0x4;
  35. // -----------------------------------------------------------------------
  36. struct PluginAudioPort {
  37. uint32_t rindex;
  38. CarlaEngineAudioPort* port;
  39. PluginAudioPort()
  40. : rindex(0),
  41. port(nullptr) {}
  42. ~PluginAudioPort()
  43. {
  44. CARLA_ASSERT(port == nullptr);
  45. }
  46. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginAudioPort)
  47. };
  48. struct PluginAudioData {
  49. uint32_t count;
  50. PluginAudioPort* ports;
  51. PluginAudioData()
  52. : count(0),
  53. ports(nullptr) {}
  54. ~PluginAudioData()
  55. {
  56. CARLA_ASSERT_INT(count == 0, count);
  57. CARLA_ASSERT(ports == nullptr);
  58. }
  59. void createNew(const uint32_t newCount)
  60. {
  61. CARLA_ASSERT_INT(count == 0, count);
  62. CARLA_ASSERT(ports == nullptr);
  63. CARLA_ASSERT_INT(newCount > 0, newCount);
  64. if (ports != nullptr || newCount == 0)
  65. return;
  66. ports = new PluginAudioPort[newCount];
  67. count = newCount;
  68. }
  69. void clear()
  70. {
  71. if (ports != nullptr)
  72. {
  73. for (uint32_t i=0; i < count; i++)
  74. {
  75. if (ports[i].port != nullptr)
  76. {
  77. delete ports[i].port;
  78. ports[i].port = nullptr;
  79. }
  80. }
  81. delete[] ports;
  82. ports = nullptr;
  83. }
  84. count = 0;
  85. }
  86. void initBuffers(CarlaEngine* const engine)
  87. {
  88. for (uint32_t i=0; i < count; i++)
  89. {
  90. if (ports[i].port != nullptr)
  91. ports[i].port->initBuffer(engine);
  92. }
  93. }
  94. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginAudioData)
  95. };
  96. // -----------------------------------------------------------------------
  97. struct PluginEventData {
  98. CarlaEngineEventPort* portIn;
  99. CarlaEngineEventPort* portOut;
  100. PluginEventData()
  101. : portIn(nullptr),
  102. portOut(nullptr) {}
  103. ~PluginEventData()
  104. {
  105. CARLA_ASSERT(portIn == nullptr);
  106. CARLA_ASSERT(portOut == nullptr);
  107. }
  108. void clear()
  109. {
  110. if (portIn != nullptr)
  111. {
  112. delete portIn;
  113. portIn = nullptr;
  114. }
  115. if (portOut != nullptr)
  116. {
  117. delete portOut;
  118. portOut = nullptr;
  119. }
  120. }
  121. void initBuffers(CarlaEngine* const engine)
  122. {
  123. if (portIn != nullptr)
  124. portIn->initBuffer(engine);
  125. if (portOut != nullptr)
  126. portOut->initBuffer(engine);
  127. }
  128. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginEventData)
  129. };
  130. // -----------------------------------------------------------------------
  131. struct PluginParameterData {
  132. uint32_t count;
  133. ParameterData* data;
  134. ParameterRanges* ranges;
  135. PluginParameterData()
  136. : count(0),
  137. data(nullptr),
  138. ranges(nullptr) {}
  139. ~PluginParameterData()
  140. {
  141. CARLA_ASSERT_INT(count == 0, count);
  142. CARLA_ASSERT(data == nullptr);
  143. CARLA_ASSERT(ranges == nullptr);
  144. }
  145. void createNew(const uint32_t newCount)
  146. {
  147. CARLA_ASSERT_INT(count == 0, count);
  148. CARLA_ASSERT(data == nullptr);
  149. CARLA_ASSERT(ranges == nullptr);
  150. CARLA_ASSERT_INT(newCount > 0, newCount);
  151. if (data != nullptr || ranges != nullptr || newCount == 0)
  152. return;
  153. data = new ParameterData[newCount];
  154. ranges = new ParameterRanges[newCount];
  155. count = newCount;
  156. }
  157. void clear()
  158. {
  159. if (data != nullptr)
  160. {
  161. delete[] data;
  162. data = nullptr;
  163. }
  164. if (ranges != nullptr)
  165. {
  166. delete[] ranges;
  167. ranges = nullptr;
  168. }
  169. count = 0;
  170. }
  171. float fixValue(const uint32_t parameterId, const float& value)
  172. {
  173. CARLA_ASSERT(parameterId < count);
  174. return ranges[parameterId].fixValue(value);
  175. }
  176. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginParameterData)
  177. };
  178. // -----------------------------------------------------------------------
  179. typedef const char* ProgramName;
  180. struct PluginProgramData {
  181. uint32_t count;
  182. int32_t current;
  183. ProgramName* names;
  184. PluginProgramData()
  185. : count(0),
  186. current(-1),
  187. names(nullptr) {}
  188. ~PluginProgramData()
  189. {
  190. CARLA_ASSERT_INT(count == 0, count);
  191. CARLA_ASSERT_INT(current == -1, current);
  192. CARLA_ASSERT(names == nullptr);
  193. }
  194. void createNew(const uint32_t newCount)
  195. {
  196. CARLA_ASSERT_INT(count == 0, count);
  197. CARLA_ASSERT_INT(current == -1, current);
  198. CARLA_ASSERT(names == nullptr);
  199. CARLA_ASSERT_INT(newCount > 0, newCount);
  200. if (names != nullptr || newCount == 0)
  201. return;
  202. names = new ProgramName[newCount];
  203. count = newCount;
  204. for (uint32_t i=0; i < newCount; i++)
  205. names[i] = nullptr;
  206. }
  207. void clear()
  208. {
  209. if (names != nullptr)
  210. {
  211. for (uint32_t i=0; i < count; i++)
  212. {
  213. if (names[i] != nullptr)
  214. delete[] names[i];
  215. }
  216. delete[] names;
  217. names = nullptr;
  218. }
  219. count = 0;
  220. current = -1;
  221. }
  222. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginProgramData)
  223. };
  224. // -----------------------------------------------------------------------
  225. struct PluginMidiProgramData {
  226. uint32_t count;
  227. int32_t current;
  228. MidiProgramData* data;
  229. PluginMidiProgramData()
  230. : count(0),
  231. current(-1),
  232. data(nullptr) {}
  233. ~PluginMidiProgramData()
  234. {
  235. CARLA_ASSERT_INT(count == 0, count);
  236. CARLA_ASSERT_INT(current == -1, current);
  237. CARLA_ASSERT(data == nullptr);
  238. }
  239. void createNew(const uint32_t newCount)
  240. {
  241. CARLA_ASSERT_INT(count == 0, count);
  242. CARLA_ASSERT_INT(current == -1, current);
  243. CARLA_ASSERT(data == nullptr);
  244. CARLA_ASSERT_INT(newCount > 0, newCount);
  245. if (data != nullptr || newCount == 0)
  246. return;
  247. data = new MidiProgramData[newCount];
  248. count = newCount;
  249. }
  250. void clear()
  251. {
  252. if (data != nullptr)
  253. {
  254. delete[] data;
  255. data = nullptr;
  256. }
  257. count = 0;
  258. current = -1;
  259. }
  260. const MidiProgramData& getCurrent() const
  261. {
  262. CARLA_ASSERT(current >= 0 && current < static_cast<int32_t>(count));
  263. return data[current];
  264. }
  265. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginMidiProgramData)
  266. };
  267. // -----------------------------------------------------------------------
  268. struct PluginPostRtEvent {
  269. PluginPostRtEventType type;
  270. int32_t value1;
  271. int32_t value2;
  272. float value3;
  273. PluginPostRtEvent()
  274. : type(kPluginPostRtEventNull),
  275. value1(-1),
  276. value2(-1),
  277. value3(0.0f) {}
  278. #if 1//def DEBUG
  279. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginPostRtEvent)
  280. #else
  281. CARLA_DECLARE_NON_COPY_STRUCT(PluginPostRtEvent)
  282. #endif
  283. };
  284. // -----------------------------------------------------------------------
  285. struct ExternalMidiNote {
  286. int8_t channel; // invalid == -1
  287. uint8_t note;
  288. uint8_t velo; // note-off if 0
  289. ExternalMidiNote()
  290. : channel(-1),
  291. note(0),
  292. velo(0) {}
  293. #if 1//def DEBUG
  294. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(ExternalMidiNote)
  295. #else
  296. CARLA_DECLARE_NON_COPY_STRUCT(ExternalMidiNote)
  297. #endif
  298. };
  299. // -----------------------------------------------------------------------
  300. class CarlaPluginGui;
  301. struct CarlaPluginProtectedData {
  302. CarlaEngine* const engine;
  303. CarlaEngineClient* client;
  304. CarlaPluginGui* gui;
  305. bool active;
  306. bool needsReset;
  307. void* lib;
  308. // misc
  309. int8_t ctrlChannel;
  310. unsigned int extraHints;
  311. // latency
  312. uint32_t latency;
  313. float** latencyBuffers;
  314. // data
  315. PluginAudioData audioIn;
  316. PluginAudioData audioOut;
  317. PluginEventData event;
  318. PluginParameterData param;
  319. PluginProgramData prog;
  320. PluginMidiProgramData midiprog;
  321. NonRtList<CustomData> custom;
  322. CarlaMutex masterMutex; // global master lock
  323. CarlaMutex singleMutex; // small lock used only in processSingle()
  324. struct ExternalNotes {
  325. CarlaMutex mutex;
  326. RtList<ExternalMidiNote>::Pool dataPool;
  327. RtList<ExternalMidiNote> data;
  328. ExternalNotes()
  329. : dataPool(32, 152),
  330. data(&dataPool) {}
  331. ~ExternalNotes()
  332. {
  333. mutex.lock();
  334. data.clear();
  335. mutex.unlock();
  336. }
  337. void append(const ExternalMidiNote& note)
  338. {
  339. mutex.lock();
  340. data.append_sleepy(note);
  341. mutex.unlock();
  342. }
  343. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(ExternalNotes)
  344. } extNotes;
  345. struct PostRtEvents {
  346. CarlaMutex mutex;
  347. RtList<PluginPostRtEvent>::Pool dataPool;
  348. RtList<PluginPostRtEvent> data;
  349. RtList<PluginPostRtEvent> dataPendingRT;
  350. PostRtEvents()
  351. : dataPool(MAX_RT_EVENTS, MAX_RT_EVENTS),
  352. data(&dataPool),
  353. dataPendingRT(&dataPool) {}
  354. ~PostRtEvents()
  355. {
  356. clear();
  357. }
  358. void appendRT(const PluginPostRtEvent& event)
  359. {
  360. dataPendingRT.append(event);
  361. }
  362. void trySplice()
  363. {
  364. if (mutex.tryLock())
  365. {
  366. dataPendingRT.spliceAppend(data, true);
  367. mutex.unlock();
  368. }
  369. }
  370. void clear()
  371. {
  372. mutex.lock();
  373. data.clear();
  374. dataPendingRT.clear();
  375. mutex.unlock();
  376. }
  377. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PostRtEvents)
  378. } postRtEvents;
  379. struct PostProc {
  380. float dryWet;
  381. float volume;
  382. float balanceLeft;
  383. float balanceRight;
  384. float panning;
  385. PostProc()
  386. : dryWet(1.0f),
  387. volume(1.0f),
  388. balanceLeft(-1.0f),
  389. balanceRight(1.0f),
  390. panning(0.0f) {}
  391. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PostProc)
  392. } postProc;
  393. struct OSC {
  394. CarlaOscData data;
  395. CarlaPluginThread thread;
  396. OSC(CarlaEngine* const engine, CarlaPlugin* const plugin)
  397. : thread(engine, plugin) {}
  398. OSC() = delete;
  399. OSC(OSC&) = delete;
  400. OSC(const OSC&) = delete;
  401. CARLA_LEAK_DETECTOR(OSC)
  402. } osc;
  403. CarlaPluginProtectedData(CarlaEngine* const engine_, CarlaPlugin* const plugin)
  404. : engine(engine_),
  405. client(nullptr),
  406. gui(nullptr),
  407. active(false),
  408. needsReset(false),
  409. lib(nullptr),
  410. ctrlChannel(0),
  411. extraHints(0x0),
  412. latency(0),
  413. latencyBuffers(nullptr),
  414. osc(engine, plugin) {}
  415. CarlaPluginProtectedData() = delete;
  416. CarlaPluginProtectedData(CarlaPluginProtectedData&) = delete;
  417. CarlaPluginProtectedData(const CarlaPluginProtectedData&) = delete;
  418. CARLA_LEAK_DETECTOR(CarlaPluginProtectedData)
  419. ~CarlaPluginProtectedData()
  420. {
  421. CARLA_ASSERT(gui == nullptr);
  422. CARLA_ASSERT(! active);
  423. CARLA_ASSERT(! needsReset);
  424. CARLA_ASSERT(lib == nullptr);
  425. CARLA_ASSERT(latency == 0);
  426. CARLA_ASSERT(latencyBuffers == nullptr);
  427. }
  428. void recreateLatencyBuffers()
  429. {
  430. if (latencyBuffers != nullptr)
  431. {
  432. for (uint32_t i=0; i < audioIn.count; i++)
  433. {
  434. CARLA_ASSERT(latencyBuffers[i] != nullptr);
  435. if (latencyBuffers[i] != nullptr)
  436. delete[] latencyBuffers[i];
  437. }
  438. delete[] latencyBuffers;
  439. latencyBuffers = nullptr;
  440. }
  441. if (audioIn.count > 0 && latency > 0)
  442. {
  443. latencyBuffers = new float*[audioIn.count];
  444. for (uint32_t i=0; i < audioIn.count; i++)
  445. {
  446. latencyBuffers[i] = new float[latency];
  447. carla_zeroFloat(latencyBuffers[i], latency);
  448. }
  449. }
  450. }
  451. static CarlaEngine* getEngine(CarlaPlugin* const plugin)
  452. {
  453. return plugin->kData->engine;
  454. }
  455. static CarlaEngineAudioPort* getAudioInPort(CarlaPlugin* const plugin, const uint32_t index)
  456. {
  457. return plugin->kData->audioIn.ports[index].port;
  458. }
  459. static CarlaEngineAudioPort* getAudioOutPort(CarlaPlugin* const plugin, const uint32_t index)
  460. {
  461. return plugin->kData->audioOut.ports[index].port;
  462. }
  463. static bool canRunInRack(CarlaPlugin* const plugin)
  464. {
  465. return (plugin->kData->extraHints & PLUGIN_HINT_CAN_RUN_RACK);
  466. }
  467. };
  468. // -----------------------------------------------------------------------
  469. CARLA_BACKEND_END_NAMESPACE
  470. #endif // __CARLA_PLUGIN_INTERNAL_HPP__