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.

825 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. // -----------------------------------------------------------------------
  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. bool active;
  396. bool needsReset;
  397. void* lib;
  398. void* uiLib;
  399. // misc
  400. int8_t ctrlChannel;
  401. unsigned int extraHints;
  402. CarlaString idStr;
  403. // latency
  404. uint32_t latency;
  405. float** latencyBuffers;
  406. // data
  407. PluginAudioData audioIn;
  408. PluginAudioData audioOut;
  409. PluginEventData event;
  410. PluginParameterData param;
  411. PluginProgramData prog;
  412. PluginMidiProgramData midiprog;
  413. List<CustomData> custom;
  414. SaveState saveState;
  415. CarlaMutex masterMutex; // global master lock
  416. CarlaMutex singleMutex; // small lock used only in processSingle()
  417. struct ExternalNotes {
  418. CarlaMutex mutex;
  419. RtList<ExternalMidiNote>::Pool dataPool;
  420. RtList<ExternalMidiNote> data;
  421. ExternalNotes()
  422. : dataPool(32, 152),
  423. data(dataPool) {}
  424. ~ExternalNotes()
  425. {
  426. mutex.lock();
  427. data.clear();
  428. mutex.unlock();
  429. }
  430. void append(const ExternalMidiNote& note)
  431. {
  432. mutex.lock();
  433. data.append_sleepy(note);
  434. mutex.unlock();
  435. }
  436. CARLA_DECLARE_NON_COPY_STRUCT(ExternalNotes)
  437. } extNotes;
  438. struct PostRtEvents {
  439. CarlaMutex mutex;
  440. RtList<PluginPostRtEvent>::Pool dataPool;
  441. RtList<PluginPostRtEvent> data;
  442. RtList<PluginPostRtEvent> dataPendingRT;
  443. PostRtEvents()
  444. : dataPool(128, 128),
  445. data(dataPool),
  446. dataPendingRT(dataPool) {}
  447. ~PostRtEvents()
  448. {
  449. clear();
  450. }
  451. void appendRT(const PluginPostRtEvent& event)
  452. {
  453. dataPendingRT.append(event);
  454. }
  455. void trySplice()
  456. {
  457. if (mutex.tryLock())
  458. {
  459. dataPendingRT.spliceAppend(data);
  460. mutex.unlock();
  461. }
  462. }
  463. void clear()
  464. {
  465. mutex.lock();
  466. data.clear();
  467. dataPendingRT.clear();
  468. mutex.unlock();
  469. }
  470. CARLA_DECLARE_NON_COPY_STRUCT(PostRtEvents)
  471. } postRtEvents;
  472. #ifndef BUILD_BRIDGE
  473. struct PostProc {
  474. float dryWet;
  475. float volume;
  476. float balanceLeft;
  477. float balanceRight;
  478. float panning;
  479. PostProc() noexcept
  480. : dryWet(1.0f),
  481. volume(1.0f),
  482. balanceLeft(-1.0f),
  483. balanceRight(1.0f),
  484. panning(0.0f) {}
  485. CARLA_DECLARE_NON_COPY_STRUCT(PostProc)
  486. } postProc;
  487. #endif
  488. struct OSC {
  489. CarlaOscData data;
  490. CarlaPluginThread thread;
  491. OSC(CarlaEngine* const engine, CarlaPlugin* const plugin)
  492. : thread(engine, plugin) {}
  493. #ifdef CARLA_PROPER_CPP11_SUPPORT
  494. OSC() = delete;
  495. CARLA_DECLARE_NON_COPY_STRUCT(OSC)
  496. #endif
  497. } osc;
  498. CarlaPluginProtectedData(CarlaEngine* const eng, CarlaPlugin* const plug)
  499. : engine(eng),
  500. client(nullptr),
  501. active(false),
  502. needsReset(false),
  503. lib(nullptr),
  504. uiLib(nullptr),
  505. ctrlChannel(0),
  506. extraHints(0x0),
  507. latency(0),
  508. latencyBuffers(nullptr),
  509. osc(eng, plug) {}
  510. #ifdef CARLA_PROPER_CPP11_SUPPORT
  511. CarlaPluginProtectedData() = delete;
  512. CARLA_DECLARE_NON_COPY_STRUCT(CarlaPluginProtectedData)
  513. #endif
  514. ~CarlaPluginProtectedData()
  515. {
  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. // mutex MUST have been locked before
  530. const bool lockMaster(masterMutex.tryLock());
  531. const bool lockSingle(singleMutex.tryLock());
  532. CARLA_ASSERT(! lockMaster);
  533. CARLA_ASSERT(! lockSingle);
  534. }
  535. if (client != nullptr)
  536. {
  537. if (client->isActive())
  538. {
  539. // must not happen
  540. carla_safe_assert("client->isActive()", __FILE__, __LINE__);
  541. client->deactivate();
  542. }
  543. clearBuffers();
  544. delete client;
  545. client = nullptr;
  546. }
  547. for (List<CustomData>::Itenerator it = custom.begin(); it.valid(); it.next())
  548. {
  549. CustomData& cData(*it);
  550. if (cData.type != nullptr)
  551. {
  552. delete[] cData.type;
  553. cData.type = nullptr;
  554. }
  555. else
  556. carla_safe_assert("cData.type != nullptr", __FILE__, __LINE__);
  557. if (cData.key != nullptr)
  558. {
  559. delete[] cData.key;
  560. cData.key = nullptr;
  561. }
  562. else
  563. carla_safe_assert("cData.key != nullptr", __FILE__, __LINE__);
  564. if (cData.value != nullptr)
  565. {
  566. delete[] cData.value;
  567. cData.value = nullptr;
  568. }
  569. else
  570. carla_safe_assert("cData.value != nullptr", __FILE__, __LINE__);
  571. }
  572. prog.clear();
  573. midiprog.clear();
  574. custom.clear();
  575. // MUST have been locked before
  576. masterMutex.unlock();
  577. singleMutex.unlock();
  578. if (lib != nullptr)
  579. libClose();
  580. }
  581. // -------------------------------------------------------------------
  582. // Buffer functions
  583. void clearBuffers()
  584. {
  585. if (latencyBuffers != nullptr)
  586. {
  587. CARLA_ASSERT(audioIn.count > 0);
  588. for (uint32_t i=0; i < audioIn.count; ++i)
  589. {
  590. CARLA_SAFE_ASSERT_CONTINUE(latencyBuffers[i] != nullptr);
  591. delete[] latencyBuffers[i];
  592. latencyBuffers[i] = nullptr;
  593. }
  594. delete[] latencyBuffers;
  595. latencyBuffers = nullptr;
  596. latency = 0;
  597. }
  598. else
  599. {
  600. CARLA_ASSERT(latency == 0);
  601. }
  602. audioIn.clear();
  603. audioOut.clear();
  604. param.clear();
  605. event.clear();
  606. }
  607. void recreateLatencyBuffers()
  608. {
  609. if (latencyBuffers != nullptr)
  610. {
  611. CARLA_ASSERT(audioIn.count > 0);
  612. for (uint32_t i=0; i < audioIn.count; ++i)
  613. {
  614. CARLA_SAFE_ASSERT_CONTINUE(latencyBuffers[i] != nullptr);
  615. delete[] latencyBuffers[i];
  616. latencyBuffers[i] = nullptr;
  617. }
  618. delete[] latencyBuffers;
  619. latencyBuffers = nullptr;
  620. }
  621. if (audioIn.count > 0 && latency > 0)
  622. {
  623. latencyBuffers = new float*[audioIn.count];
  624. for (uint32_t i=0; i < audioIn.count; ++i)
  625. {
  626. latencyBuffers[i] = new float[latency];
  627. #ifdef USE_JUCE
  628. FloatVectorOperations::clear(latencyBuffers[i], latency);
  629. #else
  630. carla_zeroFloat(latencyBuffers[i], latency);
  631. #endif
  632. }
  633. }
  634. }
  635. // -------------------------------------------------------------------
  636. // Post-poned events
  637. void postponeRtEvent(const PluginPostRtEventType type, const int32_t value1, const int32_t value2, const float value3)
  638. {
  639. CARLA_SAFE_ASSERT_RETURN(type != kPluginPostRtEventNull,);
  640. PluginPostRtEvent event;
  641. event.type = type;
  642. event.value1 = value1;
  643. event.value2 = value2;
  644. event.value3 = value3;
  645. postRtEvents.appendRT(event);
  646. }
  647. // -------------------------------------------------------------------
  648. // Library functions, see CarlaPlugin.cpp
  649. bool libOpen(const char* const filename);
  650. bool libClose();
  651. void* libSymbol(const char* const symbol);
  652. bool uiLibOpen(const char* const filename);
  653. bool uiLibClose();
  654. void* uiLibSymbol(const char* const symbol);
  655. // -------------------------------------------------------------------
  656. // Settings functions, see CarlaPlugin.cpp
  657. void saveSetting(const unsigned int option, const bool yesNo);
  658. unsigned int loadSettings(const unsigned int options, const unsigned int availOptions);
  659. };
  660. CARLA_BACKEND_END_NAMESPACE
  661. #endif // CARLA_PLUGIN_INTERNAL_HPP_INCLUDED