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.

860 lines
21KB

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