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.

690 lines
16KB

  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. class CarlaPluginGUI : public QMainWindow
  309. {
  310. public:
  311. class Callback
  312. {
  313. public:
  314. virtual ~Callback() {}
  315. virtual void guiClosedCallback() = 0;
  316. };
  317. CarlaPluginGUI(QWidget* const parent, Callback* const callback);
  318. ~CarlaPluginGUI();
  319. private:
  320. Callback* const kCallback;
  321. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginGUI)
  322. };
  323. // -----------------------------------------------------------------------
  324. struct CarlaPluginProtectedData {
  325. CarlaEngine* const engine;
  326. CarlaEngineClient* client;
  327. CarlaPluginGUI* gui;
  328. bool active;
  329. bool activeBefore;
  330. bool needsReset;
  331. void* lib;
  332. // misc
  333. int8_t ctrlChannel;
  334. unsigned int extraHints;
  335. // latency
  336. uint32_t latency;
  337. float** latencyBuffers;
  338. // data
  339. PluginAudioData audioIn;
  340. PluginAudioData audioOut;
  341. PluginEventData event;
  342. PluginParameterData param;
  343. PluginProgramData prog;
  344. PluginMidiProgramData midiprog;
  345. NonRtList<CustomData*> custom;
  346. CarlaMutex mutex;
  347. struct ExternalNotes {
  348. CarlaMutex mutex;
  349. RtList<ExternalMidiNote>::Pool dataPool;
  350. RtList<ExternalMidiNote> data;
  351. ExternalNotes()
  352. : dataPool(32, 152),
  353. data(&dataPool) {}
  354. ~ExternalNotes()
  355. {
  356. mutex.lock();
  357. data.clear();
  358. mutex.unlock();
  359. }
  360. void append(const ExternalMidiNote& note)
  361. {
  362. mutex.lock();
  363. data.append_sleepy(note);
  364. mutex.unlock();
  365. }
  366. ExternalNotes(ExternalNotes&) = delete;
  367. ExternalNotes(const ExternalNotes&) = delete;
  368. } extNotes;
  369. struct PostRtEvents {
  370. CarlaMutex mutex;
  371. RtList<PluginPostRtEvent>::Pool dataPool;
  372. RtList<PluginPostRtEvent> data;
  373. RtList<PluginPostRtEvent> dataPendingRT;
  374. PostRtEvents()
  375. : dataPool(MAX_RT_EVENTS, MAX_RT_EVENTS),
  376. data(&dataPool),
  377. dataPendingRT(&dataPool) {}
  378. ~PostRtEvents()
  379. {
  380. clear();
  381. }
  382. void appendRT(const PluginPostRtEvent& event)
  383. {
  384. dataPendingRT.append(event);
  385. }
  386. void trySplice()
  387. {
  388. if (mutex.tryLock())
  389. {
  390. dataPendingRT.spliceAppend(data, true);
  391. mutex.unlock();
  392. }
  393. }
  394. void clear()
  395. {
  396. mutex.lock();
  397. data.clear();
  398. dataPendingRT.clear();
  399. mutex.unlock();
  400. }
  401. PostRtEvents(PostRtEvents&) = delete;
  402. PostRtEvents(const PostRtEvents&) = delete;
  403. } postRtEvents;
  404. struct PostProc {
  405. float dryWet;
  406. float volume;
  407. float balanceLeft;
  408. float balanceRight;
  409. float panning;
  410. PostProc()
  411. : dryWet(1.0f),
  412. volume(1.0f),
  413. balanceLeft(-1.0f),
  414. balanceRight(1.0f),
  415. panning(0.0f) {}
  416. PostProc(PostProc&) = delete;
  417. PostProc(const PostProc&) = delete;
  418. } postProc;
  419. struct OSC {
  420. CarlaOscData data;
  421. CarlaPluginThread thread;
  422. OSC(CarlaEngine* const engine, CarlaPlugin* const plugin)
  423. : thread(engine, plugin) {}
  424. OSC() = delete;
  425. OSC(OSC&) = delete;
  426. OSC(const OSC&) = delete;
  427. } osc;
  428. CarlaPluginProtectedData(CarlaEngine* const engine_, CarlaPlugin* const plugin)
  429. : engine(engine_),
  430. client(nullptr),
  431. gui(nullptr),
  432. active(false),
  433. activeBefore(false),
  434. needsReset(false),
  435. lib(nullptr),
  436. ctrlChannel(-1),
  437. extraHints(0x0),
  438. latency(0),
  439. latencyBuffers(nullptr),
  440. osc(engine_, plugin) {}
  441. CarlaPluginProtectedData() = delete;
  442. CarlaPluginProtectedData(CarlaPluginProtectedData&) = delete;
  443. CarlaPluginProtectedData(const CarlaPluginProtectedData&) = delete;
  444. static CarlaEngine* getEngine(CarlaPlugin* const plugin)
  445. {
  446. return plugin->kData->engine;
  447. }
  448. static CarlaEngineAudioPort* getAudioInPort(CarlaPlugin* const plugin, const uint32_t index)
  449. {
  450. return plugin->kData->audioIn.ports[index].port;
  451. }
  452. static CarlaEngineAudioPort* getAudioOutPort(CarlaPlugin* const plugin, const uint32_t index)
  453. {
  454. return plugin->kData->audioOut.ports[index].port;
  455. }
  456. static bool canRunInRack(CarlaPlugin* const plugin)
  457. {
  458. return (plugin->kData->extraHints & PLUGIN_HINT_CAN_RUN_RACK);
  459. }
  460. CARLA_LEAK_DETECTOR(CarlaPluginProtectedData)
  461. };
  462. // -----------------------------------------------------------------------
  463. CARLA_BACKEND_END_NAMESPACE
  464. #endif // __CARLA_PLUGIN_INTERNAL_HPP__
  465. // common includes
  466. //#include <cmath>
  467. //#include <vector>
  468. //#include <QtCore/QMutex>
  469. //#include <QtGui/QMainWindow>
  470. //#ifdef Q_WS_X11
  471. //# include <QtGui/QX11EmbedContainer>
  472. //typedef QX11EmbedContainer GuiContainer;
  473. //#else
  474. //# include <QtGui/QWidget>
  475. //typedef QWidget GuiContainer;
  476. //#endif
  477. #if 0
  478. /*!
  479. * \class CarlaPluginGUI
  480. *
  481. * \brief Carla Backend gui plugin class
  482. *
  483. * \see CarlaPlugin
  484. */
  485. class CarlaPluginGUI : public QMainWindow
  486. {
  487. public:
  488. /*!
  489. * \class Callback
  490. *
  491. * \brief Carla plugin GUI callback
  492. */
  493. class Callback
  494. {
  495. public:
  496. virtual ~Callback() {}
  497. virtual void guiClosedCallback() = 0;
  498. };
  499. // -------------------------------------------------------------------
  500. // Constructor and destructor
  501. /*!
  502. * TODO
  503. */
  504. CarlaPluginGUI(QWidget* const parent, Callback* const callback);
  505. /*!
  506. * TODO
  507. */
  508. ~CarlaPluginGUI();
  509. // -------------------------------------------------------------------
  510. // Get data
  511. /*!
  512. * TODO
  513. */
  514. GuiContainer* getContainer() const;
  515. /*!
  516. * TODO
  517. */
  518. WId getWinId() const;
  519. // -------------------------------------------------------------------
  520. // Set data
  521. /*!
  522. * TODO
  523. */
  524. void setNewSize(const int width, const int height);
  525. /*!
  526. * TODO
  527. */
  528. void setResizable(const bool resizable);
  529. /*!
  530. * TODO
  531. */
  532. void setTitle(const char* const title);
  533. /*!
  534. * TODO
  535. */
  536. void setVisible(const bool yesNo);
  537. // -------------------------------------------------------------------
  538. private:
  539. Callback* const m_callback;
  540. GuiContainer* m_container;
  541. QByteArray m_geometry;
  542. bool m_resizable;
  543. void hideEvent(QHideEvent* const event);
  544. void closeEvent(QCloseEvent* const event);
  545. };
  546. #endif