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.

CarlaPluginInternal.hpp 15KB

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