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.

643 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. double value3;
  261. PluginPostRtEvent()
  262. : type(kPluginPostRtEventNull),
  263. value1(-1),
  264. value2(-1),
  265. value3(0.0) {}
  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(152, 512),
  326. data(&dataPool) {}
  327. ~ExternalNotes()
  328. {
  329. data.clear();
  330. }
  331. void append(const ExternalMidiNote& note)
  332. {
  333. data.append_sleepy(note);
  334. }
  335. } extNotes;
  336. struct PostRtEvents {
  337. CarlaMutex mutex;
  338. RtList<PluginPostRtEvent>::Pool dataPool;
  339. RtList<PluginPostRtEvent> data;
  340. RtList<PluginPostRtEvent> dataPendingRT;
  341. PostRtEvents()
  342. : dataPool(MAX_RT_EVENTS, MAX_RT_EVENTS),
  343. data(&dataPool),
  344. dataPendingRT(&dataPool) {}
  345. ~PostRtEvents()
  346. {
  347. clear();
  348. }
  349. void appendRT(const PluginPostRtEvent& event)
  350. {
  351. // FIXME!! need lock?
  352. dataPendingRT.append(event);
  353. }
  354. void trySplice()
  355. {
  356. if (mutex.tryLock())
  357. {
  358. dataPendingRT.splice(data, true);
  359. mutex.unlock();
  360. }
  361. }
  362. void clear()
  363. {
  364. mutex.lock();
  365. data.clear();
  366. dataPendingRT.clear();
  367. mutex.unlock();
  368. }
  369. } postRtEvents;
  370. struct PostProc {
  371. float dryWet;
  372. float volume;
  373. float balanceLeft;
  374. float balanceRight;
  375. float panning;
  376. PostProc()
  377. : dryWet(1.0f),
  378. volume(1.0f),
  379. balanceLeft(-1.0f),
  380. balanceRight(1.0f),
  381. panning(0.0f) {}
  382. } postProc;
  383. struct OSC {
  384. CarlaOscData data;
  385. CarlaPluginThread thread;
  386. OSC(CarlaEngine* const engine, CarlaPlugin* const plugin, const CarlaPluginThread::Mode mode)
  387. : thread(engine, plugin, mode) {}
  388. } osc;
  389. CarlaPluginProtectedData(CarlaEngine* const engine_, CarlaPlugin* const plugin, const CarlaPluginThread::Mode mode)
  390. : engine(engine_),
  391. client(nullptr),
  392. gui(nullptr),
  393. active(false),
  394. activeBefore(false),
  395. needsReset(false),
  396. lib(nullptr),
  397. availOptions(0x0),
  398. extraHints(0x0),
  399. ctrlChannel(-1),
  400. latency(0),
  401. latencyBuffers(nullptr),
  402. osc(engine_, plugin, mode) {}
  403. CarlaPluginProtectedData() = delete;
  404. CarlaPluginProtectedData(CarlaPluginProtectedData&) = delete;
  405. CarlaPluginProtectedData(const CarlaPluginProtectedData&) = delete;
  406. static CarlaEngine* getEngine(CarlaPlugin* const plugin)
  407. {
  408. return plugin->kData->engine;
  409. }
  410. static CarlaEngineAudioPort* getAudioInPort(CarlaPlugin* const plugin, uint32_t index)
  411. {
  412. return plugin->kData->audioIn.ports[index].port;
  413. }
  414. static CarlaEngineAudioPort* getAudioOutPort(CarlaPlugin* const plugin, uint32_t index)
  415. {
  416. return plugin->kData->audioOut.ports[index].port;
  417. }
  418. CARLA_LEAK_DETECTOR(CarlaPluginProtectedData)
  419. };
  420. // -----------------------------------------------------------------------
  421. CARLA_BACKEND_END_NAMESPACE
  422. #endif // __CARLA_PLUGIN_INTERNAL_HPP__
  423. // common includes
  424. //#include <cmath>
  425. //#include <vector>
  426. //#include <QtCore/QMutex>
  427. //#include <QtGui/QMainWindow>
  428. //#ifdef Q_WS_X11
  429. //# include <QtGui/QX11EmbedContainer>
  430. //typedef QX11EmbedContainer GuiContainer;
  431. //#else
  432. //# include <QtGui/QWidget>
  433. //typedef QWidget GuiContainer;
  434. //#endif
  435. #if 0
  436. /*!
  437. * \class CarlaPluginGUI
  438. *
  439. * \brief Carla Backend gui plugin class
  440. *
  441. * \see CarlaPlugin
  442. */
  443. class CarlaPluginGUI : public QMainWindow
  444. {
  445. public:
  446. /*!
  447. * \class Callback
  448. *
  449. * \brief Carla plugin GUI callback
  450. */
  451. class Callback
  452. {
  453. public:
  454. virtual ~Callback() {}
  455. virtual void guiClosedCallback() = 0;
  456. };
  457. // -------------------------------------------------------------------
  458. // Constructor and destructor
  459. /*!
  460. * TODO
  461. */
  462. CarlaPluginGUI(QWidget* const parent, Callback* const callback);
  463. /*!
  464. * TODO
  465. */
  466. ~CarlaPluginGUI();
  467. // -------------------------------------------------------------------
  468. // Get data
  469. /*!
  470. * TODO
  471. */
  472. GuiContainer* getContainer() const;
  473. /*!
  474. * TODO
  475. */
  476. WId getWinId() const;
  477. // -------------------------------------------------------------------
  478. // Set data
  479. /*!
  480. * TODO
  481. */
  482. void setNewSize(const int width, const int height);
  483. /*!
  484. * TODO
  485. */
  486. void setResizable(const bool resizable);
  487. /*!
  488. * TODO
  489. */
  490. void setTitle(const char* const title);
  491. /*!
  492. * TODO
  493. */
  494. void setVisible(const bool yesNo);
  495. // -------------------------------------------------------------------
  496. private:
  497. Callback* const m_callback;
  498. GuiContainer* m_container;
  499. QByteArray m_geometry;
  500. bool m_resizable;
  501. void hideEvent(QHideEvent* const event);
  502. void closeEvent(QCloseEvent* const event);
  503. };
  504. #endif