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.

812 lines
19KB

  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 doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_PLUGIN_INTERNAL_HPP_INCLUDED
  18. #define CARLA_PLUGIN_INTERNAL_HPP_INCLUDED
  19. #include "CarlaPlugin.hpp"
  20. #include "CarlaPluginThread.hpp"
  21. #include "CarlaEngine.hpp"
  22. #include "CarlaBackendUtils.hpp"
  23. #include "CarlaOscUtils.hpp"
  24. #include "CarlaMutex.hpp"
  25. #include "CarlaMIDI.h"
  26. #include "RtList.hpp"
  27. #define CARLA_PROCESS_CONTINUE_CHECK if (! fEnabled) { kData->engine->callback(CALLBACK_DEBUG, fId, 0, 0, 0.0f, "Processing while plugin is disabled!!"); return; }
  28. CARLA_BACKEND_START_NAMESPACE
  29. // -----------------------------------------------------------------------
  30. const unsigned short MAX_MIDI_EVENTS = 512;
  31. const unsigned int PLUGIN_HINT_HAS_MIDI_IN = 0x1;
  32. const unsigned int PLUGIN_HINT_HAS_MIDI_OUT = 0x2;
  33. const unsigned int PLUGIN_HINT_CAN_RUN_RACK = 0x4;
  34. // -----------------------------------------------------------------------
  35. /*!
  36. * Post-RT event type.\n
  37. * These are events postponned from within the process function,
  38. *
  39. * During process, we cannot lock, allocate memory or do UI stuff,\n
  40. * so events have to be postponned to be executed later, on a separate thread.
  41. */
  42. enum PluginPostRtEventType {
  43. kPluginPostRtEventNull,
  44. kPluginPostRtEventDebug,
  45. kPluginPostRtEventParameterChange, // param, SP*, value (SP: if 1, don't report change to Callback and OSC)
  46. kPluginPostRtEventProgramChange, // index
  47. kPluginPostRtEventMidiProgramChange, // index
  48. kPluginPostRtEventNoteOn, // channel, note, velo
  49. kPluginPostRtEventNoteOff // channel, note
  50. };
  51. /*!
  52. * A Post-RT event.
  53. * \see PluginPostRtEventType
  54. */
  55. struct PluginPostRtEvent {
  56. PluginPostRtEventType type;
  57. int32_t value1;
  58. int32_t value2;
  59. float value3;
  60. PluginPostRtEvent() noexcept
  61. : type(kPluginPostRtEventNull),
  62. value1(-1),
  63. value2(-1),
  64. value3(0.0f) {}
  65. };
  66. // -----------------------------------------------------------------------
  67. struct PluginAudioPort {
  68. uint32_t rindex;
  69. CarlaEngineAudioPort* port;
  70. PluginAudioPort()
  71. : rindex(0),
  72. port(nullptr) {}
  73. ~PluginAudioPort()
  74. {
  75. CARLA_ASSERT(port == nullptr);
  76. }
  77. CARLA_DECLARE_NON_COPY_STRUCT(PluginAudioPort)
  78. };
  79. struct PluginAudioData {
  80. uint32_t count;
  81. PluginAudioPort* ports;
  82. PluginAudioData()
  83. : count(0),
  84. ports(nullptr) {}
  85. ~PluginAudioData()
  86. {
  87. CARLA_ASSERT_INT(count == 0, count);
  88. CARLA_ASSERT(ports == nullptr);
  89. }
  90. void createNew(const uint32_t newCount)
  91. {
  92. CARLA_ASSERT_INT(count == 0, count);
  93. CARLA_ASSERT(ports == nullptr);
  94. CARLA_ASSERT_INT(newCount > 0, newCount);
  95. if (ports != nullptr || newCount == 0)
  96. return;
  97. ports = new PluginAudioPort[newCount];
  98. count = newCount;
  99. }
  100. void clear()
  101. {
  102. if (ports != nullptr)
  103. {
  104. for (uint32_t i=0; i < count; ++i)
  105. {
  106. if (ports[i].port != nullptr)
  107. {
  108. delete ports[i].port;
  109. ports[i].port = nullptr;
  110. }
  111. }
  112. delete[] ports;
  113. ports = nullptr;
  114. }
  115. count = 0;
  116. }
  117. void initBuffers(CarlaEngine* const engine)
  118. {
  119. for (uint32_t i=0; i < count; ++i)
  120. {
  121. if (ports[i].port != nullptr)
  122. ports[i].port->initBuffer(engine);
  123. }
  124. }
  125. CARLA_DECLARE_NON_COPY_STRUCT(PluginAudioData)
  126. };
  127. // -----------------------------------------------------------------------
  128. struct PluginCVPort {
  129. uint32_t rindex;
  130. uint32_t param;
  131. CarlaEngineCVPort* port;
  132. PluginCVPort()
  133. : rindex(0),
  134. param(0),
  135. port(nullptr) {}
  136. ~PluginCVPort()
  137. {
  138. CARLA_ASSERT(port == nullptr);
  139. }
  140. CARLA_DECLARE_NON_COPY_STRUCT(PluginCVPort)
  141. };
  142. struct PluginCVData {
  143. uint32_t count;
  144. PluginCVPort* ports;
  145. PluginCVData()
  146. : count(0),
  147. ports(nullptr) {}
  148. ~PluginCVData()
  149. {
  150. CARLA_ASSERT_INT(count == 0, count);
  151. CARLA_ASSERT(ports == nullptr);
  152. }
  153. void createNew(const uint32_t newCount)
  154. {
  155. CARLA_ASSERT_INT(count == 0, count);
  156. CARLA_ASSERT(ports == nullptr);
  157. CARLA_ASSERT_INT(newCount > 0, newCount);
  158. if (ports != nullptr || newCount == 0)
  159. return;
  160. ports = new PluginCVPort[newCount];
  161. count = newCount;
  162. }
  163. void clear()
  164. {
  165. if (ports != nullptr)
  166. {
  167. for (uint32_t i=0; i < count; ++i)
  168. {
  169. if (ports[i].port != nullptr)
  170. {
  171. delete ports[i].port;
  172. ports[i].port = nullptr;
  173. }
  174. }
  175. delete[] ports;
  176. ports = nullptr;
  177. }
  178. count = 0;
  179. }
  180. void initBuffers(CarlaEngine* const engine)
  181. {
  182. for (uint32_t i=0; i < count; ++i)
  183. {
  184. if (ports[i].port != nullptr)
  185. ports[i].port->initBuffer(engine);
  186. }
  187. }
  188. CARLA_DECLARE_NON_COPY_STRUCT(PluginCVData)
  189. };
  190. // -----------------------------------------------------------------------
  191. struct PluginEventData {
  192. CarlaEngineEventPort* portIn;
  193. CarlaEngineEventPort* portOut;
  194. PluginEventData()
  195. : portIn(nullptr),
  196. portOut(nullptr) {}
  197. ~PluginEventData()
  198. {
  199. CARLA_ASSERT(portIn == nullptr);
  200. CARLA_ASSERT(portOut == nullptr);
  201. }
  202. void clear()
  203. {
  204. if (portIn != nullptr)
  205. {
  206. delete portIn;
  207. portIn = nullptr;
  208. }
  209. if (portOut != nullptr)
  210. {
  211. delete portOut;
  212. portOut = nullptr;
  213. }
  214. }
  215. void initBuffers(CarlaEngine* const engine)
  216. {
  217. if (portIn != nullptr)
  218. portIn->initBuffer(engine);
  219. if (portOut != nullptr)
  220. portOut->initBuffer(engine);
  221. }
  222. CARLA_DECLARE_NON_COPY_STRUCT(PluginEventData)
  223. };
  224. // -----------------------------------------------------------------------
  225. struct PluginParameterData {
  226. uint32_t count;
  227. ParameterData* data;
  228. ParameterRanges* ranges;
  229. PluginParameterData()
  230. : count(0),
  231. data(nullptr),
  232. ranges(nullptr) {}
  233. ~PluginParameterData()
  234. {
  235. CARLA_ASSERT_INT(count == 0, count);
  236. CARLA_ASSERT(data == nullptr);
  237. CARLA_ASSERT(ranges == nullptr);
  238. }
  239. void createNew(const uint32_t newCount)
  240. {
  241. CARLA_ASSERT_INT(count == 0, count);
  242. CARLA_ASSERT(data == nullptr);
  243. CARLA_ASSERT(ranges == nullptr);
  244. CARLA_ASSERT_INT(newCount > 0, newCount);
  245. if (data != nullptr || ranges != nullptr || newCount == 0)
  246. return;
  247. data = new ParameterData[newCount];
  248. ranges = new ParameterRanges[newCount];
  249. count = newCount;
  250. }
  251. void clear()
  252. {
  253. if (data != nullptr)
  254. {
  255. delete[] data;
  256. data = nullptr;
  257. }
  258. if (ranges != nullptr)
  259. {
  260. delete[] ranges;
  261. ranges = nullptr;
  262. }
  263. count = 0;
  264. }
  265. float getFixedValue(const uint32_t parameterId, const float& value) const
  266. {
  267. CARLA_SAFE_ASSERT_RETURN(parameterId < count, 0.0f);
  268. return ranges[parameterId].getFixedValue(value);
  269. }
  270. CARLA_DECLARE_NON_COPY_STRUCT(PluginParameterData)
  271. };
  272. // -----------------------------------------------------------------------
  273. typedef const char* ProgramName;
  274. struct PluginProgramData {
  275. uint32_t count;
  276. int32_t current;
  277. ProgramName* names;
  278. PluginProgramData()
  279. : count(0),
  280. current(-1),
  281. names(nullptr) {}
  282. ~PluginProgramData()
  283. {
  284. CARLA_ASSERT_INT(count == 0, count);
  285. CARLA_ASSERT_INT(current == -1, current);
  286. CARLA_ASSERT(names == nullptr);
  287. }
  288. void createNew(const uint32_t newCount)
  289. {
  290. CARLA_ASSERT_INT(count == 0, count);
  291. CARLA_ASSERT_INT(current == -1, current);
  292. CARLA_ASSERT(names == nullptr);
  293. CARLA_ASSERT_INT(newCount > 0, newCount);
  294. if (names != nullptr || newCount == 0)
  295. return;
  296. names = new ProgramName[newCount];
  297. count = newCount;
  298. for (uint32_t i=0; i < newCount; ++i)
  299. names[i] = nullptr;
  300. }
  301. void clear()
  302. {
  303. if (names != nullptr)
  304. {
  305. for (uint32_t i=0; i < count; ++i)
  306. {
  307. if (names[i] != nullptr)
  308. {
  309. delete[] names[i];
  310. names[i] = nullptr;
  311. }
  312. }
  313. delete[] names;
  314. names = nullptr;
  315. }
  316. count = 0;
  317. current = -1;
  318. }
  319. CARLA_DECLARE_NON_COPY_STRUCT(PluginProgramData)
  320. };
  321. // -----------------------------------------------------------------------
  322. struct PluginMidiProgramData {
  323. uint32_t count;
  324. int32_t current;
  325. MidiProgramData* data;
  326. PluginMidiProgramData()
  327. : count(0),
  328. current(-1),
  329. data(nullptr) {}
  330. ~PluginMidiProgramData()
  331. {
  332. CARLA_ASSERT_INT(count == 0, count);
  333. CARLA_ASSERT_INT(current == -1, current);
  334. CARLA_ASSERT(data == nullptr);
  335. }
  336. void createNew(const uint32_t newCount)
  337. {
  338. CARLA_ASSERT_INT(count == 0, count);
  339. CARLA_ASSERT_INT(current == -1, current);
  340. CARLA_ASSERT(data == nullptr);
  341. CARLA_ASSERT_INT(newCount > 0, newCount);
  342. if (data != nullptr || newCount == 0)
  343. return;
  344. data = new MidiProgramData[newCount];
  345. count = newCount;
  346. }
  347. void clear()
  348. {
  349. if (data != nullptr)
  350. {
  351. for (uint32_t i=0; i < count; ++i)
  352. {
  353. if (data[i].name != nullptr)
  354. {
  355. delete[] data[i].name;
  356. data[i].name = nullptr;
  357. }
  358. }
  359. delete[] data;
  360. data = nullptr;
  361. }
  362. count = 0;
  363. current = -1;
  364. }
  365. const MidiProgramData& getCurrent() const
  366. {
  367. CARLA_ASSERT_INT2(current >= 0 && current < static_cast<int32_t>(count), current, count);
  368. return data[current];
  369. }
  370. CARLA_DECLARE_NON_COPY_STRUCT(PluginMidiProgramData)
  371. };
  372. // -----------------------------------------------------------------------
  373. struct ExternalMidiNote {
  374. int8_t channel; // invalid == -1
  375. uint8_t note;
  376. uint8_t velo; // note-off if 0
  377. ExternalMidiNote()
  378. : channel(-1),
  379. note(0),
  380. velo(0) {}
  381. };
  382. // -----------------------------------------------------------------------
  383. class CarlaPluginGui;
  384. struct CarlaPluginProtectedData {
  385. CarlaEngine* const engine;
  386. CarlaEngineClient* client;
  387. CarlaPluginGui* gui;
  388. QByteArray guiGeometry;
  389. bool active;
  390. bool needsReset;
  391. void* lib;
  392. void* uiLib;
  393. // misc
  394. int8_t ctrlChannel;
  395. unsigned int extraHints;
  396. CarlaString idStr;
  397. // latency
  398. uint32_t latency;
  399. float** latencyBuffers;
  400. // data
  401. PluginAudioData audioIn;
  402. PluginAudioData audioOut;
  403. PluginEventData event;
  404. PluginParameterData param;
  405. PluginProgramData prog;
  406. PluginMidiProgramData midiprog;
  407. NonRtList<CustomData> custom;
  408. CarlaMutex masterMutex; // global master lock
  409. CarlaMutex singleMutex; // small lock used only in processSingle()
  410. struct ExternalNotes {
  411. CarlaMutex mutex;
  412. RtList<ExternalMidiNote>::Pool dataPool;
  413. RtList<ExternalMidiNote> data;
  414. ExternalNotes()
  415. : dataPool(32, 152),
  416. data(dataPool) {}
  417. ~ExternalNotes()
  418. {
  419. mutex.lock();
  420. data.clear();
  421. mutex.unlock();
  422. }
  423. void append(const ExternalMidiNote& note)
  424. {
  425. mutex.lock();
  426. data.append_sleepy(note);
  427. mutex.unlock();
  428. }
  429. CARLA_DECLARE_NON_COPY_STRUCT(ExternalNotes)
  430. } extNotes;
  431. struct PostRtEvents {
  432. CarlaMutex mutex;
  433. RtList<PluginPostRtEvent>::Pool dataPool;
  434. RtList<PluginPostRtEvent> data;
  435. RtList<PluginPostRtEvent> dataPendingRT;
  436. PostRtEvents()
  437. : dataPool(128, 128),
  438. data(dataPool),
  439. dataPendingRT(dataPool) {}
  440. ~PostRtEvents()
  441. {
  442. clear();
  443. }
  444. void appendRT(const PluginPostRtEvent& event)
  445. {
  446. dataPendingRT.append(event);
  447. }
  448. void trySplice()
  449. {
  450. if (mutex.tryLock())
  451. {
  452. dataPendingRT.spliceAppend(data);
  453. mutex.unlock();
  454. }
  455. }
  456. void clear()
  457. {
  458. mutex.lock();
  459. data.clear();
  460. dataPendingRT.clear();
  461. mutex.unlock();
  462. }
  463. CARLA_DECLARE_NON_COPY_STRUCT(PostRtEvents)
  464. } postRtEvents;
  465. #ifndef BUILD_BRIDGE
  466. struct PostProc {
  467. float dryWet;
  468. float volume;
  469. float balanceLeft;
  470. float balanceRight;
  471. float panning;
  472. PostProc()
  473. : dryWet(1.0f),
  474. volume(1.0f),
  475. balanceLeft(-1.0f),
  476. balanceRight(1.0f),
  477. panning(0.0f) {}
  478. CARLA_DECLARE_NON_COPY_STRUCT(PostProc)
  479. } postProc;
  480. #endif
  481. struct OSC {
  482. CarlaOscData data;
  483. CarlaPluginThread thread;
  484. OSC(CarlaEngine* const engine, CarlaPlugin* const plugin)
  485. : thread(engine, plugin) {}
  486. #ifdef CARLA_PROPER_CPP11_SUPPORT
  487. OSC() = delete;
  488. OSC(OSC&) = delete;
  489. OSC(const OSC&) = delete;
  490. #endif
  491. CARLA_LEAK_DETECTOR(OSC)
  492. } osc;
  493. CarlaPluginProtectedData(CarlaEngine* const engine_, CarlaPlugin* const plugin)
  494. : engine(engine_),
  495. client(nullptr),
  496. gui(nullptr),
  497. active(false),
  498. needsReset(false),
  499. lib(nullptr),
  500. uiLib(nullptr),
  501. ctrlChannel(0),
  502. extraHints(0x0),
  503. latency(0),
  504. latencyBuffers(nullptr),
  505. osc(engine, plugin) {}
  506. #ifdef CARLA_PROPER_CPP11_SUPPORT
  507. CarlaPluginProtectedData() = delete;
  508. CarlaPluginProtectedData(CarlaPluginProtectedData&) = delete;
  509. CarlaPluginProtectedData(const CarlaPluginProtectedData&) = delete;
  510. #endif
  511. CARLA_LEAK_DETECTOR(CarlaPluginProtectedData)
  512. ~CarlaPluginProtectedData()
  513. {
  514. CARLA_ASSERT(gui == nullptr);
  515. CARLA_ASSERT(client == nullptr);
  516. CARLA_ASSERT(! active);
  517. CARLA_ASSERT(lib == nullptr);
  518. CARLA_ASSERT(uiLib == nullptr);
  519. CARLA_ASSERT(latency == 0);
  520. CARLA_ASSERT(latencyBuffers == nullptr);
  521. CARLA_SAFE_ASSERT(! needsReset);
  522. }
  523. // -------------------------------------------------------------------
  524. // Cleanup
  525. void cleanup()
  526. {
  527. {
  528. const bool lockMaster(masterMutex.tryLock());
  529. const bool lockSingle(singleMutex.tryLock());
  530. CARLA_ASSERT(! lockMaster);
  531. CARLA_ASSERT(! lockSingle);
  532. }
  533. if (client != nullptr)
  534. {
  535. CARLA_ASSERT(! client->isActive());
  536. if (client->isActive())
  537. client->deactivate();
  538. clearBuffers();
  539. delete client;
  540. client = nullptr;
  541. }
  542. for (NonRtList<CustomData>::Itenerator it = custom.begin(); it.valid(); it.next())
  543. {
  544. CustomData& cData(*it);
  545. CARLA_ASSERT(cData.type != nullptr);
  546. CARLA_ASSERT(cData.key != nullptr);
  547. CARLA_ASSERT(cData.value != nullptr);
  548. if (cData.type != nullptr)
  549. {
  550. delete[] cData.type;
  551. cData.type = nullptr;
  552. }
  553. if (cData.key != nullptr)
  554. {
  555. delete[] cData.key;
  556. cData.key = nullptr;
  557. }
  558. if (cData.value != nullptr)
  559. {
  560. delete[] cData.value;
  561. cData.value = nullptr;
  562. }
  563. }
  564. prog.clear();
  565. midiprog.clear();
  566. custom.clear();
  567. // MUST have been locked before
  568. masterMutex.unlock();
  569. singleMutex.unlock();
  570. if (lib != nullptr)
  571. libClose();
  572. }
  573. // -------------------------------------------------------------------
  574. // Buffer functions
  575. void clearBuffers()
  576. {
  577. if (latencyBuffers != nullptr)
  578. {
  579. for (uint32_t i=0; i < audioIn.count; ++i)
  580. {
  581. CARLA_ASSERT(latencyBuffers[i] != nullptr);
  582. if (latencyBuffers[i] != nullptr)
  583. {
  584. delete[] latencyBuffers[i];
  585. latencyBuffers[i] = nullptr;
  586. }
  587. }
  588. delete[] latencyBuffers;
  589. latencyBuffers = nullptr;
  590. latency = 0;
  591. }
  592. audioIn.clear();
  593. audioOut.clear();
  594. param.clear();
  595. event.clear();
  596. }
  597. void recreateLatencyBuffers()
  598. {
  599. if (latencyBuffers != nullptr)
  600. {
  601. for (uint32_t i=0; i < audioIn.count; ++i)
  602. {
  603. CARLA_ASSERT(latencyBuffers[i] != nullptr);
  604. if (latencyBuffers[i] != nullptr)
  605. {
  606. delete[] latencyBuffers[i];
  607. latencyBuffers[i] = nullptr;
  608. }
  609. }
  610. delete[] latencyBuffers;
  611. latencyBuffers = nullptr;
  612. }
  613. if (audioIn.count > 0 && latency > 0)
  614. {
  615. latencyBuffers = new float*[audioIn.count];
  616. for (uint32_t i=0; i < audioIn.count; ++i)
  617. {
  618. latencyBuffers[i] = new float[latency];
  619. carla_zeroFloat(latencyBuffers[i], latency);
  620. }
  621. }
  622. }
  623. // -------------------------------------------------------------------
  624. // Post-poned events
  625. void postponeRtEvent(const PluginPostRtEventType type, const int32_t value1, const int32_t value2, const float value3)
  626. {
  627. PluginPostRtEvent event;
  628. event.type = type;
  629. event.value1 = value1;
  630. event.value2 = value2;
  631. event.value3 = value3;
  632. postRtEvents.appendRT(event);
  633. }
  634. // -------------------------------------------------------------------
  635. // Library functions, see CarlaPlugin.cpp
  636. bool libOpen(const char* const filename);
  637. bool libClose();
  638. void* libSymbol(const char* const symbol);
  639. bool uiLibOpen(const char* const filename);
  640. bool uiLibClose();
  641. void* uiLibSymbol(const char* const symbol);
  642. const char* libError(const char* const filename);
  643. // -------------------------------------------------------------------
  644. // Settings functions, see CarlaPlugin.cpp
  645. void saveSetting(const unsigned int option, const bool yesNo);
  646. unsigned int loadSettings(const unsigned int options, const unsigned int availOptions);
  647. };
  648. CARLA_BACKEND_END_NAMESPACE
  649. #endif // CARLA_PLUGIN_INTERNAL_HPP_INCLUDED