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.

725 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. CARLA_DECLARE_NON_COPY_STRUCT(PluginPostRtEvent)
  289. };
  290. // -----------------------------------------------------------------------
  291. struct ExternalMidiNote {
  292. int8_t channel; // invalid == -1
  293. uint8_t note;
  294. uint8_t velo; // note-off if 0
  295. ExternalMidiNote()
  296. : channel(-1),
  297. note(0),
  298. velo(0) {}
  299. CARLA_DECLARE_NON_COPY_STRUCT(ExternalMidiNote)
  300. };
  301. // -----------------------------------------------------------------------
  302. class CarlaPluginGui;
  303. struct CarlaPluginProtectedData {
  304. CarlaEngine* const engine;
  305. CarlaEngineClient* client;
  306. CarlaPluginGui* gui;
  307. QByteArray guiGeometry;
  308. bool active;
  309. bool needsReset;
  310. void* lib;
  311. void* uiLib;
  312. // misc
  313. int8_t ctrlChannel;
  314. unsigned int extraHints;
  315. CarlaString idStr;
  316. // latency
  317. uint32_t latency;
  318. float** latencyBuffers;
  319. // data
  320. PluginAudioData audioIn;
  321. PluginAudioData audioOut;
  322. PluginEventData event;
  323. PluginParameterData param;
  324. PluginProgramData prog;
  325. PluginMidiProgramData midiprog;
  326. NonRtList<CustomData> custom;
  327. CarlaMutex masterMutex; // global master lock
  328. CarlaMutex singleMutex; // small lock used only in processSingle()
  329. struct ExternalNotes {
  330. CarlaMutex mutex;
  331. RtList<ExternalMidiNote>::Pool dataPool;
  332. RtList<ExternalMidiNote> data;
  333. ExternalNotes()
  334. : dataPool(32, 152),
  335. data(dataPool) {}
  336. ~ExternalNotes()
  337. {
  338. mutex.lock();
  339. data.clear();
  340. mutex.unlock();
  341. }
  342. void append(const ExternalMidiNote& note)
  343. {
  344. mutex.lock();
  345. data.append_sleepy(note);
  346. mutex.unlock();
  347. }
  348. CARLA_DECLARE_NON_COPY_STRUCT(ExternalNotes)
  349. } extNotes;
  350. struct PostRtEvents {
  351. CarlaMutex mutex;
  352. RtList<PluginPostRtEvent>::Pool dataPool;
  353. RtList<PluginPostRtEvent> data;
  354. RtList<PluginPostRtEvent> dataPendingRT;
  355. PostRtEvents()
  356. : dataPool(128, 128),
  357. data(dataPool),
  358. dataPendingRT(dataPool) {}
  359. ~PostRtEvents()
  360. {
  361. clear();
  362. }
  363. void appendRT(const PluginPostRtEvent& event)
  364. {
  365. dataPendingRT.append(event);
  366. }
  367. void trySplice()
  368. {
  369. if (mutex.tryLock())
  370. {
  371. dataPendingRT.spliceAppend(data, true);
  372. mutex.unlock();
  373. }
  374. }
  375. void clear()
  376. {
  377. mutex.lock();
  378. data.clear();
  379. dataPendingRT.clear();
  380. mutex.unlock();
  381. }
  382. CARLA_DECLARE_NON_COPY_STRUCT(PostRtEvents)
  383. } postRtEvents;
  384. #ifndef BUILD_BRIDGE
  385. struct PostProc {
  386. float dryWet;
  387. float volume;
  388. float balanceLeft;
  389. float balanceRight;
  390. float panning;
  391. PostProc()
  392. : dryWet(1.0f),
  393. volume(1.0f),
  394. balanceLeft(-1.0f),
  395. balanceRight(1.0f),
  396. panning(0.0f) {}
  397. CARLA_DECLARE_NON_COPY_STRUCT(PostProc)
  398. } postProc;
  399. #endif
  400. struct OSC {
  401. CarlaOscData data;
  402. CarlaPluginThread thread;
  403. OSC(CarlaEngine* const engine, CarlaPlugin* const plugin)
  404. : thread(engine, plugin) {}
  405. #ifdef CARLA_PROPER_CPP11_SUPPORT
  406. OSC() = delete;
  407. OSC(OSC&) = delete;
  408. OSC(const OSC&) = delete;
  409. #endif
  410. } osc;
  411. CarlaPluginProtectedData(CarlaEngine* const engine_, CarlaPlugin* const plugin)
  412. : engine(engine_),
  413. client(nullptr),
  414. gui(nullptr),
  415. active(false),
  416. needsReset(false),
  417. lib(nullptr),
  418. uiLib(nullptr),
  419. ctrlChannel(0),
  420. extraHints(0x0),
  421. latency(0),
  422. latencyBuffers(nullptr),
  423. osc(engine, plugin) {}
  424. #ifdef CARLA_PROPER_CPP11_SUPPORT
  425. CarlaPluginProtectedData() = delete;
  426. CarlaPluginProtectedData(CarlaPluginProtectedData&) = delete;
  427. CarlaPluginProtectedData(const CarlaPluginProtectedData&) = delete;
  428. #endif
  429. CARLA_LEAK_DETECTOR(CarlaPluginProtectedData)
  430. ~CarlaPluginProtectedData()
  431. {
  432. CARLA_ASSERT(gui == nullptr);
  433. CARLA_ASSERT(client == nullptr);
  434. CARLA_ASSERT(! active);
  435. CARLA_ASSERT(lib == nullptr);
  436. CARLA_ASSERT(uiLib == nullptr);
  437. CARLA_ASSERT(latency == 0);
  438. CARLA_ASSERT(latencyBuffers == nullptr);
  439. CARLA_SAFE_ASSERT(! needsReset);
  440. }
  441. // -------------------------------------------------------------------
  442. // Cleanup
  443. void cleanup()
  444. {
  445. {
  446. const bool lockMaster(masterMutex.tryLock());
  447. const bool lockSingle(singleMutex.tryLock());
  448. CARLA_ASSERT(! lockMaster);
  449. CARLA_ASSERT(! lockSingle);
  450. }
  451. if (client != nullptr)
  452. {
  453. CARLA_ASSERT(! client->isActive());
  454. if (client->isActive())
  455. client->deactivate();
  456. clearBuffers();
  457. delete client;
  458. client = nullptr;
  459. }
  460. for (NonRtList<CustomData>::Itenerator it = custom.begin(); it.valid(); it.next())
  461. {
  462. CustomData& cData(*it);
  463. CARLA_ASSERT(cData.type != nullptr);
  464. CARLA_ASSERT(cData.key != nullptr);
  465. CARLA_ASSERT(cData.value != nullptr);
  466. if (cData.type != nullptr)
  467. {
  468. delete[] cData.type;
  469. cData.type = nullptr;
  470. }
  471. if (cData.key != nullptr)
  472. {
  473. delete[] cData.key;
  474. cData.key = nullptr;
  475. }
  476. if (cData.value != nullptr)
  477. {
  478. delete[] cData.value;
  479. cData.value = nullptr;
  480. }
  481. }
  482. prog.clear();
  483. midiprog.clear();
  484. custom.clear();
  485. // MUST have been locked before
  486. masterMutex.unlock();
  487. singleMutex.unlock();
  488. if (lib != nullptr)
  489. libClose();
  490. }
  491. // -------------------------------------------------------------------
  492. // Buffer functions
  493. void clearBuffers()
  494. {
  495. if (latencyBuffers != nullptr)
  496. {
  497. for (uint32_t i=0; i < audioIn.count; ++i)
  498. {
  499. CARLA_ASSERT(latencyBuffers[i] != nullptr);
  500. if (latencyBuffers[i] != nullptr)
  501. {
  502. delete[] latencyBuffers[i];
  503. latencyBuffers[i] = nullptr;
  504. }
  505. }
  506. delete[] latencyBuffers;
  507. latencyBuffers = nullptr;
  508. latency = 0;
  509. }
  510. audioIn.clear();
  511. audioOut.clear();
  512. param.clear();
  513. event.clear();
  514. }
  515. void recreateLatencyBuffers()
  516. {
  517. if (latencyBuffers != nullptr)
  518. {
  519. for (uint32_t i=0; i < audioIn.count; ++i)
  520. {
  521. CARLA_ASSERT(latencyBuffers[i] != nullptr);
  522. if (latencyBuffers[i] != nullptr)
  523. {
  524. delete[] latencyBuffers[i];
  525. latencyBuffers[i] = nullptr;
  526. }
  527. }
  528. delete[] latencyBuffers;
  529. latencyBuffers = nullptr;
  530. }
  531. if (audioIn.count > 0 && latency > 0)
  532. {
  533. latencyBuffers = new float*[audioIn.count];
  534. for (uint32_t i=0; i < audioIn.count; ++i)
  535. {
  536. latencyBuffers[i] = new float[latency];
  537. carla_zeroFloat(latencyBuffers[i], latency);
  538. }
  539. }
  540. }
  541. // -------------------------------------------------------------------
  542. // Library functions, see CarlaPlugin.cpp
  543. bool libOpen(const char* const filename);
  544. bool uiLibOpen(const char* const filename);
  545. bool libClose();
  546. bool uiLibClose();
  547. void* libSymbol(const char* const symbol);
  548. void* uiLibSymbol(const char* const symbol);
  549. const char* libError(const char* const filename);
  550. // -------------------------------------------------------------------
  551. // Settings functions, see CarlaPlugin.cpp
  552. void saveSetting(const unsigned int option, const bool yesNo);
  553. unsigned int loadSettings(const unsigned int options, const unsigned int availOptions);
  554. // -------------------------------------------------------------------
  555. // Static helper functions
  556. static CarlaEngine* getEngine(CarlaPlugin* const plugin)
  557. {
  558. return plugin->kData->engine;
  559. }
  560. static CarlaEngineClient* getEngineClient(CarlaPlugin* const plugin)
  561. {
  562. return plugin->kData->client;
  563. }
  564. static CarlaEngineAudioPort* getAudioInPort(CarlaPlugin* const plugin, const uint32_t index)
  565. {
  566. return plugin->kData->audioIn.ports[index].port;
  567. }
  568. static CarlaEngineAudioPort* getAudioOutPort(CarlaPlugin* const plugin, const uint32_t index)
  569. {
  570. return plugin->kData->audioOut.ports[index].port;
  571. }
  572. static bool canRunInRack(CarlaPlugin* const plugin)
  573. {
  574. return (plugin->kData->extraHints & PLUGIN_HINT_CAN_RUN_RACK);
  575. }
  576. };
  577. // -----------------------------------------------------------------------
  578. CARLA_BACKEND_END_NAMESPACE
  579. #endif // __CARLA_PLUGIN_INTERNAL_HPP__