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.

818 lines
19KB

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