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.

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