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.

CarlaPluginInternal.hpp 17KB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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, nullptr); 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(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. delete[] names[i];
  214. }
  215. delete[] names;
  216. names = nullptr;
  217. }
  218. count = 0;
  219. current = -1;
  220. }
  221. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginProgramData)
  222. };
  223. // -----------------------------------------------------------------------
  224. struct PluginMidiProgramData {
  225. uint32_t count;
  226. int32_t current;
  227. MidiProgramData* data;
  228. PluginMidiProgramData()
  229. : count(0),
  230. current(-1),
  231. data(nullptr) {}
  232. ~PluginMidiProgramData()
  233. {
  234. CARLA_ASSERT_INT(count == 0, count);
  235. CARLA_ASSERT_INT(current == -1, current);
  236. CARLA_ASSERT(data == nullptr);
  237. }
  238. void createNew(const uint32_t newCount)
  239. {
  240. CARLA_ASSERT_INT(count == 0, count);
  241. CARLA_ASSERT_INT(current == -1, current);
  242. CARLA_ASSERT(data == nullptr);
  243. CARLA_ASSERT_INT(newCount > 0, newCount);
  244. if (data != nullptr || newCount == 0)
  245. return;
  246. data = new MidiProgramData[newCount];
  247. count = newCount;
  248. }
  249. void clear()
  250. {
  251. if (data != nullptr)
  252. {
  253. for (uint32_t i=0; i < count; ++i)
  254. {
  255. if (data[i].name != nullptr)
  256. delete[] data[i].name;
  257. }
  258. delete[] data;
  259. data = nullptr;
  260. }
  261. count = 0;
  262. current = -1;
  263. }
  264. const MidiProgramData& getCurrent() const
  265. {
  266. CARLA_ASSERT(current >= 0 && current < static_cast<int32_t>(count));
  267. return data[current];
  268. }
  269. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginMidiProgramData)
  270. };
  271. // -----------------------------------------------------------------------
  272. struct PluginPostRtEvent {
  273. PluginPostRtEventType type;
  274. int32_t value1;
  275. int32_t value2;
  276. float value3;
  277. PluginPostRtEvent()
  278. : type(kPluginPostRtEventNull),
  279. value1(-1),
  280. value2(-1),
  281. value3(0.0f) {}
  282. #if 1//def DEBUG
  283. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PluginPostRtEvent)
  284. #else
  285. CARLA_DECLARE_NON_COPY_STRUCT(PluginPostRtEvent)
  286. #endif
  287. };
  288. // -----------------------------------------------------------------------
  289. struct ExternalMidiNote {
  290. int8_t channel; // invalid == -1
  291. uint8_t note;
  292. uint8_t velo; // note-off if 0
  293. ExternalMidiNote()
  294. : channel(-1),
  295. note(0),
  296. velo(0) {}
  297. #if 1//def DEBUG
  298. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(ExternalMidiNote)
  299. #else
  300. CARLA_DECLARE_NON_COPY_STRUCT(ExternalMidiNote)
  301. #endif
  302. };
  303. // -----------------------------------------------------------------------
  304. class CarlaPluginGui;
  305. struct CarlaPluginProtectedData {
  306. CarlaEngine* const engine;
  307. CarlaEngineClient* client;
  308. CarlaPluginGui* gui;
  309. bool active;
  310. bool needsReset;
  311. void* lib;
  312. void* uiLib;
  313. // misc
  314. int8_t ctrlChannel;
  315. unsigned int extraHints;
  316. CarlaString idStr;
  317. // latency
  318. uint32_t latency;
  319. float** latencyBuffers;
  320. // data
  321. PluginAudioData audioIn;
  322. PluginAudioData audioOut;
  323. PluginEventData event;
  324. PluginParameterData param;
  325. PluginProgramData prog;
  326. PluginMidiProgramData midiprog;
  327. NonRtList<CustomData> custom;
  328. CarlaMutex masterMutex; // global master lock
  329. CarlaMutex singleMutex; // small lock used only in processSingle()
  330. struct ExternalNotes {
  331. CarlaMutex mutex;
  332. RtList<ExternalMidiNote>::Pool dataPool;
  333. RtList<ExternalMidiNote> data;
  334. ExternalNotes()
  335. : dataPool(32, 152),
  336. data(&dataPool) {}
  337. ~ExternalNotes()
  338. {
  339. mutex.lock();
  340. data.clear();
  341. mutex.unlock();
  342. }
  343. void append(const ExternalMidiNote& note)
  344. {
  345. mutex.lock();
  346. data.append_sleepy(note);
  347. mutex.unlock();
  348. }
  349. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(ExternalNotes)
  350. } extNotes;
  351. struct PostRtEvents {
  352. CarlaMutex mutex;
  353. RtList<PluginPostRtEvent>::Pool dataPool;
  354. RtList<PluginPostRtEvent> data;
  355. RtList<PluginPostRtEvent> dataPendingRT;
  356. PostRtEvents()
  357. : dataPool(128, 128),
  358. data(&dataPool),
  359. dataPendingRT(&dataPool) {}
  360. ~PostRtEvents()
  361. {
  362. clear();
  363. }
  364. void appendRT(const PluginPostRtEvent& event)
  365. {
  366. dataPendingRT.append(event);
  367. }
  368. void trySplice()
  369. {
  370. if (mutex.tryLock())
  371. {
  372. dataPendingRT.spliceAppend(data, true);
  373. mutex.unlock();
  374. }
  375. }
  376. void clear()
  377. {
  378. mutex.lock();
  379. data.clear();
  380. dataPendingRT.clear();
  381. mutex.unlock();
  382. }
  383. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PostRtEvents)
  384. } postRtEvents;
  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_WITH_LEAK_DETECTOR(PostProc)
  398. } postProc;
  399. struct OSC {
  400. CarlaOscData data;
  401. CarlaPluginThread thread;
  402. OSC(CarlaEngine* const engine, CarlaPlugin* const plugin)
  403. : thread(engine, plugin) {}
  404. OSC() = delete;
  405. OSC(OSC&) = delete;
  406. OSC(const OSC&) = delete;
  407. CARLA_LEAK_DETECTOR(OSC)
  408. } osc;
  409. CarlaPluginProtectedData(CarlaEngine* const engine_, CarlaPlugin* const plugin)
  410. : engine(engine_),
  411. client(nullptr),
  412. gui(nullptr),
  413. active(false),
  414. needsReset(false),
  415. lib(nullptr),
  416. uiLib(nullptr),
  417. ctrlChannel(0),
  418. extraHints(0x0),
  419. latency(0),
  420. latencyBuffers(nullptr),
  421. osc(engine, plugin) {}
  422. CarlaPluginProtectedData() = delete;
  423. CarlaPluginProtectedData(CarlaPluginProtectedData&) = delete;
  424. CarlaPluginProtectedData(const CarlaPluginProtectedData&) = delete;
  425. CARLA_LEAK_DETECTOR(CarlaPluginProtectedData)
  426. ~CarlaPluginProtectedData()
  427. {
  428. CARLA_ASSERT(gui == nullptr);
  429. CARLA_ASSERT(! active);
  430. CARLA_ASSERT(! needsReset);
  431. CARLA_ASSERT(lib == nullptr);
  432. CARLA_ASSERT(uiLib == nullptr);
  433. CARLA_ASSERT(latency == 0);
  434. CARLA_ASSERT(latencyBuffers == nullptr);
  435. }
  436. // -------------------------------------------------------------------
  437. // Cleanup
  438. void cleanup()
  439. {
  440. {
  441. const bool lockMaster(masterMutex.tryLock());
  442. const bool lockSingle(singleMutex.tryLock());
  443. CARLA_ASSERT(! lockMaster);
  444. CARLA_ASSERT(! lockSingle);
  445. }
  446. if (client != nullptr)
  447. {
  448. if (client->isActive())
  449. client->deactivate();
  450. clearBuffers();
  451. delete client;
  452. client = nullptr;
  453. }
  454. for (auto it = custom.begin(); it.valid(); it.next())
  455. {
  456. CustomData& cData(*it);
  457. CARLA_ASSERT(cData.type != nullptr);
  458. CARLA_ASSERT(cData.key != nullptr);
  459. CARLA_ASSERT(cData.value != nullptr);
  460. if (cData.type != nullptr)
  461. delete[] cData.type;
  462. if (cData.key != nullptr)
  463. delete[] cData.key;
  464. if (cData.value != nullptr)
  465. delete[] cData.value;
  466. }
  467. prog.clear();
  468. midiprog.clear();
  469. custom.clear();
  470. // MUST have been unlocked before
  471. masterMutex.unlock();
  472. singleMutex.unlock();
  473. if (lib != nullptr)
  474. libClose();
  475. }
  476. // -------------------------------------------------------------------
  477. // Buffer functions
  478. void clearBuffers()
  479. {
  480. if (latencyBuffers != nullptr)
  481. {
  482. for (uint32_t i=0; i < audioIn.count; ++i)
  483. {
  484. CARLA_ASSERT(latencyBuffers[i] != nullptr);
  485. if (latencyBuffers[i] != nullptr)
  486. delete[] latencyBuffers[i];
  487. }
  488. delete[] latencyBuffers;
  489. latencyBuffers = nullptr;
  490. latency = 0;
  491. }
  492. audioIn.clear();
  493. audioOut.clear();
  494. param.clear();
  495. event.clear();
  496. }
  497. void recreateLatencyBuffers()
  498. {
  499. if (latencyBuffers != nullptr)
  500. {
  501. for (uint32_t i=0; i < audioIn.count; ++i)
  502. {
  503. CARLA_ASSERT(latencyBuffers[i] != nullptr);
  504. if (latencyBuffers[i] != nullptr)
  505. delete[] latencyBuffers[i];
  506. }
  507. delete[] latencyBuffers;
  508. latencyBuffers = nullptr;
  509. }
  510. if (audioIn.count > 0 && latency > 0)
  511. {
  512. latencyBuffers = new float*[audioIn.count];
  513. for (uint32_t i=0; i < audioIn.count; ++i)
  514. {
  515. latencyBuffers[i] = new float[latency];
  516. carla_zeroFloat(latencyBuffers[i], latency);
  517. }
  518. }
  519. }
  520. // -------------------------------------------------------------------
  521. // Library functions, see CarlaPlugin.cpp
  522. bool libOpen(const char* const filename);
  523. bool uiLibOpen(const char* const filename);
  524. bool libClose();
  525. bool uiLibClose();
  526. void* libSymbol(const char* const symbol);
  527. void* uiLibSymbol(const char* const symbol);
  528. const char* libError(const char* const filename);
  529. // -------------------------------------------------------------------
  530. // Settings functions, see CarlaPlugin.cpp
  531. void saveSetting(const unsigned int option, const bool yesNo);
  532. unsigned int loadSettings(const unsigned int options, const unsigned int availOptions);
  533. // -------------------------------------------------------------------
  534. // Static helper functions
  535. static CarlaEngine* getEngine(CarlaPlugin* const plugin)
  536. {
  537. return plugin->kData->engine;
  538. }
  539. static CarlaEngineClient* getEngineClient(CarlaPlugin* const plugin)
  540. {
  541. return plugin->kData->client;
  542. }
  543. static CarlaEngineAudioPort* getAudioInPort(CarlaPlugin* const plugin, const uint32_t index)
  544. {
  545. return plugin->kData->audioIn.ports[index].port;
  546. }
  547. static CarlaEngineAudioPort* getAudioOutPort(CarlaPlugin* const plugin, const uint32_t index)
  548. {
  549. return plugin->kData->audioOut.ports[index].port;
  550. }
  551. static bool canRunInRack(CarlaPlugin* const plugin)
  552. {
  553. return (plugin->kData->extraHints & PLUGIN_HINT_CAN_RUN_RACK);
  554. }
  555. };
  556. // -----------------------------------------------------------------------
  557. CARLA_BACKEND_END_NAMESPACE
  558. #endif // __CARLA_PLUGIN_INTERNAL_HPP__