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.

699 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. // 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_WITH_LEAK_DETECTOR(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_WITH_LEAK_DETECTOR(PostRtEvents)
  383. } postRtEvents;
  384. struct PostProc {
  385. float dryWet;
  386. float volume;
  387. float balanceLeft;
  388. float balanceRight;
  389. float panning;
  390. PostProc()
  391. : dryWet(1.0f),
  392. volume(1.0f),
  393. balanceLeft(-1.0f),
  394. balanceRight(1.0f),
  395. panning(0.0f) {}
  396. CARLA_DECLARE_NON_COPY_STRUCT_WITH_LEAK_DETECTOR(PostProc)
  397. } postProc;
  398. struct OSC {
  399. CarlaOscData data;
  400. CarlaPluginThread thread;
  401. OSC(CarlaEngine* const engine, CarlaPlugin* const plugin)
  402. : thread(engine, plugin) {}
  403. OSC() = delete;
  404. OSC(OSC&) = delete;
  405. OSC(const OSC&) = delete;
  406. CARLA_LEAK_DETECTOR(OSC)
  407. } osc;
  408. CarlaPluginProtectedData(CarlaEngine* const engine_, CarlaPlugin* const plugin)
  409. : engine(engine_),
  410. client(nullptr),
  411. gui(nullptr),
  412. active(false),
  413. needsReset(false),
  414. lib(nullptr),
  415. ctrlChannel(0),
  416. extraHints(0x0),
  417. latency(0),
  418. latencyBuffers(nullptr),
  419. osc(engine, plugin) {}
  420. CarlaPluginProtectedData() = delete;
  421. CarlaPluginProtectedData(CarlaPluginProtectedData&) = delete;
  422. CarlaPluginProtectedData(const CarlaPluginProtectedData&) = delete;
  423. CARLA_LEAK_DETECTOR(CarlaPluginProtectedData)
  424. ~CarlaPluginProtectedData()
  425. {
  426. CARLA_ASSERT(gui == nullptr);
  427. CARLA_ASSERT(! active);
  428. CARLA_ASSERT(! needsReset);
  429. CARLA_ASSERT(lib == nullptr);
  430. CARLA_ASSERT(latency == 0);
  431. CARLA_ASSERT(latencyBuffers == nullptr);
  432. }
  433. // -------------------------------------------------------------------
  434. // Cleanup
  435. void cleanup()
  436. {
  437. {
  438. const bool lockMaster(masterMutex.tryLock());
  439. const bool lockSingle(singleMutex.tryLock());
  440. CARLA_ASSERT(! lockMaster);
  441. CARLA_ASSERT(! lockSingle);
  442. }
  443. if (client != nullptr)
  444. {
  445. if (client->isActive())
  446. client->deactivate();
  447. clearBuffers();
  448. delete client;
  449. client = nullptr;
  450. }
  451. for (auto it = custom.begin(); it.valid(); it.next())
  452. {
  453. CustomData& cData(*it);
  454. CARLA_ASSERT(cData.type != nullptr);
  455. CARLA_ASSERT(cData.key != nullptr);
  456. CARLA_ASSERT(cData.value != nullptr);
  457. if (cData.type != nullptr)
  458. delete[] cData.type;
  459. if (cData.key != nullptr)
  460. delete[] cData.key;
  461. if (cData.value != nullptr)
  462. delete[] cData.value;
  463. }
  464. prog.clear();
  465. midiprog.clear();
  466. custom.clear();
  467. // MUST have been unlocked before
  468. masterMutex.unlock();
  469. singleMutex.unlock();
  470. if (lib != nullptr)
  471. libClose();
  472. }
  473. // -------------------------------------------------------------------
  474. // Buffer functions
  475. void clearBuffers()
  476. {
  477. if (latencyBuffers != nullptr)
  478. {
  479. for (uint32_t i=0; i < audioIn.count; i++)
  480. {
  481. CARLA_ASSERT(latencyBuffers[i] != nullptr);
  482. if (latencyBuffers[i] != nullptr)
  483. delete[] latencyBuffers[i];
  484. }
  485. delete[] latencyBuffers;
  486. latencyBuffers = nullptr;
  487. latency = 0;
  488. }
  489. audioIn.clear();
  490. audioOut.clear();
  491. param.clear();
  492. event.clear();
  493. }
  494. void recreateLatencyBuffers()
  495. {
  496. if (latencyBuffers != nullptr)
  497. {
  498. for (uint32_t i=0; i < audioIn.count; i++)
  499. {
  500. CARLA_ASSERT(latencyBuffers[i] != nullptr);
  501. if (latencyBuffers[i] != nullptr)
  502. delete[] latencyBuffers[i];
  503. }
  504. delete[] latencyBuffers;
  505. latencyBuffers = nullptr;
  506. }
  507. if (audioIn.count > 0 && latency > 0)
  508. {
  509. latencyBuffers = new float*[audioIn.count];
  510. for (uint32_t i=0; i < audioIn.count; i++)
  511. {
  512. latencyBuffers[i] = new float[latency];
  513. carla_zeroFloat(latencyBuffers[i], latency);
  514. }
  515. }
  516. }
  517. // -------------------------------------------------------------------
  518. // Library functions, see CarlaPlugin.cpp
  519. bool libOpen(const char* const filename);
  520. bool libClose();
  521. void* libSymbol(const char* const symbol);
  522. const char* libError(const char* const filename);
  523. // -------------------------------------------------------------------
  524. // Settings functions, see CarlaPlugin.cpp
  525. void saveSetting(const unsigned int option, const bool yesNo);
  526. unsigned int loadSettings(const unsigned int options, const unsigned int availOptions);
  527. // -------------------------------------------------------------------
  528. // Static helper functions
  529. static CarlaEngine* getEngine(CarlaPlugin* const plugin)
  530. {
  531. return plugin->kData->engine;
  532. }
  533. static CarlaEngineClient* getEngineClient(CarlaPlugin* const plugin)
  534. {
  535. return plugin->kData->client;
  536. }
  537. static CarlaEngineAudioPort* getAudioInPort(CarlaPlugin* const plugin, const uint32_t index)
  538. {
  539. return plugin->kData->audioIn.ports[index].port;
  540. }
  541. static CarlaEngineAudioPort* getAudioOutPort(CarlaPlugin* const plugin, const uint32_t index)
  542. {
  543. return plugin->kData->audioOut.ports[index].port;
  544. }
  545. static bool canRunInRack(CarlaPlugin* const plugin)
  546. {
  547. return (plugin->kData->extraHints & PLUGIN_HINT_CAN_RUN_RACK);
  548. }
  549. };
  550. // -----------------------------------------------------------------------
  551. CARLA_BACKEND_END_NAMESPACE
  552. #endif // __CARLA_PLUGIN_INTERNAL_HPP__