DISTRHO Plugin Framework
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.

1326 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. std::memset(&fTimePosition, 0, sizeof(TimePosition));
  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_noise(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. uint32_t offset = fEventsOutData.offset;
  521. const uint32_t capacity = fEventsOutData.capacity;
  522. for (uint32_t i=0, count=fPlugin.getStateCount(); i < count; ++i)
  523. {
  524. if (! fNeededUiSends[i])
  525. continue;
  526. const String& key = fPlugin.getStateKey(i);
  527. for (StringMap::const_iterator cit=fStateMap.begin(), cite=fStateMap.end(); cit != cite; ++cit)
  528. {
  529. const String& curKey = cit->first;
  530. if (curKey != key)
  531. continue;
  532. const String& value = cit->second;
  533. // set msg size (key + value + separator + 2x null terminator)
  534. const size_t msgSize(key.length()+value.length()+3);
  535. if (sizeof(LV2_Atom_Event) + msgSize > capacity - offset)
  536. break;
  537. // reserve msg space
  538. // FIXME create a large enough buffer beforehand
  539. char msgBuf[msgSize];
  540. std::memset(msgBuf, 0, msgSize);
  541. // write key and value in atom bufer
  542. std::memcpy(msgBuf, key.buffer(), key.length());
  543. std::memcpy(msgBuf+(key.length()+1), value.buffer(), value.length());
  544. // put data
  545. aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, fEventsOutData.port) + offset);
  546. aev->time.frames = 0;
  547. aev->body.type = fURIDs.distrhoState;
  548. aev->body.size = msgSize;
  549. std::memcpy(LV2_ATOM_BODY(&aev->body), msgBuf, msgSize-1);
  550. fEventsOutData.growBy(lv2_atom_pad_size(sizeof(LV2_Atom_Event) + msgSize));
  551. fNeededUiSends[i] = false;
  552. break;
  553. }
  554. }
  555. #endif
  556. #if DISTRHO_LV2_USE_EVENTS_OUT
  557. fEventsOutData.endRun();
  558. #endif
  559. }
  560. // -------------------------------------------------------------------
  561. uint32_t lv2_get_options(LV2_Options_Option* const /*options*/)
  562. {
  563. // currently unused
  564. return LV2_OPTIONS_ERR_UNKNOWN;
  565. }
  566. uint32_t lv2_set_options(const LV2_Options_Option* const options)
  567. {
  568. for (int i=0; options[i].key != 0; ++i)
  569. {
  570. if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__nominalBlockLength))
  571. {
  572. if (options[i].type == fURIDs.atomInt)
  573. {
  574. const int32_t bufferSize(*(const int32_t*)options[i].value);
  575. fPlugin.setBufferSize(bufferSize);
  576. }
  577. else
  578. {
  579. d_stderr("Host changed nominalBlockLength but with wrong value type");
  580. }
  581. }
  582. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__maxBlockLength) && ! fUsingNominal)
  583. {
  584. if (options[i].type == fURIDs.atomInt)
  585. {
  586. const int32_t bufferSize(*(const int32_t*)options[i].value);
  587. fPlugin.setBufferSize(bufferSize);
  588. }
  589. else
  590. {
  591. d_stderr("Host changed maxBlockLength but with wrong value type");
  592. }
  593. }
  594. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_PARAMETERS__sampleRate))
  595. {
  596. if (options[i].type == fURIDs.atomFloat)
  597. {
  598. const float sampleRate(*(const float*)options[i].value);
  599. fSampleRate = sampleRate;
  600. fPlugin.setSampleRate(sampleRate);
  601. }
  602. else
  603. {
  604. d_stderr("Host changed sampleRate but with wrong value type");
  605. }
  606. }
  607. }
  608. return LV2_OPTIONS_SUCCESS;
  609. }
  610. // -------------------------------------------------------------------
  611. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  612. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index)
  613. {
  614. if (index >= fPlugin.getProgramCount())
  615. return nullptr;
  616. static LV2_Program_Descriptor desc;
  617. desc.bank = index / 128;
  618. desc.program = index % 128;
  619. desc.name = fPlugin.getProgramName(index);
  620. return &desc;
  621. }
  622. void lv2_select_program(const uint32_t bank, const uint32_t program)
  623. {
  624. const uint32_t realProgram(bank * 128 + program);
  625. if (realProgram >= fPlugin.getProgramCount())
  626. return;
  627. fPlugin.loadProgram(realProgram);
  628. // Update control inputs
  629. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  630. {
  631. if (fPlugin.isParameterOutput(i))
  632. continue;
  633. fLastControlValues[i] = fPlugin.getParameterValue(i);
  634. if (fPortControls[i] != nullptr)
  635. *fPortControls[i] = fLastControlValues[i];
  636. }
  637. # if DISTRHO_PLUGIN_WANT_FULL_STATE
  638. // Update state
  639. for (StringMap::const_iterator cit=fStateMap.begin(), cite=fStateMap.end(); cit != cite; ++cit)
  640. {
  641. const String& key = cit->first;
  642. fStateMap[key] = fPlugin.getState(key);
  643. }
  644. # endif
  645. }
  646. #endif
  647. // -------------------------------------------------------------------
  648. #if DISTRHO_PLUGIN_WANT_STATE
  649. LV2_State_Status lv2_save(const LV2_State_Store_Function store, const LV2_State_Handle handle)
  650. {
  651. # if DISTRHO_PLUGIN_WANT_FULL_STATE
  652. // Update current state
  653. for (StringMap::const_iterator cit=fStateMap.begin(), cite=fStateMap.end(); cit != cite; ++cit)
  654. {
  655. const String& key = cit->first;
  656. fStateMap[key] = fPlugin.getState(key);
  657. }
  658. # endif
  659. for (StringMap::const_iterator cit=fStateMap.begin(), cite=fStateMap.end(); cit != cite; ++cit)
  660. {
  661. const String& key = cit->first;
  662. const String& value = cit->second;
  663. const String urnKey(DISTRHO_PLUGIN_LV2_STATE_PREFIX + key);
  664. // some hosts need +1 for the null terminator, even though the type is string
  665. store(handle, fUridMap->map(fUridMap->handle, urnKey.buffer()), value.buffer(), value.length()+1, fURIDs.atomString, LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  666. }
  667. return LV2_STATE_SUCCESS;
  668. }
  669. LV2_State_Status lv2_restore(const LV2_State_Retrieve_Function retrieve, const LV2_State_Handle handle)
  670. {
  671. size_t size;
  672. uint32_t type, flags;
  673. for (uint32_t i=0, count=fPlugin.getStateCount(); i < count; ++i)
  674. {
  675. const String& key(fPlugin.getStateKey(i));
  676. const String urnKey(DISTRHO_PLUGIN_LV2_STATE_PREFIX + key);
  677. size = 0;
  678. type = 0;
  679. flags = LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE;
  680. const void* data = retrieve(handle, fUridMap->map(fUridMap->handle, urnKey.buffer()), &size, &type, &flags);
  681. if (data == nullptr || size == 0)
  682. continue;
  683. DISTRHO_SAFE_ASSERT_CONTINUE(type == fURIDs.atomString);
  684. const char* const value((const char*)data);
  685. const std::size_t length(std::strlen(value));
  686. DISTRHO_SAFE_ASSERT_CONTINUE(length == size || length+1 == size);
  687. setState(key, value);
  688. #if DISTRHO_LV2_USE_EVENTS_OUT
  689. // signal msg needed for UI
  690. fNeededUiSends[i] = true;
  691. #endif
  692. }
  693. return LV2_STATE_SUCCESS;
  694. }
  695. // -------------------------------------------------------------------
  696. LV2_Worker_Status lv2_work(const void* const data)
  697. {
  698. const char* const key((const char*)data);
  699. const char* const value(key+std::strlen(key)+1);
  700. setState(key, value);
  701. return LV2_WORKER_SUCCESS;
  702. }
  703. #endif
  704. // -------------------------------------------------------------------
  705. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  706. void* lv2_get_instance_pointer()
  707. {
  708. return fPlugin.getInstancePointer();
  709. }
  710. #endif
  711. // -------------------------------------------------------------------
  712. private:
  713. PluginExporter fPlugin;
  714. const bool fUsingNominal; // if false use maxBlockLength
  715. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  716. uint32_t fRunCount;
  717. #endif
  718. // LV2 ports
  719. #if DISTRHO_PLUGIN_NUM_INPUTS > 0
  720. const float* fPortAudioIns[DISTRHO_PLUGIN_NUM_INPUTS];
  721. #else
  722. const float** fPortAudioIns;
  723. #endif
  724. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  725. float* fPortAudioOuts[DISTRHO_PLUGIN_NUM_OUTPUTS];
  726. #else
  727. float** fPortAudioOuts;
  728. #endif
  729. float** fPortControls;
  730. #if DISTRHO_LV2_USE_EVENTS_IN
  731. LV2_Atom_Sequence* fPortEventsIn;
  732. #endif
  733. #if DISTRHO_PLUGIN_WANT_LATENCY
  734. float* fPortLatency;
  735. #endif
  736. // Temporary data
  737. float* fLastControlValues;
  738. double fSampleRate;
  739. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  740. MidiEvent fMidiEvents[kMaxMidiEvents];
  741. #endif
  742. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  743. TimePosition fTimePosition;
  744. struct Lv2PositionData {
  745. int64_t bar;
  746. float barBeat;
  747. uint32_t beatUnit;
  748. float beatsPerBar;
  749. float beatsPerMinute;
  750. int64_t frame;
  751. double speed;
  752. double ticksPerBeat;
  753. Lv2PositionData()
  754. : bar(-1),
  755. barBeat(-1.0f),
  756. beatUnit(0),
  757. beatsPerBar(0.0f),
  758. beatsPerMinute(0.0f),
  759. frame(-1),
  760. speed(0.0),
  761. ticksPerBeat(-1.0) {}
  762. } fLastPositionData;
  763. #endif
  764. #if DISTRHO_LV2_USE_EVENTS_OUT
  765. struct Lv2EventsOutData {
  766. uint32_t capacity, offset;
  767. LV2_Atom_Sequence* port;
  768. Lv2EventsOutData()
  769. : capacity(0),
  770. offset(0),
  771. port(nullptr) {}
  772. void initIfNeeded(const LV2_URID uridAtomSequence)
  773. {
  774. if (capacity != 0)
  775. return;
  776. capacity = port->atom.size;
  777. port->atom.size = sizeof(LV2_Atom_Sequence_Body);
  778. port->atom.type = uridAtomSequence;
  779. port->body.unit = 0;
  780. port->body.pad = 0;
  781. }
  782. void growBy(const uint32_t size)
  783. {
  784. offset += size;
  785. port->atom.size += size;
  786. }
  787. void endRun()
  788. {
  789. capacity = 0;
  790. offset = 0;
  791. }
  792. } fEventsOutData;
  793. #endif
  794. // LV2 URIDs
  795. struct URIDs {
  796. LV2_URID atomBlank;
  797. LV2_URID atomObject;
  798. LV2_URID atomDouble;
  799. LV2_URID atomFloat;
  800. LV2_URID atomInt;
  801. LV2_URID atomLong;
  802. LV2_URID atomSequence;
  803. LV2_URID atomString;
  804. LV2_URID distrhoState;
  805. LV2_URID midiEvent;
  806. LV2_URID timePosition;
  807. LV2_URID timeBar;
  808. LV2_URID timeBarBeat;
  809. LV2_URID timeBeatUnit;
  810. LV2_URID timeBeatsPerBar;
  811. LV2_URID timeBeatsPerMinute;
  812. LV2_URID timeTicksPerBeat;
  813. LV2_URID timeFrame;
  814. LV2_URID timeSpeed;
  815. URIDs(const LV2_URID_Map* const uridMap)
  816. : atomBlank(uridMap->map(uridMap->handle, LV2_ATOM__Blank)),
  817. atomObject(uridMap->map(uridMap->handle, LV2_ATOM__Object)),
  818. atomDouble(uridMap->map(uridMap->handle, LV2_ATOM__Double)),
  819. atomFloat(uridMap->map(uridMap->handle, LV2_ATOM__Float)),
  820. atomInt(uridMap->map(uridMap->handle, LV2_ATOM__Int)),
  821. atomLong(uridMap->map(uridMap->handle, LV2_ATOM__Long)),
  822. atomSequence(uridMap->map(uridMap->handle, LV2_ATOM__Sequence)),
  823. atomString(uridMap->map(uridMap->handle, LV2_ATOM__String)),
  824. distrhoState(uridMap->map(uridMap->handle, DISTRHO_PLUGIN_LV2_STATE_PREFIX "KeyValueState")),
  825. midiEvent(uridMap->map(uridMap->handle, LV2_MIDI__MidiEvent)),
  826. timePosition(uridMap->map(uridMap->handle, LV2_TIME__Position)),
  827. timeBar(uridMap->map(uridMap->handle, LV2_TIME__bar)),
  828. timeBarBeat(uridMap->map(uridMap->handle, LV2_TIME__barBeat)),
  829. timeBeatUnit(uridMap->map(uridMap->handle, LV2_TIME__beatUnit)),
  830. timeBeatsPerBar(uridMap->map(uridMap->handle, LV2_TIME__beatsPerBar)),
  831. timeBeatsPerMinute(uridMap->map(uridMap->handle, LV2_TIME__beatsPerMinute)),
  832. timeTicksPerBeat(uridMap->map(uridMap->handle, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat)),
  833. timeFrame(uridMap->map(uridMap->handle, LV2_TIME__frame)),
  834. timeSpeed(uridMap->map(uridMap->handle, LV2_TIME__speed)) {}
  835. } fURIDs;
  836. // LV2 features
  837. const LV2_URID_Map* const fUridMap;
  838. const LV2_Worker_Schedule* const fWorker;
  839. #if DISTRHO_PLUGIN_WANT_STATE
  840. StringMap fStateMap;
  841. bool* fNeededUiSends;
  842. void setState(const char* const key, const char* const newValue)
  843. {
  844. fPlugin.setState(key, newValue);
  845. // check if we want to save this key
  846. if (! fPlugin.wantStateKey(key))
  847. return;
  848. // check if key already exists
  849. for (StringMap::iterator it=fStateMap.begin(), ite=fStateMap.end(); it != ite; ++it)
  850. {
  851. const String& dkey(it->first);
  852. if (dkey == key)
  853. {
  854. it->second = newValue;
  855. return;
  856. }
  857. }
  858. d_stderr("Failed to find plugin state with key \"%s\"", key);
  859. }
  860. #endif
  861. void updateParameterOutputsAndTriggers()
  862. {
  863. float curValue;
  864. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  865. {
  866. if (fPlugin.isParameterOutput(i))
  867. {
  868. curValue = fLastControlValues[i] = fPlugin.getParameterValue(i);
  869. if (fPortControls[i] != nullptr)
  870. *fPortControls[i] = curValue;
  871. }
  872. else if ((fPlugin.getParameterHints(i) & kParameterIsTrigger) == kParameterIsTrigger)
  873. {
  874. // NOTE: host is responsible for auto-updating control port buffers
  875. }
  876. }
  877. #if DISTRHO_PLUGIN_WANT_LATENCY
  878. if (fPortLatency != nullptr)
  879. *fPortLatency = fPlugin.getLatency();
  880. #endif
  881. }
  882. #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  883. bool writeMidi(const MidiEvent& midiEvent)
  884. {
  885. DISTRHO_SAFE_ASSERT_RETURN(fEventsOutData.port != nullptr, false);
  886. fEventsOutData.initIfNeeded(fURIDs.atomSequence);
  887. const uint32_t capacity = fEventsOutData.capacity;
  888. const uint32_t offset = fEventsOutData.offset;
  889. if (sizeof(LV2_Atom_Event) + midiEvent.size > capacity - offset)
  890. return false;
  891. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, fEventsOutData.port) + offset);
  892. aev->time.frames = midiEvent.frame;
  893. aev->body.type = fURIDs.midiEvent;
  894. aev->body.size = midiEvent.size;
  895. std::memcpy(LV2_ATOM_BODY(&aev->body),
  896. midiEvent.size > MidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data,
  897. midiEvent.size);
  898. fEventsOutData.growBy(lv2_atom_pad_size(sizeof(LV2_Atom_Event) + midiEvent.size));
  899. return true;
  900. }
  901. static bool writeMidiCallback(void* ptr, const MidiEvent& midiEvent)
  902. {
  903. return ((PluginLv2*)ptr)->writeMidi(midiEvent);
  904. }
  905. #endif
  906. };
  907. // -----------------------------------------------------------------------
  908. static LV2_Handle lv2_instantiate(const LV2_Descriptor*, double sampleRate, const char*, const LV2_Feature* const* features)
  909. {
  910. const LV2_Options_Option* options = nullptr;
  911. const LV2_URID_Map* uridMap = nullptr;
  912. const LV2_Worker_Schedule* worker = nullptr;
  913. for (int i=0; features[i] != nullptr; ++i)
  914. {
  915. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  916. options = (const LV2_Options_Option*)features[i]->data;
  917. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  918. uridMap = (const LV2_URID_Map*)features[i]->data;
  919. else if (std::strcmp(features[i]->URI, LV2_WORKER__schedule) == 0)
  920. worker = (const LV2_Worker_Schedule*)features[i]->data;
  921. }
  922. if (options == nullptr)
  923. {
  924. d_stderr("Options feature missing, cannot continue!");
  925. return nullptr;
  926. }
  927. if (uridMap == nullptr)
  928. {
  929. d_stderr("URID Map feature missing, cannot continue!");
  930. return nullptr;
  931. }
  932. #if DISTRHO_PLUGIN_WANT_STATE
  933. if (worker == nullptr)
  934. {
  935. d_stderr("Worker feature missing, cannot continue!");
  936. return nullptr;
  937. }
  938. #endif
  939. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  940. mod_license_check(features, DISTRHO_PLUGIN_URI);
  941. #endif
  942. d_lastBufferSize = 0;
  943. bool usingNominal = false;
  944. for (int i=0; options[i].key != 0; ++i)
  945. {
  946. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__nominalBlockLength))
  947. {
  948. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  949. {
  950. d_lastBufferSize = *(const int*)options[i].value;
  951. usingNominal = true;
  952. }
  953. else
  954. {
  955. d_stderr("Host provides nominalBlockLength but has wrong value type");
  956. }
  957. break;
  958. }
  959. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  960. {
  961. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  962. d_lastBufferSize = *(const int*)options[i].value;
  963. else
  964. d_stderr("Host provides maxBlockLength but has wrong value type");
  965. // no break, continue in case host supports nominalBlockLength
  966. }
  967. }
  968. if (d_lastBufferSize == 0)
  969. {
  970. d_stderr("Host does not provide nominalBlockLength or maxBlockLength options");
  971. d_lastBufferSize = 2048;
  972. }
  973. d_lastSampleRate = sampleRate;
  974. return new PluginLv2(sampleRate, uridMap, worker, usingNominal);
  975. }
  976. #define instancePtr ((PluginLv2*)instance)
  977. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  978. {
  979. instancePtr->lv2_connect_port(port, dataLocation);
  980. }
  981. static void lv2_activate(LV2_Handle instance)
  982. {
  983. instancePtr->lv2_activate();
  984. }
  985. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  986. {
  987. instancePtr->lv2_run(sampleCount);
  988. }
  989. static void lv2_deactivate(LV2_Handle instance)
  990. {
  991. instancePtr->lv2_deactivate();
  992. }
  993. static void lv2_cleanup(LV2_Handle instance)
  994. {
  995. delete instancePtr;
  996. }
  997. // -----------------------------------------------------------------------
  998. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  999. {
  1000. return instancePtr->lv2_get_options(options);
  1001. }
  1002. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  1003. {
  1004. return instancePtr->lv2_set_options(options);
  1005. }
  1006. // -----------------------------------------------------------------------
  1007. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  1008. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  1009. {
  1010. return instancePtr->lv2_get_program(index);
  1011. }
  1012. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  1013. {
  1014. instancePtr->lv2_select_program(bank, program);
  1015. }
  1016. #endif
  1017. // -----------------------------------------------------------------------
  1018. #if DISTRHO_PLUGIN_WANT_STATE
  1019. static LV2_State_Status lv2_save(LV2_Handle instance, LV2_State_Store_Function store, LV2_State_Handle handle, uint32_t, const LV2_Feature* const*)
  1020. {
  1021. return instancePtr->lv2_save(store, handle);
  1022. }
  1023. static LV2_State_Status lv2_restore(LV2_Handle instance, LV2_State_Retrieve_Function retrieve, LV2_State_Handle handle, uint32_t, const LV2_Feature* const*)
  1024. {
  1025. return instancePtr->lv2_restore(retrieve, handle);
  1026. }
  1027. LV2_Worker_Status lv2_work(LV2_Handle instance, LV2_Worker_Respond_Function, LV2_Worker_Respond_Handle, uint32_t, const void* data)
  1028. {
  1029. return instancePtr->lv2_work(data);
  1030. }
  1031. #endif
  1032. // -----------------------------------------------------------------------
  1033. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  1034. static void* lv2_get_instance_pointer(LV2_Handle instance)
  1035. {
  1036. return instancePtr->lv2_get_instance_pointer();
  1037. }
  1038. #endif
  1039. // -----------------------------------------------------------------------
  1040. static const void* lv2_extension_data(const char* uri)
  1041. {
  1042. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  1043. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  1044. return &options;
  1045. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  1046. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  1047. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  1048. return &programs;
  1049. #endif
  1050. #if DISTRHO_PLUGIN_WANT_STATE
  1051. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  1052. static const LV2_Worker_Interface worker = { lv2_work, nullptr, nullptr };
  1053. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  1054. return &state;
  1055. if (std::strcmp(uri, LV2_WORKER__interface) == 0)
  1056. return &worker;
  1057. #endif
  1058. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  1059. struct LV2_DirectAccess_Interface {
  1060. void* (*get_instance_pointer)(LV2_Handle handle);
  1061. };
  1062. static const LV2_DirectAccess_Interface directaccess = { lv2_get_instance_pointer };
  1063. if (std::strcmp(uri, DISTRHO_PLUGIN_LV2_STATE_PREFIX "direct-access") == 0)
  1064. return &directaccess;
  1065. #endif
  1066. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  1067. return mod_license_interface(uri);
  1068. #else
  1069. return nullptr;
  1070. #endif
  1071. }
  1072. #undef instancePtr
  1073. // -----------------------------------------------------------------------
  1074. static const LV2_Descriptor sLv2Descriptor = {
  1075. DISTRHO_PLUGIN_URI,
  1076. lv2_instantiate,
  1077. lv2_connect_port,
  1078. lv2_activate,
  1079. lv2_run,
  1080. lv2_deactivate,
  1081. lv2_cleanup,
  1082. lv2_extension_data
  1083. };
  1084. // -----------------------------------------------------------------------
  1085. END_NAMESPACE_DISTRHO
  1086. DISTRHO_PLUGIN_EXPORT
  1087. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  1088. {
  1089. USE_NAMESPACE_DISTRHO
  1090. return (index == 0) ? &sLv2Descriptor : nullptr;
  1091. }
  1092. // -----------------------------------------------------------------------