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.

842 lines
20KB

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