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.

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