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.

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