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.

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