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.

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