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.

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