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.

634 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. names = new ProgramName[count];
  195. for (uint32_t i=0; i < count; i++)
  196. names[i] = nullptr;
  197. this->count = count;
  198. }
  199. void clear()
  200. {
  201. if (names != nullptr)
  202. {
  203. for (uint32_t i=0; i < count; i++)
  204. {
  205. if (names[i] != nullptr)
  206. delete[] names[i];
  207. }
  208. delete[] names;
  209. names = nullptr;
  210. }
  211. count = 0;
  212. current = -1;
  213. }
  214. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginProgramData)
  215. };
  216. // -----------------------------------------------------------------------
  217. struct PluginMidiProgramData {
  218. uint32_t count;
  219. int32_t current;
  220. MidiProgramData* data;
  221. PluginMidiProgramData()
  222. : count(0),
  223. current(-1),
  224. data(nullptr) {}
  225. ~PluginMidiProgramData()
  226. {
  227. CARLA_ASSERT(data == nullptr);
  228. }
  229. void createNew(const uint32_t count)
  230. {
  231. CARLA_ASSERT(data == nullptr);
  232. if (data == nullptr)
  233. data = new MidiProgramData[count];
  234. this->count = count;
  235. }
  236. void clear()
  237. {
  238. if (data != nullptr)
  239. {
  240. delete[] data;
  241. data = nullptr;
  242. }
  243. count = 0;
  244. current = -1;
  245. }
  246. const MidiProgramData& getCurrent() const
  247. {
  248. CARLA_ASSERT(current >= 0 && current < static_cast<int32_t>(count));
  249. return data[current];
  250. }
  251. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginMidiProgramData)
  252. };
  253. // -----------------------------------------------------------------------
  254. struct PluginPostRtEvent {
  255. PluginPostRtEventType type;
  256. int32_t value1;
  257. int32_t value2;
  258. double value3;
  259. PluginPostRtEvent()
  260. : type(kPluginPostRtEventNull),
  261. value1(-1),
  262. value2(-1),
  263. value3(0.0) {}
  264. CARLA_DECLARE_NON_COPY_STRUCT(PluginPostRtEvent)
  265. };
  266. // -----------------------------------------------------------------------
  267. struct ExternalMidiNote {
  268. int8_t channel; // invalid = -1
  269. uint8_t note;
  270. uint8_t velo;
  271. ExternalMidiNote()
  272. : channel(-1),
  273. note(0),
  274. velo(0) {}
  275. CARLA_DECLARE_NON_COPY_STRUCT(ExternalMidiNote)
  276. };
  277. // -----------------------------------------------------------------------
  278. class CarlaPluginGUI : public QMainWindow
  279. {
  280. public:
  281. class Callback
  282. {
  283. public:
  284. virtual ~Callback() {}
  285. virtual void guiClosedCallback() = 0;
  286. };
  287. CarlaPluginGUI(QWidget* const parent, Callback* const callback);
  288. ~CarlaPluginGUI();
  289. private:
  290. Callback* const kCallback;
  291. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginGUI)
  292. };
  293. // -----------------------------------------------------------------------
  294. struct CarlaPluginProtectedData {
  295. CarlaEngine* const engine;
  296. CarlaEngineClient* client;
  297. CarlaPluginGUI* gui;
  298. bool active;
  299. bool activeBefore;
  300. void* lib;
  301. // misc
  302. int8_t ctrlInChannel;
  303. unsigned int extraHints;
  304. uint32_t latency;
  305. float** latencyBuffers;
  306. // data
  307. PluginAudioData audioIn;
  308. PluginAudioData audioOut;
  309. PluginEventData event;
  310. PluginParameterData param;
  311. PluginProgramData prog;
  312. PluginMidiProgramData midiprog;
  313. NonRtListNew<CustomData> custom;
  314. struct ExternalNotes {
  315. CarlaMutex mutex;
  316. RtList<ExternalMidiNote>::Pool dataPool;
  317. RtList<ExternalMidiNote> data;
  318. ExternalNotes()
  319. : dataPool(152, 512),
  320. data(&dataPool) {}
  321. ~ExternalNotes()
  322. {
  323. data.clear();
  324. }
  325. void append(const ExternalMidiNote& note)
  326. {
  327. data.append_sleepy(note);
  328. }
  329. } extNotes;
  330. struct PostRtEvents {
  331. CarlaMutex mutex;
  332. RtList<PluginPostRtEvent>::Pool dataPool;
  333. RtList<PluginPostRtEvent> data;
  334. RtList<PluginPostRtEvent> dataPendingRT;
  335. PostRtEvents()
  336. : dataPool(MAX_RT_EVENTS, MAX_RT_EVENTS),
  337. data(&dataPool),
  338. dataPendingRT(&dataPool) {}
  339. ~PostRtEvents()
  340. {
  341. clear();
  342. }
  343. void appendRT(const PluginPostRtEvent& event)
  344. {
  345. // FIXME!! need lock?
  346. dataPendingRT.append(event);
  347. }
  348. void trySplice()
  349. {
  350. if (mutex.tryLock())
  351. {
  352. dataPendingRT.splice(data, true);
  353. mutex.unlock();
  354. }
  355. }
  356. void clear()
  357. {
  358. mutex.lock();
  359. data.clear();
  360. dataPendingRT.clear();
  361. mutex.unlock();
  362. }
  363. } postRtEvents;
  364. struct PostProc {
  365. float dryWet;
  366. float volume;
  367. float balanceLeft;
  368. float balanceRight;
  369. float panning;
  370. PostProc()
  371. : dryWet(1.0f),
  372. volume(1.0f),
  373. balanceLeft(-1.0f),
  374. balanceRight(1.0f),
  375. panning(0.0f) {}
  376. } postProc;
  377. struct OSC {
  378. CarlaOscData data;
  379. CarlaPluginThread thread;
  380. OSC(CarlaEngine* const engine, CarlaPlugin* const plugin, const CarlaPluginThread::Mode mode)
  381. : thread(engine, plugin, mode) {}
  382. } osc;
  383. CarlaPluginProtectedData(CarlaEngine* const engine_, CarlaPlugin* const plugin, const CarlaPluginThread::Mode mode)
  384. : engine(engine_),
  385. client(nullptr),
  386. gui(nullptr),
  387. active(false),
  388. activeBefore(false),
  389. lib(nullptr),
  390. ctrlInChannel(-1),
  391. extraHints(0x0),
  392. latency(0),
  393. latencyBuffers(nullptr),
  394. osc(engine_, plugin, mode) {}
  395. CarlaPluginProtectedData() = delete;
  396. CarlaPluginProtectedData(CarlaPluginProtectedData&) = delete;
  397. CarlaPluginProtectedData(const CarlaPluginProtectedData&) = delete;
  398. static CarlaEngine* getEngine(CarlaPlugin* const plugin)
  399. {
  400. return plugin->kData->engine;
  401. }
  402. static CarlaEngineAudioPort* getAudioInPort(CarlaPlugin* const plugin, uint32_t index)
  403. {
  404. return plugin->kData->audioIn.ports[index].port;
  405. }
  406. static CarlaEngineAudioPort* getAudioOutPort(CarlaPlugin* const plugin, uint32_t index)
  407. {
  408. return plugin->kData->audioOut.ports[index].port;
  409. }
  410. CARLA_LEAK_DETECTOR(CarlaPluginProtectedData)
  411. };
  412. // -----------------------------------------------------------------------
  413. CARLA_BACKEND_END_NAMESPACE
  414. #endif // __CARLA_PLUGIN_INTERNAL_HPP__
  415. // common includes
  416. //#include <cmath>
  417. //#include <vector>
  418. //#include <QtCore/QMutex>
  419. //#include <QtGui/QMainWindow>
  420. //#ifdef Q_WS_X11
  421. //# include <QtGui/QX11EmbedContainer>
  422. //typedef QX11EmbedContainer GuiContainer;
  423. //#else
  424. //# include <QtGui/QWidget>
  425. //typedef QWidget GuiContainer;
  426. //#endif
  427. #if 0
  428. /*!
  429. * \class CarlaPluginGUI
  430. *
  431. * \brief Carla Backend gui plugin class
  432. *
  433. * \see CarlaPlugin
  434. */
  435. class CarlaPluginGUI : public QMainWindow
  436. {
  437. public:
  438. /*!
  439. * \class Callback
  440. *
  441. * \brief Carla plugin GUI callback
  442. */
  443. class Callback
  444. {
  445. public:
  446. virtual ~Callback() {}
  447. virtual void guiClosedCallback() = 0;
  448. };
  449. // -------------------------------------------------------------------
  450. // Constructor and destructor
  451. /*!
  452. * TODO
  453. */
  454. CarlaPluginGUI(QWidget* const parent, Callback* const callback);
  455. /*!
  456. * TODO
  457. */
  458. ~CarlaPluginGUI();
  459. // -------------------------------------------------------------------
  460. // Get data
  461. /*!
  462. * TODO
  463. */
  464. GuiContainer* getContainer() const;
  465. /*!
  466. * TODO
  467. */
  468. WId getWinId() const;
  469. // -------------------------------------------------------------------
  470. // Set data
  471. /*!
  472. * TODO
  473. */
  474. void setNewSize(const int width, const int height);
  475. /*!
  476. * TODO
  477. */
  478. void setResizable(const bool resizable);
  479. /*!
  480. * TODO
  481. */
  482. void setTitle(const char* const title);
  483. /*!
  484. * TODO
  485. */
  486. void setVisible(const bool yesNo);
  487. // -------------------------------------------------------------------
  488. private:
  489. Callback* const m_callback;
  490. GuiContainer* m_container;
  491. QByteArray m_geometry;
  492. bool m_resizable;
  493. void hideEvent(QHideEvent* const event);
  494. void closeEvent(QCloseEvent* const event);
  495. };
  496. #endif