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.

705 lines
17KB

  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__