Collection of DPF-based plugins for packaging
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.

1338 lines
44KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. * or without fee is hereby granted, provided that the above copyright notice and this
  7. * permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  10. * TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  11. * NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  12. * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  13. * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  14. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "DistrhoPluginInternal.hpp"
  17. #include "lv2/atom.h"
  18. #include "lv2/atom-util.h"
  19. #include "lv2/buf-size.h"
  20. #include "lv2/data-access.h"
  21. #include "lv2/instance-access.h"
  22. #include "lv2/midi.h"
  23. #include "lv2/options.h"
  24. #include "lv2/parameters.h"
  25. #include "lv2/state.h"
  26. #include "lv2/time.h"
  27. #include "lv2/urid.h"
  28. #include "lv2/worker.h"
  29. #include "lv2/lv2_kxstudio_properties.h"
  30. #include "lv2/lv2_programs.h"
  31. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  32. # include "libmodla.h"
  33. #endif
  34. #ifdef noexcept
  35. # undef noexcept
  36. #endif
  37. #include <map>
  38. #ifndef DISTRHO_PLUGIN_URI
  39. # error DISTRHO_PLUGIN_URI undefined!
  40. #endif
  41. #ifndef DISTRHO_PLUGIN_LV2_STATE_PREFIX
  42. # define DISTRHO_PLUGIN_LV2_STATE_PREFIX "urn:distrho:"
  43. #endif
  44. #define DISTRHO_LV2_USE_EVENTS_IN (DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_TIMEPOS || (DISTRHO_PLUGIN_WANT_STATE && DISTRHO_PLUGIN_HAS_UI))
  45. #define DISTRHO_LV2_USE_EVENTS_OUT (DISTRHO_PLUGIN_WANT_MIDI_OUTPUT || (DISTRHO_PLUGIN_WANT_STATE && DISTRHO_PLUGIN_HAS_UI))
  46. START_NAMESPACE_DISTRHO
  47. typedef std::map<const String, String> StringMap;
  48. #if ! DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  49. static const writeMidiFunc writeMidiCallback = nullptr;
  50. #endif
  51. // -----------------------------------------------------------------------
  52. class PluginLv2
  53. {
  54. public:
  55. PluginLv2(const double sampleRate, const LV2_URID_Map* const uridMap, const LV2_Worker_Schedule* const worker, const bool usingNominal)
  56. : fPlugin(this, writeMidiCallback),
  57. fUsingNominal(usingNominal),
  58. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  59. fRunCount(0),
  60. #endif
  61. fPortControls(nullptr),
  62. fLastControlValues(nullptr),
  63. fSampleRate(sampleRate),
  64. fURIDs(uridMap),
  65. fUridMap(uridMap),
  66. fWorker(worker)
  67. {
  68. #if DISTRHO_PLUGIN_NUM_INPUTS > 0
  69. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_INPUTS; ++i)
  70. fPortAudioIns[i] = nullptr;
  71. #else
  72. fPortAudioIns = nullptr;
  73. #endif
  74. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  75. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  76. fPortAudioOuts[i] = nullptr;
  77. #else
  78. fPortAudioOuts = nullptr;
  79. #endif
  80. if (const uint32_t count = fPlugin.getParameterCount())
  81. {
  82. fPortControls = new float*[count];
  83. fLastControlValues = new float[count];
  84. for (uint32_t i=0; i < count; ++i)
  85. {
  86. fPortControls[i] = nullptr;
  87. fLastControlValues[i] = fPlugin.getParameterValue(i);
  88. }
  89. }
  90. else
  91. {
  92. fPortControls = nullptr;
  93. fLastControlValues = nullptr;
  94. }
  95. #if DISTRHO_LV2_USE_EVENTS_IN
  96. fPortEventsIn = nullptr;
  97. #endif
  98. #if DISTRHO_PLUGIN_WANT_LATENCY
  99. fPortLatency = nullptr;
  100. #endif
  101. #if DISTRHO_PLUGIN_WANT_STATE
  102. if (const uint32_t count = fPlugin.getStateCount())
  103. {
  104. fNeededUiSends = new bool[count];
  105. for (uint32_t i=0; i < count; ++i)
  106. {
  107. fNeededUiSends[i] = false;
  108. const String& dkey(fPlugin.getStateKey(i));
  109. fStateMap[dkey] = fPlugin.getStateDefaultValue(i);
  110. }
  111. }
  112. else
  113. {
  114. fNeededUiSends = nullptr;
  115. }
  116. #else
  117. // unused
  118. (void)fWorker;
  119. #endif
  120. }
  121. ~PluginLv2()
  122. {
  123. if (fPortControls != nullptr)
  124. {
  125. delete[] fPortControls;
  126. fPortControls = nullptr;
  127. }
  128. if (fLastControlValues)
  129. {
  130. delete[] fLastControlValues;
  131. fLastControlValues = nullptr;
  132. }
  133. #if DISTRHO_PLUGIN_WANT_STATE
  134. if (fNeededUiSends != nullptr)
  135. {
  136. delete[] fNeededUiSends;
  137. fNeededUiSends = nullptr;
  138. }
  139. fStateMap.clear();
  140. #endif
  141. }
  142. // -------------------------------------------------------------------
  143. void lv2_activate()
  144. {
  145. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  146. fTimePosition.clear();
  147. // hosts may not send all values, resulting on some invalid data
  148. fTimePosition.bbt.bar = 1;
  149. fTimePosition.bbt.beat = 1;
  150. fTimePosition.bbt.tick = 0;
  151. fTimePosition.bbt.barStartTick = 0;
  152. fTimePosition.bbt.beatsPerBar = 4;
  153. fTimePosition.bbt.beatType = 4;
  154. fTimePosition.bbt.ticksPerBeat = 960.0;
  155. fTimePosition.bbt.beatsPerMinute = 120.0;
  156. #endif
  157. fPlugin.activate();
  158. }
  159. void lv2_deactivate()
  160. {
  161. fPlugin.deactivate();
  162. }
  163. // -------------------------------------------------------------------
  164. void lv2_connect_port(const uint32_t port, void* const dataLocation)
  165. {
  166. uint32_t index = 0;
  167. #if DISTRHO_PLUGIN_NUM_INPUTS > 0
  168. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_INPUTS; ++i)
  169. {
  170. if (port == index++)
  171. {
  172. fPortAudioIns[i] = (const float*)dataLocation;
  173. return;
  174. }
  175. }
  176. #endif
  177. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  178. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  179. {
  180. if (port == index++)
  181. {
  182. fPortAudioOuts[i] = (float*)dataLocation;
  183. return;
  184. }
  185. }
  186. #endif
  187. #if DISTRHO_LV2_USE_EVENTS_IN
  188. if (port == index++)
  189. {
  190. fPortEventsIn = (LV2_Atom_Sequence*)dataLocation;
  191. return;
  192. }
  193. #endif
  194. #if DISTRHO_LV2_USE_EVENTS_OUT
  195. if (port == index++)
  196. {
  197. fEventsOutData.port = (LV2_Atom_Sequence*)dataLocation;
  198. return;
  199. }
  200. #endif
  201. #if DISTRHO_PLUGIN_WANT_LATENCY
  202. if (port == index++)
  203. {
  204. fPortLatency = (float*)dataLocation;
  205. return;
  206. }
  207. #endif
  208. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  209. {
  210. if (port == index++)
  211. {
  212. fPortControls[i] = (float*)dataLocation;
  213. return;
  214. }
  215. }
  216. }
  217. // -------------------------------------------------------------------
  218. void lv2_run(const uint32_t sampleCount)
  219. {
  220. // cache midi input and time position first
  221. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  222. uint32_t midiEventCount = 0;
  223. #endif
  224. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_TIMEPOS
  225. LV2_ATOM_SEQUENCE_FOREACH(fPortEventsIn, event)
  226. {
  227. if (event == nullptr)
  228. break;
  229. # if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  230. if (event->body.type == fURIDs.midiEvent)
  231. {
  232. if (midiEventCount >= kMaxMidiEvents)
  233. continue;
  234. const uint8_t* const data((const uint8_t*)(event + 1));
  235. MidiEvent& midiEvent(fMidiEvents[midiEventCount++]);
  236. midiEvent.frame = event->time.frames;
  237. midiEvent.size = event->body.size;
  238. if (midiEvent.size > MidiEvent::kDataSize)
  239. {
  240. midiEvent.dataExt = data;
  241. std::memset(midiEvent.data, 0, MidiEvent::kDataSize);
  242. }
  243. else
  244. {
  245. midiEvent.dataExt = nullptr;
  246. std::memcpy(midiEvent.data, data, midiEvent.size);
  247. }
  248. continue;
  249. }
  250. # endif
  251. # if DISTRHO_PLUGIN_WANT_TIMEPOS
  252. if (event->body.type == fURIDs.atomBlank || event->body.type == fURIDs.atomObject)
  253. {
  254. const LV2_Atom_Object* const obj((const LV2_Atom_Object*)&event->body);
  255. if (obj->body.otype != fURIDs.timePosition)
  256. continue;
  257. LV2_Atom* bar = nullptr;
  258. LV2_Atom* barBeat = nullptr;
  259. LV2_Atom* beatUnit = nullptr;
  260. LV2_Atom* beatsPerBar = nullptr;
  261. LV2_Atom* beatsPerMinute = nullptr;
  262. LV2_Atom* frame = nullptr;
  263. LV2_Atom* speed = nullptr;
  264. LV2_Atom* ticksPerBeat = nullptr;
  265. lv2_atom_object_get(obj,
  266. fURIDs.timeBar, &bar,
  267. fURIDs.timeBarBeat, &barBeat,
  268. fURIDs.timeBeatUnit, &beatUnit,
  269. fURIDs.timeBeatsPerBar, &beatsPerBar,
  270. fURIDs.timeBeatsPerMinute, &beatsPerMinute,
  271. fURIDs.timeFrame, &frame,
  272. fURIDs.timeSpeed, &speed,
  273. fURIDs.timeTicksPerBeat, &ticksPerBeat,
  274. 0);
  275. // need to handle this first as other values depend on it
  276. if (ticksPerBeat != nullptr)
  277. {
  278. /**/ if (ticksPerBeat->type == fURIDs.atomDouble)
  279. fLastPositionData.ticksPerBeat = ((LV2_Atom_Double*)ticksPerBeat)->body;
  280. else if (ticksPerBeat->type == fURIDs.atomFloat)
  281. fLastPositionData.ticksPerBeat = ((LV2_Atom_Float*)ticksPerBeat)->body;
  282. else if (ticksPerBeat->type == fURIDs.atomInt)
  283. fLastPositionData.ticksPerBeat = ((LV2_Atom_Int*)ticksPerBeat)->body;
  284. else if (ticksPerBeat->type == fURIDs.atomLong)
  285. fLastPositionData.ticksPerBeat = ((LV2_Atom_Long*)ticksPerBeat)->body;
  286. else
  287. d_stderr("Unknown lv2 ticksPerBeat value type");
  288. if (fLastPositionData.ticksPerBeat > 0.0)
  289. fTimePosition.bbt.ticksPerBeat = fLastPositionData.ticksPerBeat;
  290. }
  291. // same
  292. if (speed != nullptr)
  293. {
  294. /**/ if (speed->type == fURIDs.atomDouble)
  295. fLastPositionData.speed = ((LV2_Atom_Double*)speed)->body;
  296. else if (speed->type == fURIDs.atomFloat)
  297. fLastPositionData.speed = ((LV2_Atom_Float*)speed)->body;
  298. else if (speed->type == fURIDs.atomInt)
  299. fLastPositionData.speed = ((LV2_Atom_Int*)speed)->body;
  300. else if (speed->type == fURIDs.atomLong)
  301. fLastPositionData.speed = ((LV2_Atom_Long*)speed)->body;
  302. else
  303. d_stderr("Unknown lv2 speed value type");
  304. fTimePosition.playing = d_isNotZero(fLastPositionData.speed);
  305. }
  306. if (bar != nullptr)
  307. {
  308. /**/ if (bar->type == fURIDs.atomDouble)
  309. fLastPositionData.bar = ((LV2_Atom_Double*)bar)->body;
  310. else if (bar->type == fURIDs.atomFloat)
  311. fLastPositionData.bar = ((LV2_Atom_Float*)bar)->body;
  312. else if (bar->type == fURIDs.atomInt)
  313. fLastPositionData.bar = ((LV2_Atom_Int*)bar)->body;
  314. else if (bar->type == fURIDs.atomLong)
  315. fLastPositionData.bar = ((LV2_Atom_Long*)bar)->body;
  316. else
  317. d_stderr("Unknown lv2 bar value type");
  318. if (fLastPositionData.bar >= 0)
  319. fTimePosition.bbt.bar = fLastPositionData.bar + 1;
  320. }
  321. if (barBeat != nullptr)
  322. {
  323. /**/ if (barBeat->type == fURIDs.atomDouble)
  324. fLastPositionData.barBeat = ((LV2_Atom_Double*)barBeat)->body;
  325. else if (barBeat->type == fURIDs.atomFloat)
  326. fLastPositionData.barBeat = ((LV2_Atom_Float*)barBeat)->body;
  327. else if (barBeat->type == fURIDs.atomInt)
  328. fLastPositionData.barBeat = ((LV2_Atom_Int*)barBeat)->body;
  329. else if (barBeat->type == fURIDs.atomLong)
  330. fLastPositionData.barBeat = ((LV2_Atom_Long*)barBeat)->body;
  331. else
  332. d_stderr("Unknown lv2 barBeat value type");
  333. if (fLastPositionData.barBeat >= 0.0f)
  334. {
  335. const double rest = std::fmod(fLastPositionData.barBeat, 1.0f);
  336. fTimePosition.bbt.beat = std::round(fLastPositionData.barBeat-rest+1.0);
  337. fTimePosition.bbt.tick = rest*fTimePosition.bbt.ticksPerBeat+0.5;
  338. }
  339. }
  340. if (beatUnit != nullptr)
  341. {
  342. /**/ if (beatUnit->type == fURIDs.atomDouble)
  343. fLastPositionData.beatUnit = ((LV2_Atom_Double*)beatUnit)->body;
  344. else if (beatUnit->type == fURIDs.atomFloat)
  345. fLastPositionData.beatUnit = ((LV2_Atom_Float*)beatUnit)->body;
  346. else if (beatUnit->type == fURIDs.atomInt)
  347. fLastPositionData.beatUnit = ((LV2_Atom_Int*)beatUnit)->body;
  348. else if (beatUnit->type == fURIDs.atomLong)
  349. fLastPositionData.beatUnit = ((LV2_Atom_Long*)beatUnit)->body;
  350. else
  351. d_stderr("Unknown lv2 beatUnit value type");
  352. if (fLastPositionData.beatUnit > 0)
  353. fTimePosition.bbt.beatType = fLastPositionData.beatUnit;
  354. }
  355. if (beatsPerBar != nullptr)
  356. {
  357. /**/ if (beatsPerBar->type == fURIDs.atomDouble)
  358. fLastPositionData.beatsPerBar = ((LV2_Atom_Double*)beatsPerBar)->body;
  359. else if (beatsPerBar->type == fURIDs.atomFloat)
  360. fLastPositionData.beatsPerBar = ((LV2_Atom_Float*)beatsPerBar)->body;
  361. else if (beatsPerBar->type == fURIDs.atomInt)
  362. fLastPositionData.beatsPerBar = ((LV2_Atom_Int*)beatsPerBar)->body;
  363. else if (beatsPerBar->type == fURIDs.atomLong)
  364. fLastPositionData.beatsPerBar = ((LV2_Atom_Long*)beatsPerBar)->body;
  365. else
  366. d_stderr("Unknown lv2 beatsPerBar value type");
  367. if (fLastPositionData.beatsPerBar > 0.0f)
  368. fTimePosition.bbt.beatsPerBar = fLastPositionData.beatsPerBar;
  369. }
  370. if (beatsPerMinute != nullptr)
  371. {
  372. /**/ if (beatsPerMinute->type == fURIDs.atomDouble)
  373. fLastPositionData.beatsPerMinute = ((LV2_Atom_Double*)beatsPerMinute)->body;
  374. else if (beatsPerMinute->type == fURIDs.atomFloat)
  375. fLastPositionData.beatsPerMinute = ((LV2_Atom_Float*)beatsPerMinute)->body;
  376. else if (beatsPerMinute->type == fURIDs.atomInt)
  377. fLastPositionData.beatsPerMinute = ((LV2_Atom_Int*)beatsPerMinute)->body;
  378. else if (beatsPerMinute->type == fURIDs.atomLong)
  379. fLastPositionData.beatsPerMinute = ((LV2_Atom_Long*)beatsPerMinute)->body;
  380. else
  381. d_stderr("Unknown lv2 beatsPerMinute value type");
  382. if (fLastPositionData.beatsPerMinute > 0.0f)
  383. {
  384. fTimePosition.bbt.beatsPerMinute = fLastPositionData.beatsPerMinute;
  385. if (d_isNotZero(fLastPositionData.speed))
  386. fTimePosition.bbt.beatsPerMinute *= std::abs(fLastPositionData.speed);
  387. }
  388. }
  389. if (frame != nullptr)
  390. {
  391. /**/ if (frame->type == fURIDs.atomDouble)
  392. fLastPositionData.frame = ((LV2_Atom_Double*)frame)->body;
  393. else if (frame->type == fURIDs.atomFloat)
  394. fLastPositionData.frame = ((LV2_Atom_Float*)frame)->body;
  395. else if (frame->type == fURIDs.atomInt)
  396. fLastPositionData.frame = ((LV2_Atom_Int*)frame)->body;
  397. else if (frame->type == fURIDs.atomLong)
  398. fLastPositionData.frame = ((LV2_Atom_Long*)frame)->body;
  399. else
  400. d_stderr("Unknown lv2 frame value type");
  401. if (fLastPositionData.frame >= 0)
  402. fTimePosition.frame = fLastPositionData.frame;
  403. }
  404. fTimePosition.bbt.barStartTick = fTimePosition.bbt.ticksPerBeat*
  405. fTimePosition.bbt.beatsPerBar*
  406. (fTimePosition.bbt.bar-1);
  407. fTimePosition.bbt.valid = (fLastPositionData.beatsPerMinute > 0.0 &&
  408. fLastPositionData.beatUnit > 0 &&
  409. fLastPositionData.beatsPerBar > 0.0f);
  410. fPlugin.setTimePosition(fTimePosition);
  411. continue;
  412. }
  413. # endif
  414. }
  415. #endif
  416. // check for messages from UI
  417. #if DISTRHO_PLUGIN_WANT_STATE && DISTRHO_PLUGIN_HAS_UI
  418. LV2_ATOM_SEQUENCE_FOREACH(fPortEventsIn, event)
  419. {
  420. if (event == nullptr)
  421. break;
  422. if (event->body.type == fURIDs.distrhoState && fWorker != nullptr)
  423. {
  424. const void* const data((const void*)(event + 1));
  425. // check if this is our special message
  426. if (std::strcmp((const char*)data, "__dpf_ui_data__") == 0)
  427. {
  428. for (uint32_t i=0, count=fPlugin.getStateCount(); i < count; ++i)
  429. fNeededUiSends[i] = true;
  430. }
  431. else
  432. // no, send to DSP as usual
  433. {
  434. fWorker->schedule_work(fWorker->handle, event->body.size, data);
  435. }
  436. }
  437. }
  438. #endif
  439. // Check for updated parameters
  440. float curValue;
  441. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  442. {
  443. if (fPortControls[i] == nullptr)
  444. continue;
  445. curValue = *fPortControls[i];
  446. if (fPlugin.isParameterInput(i) && d_isNotEqual(fLastControlValues[i], curValue))
  447. {
  448. fLastControlValues[i] = curValue;
  449. if (fPlugin.getParameterDesignation(i) == kParameterDesignationBypass)
  450. curValue = 1.0f - curValue;
  451. fPlugin.setParameterValue(i, curValue);
  452. }
  453. }
  454. // Run plugin
  455. if (sampleCount != 0)
  456. {
  457. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  458. fRunCount = mod_license_run_begin(fRunCount, sampleCount);
  459. #endif
  460. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  461. fPlugin.run(fPortAudioIns, fPortAudioOuts, sampleCount, fMidiEvents, midiEventCount);
  462. #else
  463. fPlugin.run(fPortAudioIns, fPortAudioOuts, sampleCount);
  464. #endif
  465. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  466. for (uint32_t i=0; i<DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  467. mod_license_run_silence(fRunCount, fPortAudioOuts[i], sampleCount, i);
  468. #endif
  469. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  470. // update timePos for next callback
  471. if (d_isNotZero(fLastPositionData.speed))
  472. {
  473. if (fLastPositionData.speed > 0.0)
  474. {
  475. // playing forwards
  476. fLastPositionData.frame += sampleCount;
  477. }
  478. else
  479. {
  480. // playing backwards
  481. fLastPositionData.frame -= sampleCount;
  482. if (fLastPositionData.frame < 0)
  483. fLastPositionData.frame = 0;
  484. }
  485. fTimePosition.frame = fLastPositionData.frame;
  486. if (fTimePosition.bbt.valid)
  487. {
  488. const double beatsPerMinute = fLastPositionData.beatsPerMinute * fLastPositionData.speed;
  489. const double framesPerBeat = 60.0 * fSampleRate / beatsPerMinute;
  490. const double addedBarBeats = double(sampleCount) / framesPerBeat;
  491. if (fLastPositionData.barBeat >= 0.0f)
  492. {
  493. fLastPositionData.barBeat = std::fmod(fLastPositionData.barBeat+addedBarBeats,
  494. (double)fLastPositionData.beatsPerBar);
  495. const double rest = std::fmod(fLastPositionData.barBeat, 1.0f);
  496. fTimePosition.bbt.beat = std::round(fLastPositionData.barBeat-rest+1.0);
  497. fTimePosition.bbt.tick = rest*fTimePosition.bbt.ticksPerBeat+0.5;
  498. if (fLastPositionData.bar >= 0)
  499. {
  500. fLastPositionData.bar += std::floor((fLastPositionData.barBeat+addedBarBeats)/
  501. fLastPositionData.beatsPerBar);
  502. if (fLastPositionData.bar < 0)
  503. fLastPositionData.bar = 0;
  504. fTimePosition.bbt.bar = fLastPositionData.bar + 1;
  505. fTimePosition.bbt.barStartTick = fTimePosition.bbt.ticksPerBeat*
  506. fTimePosition.bbt.beatsPerBar*
  507. (fTimePosition.bbt.bar-1);
  508. }
  509. }
  510. fTimePosition.bbt.beatsPerMinute = std::abs(beatsPerMinute);
  511. }
  512. fPlugin.setTimePosition(fTimePosition);
  513. }
  514. #endif
  515. }
  516. updateParameterOutputsAndTriggers();
  517. #if DISTRHO_PLUGIN_WANT_STATE && DISTRHO_PLUGIN_HAS_UI
  518. fEventsOutData.initIfNeeded(fURIDs.atomSequence);
  519. LV2_Atom_Event* aev;
  520. const uint32_t capacity = fEventsOutData.capacity;
  521. for (uint32_t i=0, count=fPlugin.getStateCount(); i < count; ++i)
  522. {
  523. if (! fNeededUiSends[i])
  524. continue;
  525. const String& key = fPlugin.getStateKey(i);
  526. for (StringMap::const_iterator cit=fStateMap.begin(), cite=fStateMap.end(); cit != cite; ++cit)
  527. {
  528. const String& curKey = cit->first;
  529. if (curKey != key)
  530. continue;
  531. const String& value = cit->second;
  532. // set msg size (key + value + separator + 2x null terminator)
  533. const size_t msgSize(key.length()+value.length()+3);
  534. if (sizeof(LV2_Atom_Event) + msgSize > capacity - fEventsOutData.offset)
  535. {
  536. d_stdout("Sending key '%s' to UI failed, out of space", key.buffer());
  537. break;
  538. }
  539. // reserve msg space
  540. // FIXME create a large enough buffer beforehand
  541. char msgBuf[msgSize];
  542. std::memset(msgBuf, 0, msgSize);
  543. // write key and value in atom bufer
  544. std::memcpy(msgBuf, key.buffer(), key.length()+1);
  545. std::memcpy(msgBuf+(key.length()+1), value.buffer(), value.length()+1);
  546. // put data
  547. aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, fEventsOutData.port) + fEventsOutData.offset);
  548. aev->time.frames = 0;
  549. aev->body.type = fURIDs.distrhoState;
  550. aev->body.size = msgSize;
  551. std::memcpy(LV2_ATOM_BODY(&aev->body), msgBuf, msgSize);
  552. fEventsOutData.growBy(lv2_atom_pad_size(sizeof(LV2_Atom_Event) + msgSize));
  553. fNeededUiSends[i] = false;
  554. break;
  555. }
  556. }
  557. #endif
  558. #if DISTRHO_LV2_USE_EVENTS_OUT
  559. fEventsOutData.endRun();
  560. #endif
  561. }
  562. // -------------------------------------------------------------------
  563. uint32_t lv2_get_options(LV2_Options_Option* const /*options*/)
  564. {
  565. // currently unused
  566. return LV2_OPTIONS_ERR_UNKNOWN;
  567. }
  568. uint32_t lv2_set_options(const LV2_Options_Option* const options)
  569. {
  570. for (int i=0; options[i].key != 0; ++i)
  571. {
  572. if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__nominalBlockLength))
  573. {
  574. if (options[i].type == fURIDs.atomInt)
  575. {
  576. const int32_t bufferSize(*(const int32_t*)options[i].value);
  577. fPlugin.setBufferSize(bufferSize);
  578. }
  579. else
  580. {
  581. d_stderr("Host changed nominalBlockLength but with wrong value type");
  582. }
  583. }
  584. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__maxBlockLength) && ! fUsingNominal)
  585. {
  586. if (options[i].type == fURIDs.atomInt)
  587. {
  588. const int32_t bufferSize(*(const int32_t*)options[i].value);
  589. fPlugin.setBufferSize(bufferSize);
  590. }
  591. else
  592. {
  593. d_stderr("Host changed maxBlockLength but with wrong value type");
  594. }
  595. }
  596. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_PARAMETERS__sampleRate))
  597. {
  598. if (options[i].type == fURIDs.atomFloat)
  599. {
  600. const float sampleRate(*(const float*)options[i].value);
  601. fSampleRate = sampleRate;
  602. fPlugin.setSampleRate(sampleRate);
  603. }
  604. else
  605. {
  606. d_stderr("Host changed sampleRate but with wrong value type");
  607. }
  608. }
  609. }
  610. return LV2_OPTIONS_SUCCESS;
  611. }
  612. // -------------------------------------------------------------------
  613. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  614. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index)
  615. {
  616. if (index >= fPlugin.getProgramCount())
  617. return nullptr;
  618. static LV2_Program_Descriptor desc;
  619. desc.bank = index / 128;
  620. desc.program = index % 128;
  621. desc.name = fPlugin.getProgramName(index);
  622. return &desc;
  623. }
  624. void lv2_select_program(const uint32_t bank, const uint32_t program)
  625. {
  626. const uint32_t realProgram(bank * 128 + program);
  627. if (realProgram >= fPlugin.getProgramCount())
  628. return;
  629. fPlugin.loadProgram(realProgram);
  630. // Update control inputs
  631. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  632. {
  633. if (fPlugin.isParameterOutput(i))
  634. continue;
  635. fLastControlValues[i] = fPlugin.getParameterValue(i);
  636. if (fPortControls[i] != nullptr)
  637. *fPortControls[i] = fLastControlValues[i];
  638. }
  639. # if DISTRHO_PLUGIN_WANT_FULL_STATE
  640. // Update state
  641. for (StringMap::const_iterator cit=fStateMap.begin(), cite=fStateMap.end(); cit != cite; ++cit)
  642. {
  643. const String& key = cit->first;
  644. fStateMap[key] = fPlugin.getState(key);
  645. }
  646. # endif
  647. }
  648. #endif
  649. // -------------------------------------------------------------------
  650. #if DISTRHO_PLUGIN_WANT_STATE
  651. LV2_State_Status lv2_save(const LV2_State_Store_Function store, const LV2_State_Handle handle)
  652. {
  653. # if DISTRHO_PLUGIN_WANT_FULL_STATE
  654. // Update current state
  655. for (StringMap::const_iterator cit=fStateMap.begin(), cite=fStateMap.end(); cit != cite; ++cit)
  656. {
  657. const String& key = cit->first;
  658. fStateMap[key] = fPlugin.getState(key);
  659. }
  660. # endif
  661. for (StringMap::const_iterator cit=fStateMap.begin(), cite=fStateMap.end(); cit != cite; ++cit)
  662. {
  663. const String& key = cit->first;
  664. const String& value = cit->second;
  665. const String urnKey(DISTRHO_PLUGIN_LV2_STATE_PREFIX + key);
  666. // some hosts need +1 for the null terminator, even though the type is string
  667. store(handle, fUridMap->map(fUridMap->handle, urnKey.buffer()), value.buffer(), value.length()+1, fURIDs.atomString, LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  668. }
  669. return LV2_STATE_SUCCESS;
  670. }
  671. LV2_State_Status lv2_restore(const LV2_State_Retrieve_Function retrieve, const LV2_State_Handle handle)
  672. {
  673. size_t size;
  674. uint32_t type, flags;
  675. for (uint32_t i=0, count=fPlugin.getStateCount(); i < count; ++i)
  676. {
  677. const String& key(fPlugin.getStateKey(i));
  678. const String urnKey(DISTRHO_PLUGIN_LV2_STATE_PREFIX + key);
  679. size = 0;
  680. type = 0;
  681. flags = LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE;
  682. const void* data = retrieve(handle, fUridMap->map(fUridMap->handle, urnKey.buffer()), &size, &type, &flags);
  683. if (data == nullptr || size == 0)
  684. continue;
  685. DISTRHO_SAFE_ASSERT_CONTINUE(type == fURIDs.atomString);
  686. const char* const value((const char*)data);
  687. const std::size_t length(std::strlen(value));
  688. DISTRHO_SAFE_ASSERT_CONTINUE(length == size || length+1 == size);
  689. setState(key, value);
  690. #if DISTRHO_LV2_USE_EVENTS_OUT
  691. // signal msg needed for UI
  692. fNeededUiSends[i] = true;
  693. #endif
  694. }
  695. return LV2_STATE_SUCCESS;
  696. }
  697. // -------------------------------------------------------------------
  698. LV2_Worker_Status lv2_work(const void* const data)
  699. {
  700. const char* const key((const char*)data);
  701. const char* const value(key+std::strlen(key)+1);
  702. setState(key, value);
  703. return LV2_WORKER_SUCCESS;
  704. }
  705. LV2_Worker_Status lv2_work_response(uint32_t, const void*)
  706. {
  707. return LV2_WORKER_SUCCESS;
  708. }
  709. #endif
  710. // -------------------------------------------------------------------
  711. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  712. void* lv2_get_instance_pointer()
  713. {
  714. return fPlugin.getInstancePointer();
  715. }
  716. #endif
  717. // -------------------------------------------------------------------
  718. private:
  719. PluginExporter fPlugin;
  720. const bool fUsingNominal; // if false use maxBlockLength
  721. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  722. uint32_t fRunCount;
  723. #endif
  724. // LV2 ports
  725. #if DISTRHO_PLUGIN_NUM_INPUTS > 0
  726. const float* fPortAudioIns[DISTRHO_PLUGIN_NUM_INPUTS];
  727. #else
  728. const float** fPortAudioIns;
  729. #endif
  730. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  731. float* fPortAudioOuts[DISTRHO_PLUGIN_NUM_OUTPUTS];
  732. #else
  733. float** fPortAudioOuts;
  734. #endif
  735. float** fPortControls;
  736. #if DISTRHO_LV2_USE_EVENTS_IN
  737. LV2_Atom_Sequence* fPortEventsIn;
  738. #endif
  739. #if DISTRHO_PLUGIN_WANT_LATENCY
  740. float* fPortLatency;
  741. #endif
  742. // Temporary data
  743. float* fLastControlValues;
  744. double fSampleRate;
  745. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  746. MidiEvent fMidiEvents[kMaxMidiEvents];
  747. #endif
  748. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  749. TimePosition fTimePosition;
  750. struct Lv2PositionData {
  751. int64_t bar;
  752. float barBeat;
  753. uint32_t beatUnit;
  754. float beatsPerBar;
  755. float beatsPerMinute;
  756. int64_t frame;
  757. double speed;
  758. double ticksPerBeat;
  759. Lv2PositionData()
  760. : bar(-1),
  761. barBeat(-1.0f),
  762. beatUnit(0),
  763. beatsPerBar(0.0f),
  764. beatsPerMinute(0.0f),
  765. frame(-1),
  766. speed(0.0),
  767. ticksPerBeat(-1.0) {}
  768. } fLastPositionData;
  769. #endif
  770. #if DISTRHO_LV2_USE_EVENTS_OUT
  771. struct Lv2EventsOutData {
  772. uint32_t capacity, offset;
  773. LV2_Atom_Sequence* port;
  774. Lv2EventsOutData()
  775. : capacity(0),
  776. offset(0),
  777. port(nullptr) {}
  778. void initIfNeeded(const LV2_URID uridAtomSequence)
  779. {
  780. if (capacity != 0)
  781. return;
  782. capacity = port->atom.size;
  783. port->atom.size = sizeof(LV2_Atom_Sequence_Body);
  784. port->atom.type = uridAtomSequence;
  785. port->body.unit = 0;
  786. port->body.pad = 0;
  787. }
  788. void growBy(const uint32_t size)
  789. {
  790. offset += size;
  791. port->atom.size += size;
  792. }
  793. void endRun()
  794. {
  795. capacity = 0;
  796. offset = 0;
  797. }
  798. } fEventsOutData;
  799. #endif
  800. // LV2 URIDs
  801. struct URIDs {
  802. LV2_URID atomBlank;
  803. LV2_URID atomObject;
  804. LV2_URID atomDouble;
  805. LV2_URID atomFloat;
  806. LV2_URID atomInt;
  807. LV2_URID atomLong;
  808. LV2_URID atomSequence;
  809. LV2_URID atomString;
  810. LV2_URID distrhoState;
  811. LV2_URID midiEvent;
  812. LV2_URID timePosition;
  813. LV2_URID timeBar;
  814. LV2_URID timeBarBeat;
  815. LV2_URID timeBeatUnit;
  816. LV2_URID timeBeatsPerBar;
  817. LV2_URID timeBeatsPerMinute;
  818. LV2_URID timeTicksPerBeat;
  819. LV2_URID timeFrame;
  820. LV2_URID timeSpeed;
  821. URIDs(const LV2_URID_Map* const uridMap)
  822. : atomBlank(uridMap->map(uridMap->handle, LV2_ATOM__Blank)),
  823. atomObject(uridMap->map(uridMap->handle, LV2_ATOM__Object)),
  824. atomDouble(uridMap->map(uridMap->handle, LV2_ATOM__Double)),
  825. atomFloat(uridMap->map(uridMap->handle, LV2_ATOM__Float)),
  826. atomInt(uridMap->map(uridMap->handle, LV2_ATOM__Int)),
  827. atomLong(uridMap->map(uridMap->handle, LV2_ATOM__Long)),
  828. atomSequence(uridMap->map(uridMap->handle, LV2_ATOM__Sequence)),
  829. atomString(uridMap->map(uridMap->handle, LV2_ATOM__String)),
  830. distrhoState(uridMap->map(uridMap->handle, DISTRHO_PLUGIN_LV2_STATE_PREFIX "KeyValueState")),
  831. midiEvent(uridMap->map(uridMap->handle, LV2_MIDI__MidiEvent)),
  832. timePosition(uridMap->map(uridMap->handle, LV2_TIME__Position)),
  833. timeBar(uridMap->map(uridMap->handle, LV2_TIME__bar)),
  834. timeBarBeat(uridMap->map(uridMap->handle, LV2_TIME__barBeat)),
  835. timeBeatUnit(uridMap->map(uridMap->handle, LV2_TIME__beatUnit)),
  836. timeBeatsPerBar(uridMap->map(uridMap->handle, LV2_TIME__beatsPerBar)),
  837. timeBeatsPerMinute(uridMap->map(uridMap->handle, LV2_TIME__beatsPerMinute)),
  838. timeTicksPerBeat(uridMap->map(uridMap->handle, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat)),
  839. timeFrame(uridMap->map(uridMap->handle, LV2_TIME__frame)),
  840. timeSpeed(uridMap->map(uridMap->handle, LV2_TIME__speed)) {}
  841. } fURIDs;
  842. // LV2 features
  843. const LV2_URID_Map* const fUridMap;
  844. const LV2_Worker_Schedule* const fWorker;
  845. #if DISTRHO_PLUGIN_WANT_STATE
  846. StringMap fStateMap;
  847. bool* fNeededUiSends;
  848. void setState(const char* const key, const char* const newValue)
  849. {
  850. fPlugin.setState(key, newValue);
  851. // check if we want to save this key
  852. if (! fPlugin.wantStateKey(key))
  853. return;
  854. // check if key already exists
  855. for (StringMap::iterator it=fStateMap.begin(), ite=fStateMap.end(); it != ite; ++it)
  856. {
  857. const String& dkey(it->first);
  858. if (dkey == key)
  859. {
  860. it->second = newValue;
  861. return;
  862. }
  863. }
  864. d_stderr("Failed to find plugin state with key \"%s\"", key);
  865. }
  866. #endif
  867. void updateParameterOutputsAndTriggers()
  868. {
  869. float curValue;
  870. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  871. {
  872. if (fPlugin.isParameterOutput(i))
  873. {
  874. curValue = fLastControlValues[i] = fPlugin.getParameterValue(i);
  875. if (fPortControls[i] != nullptr)
  876. *fPortControls[i] = curValue;
  877. }
  878. else if ((fPlugin.getParameterHints(i) & kParameterIsTrigger) == kParameterIsTrigger)
  879. {
  880. // NOTE: host is responsible for auto-updating control port buffers
  881. }
  882. }
  883. #if DISTRHO_PLUGIN_WANT_LATENCY
  884. if (fPortLatency != nullptr)
  885. *fPortLatency = fPlugin.getLatency();
  886. #endif
  887. }
  888. #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  889. bool writeMidi(const MidiEvent& midiEvent)
  890. {
  891. DISTRHO_SAFE_ASSERT_RETURN(fEventsOutData.port != nullptr, false);
  892. fEventsOutData.initIfNeeded(fURIDs.atomSequence);
  893. const uint32_t capacity = fEventsOutData.capacity;
  894. const uint32_t offset = fEventsOutData.offset;
  895. if (sizeof(LV2_Atom_Event) + midiEvent.size > capacity - offset)
  896. return false;
  897. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, fEventsOutData.port) + offset);
  898. aev->time.frames = midiEvent.frame;
  899. aev->body.type = fURIDs.midiEvent;
  900. aev->body.size = midiEvent.size;
  901. std::memcpy(LV2_ATOM_BODY(&aev->body),
  902. midiEvent.size > MidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data,
  903. midiEvent.size);
  904. fEventsOutData.growBy(lv2_atom_pad_size(sizeof(LV2_Atom_Event) + midiEvent.size));
  905. return true;
  906. }
  907. static bool writeMidiCallback(void* ptr, const MidiEvent& midiEvent)
  908. {
  909. return ((PluginLv2*)ptr)->writeMidi(midiEvent);
  910. }
  911. #endif
  912. };
  913. // -----------------------------------------------------------------------
  914. static LV2_Handle lv2_instantiate(const LV2_Descriptor*, double sampleRate, const char*, const LV2_Feature* const* features)
  915. {
  916. const LV2_Options_Option* options = nullptr;
  917. const LV2_URID_Map* uridMap = nullptr;
  918. const LV2_Worker_Schedule* worker = nullptr;
  919. for (int i=0; features[i] != nullptr; ++i)
  920. {
  921. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  922. options = (const LV2_Options_Option*)features[i]->data;
  923. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  924. uridMap = (const LV2_URID_Map*)features[i]->data;
  925. else if (std::strcmp(features[i]->URI, LV2_WORKER__schedule) == 0)
  926. worker = (const LV2_Worker_Schedule*)features[i]->data;
  927. }
  928. if (options == nullptr)
  929. {
  930. d_stderr("Options feature missing, cannot continue!");
  931. return nullptr;
  932. }
  933. if (uridMap == nullptr)
  934. {
  935. d_stderr("URID Map feature missing, cannot continue!");
  936. return nullptr;
  937. }
  938. #if DISTRHO_PLUGIN_WANT_STATE
  939. if (worker == nullptr)
  940. {
  941. d_stderr("Worker feature missing, cannot continue!");
  942. return nullptr;
  943. }
  944. #endif
  945. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  946. mod_license_check(features, DISTRHO_PLUGIN_URI);
  947. #endif
  948. d_lastBufferSize = 0;
  949. bool usingNominal = false;
  950. for (int i=0; options[i].key != 0; ++i)
  951. {
  952. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__nominalBlockLength))
  953. {
  954. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  955. {
  956. d_lastBufferSize = *(const int*)options[i].value;
  957. usingNominal = true;
  958. }
  959. else
  960. {
  961. d_stderr("Host provides nominalBlockLength but has wrong value type");
  962. }
  963. break;
  964. }
  965. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  966. {
  967. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  968. d_lastBufferSize = *(const int*)options[i].value;
  969. else
  970. d_stderr("Host provides maxBlockLength but has wrong value type");
  971. // no break, continue in case host supports nominalBlockLength
  972. }
  973. }
  974. if (d_lastBufferSize == 0)
  975. {
  976. d_stderr("Host does not provide nominalBlockLength or maxBlockLength options");
  977. d_lastBufferSize = 2048;
  978. }
  979. d_lastSampleRate = sampleRate;
  980. return new PluginLv2(sampleRate, uridMap, worker, usingNominal);
  981. }
  982. #define instancePtr ((PluginLv2*)instance)
  983. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  984. {
  985. instancePtr->lv2_connect_port(port, dataLocation);
  986. }
  987. static void lv2_activate(LV2_Handle instance)
  988. {
  989. instancePtr->lv2_activate();
  990. }
  991. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  992. {
  993. instancePtr->lv2_run(sampleCount);
  994. }
  995. static void lv2_deactivate(LV2_Handle instance)
  996. {
  997. instancePtr->lv2_deactivate();
  998. }
  999. static void lv2_cleanup(LV2_Handle instance)
  1000. {
  1001. delete instancePtr;
  1002. }
  1003. // -----------------------------------------------------------------------
  1004. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  1005. {
  1006. return instancePtr->lv2_get_options(options);
  1007. }
  1008. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  1009. {
  1010. return instancePtr->lv2_set_options(options);
  1011. }
  1012. // -----------------------------------------------------------------------
  1013. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  1014. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  1015. {
  1016. return instancePtr->lv2_get_program(index);
  1017. }
  1018. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  1019. {
  1020. instancePtr->lv2_select_program(bank, program);
  1021. }
  1022. #endif
  1023. // -----------------------------------------------------------------------
  1024. #if DISTRHO_PLUGIN_WANT_STATE
  1025. static LV2_State_Status lv2_save(LV2_Handle instance, LV2_State_Store_Function store, LV2_State_Handle handle, uint32_t, const LV2_Feature* const*)
  1026. {
  1027. return instancePtr->lv2_save(store, handle);
  1028. }
  1029. static LV2_State_Status lv2_restore(LV2_Handle instance, LV2_State_Retrieve_Function retrieve, LV2_State_Handle handle, uint32_t, const LV2_Feature* const*)
  1030. {
  1031. return instancePtr->lv2_restore(retrieve, handle);
  1032. }
  1033. LV2_Worker_Status lv2_work(LV2_Handle instance, LV2_Worker_Respond_Function, LV2_Worker_Respond_Handle, uint32_t, const void* data)
  1034. {
  1035. return instancePtr->lv2_work(data);
  1036. }
  1037. LV2_Worker_Status lv2_work_response(LV2_Handle instance, uint32_t size, const void* body)
  1038. {
  1039. return instancePtr->lv2_work_response(size, body);
  1040. }
  1041. #endif
  1042. // -----------------------------------------------------------------------
  1043. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  1044. static void* lv2_get_instance_pointer(LV2_Handle instance)
  1045. {
  1046. return instancePtr->lv2_get_instance_pointer();
  1047. }
  1048. #endif
  1049. // -----------------------------------------------------------------------
  1050. static const void* lv2_extension_data(const char* uri)
  1051. {
  1052. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  1053. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  1054. return &options;
  1055. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  1056. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  1057. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  1058. return &programs;
  1059. #endif
  1060. #if DISTRHO_PLUGIN_WANT_STATE
  1061. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  1062. static const LV2_Worker_Interface worker = { lv2_work, lv2_work_response, nullptr };
  1063. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  1064. return &state;
  1065. if (std::strcmp(uri, LV2_WORKER__interface) == 0)
  1066. return &worker;
  1067. #endif
  1068. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  1069. struct LV2_DirectAccess_Interface {
  1070. void* (*get_instance_pointer)(LV2_Handle handle);
  1071. };
  1072. static const LV2_DirectAccess_Interface directaccess = { lv2_get_instance_pointer };
  1073. if (std::strcmp(uri, DISTRHO_PLUGIN_LV2_STATE_PREFIX "direct-access") == 0)
  1074. return &directaccess;
  1075. #endif
  1076. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  1077. return mod_license_interface(uri);
  1078. #else
  1079. return nullptr;
  1080. #endif
  1081. }
  1082. #undef instancePtr
  1083. // -----------------------------------------------------------------------
  1084. static const LV2_Descriptor sLv2Descriptor = {
  1085. DISTRHO_PLUGIN_URI,
  1086. lv2_instantiate,
  1087. lv2_connect_port,
  1088. lv2_activate,
  1089. lv2_run,
  1090. lv2_deactivate,
  1091. lv2_cleanup,
  1092. lv2_extension_data
  1093. };
  1094. // -----------------------------------------------------------------------
  1095. END_NAMESPACE_DISTRHO
  1096. DISTRHO_PLUGIN_EXPORT
  1097. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  1098. {
  1099. USE_NAMESPACE_DISTRHO
  1100. return (index == 0) ? &sLv2Descriptor : nullptr;
  1101. }
  1102. // -----------------------------------------------------------------------