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.

738 lines
18KB

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