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.

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