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.

1726 lines
59KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2022 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-forge.h"
  19. #include "lv2/atom-util.h"
  20. #include "lv2/buf-size.h"
  21. #include "lv2/data-access.h"
  22. #include "lv2/instance-access.h"
  23. #include "lv2/midi.h"
  24. #include "lv2/options.h"
  25. #include "lv2/parameters.h"
  26. #include "lv2/patch.h"
  27. #include "lv2/state.h"
  28. #include "lv2/time.h"
  29. #include "lv2/urid.h"
  30. #include "lv2/worker.h"
  31. #include "lv2/lv2_kxstudio_properties.h"
  32. #include "lv2/lv2_programs.h"
  33. #include "lv2/control-input-port-change-request.h"
  34. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  35. # include "libmodla.h"
  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)
  45. #define DISTRHO_LV2_USE_EVENTS_OUT (DISTRHO_PLUGIN_WANT_MIDI_OUTPUT || DISTRHO_PLUGIN_WANT_STATE)
  46. START_NAMESPACE_DISTRHO
  47. typedef std::map<const String, String> StringToStringMap;
  48. typedef std::map<const LV2_URID, String> UridToStringMap;
  49. #if ! DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  50. static const writeMidiFunc writeMidiCallback = nullptr;
  51. #endif
  52. #if ! DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST
  53. static const requestParameterValueChangeFunc requestParameterValueChangeCallback = nullptr;
  54. #endif
  55. #if ! DISTRHO_PLUGIN_WANT_STATE
  56. static const updateStateValueFunc updateStateValueCallback = nullptr;
  57. #endif
  58. // -----------------------------------------------------------------------
  59. class PluginLv2
  60. {
  61. public:
  62. PluginLv2(const double sampleRate,
  63. const LV2_URID_Map* const uridMap,
  64. const LV2_Worker_Schedule* const worker,
  65. const LV2_ControlInputPort_Change_Request* const ctrlInPortChangeReq,
  66. const bool usingNominal)
  67. : fPlugin(this, writeMidiCallback, requestParameterValueChangeCallback, updateStateValueCallback),
  68. fUsingNominal(usingNominal),
  69. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  70. fRunCount(0),
  71. #endif
  72. fPortControls(nullptr),
  73. fLastControlValues(nullptr),
  74. fSampleRate(sampleRate),
  75. fURIDs(uridMap),
  76. #if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST
  77. fCtrlInPortChangeReq(ctrlInPortChangeReq),
  78. #endif
  79. fUridMap(uridMap),
  80. fWorker(worker)
  81. {
  82. #if DISTRHO_PLUGIN_NUM_INPUTS > 0
  83. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_INPUTS; ++i)
  84. fPortAudioIns[i] = nullptr;
  85. #else
  86. fPortAudioIns = nullptr;
  87. #endif
  88. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  89. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  90. fPortAudioOuts[i] = nullptr;
  91. #else
  92. fPortAudioOuts = nullptr;
  93. #endif
  94. if (const uint32_t count = fPlugin.getParameterCount())
  95. {
  96. fPortControls = new float*[count];
  97. fLastControlValues = new float[count];
  98. for (uint32_t i=0; i < count; ++i)
  99. {
  100. fPortControls[i] = nullptr;
  101. fLastControlValues[i] = fPlugin.getParameterValue(i);
  102. }
  103. }
  104. else
  105. {
  106. fPortControls = nullptr;
  107. fLastControlValues = nullptr;
  108. }
  109. #if DISTRHO_LV2_USE_EVENTS_IN
  110. fPortEventsIn = nullptr;
  111. #endif
  112. #if DISTRHO_PLUGIN_WANT_LATENCY
  113. fPortLatency = nullptr;
  114. #endif
  115. #if DISTRHO_PLUGIN_WANT_STATE
  116. std::memset(&fAtomForge, 0, sizeof(fAtomForge));
  117. lv2_atom_forge_init(&fAtomForge, uridMap);
  118. if (const uint32_t count = fPlugin.getStateCount())
  119. {
  120. fUrids = new LV2_URID[count];
  121. fNeededUiSends = new bool[count];
  122. for (uint32_t i=0; i < count; ++i)
  123. {
  124. fNeededUiSends[i] = false;
  125. const String& statekey(fPlugin.getStateKey(i));
  126. fStateMap[statekey] = fPlugin.getStateDefaultValue(i);
  127. const String lv2key(DISTRHO_PLUGIN_URI "#" + statekey);
  128. const LV2_URID urid = fUrids[i] = uridMap->map(uridMap->handle, lv2key.buffer());
  129. fUridStateMap[urid] = statekey;
  130. }
  131. }
  132. else
  133. {
  134. fUrids = nullptr;
  135. fNeededUiSends = nullptr;
  136. }
  137. #else
  138. // unused
  139. (void)fWorker;
  140. #endif
  141. #if ! DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST
  142. // unused
  143. (void)ctrlInPortChangeReq;
  144. #endif
  145. }
  146. ~PluginLv2()
  147. {
  148. if (fPortControls != nullptr)
  149. {
  150. delete[] fPortControls;
  151. fPortControls = nullptr;
  152. }
  153. if (fLastControlValues)
  154. {
  155. delete[] fLastControlValues;
  156. fLastControlValues = nullptr;
  157. }
  158. #if DISTRHO_PLUGIN_WANT_STATE
  159. if (fNeededUiSends != nullptr)
  160. {
  161. delete[] fNeededUiSends;
  162. fNeededUiSends = nullptr;
  163. }
  164. if (fUrids != nullptr)
  165. {
  166. delete[] fUrids;
  167. fUrids = nullptr;
  168. }
  169. fStateMap.clear();
  170. #endif
  171. }
  172. // -------------------------------------------------------------------
  173. bool getPortControlValue(uint32_t index, float& value) const
  174. {
  175. if (const float* control = fPortControls[index])
  176. {
  177. switch (fPlugin.getParameterDesignation(index))
  178. {
  179. default:
  180. value = *control;
  181. break;
  182. case kParameterDesignationBypass:
  183. value = 1.0f - *control;
  184. break;
  185. }
  186. return true;
  187. }
  188. return false;
  189. }
  190. void setPortControlValue(uint32_t index, float value)
  191. {
  192. if (float* control = fPortControls[index])
  193. {
  194. switch (fPlugin.getParameterDesignation(index))
  195. {
  196. default:
  197. *control = value;
  198. break;
  199. case kParameterDesignationBypass:
  200. *control = 1.0f - value;
  201. break;
  202. }
  203. }
  204. }
  205. // -------------------------------------------------------------------
  206. void lv2_activate()
  207. {
  208. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  209. fTimePosition.clear();
  210. // hosts may not send all values, resulting on some invalid data, let's reset everything
  211. fTimePosition.bbt.bar = 1;
  212. fTimePosition.bbt.beat = 1;
  213. fTimePosition.bbt.tick = 0.0;
  214. fTimePosition.bbt.barStartTick = 0;
  215. fTimePosition.bbt.beatsPerBar = 4;
  216. fTimePosition.bbt.beatType = 4;
  217. fTimePosition.bbt.ticksPerBeat = 1920.0;
  218. fTimePosition.bbt.beatsPerMinute = 120.0;
  219. #endif
  220. fPlugin.activate();
  221. }
  222. void lv2_deactivate()
  223. {
  224. fPlugin.deactivate();
  225. }
  226. // -------------------------------------------------------------------
  227. void lv2_connect_port(const uint32_t port, void* const dataLocation)
  228. {
  229. uint32_t index = 0;
  230. #if DISTRHO_PLUGIN_NUM_INPUTS > 0
  231. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_INPUTS; ++i)
  232. {
  233. if (port == index++)
  234. {
  235. fPortAudioIns[i] = (const float*)dataLocation;
  236. return;
  237. }
  238. }
  239. #endif
  240. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  241. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  242. {
  243. if (port == index++)
  244. {
  245. fPortAudioOuts[i] = (float*)dataLocation;
  246. return;
  247. }
  248. }
  249. #endif
  250. #if DISTRHO_LV2_USE_EVENTS_IN
  251. if (port == index++)
  252. {
  253. fPortEventsIn = (LV2_Atom_Sequence*)dataLocation;
  254. return;
  255. }
  256. #endif
  257. #if DISTRHO_LV2_USE_EVENTS_OUT
  258. if (port == index++)
  259. {
  260. fEventsOutData.port = (LV2_Atom_Sequence*)dataLocation;
  261. return;
  262. }
  263. #endif
  264. #if DISTRHO_PLUGIN_WANT_LATENCY
  265. if (port == index++)
  266. {
  267. fPortLatency = (float*)dataLocation;
  268. return;
  269. }
  270. #endif
  271. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  272. {
  273. if (port == index++)
  274. {
  275. fPortControls[i] = (float*)dataLocation;
  276. return;
  277. }
  278. }
  279. }
  280. // -------------------------------------------------------------------
  281. void lv2_run(const uint32_t sampleCount)
  282. {
  283. // cache midi input and time position first
  284. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  285. uint32_t midiEventCount = 0;
  286. #endif
  287. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT || DISTRHO_PLUGIN_WANT_TIMEPOS
  288. LV2_ATOM_SEQUENCE_FOREACH(fPortEventsIn, event)
  289. {
  290. if (event == nullptr)
  291. break;
  292. # if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  293. if (event->body.type == fURIDs.midiEvent)
  294. {
  295. if (midiEventCount >= kMaxMidiEvents)
  296. continue;
  297. const uint8_t* const data((const uint8_t*)(event + 1));
  298. MidiEvent& midiEvent(fMidiEvents[midiEventCount++]);
  299. midiEvent.frame = event->time.frames;
  300. midiEvent.size = event->body.size;
  301. if (midiEvent.size > MidiEvent::kDataSize)
  302. {
  303. midiEvent.dataExt = data;
  304. std::memset(midiEvent.data, 0, MidiEvent::kDataSize);
  305. }
  306. else
  307. {
  308. midiEvent.dataExt = nullptr;
  309. std::memcpy(midiEvent.data, data, midiEvent.size);
  310. }
  311. continue;
  312. }
  313. # endif
  314. # if DISTRHO_PLUGIN_WANT_TIMEPOS
  315. if (event->body.type == fURIDs.atomBlank || event->body.type == fURIDs.atomObject)
  316. {
  317. const LV2_Atom_Object* const obj((const LV2_Atom_Object*)&event->body);
  318. if (obj->body.otype != fURIDs.timePosition)
  319. continue;
  320. LV2_Atom* bar = nullptr;
  321. LV2_Atom* barBeat = nullptr;
  322. LV2_Atom* beatUnit = nullptr;
  323. LV2_Atom* beatsPerBar = nullptr;
  324. LV2_Atom* beatsPerMinute = nullptr;
  325. LV2_Atom* frame = nullptr;
  326. LV2_Atom* speed = nullptr;
  327. LV2_Atom* ticksPerBeat = nullptr;
  328. lv2_atom_object_get(obj,
  329. fURIDs.timeBar, &bar,
  330. fURIDs.timeBarBeat, &barBeat,
  331. fURIDs.timeBeatUnit, &beatUnit,
  332. fURIDs.timeBeatsPerBar, &beatsPerBar,
  333. fURIDs.timeBeatsPerMinute, &beatsPerMinute,
  334. fURIDs.timeFrame, &frame,
  335. fURIDs.timeSpeed, &speed,
  336. fURIDs.timeTicksPerBeat, &ticksPerBeat,
  337. 0);
  338. // need to handle this first as other values depend on it
  339. if (ticksPerBeat != nullptr)
  340. {
  341. /**/ if (ticksPerBeat->type == fURIDs.atomDouble)
  342. fLastPositionData.ticksPerBeat = ((LV2_Atom_Double*)ticksPerBeat)->body;
  343. else if (ticksPerBeat->type == fURIDs.atomFloat)
  344. fLastPositionData.ticksPerBeat = ((LV2_Atom_Float*)ticksPerBeat)->body;
  345. else if (ticksPerBeat->type == fURIDs.atomInt)
  346. fLastPositionData.ticksPerBeat = ((LV2_Atom_Int*)ticksPerBeat)->body;
  347. else if (ticksPerBeat->type == fURIDs.atomLong)
  348. fLastPositionData.ticksPerBeat = ((LV2_Atom_Long*)ticksPerBeat)->body;
  349. else
  350. d_stderr("Unknown lv2 ticksPerBeat value type");
  351. if (fLastPositionData.ticksPerBeat > 0.0)
  352. fTimePosition.bbt.ticksPerBeat = fLastPositionData.ticksPerBeat;
  353. }
  354. // same
  355. if (speed != nullptr)
  356. {
  357. /**/ if (speed->type == fURIDs.atomDouble)
  358. fLastPositionData.speed = ((LV2_Atom_Double*)speed)->body;
  359. else if (speed->type == fURIDs.atomFloat)
  360. fLastPositionData.speed = ((LV2_Atom_Float*)speed)->body;
  361. else if (speed->type == fURIDs.atomInt)
  362. fLastPositionData.speed = ((LV2_Atom_Int*)speed)->body;
  363. else if (speed->type == fURIDs.atomLong)
  364. fLastPositionData.speed = ((LV2_Atom_Long*)speed)->body;
  365. else
  366. d_stderr("Unknown lv2 speed value type");
  367. fTimePosition.playing = d_isNotZero(fLastPositionData.speed);
  368. }
  369. if (bar != nullptr)
  370. {
  371. /**/ if (bar->type == fURIDs.atomDouble)
  372. fLastPositionData.bar = ((LV2_Atom_Double*)bar)->body;
  373. else if (bar->type == fURIDs.atomFloat)
  374. fLastPositionData.bar = ((LV2_Atom_Float*)bar)->body;
  375. else if (bar->type == fURIDs.atomInt)
  376. fLastPositionData.bar = ((LV2_Atom_Int*)bar)->body;
  377. else if (bar->type == fURIDs.atomLong)
  378. fLastPositionData.bar = ((LV2_Atom_Long*)bar)->body;
  379. else
  380. d_stderr("Unknown lv2 bar value type");
  381. if (fLastPositionData.bar >= 0)
  382. fTimePosition.bbt.bar = fLastPositionData.bar + 1;
  383. }
  384. if (barBeat != nullptr)
  385. {
  386. /**/ if (barBeat->type == fURIDs.atomDouble)
  387. fLastPositionData.barBeat = ((LV2_Atom_Double*)barBeat)->body;
  388. else if (barBeat->type == fURIDs.atomFloat)
  389. fLastPositionData.barBeat = ((LV2_Atom_Float*)barBeat)->body;
  390. else if (barBeat->type == fURIDs.atomInt)
  391. fLastPositionData.barBeat = ((LV2_Atom_Int*)barBeat)->body;
  392. else if (barBeat->type == fURIDs.atomLong)
  393. fLastPositionData.barBeat = ((LV2_Atom_Long*)barBeat)->body;
  394. else
  395. d_stderr("Unknown lv2 barBeat value type");
  396. if (fLastPositionData.barBeat >= 0.0f)
  397. {
  398. const double rest = std::fmod(fLastPositionData.barBeat, 1.0f);
  399. fTimePosition.bbt.beat = std::round(fLastPositionData.barBeat - rest + 1.0);
  400. fTimePosition.bbt.tick = rest * fTimePosition.bbt.ticksPerBeat;
  401. }
  402. }
  403. if (beatUnit != nullptr)
  404. {
  405. /**/ if (beatUnit->type == fURIDs.atomDouble)
  406. fLastPositionData.beatUnit = ((LV2_Atom_Double*)beatUnit)->body;
  407. else if (beatUnit->type == fURIDs.atomFloat)
  408. fLastPositionData.beatUnit = ((LV2_Atom_Float*)beatUnit)->body;
  409. else if (beatUnit->type == fURIDs.atomInt)
  410. fLastPositionData.beatUnit = ((LV2_Atom_Int*)beatUnit)->body;
  411. else if (beatUnit->type == fURIDs.atomLong)
  412. fLastPositionData.beatUnit = ((LV2_Atom_Long*)beatUnit)->body;
  413. else
  414. d_stderr("Unknown lv2 beatUnit value type");
  415. if (fLastPositionData.beatUnit > 0)
  416. fTimePosition.bbt.beatType = fLastPositionData.beatUnit;
  417. }
  418. if (beatsPerBar != nullptr)
  419. {
  420. /**/ if (beatsPerBar->type == fURIDs.atomDouble)
  421. fLastPositionData.beatsPerBar = ((LV2_Atom_Double*)beatsPerBar)->body;
  422. else if (beatsPerBar->type == fURIDs.atomFloat)
  423. fLastPositionData.beatsPerBar = ((LV2_Atom_Float*)beatsPerBar)->body;
  424. else if (beatsPerBar->type == fURIDs.atomInt)
  425. fLastPositionData.beatsPerBar = ((LV2_Atom_Int*)beatsPerBar)->body;
  426. else if (beatsPerBar->type == fURIDs.atomLong)
  427. fLastPositionData.beatsPerBar = ((LV2_Atom_Long*)beatsPerBar)->body;
  428. else
  429. d_stderr("Unknown lv2 beatsPerBar value type");
  430. if (fLastPositionData.beatsPerBar > 0.0f)
  431. fTimePosition.bbt.beatsPerBar = fLastPositionData.beatsPerBar;
  432. }
  433. if (beatsPerMinute != nullptr)
  434. {
  435. /**/ if (beatsPerMinute->type == fURIDs.atomDouble)
  436. fLastPositionData.beatsPerMinute = ((LV2_Atom_Double*)beatsPerMinute)->body;
  437. else if (beatsPerMinute->type == fURIDs.atomFloat)
  438. fLastPositionData.beatsPerMinute = ((LV2_Atom_Float*)beatsPerMinute)->body;
  439. else if (beatsPerMinute->type == fURIDs.atomInt)
  440. fLastPositionData.beatsPerMinute = ((LV2_Atom_Int*)beatsPerMinute)->body;
  441. else if (beatsPerMinute->type == fURIDs.atomLong)
  442. fLastPositionData.beatsPerMinute = ((LV2_Atom_Long*)beatsPerMinute)->body;
  443. else
  444. d_stderr("Unknown lv2 beatsPerMinute value type");
  445. if (fLastPositionData.beatsPerMinute > 0.0f)
  446. {
  447. fTimePosition.bbt.beatsPerMinute = fLastPositionData.beatsPerMinute;
  448. if (d_isNotZero(fLastPositionData.speed))
  449. fTimePosition.bbt.beatsPerMinute *= std::abs(fLastPositionData.speed);
  450. }
  451. }
  452. if (frame != nullptr)
  453. {
  454. /**/ if (frame->type == fURIDs.atomDouble)
  455. fLastPositionData.frame = ((LV2_Atom_Double*)frame)->body;
  456. else if (frame->type == fURIDs.atomFloat)
  457. fLastPositionData.frame = ((LV2_Atom_Float*)frame)->body;
  458. else if (frame->type == fURIDs.atomInt)
  459. fLastPositionData.frame = ((LV2_Atom_Int*)frame)->body;
  460. else if (frame->type == fURIDs.atomLong)
  461. fLastPositionData.frame = ((LV2_Atom_Long*)frame)->body;
  462. else
  463. d_stderr("Unknown lv2 frame value type");
  464. if (fLastPositionData.frame >= 0)
  465. fTimePosition.frame = fLastPositionData.frame;
  466. }
  467. fTimePosition.bbt.barStartTick = fTimePosition.bbt.ticksPerBeat*
  468. fTimePosition.bbt.beatsPerBar*
  469. (fTimePosition.bbt.bar-1);
  470. fTimePosition.bbt.valid = (fLastPositionData.beatsPerMinute > 0.0 &&
  471. fLastPositionData.beatUnit > 0 &&
  472. fLastPositionData.beatsPerBar > 0.0f);
  473. fPlugin.setTimePosition(fTimePosition);
  474. continue;
  475. }
  476. # endif
  477. }
  478. #endif
  479. // check for messages from UI or host
  480. #if DISTRHO_PLUGIN_WANT_STATE
  481. LV2_ATOM_SEQUENCE_FOREACH(fPortEventsIn, event)
  482. {
  483. if (event == nullptr)
  484. break;
  485. #if DISTRHO_PLUGIN_HAS_UI
  486. if (event->body.type == fURIDs.dpfKeyValue)
  487. {
  488. const void* const data = (const void*)(event + 1);
  489. // check if this is our special message
  490. if (std::strcmp((const char*)data, "__dpf_ui_data__") == 0)
  491. {
  492. for (uint32_t i=0, count=fPlugin.getStateCount(); i < count; ++i)
  493. {
  494. if (fPlugin.getStateHints(i) & kStateIsOnlyForDSP)
  495. continue;
  496. fNeededUiSends[i] = true;
  497. }
  498. }
  499. // no, send to DSP as usual
  500. else if (fWorker != nullptr)
  501. {
  502. fWorker->schedule_work(fWorker->handle, sizeof(LV2_Atom)+event->body.size, &event->body);
  503. }
  504. }
  505. else
  506. #endif
  507. if (event->body.type == fURIDs.atomObject && fWorker != nullptr)
  508. {
  509. const LV2_Atom_Object* const object = (const LV2_Atom_Object*)&event->body;
  510. const LV2_Atom* property = nullptr;
  511. const LV2_Atom* value = nullptr;
  512. lv2_atom_object_get(object, fURIDs.patchProperty, &property, fURIDs.patchValue, &value, nullptr);
  513. if (property != nullptr && property->type == fURIDs.atomURID &&
  514. value != nullptr && (value->type == fURIDs.atomPath || value->type == fURIDs.atomString))
  515. {
  516. fWorker->schedule_work(fWorker->handle, sizeof(LV2_Atom)+event->body.size, &event->body);
  517. }
  518. }
  519. }
  520. #endif
  521. // Check for updated parameters
  522. float curValue;
  523. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  524. {
  525. if (!getPortControlValue(i, curValue))
  526. continue;
  527. if (fPlugin.isParameterInput(i) && d_isNotEqual(fLastControlValues[i], curValue))
  528. {
  529. fLastControlValues[i] = curValue;
  530. fPlugin.setParameterValue(i, curValue);
  531. }
  532. }
  533. // Run plugin
  534. if (sampleCount != 0)
  535. {
  536. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  537. fRunCount = mod_license_run_begin(fRunCount, sampleCount);
  538. #endif
  539. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  540. fPlugin.run(fPortAudioIns, fPortAudioOuts, sampleCount, fMidiEvents, midiEventCount);
  541. #else
  542. fPlugin.run(fPortAudioIns, fPortAudioOuts, sampleCount);
  543. #endif
  544. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  545. for (uint32_t i=0; i<DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  546. mod_license_run_silence(fRunCount, fPortAudioOuts[i], sampleCount, i);
  547. #endif
  548. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  549. // update timePos for next callback
  550. if (d_isNotZero(fLastPositionData.speed))
  551. {
  552. if (fLastPositionData.speed > 0.0)
  553. {
  554. // playing forwards
  555. fLastPositionData.frame += sampleCount;
  556. }
  557. else
  558. {
  559. // playing backwards
  560. fLastPositionData.frame -= sampleCount;
  561. if (fLastPositionData.frame < 0)
  562. fLastPositionData.frame = 0;
  563. }
  564. fTimePosition.frame = fLastPositionData.frame;
  565. if (fTimePosition.bbt.valid)
  566. {
  567. const double beatsPerMinute = fLastPositionData.beatsPerMinute * fLastPositionData.speed;
  568. const double framesPerBeat = 60.0 * fSampleRate / beatsPerMinute;
  569. const double addedBarBeats = double(sampleCount) / framesPerBeat;
  570. if (fLastPositionData.barBeat >= 0.0f)
  571. {
  572. fLastPositionData.barBeat = std::fmod(fLastPositionData.barBeat+addedBarBeats,
  573. (double)fLastPositionData.beatsPerBar);
  574. const double rest = std::fmod(fLastPositionData.barBeat, 1.0f);
  575. fTimePosition.bbt.beat = std::round(fLastPositionData.barBeat - rest + 1.0);
  576. fTimePosition.bbt.tick = rest * fTimePosition.bbt.ticksPerBeat;
  577. if (fLastPositionData.bar >= 0)
  578. {
  579. fLastPositionData.bar += std::floor((fLastPositionData.barBeat+addedBarBeats)/
  580. fLastPositionData.beatsPerBar);
  581. if (fLastPositionData.bar < 0)
  582. fLastPositionData.bar = 0;
  583. fTimePosition.bbt.bar = fLastPositionData.bar + 1;
  584. fTimePosition.bbt.barStartTick = fTimePosition.bbt.ticksPerBeat*
  585. fTimePosition.bbt.beatsPerBar*
  586. (fTimePosition.bbt.bar-1);
  587. }
  588. }
  589. fTimePosition.bbt.beatsPerMinute = std::abs(beatsPerMinute);
  590. }
  591. fPlugin.setTimePosition(fTimePosition);
  592. }
  593. #endif
  594. }
  595. updateParameterOutputsAndTriggers();
  596. #if DISTRHO_PLUGIN_WANT_STATE
  597. fEventsOutData.initIfNeeded(fURIDs.atomSequence);
  598. LV2_Atom_Event* aev;
  599. const uint32_t capacity = fEventsOutData.capacity;
  600. for (uint32_t i=0, count=fPlugin.getStateCount(); i < count; ++i)
  601. {
  602. if (! fNeededUiSends[i])
  603. continue;
  604. const uint32_t hints = fPlugin.getStateHints(i);
  605. #if ! DISTRHO_PLUGIN_HAS_UI
  606. if ((hints & kStateIsHostReadable) == 0x0)
  607. {
  608. fNeededUiSends[i] = false;
  609. continue;
  610. }
  611. #endif
  612. const String& curKey(fPlugin.getStateKey(i));
  613. for (StringToStringMap::const_iterator cit=fStateMap.begin(), cite=fStateMap.end(); cit != cite; ++cit)
  614. {
  615. const String& key(cit->first);
  616. if (curKey != key)
  617. continue;
  618. const String& value(cit->second);
  619. // set msg size
  620. uint32_t msgSize;
  621. if (hints & kStateIsHostReadable)
  622. {
  623. // object, prop key, prop urid, value key, value
  624. msgSize = sizeof(LV2_Atom_Object)
  625. + sizeof(LV2_Atom_Property_Body) * 4
  626. + sizeof(LV2_Atom_URID) * 3
  627. + sizeof(LV2_Atom_String)
  628. + value.length() + 1;
  629. }
  630. else
  631. {
  632. // key + value + 2x null terminator + separator
  633. msgSize = static_cast<uint32_t>(key.length()+value.length())+3U;
  634. }
  635. if (sizeof(LV2_Atom_Event) + msgSize > capacity - fEventsOutData.offset)
  636. {
  637. d_stdout("Sending key '%s' to UI failed, out of space (needs %u bytes)",
  638. key.buffer(), msgSize);
  639. break;
  640. }
  641. // put data
  642. aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, fEventsOutData.port) + fEventsOutData.offset);
  643. aev->time.frames = 0;
  644. if (hints & kStateIsHostReadable)
  645. {
  646. uint8_t* const msgBuf = (uint8_t*)&aev->body;
  647. LV2_Atom_Forge atomForge = fAtomForge;
  648. lv2_atom_forge_set_buffer(&atomForge, msgBuf, msgSize);
  649. LV2_Atom_Forge_Frame forgeFrame;
  650. lv2_atom_forge_object(&atomForge, &forgeFrame, 0, fURIDs.patchSet);
  651. lv2_atom_forge_key(&atomForge, fURIDs.patchProperty);
  652. lv2_atom_forge_urid(&atomForge, fUrids[i]);
  653. lv2_atom_forge_key(&atomForge, fURIDs.patchValue);
  654. if ((hints & kStateIsFilenamePath) == kStateIsFilenamePath)
  655. lv2_atom_forge_path(&atomForge, value.buffer(), static_cast<uint32_t>(value.length()+1));
  656. else
  657. lv2_atom_forge_string(&atomForge, value.buffer(), static_cast<uint32_t>(value.length()+1));
  658. lv2_atom_forge_pop(&atomForge, &forgeFrame);
  659. msgSize = ((LV2_Atom*)msgBuf)->size;
  660. }
  661. else
  662. {
  663. aev->body.type = fURIDs.dpfKeyValue;
  664. aev->body.size = msgSize;
  665. uint8_t* const msgBuf = LV2_ATOM_BODY(&aev->body);
  666. std::memset(msgBuf, 0, msgSize);
  667. // write key and value in atom buffer
  668. std::memcpy(msgBuf, key.buffer(), key.length()+1);
  669. std::memcpy(msgBuf+(key.length()+1), value.buffer(), value.length()+1);
  670. }
  671. fEventsOutData.growBy(lv2_atom_pad_size(sizeof(LV2_Atom_Event) + msgSize));
  672. fNeededUiSends[i] = false;
  673. break;
  674. }
  675. }
  676. #endif
  677. #if DISTRHO_LV2_USE_EVENTS_OUT
  678. fEventsOutData.endRun();
  679. #endif
  680. }
  681. // -------------------------------------------------------------------
  682. uint32_t lv2_get_options(LV2_Options_Option* const /*options*/)
  683. {
  684. // currently unused
  685. return LV2_OPTIONS_ERR_UNKNOWN;
  686. }
  687. uint32_t lv2_set_options(const LV2_Options_Option* const options)
  688. {
  689. for (int i=0; options[i].key != 0; ++i)
  690. {
  691. if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__nominalBlockLength))
  692. {
  693. if (options[i].type == fURIDs.atomInt)
  694. {
  695. const int32_t bufferSize(*(const int32_t*)options[i].value);
  696. fPlugin.setBufferSize(bufferSize, true);
  697. }
  698. else
  699. {
  700. d_stderr("Host changed nominalBlockLength but with wrong value type");
  701. }
  702. }
  703. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__maxBlockLength) && ! fUsingNominal)
  704. {
  705. if (options[i].type == fURIDs.atomInt)
  706. {
  707. const int32_t bufferSize(*(const int32_t*)options[i].value);
  708. fPlugin.setBufferSize(bufferSize, true);
  709. }
  710. else
  711. {
  712. d_stderr("Host changed maxBlockLength but with wrong value type");
  713. }
  714. }
  715. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_PARAMETERS__sampleRate))
  716. {
  717. if (options[i].type == fURIDs.atomFloat)
  718. {
  719. const float sampleRate(*(const float*)options[i].value);
  720. fSampleRate = sampleRate;
  721. fPlugin.setSampleRate(sampleRate, true);
  722. }
  723. else
  724. {
  725. d_stderr("Host changed sampleRate but with wrong value type");
  726. }
  727. }
  728. }
  729. return LV2_OPTIONS_SUCCESS;
  730. }
  731. // -------------------------------------------------------------------
  732. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  733. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index)
  734. {
  735. if (index >= fPlugin.getProgramCount())
  736. return nullptr;
  737. static LV2_Program_Descriptor desc;
  738. desc.bank = index / 128;
  739. desc.program = index % 128;
  740. desc.name = fPlugin.getProgramName(index);
  741. return &desc;
  742. }
  743. void lv2_select_program(const uint32_t bank, const uint32_t program)
  744. {
  745. const uint32_t realProgram(bank * 128 + program);
  746. if (realProgram >= fPlugin.getProgramCount())
  747. return;
  748. fPlugin.loadProgram(realProgram);
  749. // Update control inputs
  750. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  751. {
  752. if (fPlugin.isParameterOutput(i))
  753. continue;
  754. fLastControlValues[i] = fPlugin.getParameterValue(i);
  755. setPortControlValue(i, fLastControlValues[i]);
  756. }
  757. #if DISTRHO_PLUGIN_WANT_FULL_STATE
  758. // Update state
  759. for (StringToStringMap::const_iterator cit=fStateMap.begin(), cite=fStateMap.end(); cit != cite; ++cit)
  760. {
  761. const String& key = cit->first;
  762. fStateMap[key] = fPlugin.getStateValue(key);
  763. }
  764. #endif
  765. }
  766. #endif
  767. // -------------------------------------------------------------------
  768. #if DISTRHO_PLUGIN_WANT_STATE
  769. LV2_State_Status lv2_save(const LV2_State_Store_Function store,
  770. const LV2_State_Handle handle,
  771. const LV2_Feature* const* const features)
  772. {
  773. #if DISTRHO_PLUGIN_WANT_FULL_STATE
  774. // Update current state
  775. for (StringToStringMap::const_iterator cit=fStateMap.begin(), cite=fStateMap.end(); cit != cite; ++cit)
  776. {
  777. const String& key = cit->first;
  778. fStateMap[key] = fPlugin.getStateValue(key);
  779. }
  780. #endif
  781. String lv2key;
  782. LV2_URID urid;
  783. for (uint32_t i=0, count=fPlugin.getStateCount(); i < count; ++i)
  784. {
  785. const String& curKey(fPlugin.getStateKey(i));
  786. for (StringToStringMap::const_iterator cit=fStateMap.begin(), cite=fStateMap.end(); cit != cite; ++cit)
  787. {
  788. const String& key(cit->first);
  789. if (curKey != key)
  790. continue;
  791. const uint32_t hints = fPlugin.getStateHints(i);
  792. #if ! DISTRHO_PLUGIN_HAS_UI && ! DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  793. // do not save UI-only messages if there is no UI available
  794. if (hints & kStateIsOnlyForUI)
  795. break;
  796. #endif
  797. if (hints & kStateIsHostReadable)
  798. {
  799. lv2key = DISTRHO_PLUGIN_URI "#";
  800. urid = (hints & kStateIsFilenamePath) == kStateIsFilenamePath
  801. ? fURIDs.atomPath
  802. : fURIDs.atomString;
  803. }
  804. else
  805. {
  806. lv2key = DISTRHO_PLUGIN_LV2_STATE_PREFIX;
  807. urid = fURIDs.atomString;
  808. }
  809. lv2key += key;
  810. const String& value(cit->second);
  811. if (urid == fURIDs.atomPath)
  812. {
  813. const LV2_State_Map_Path* mapPath = nullptr;
  814. const LV2_State_Free_Path* freePath = nullptr;
  815. for (int i=0; features[i] != nullptr; ++i)
  816. {
  817. if (std::strcmp(features[i]->URI, LV2_STATE__mapPath) == 0)
  818. mapPath = (const LV2_State_Map_Path*)features[i]->data;
  819. else if (std::strcmp(features[i]->URI, LV2_STATE__freePath) == 0)
  820. freePath = (const LV2_State_Free_Path*)features[i]->data;
  821. }
  822. if (char* const abstractPath = mapPath != nullptr
  823. ? mapPath->abstract_path(mapPath->handle, value.buffer())
  824. : nullptr)
  825. {
  826. // some hosts need +1 for the null terminator, even though the type is string/path
  827. store(handle,
  828. fUridMap->map(fUridMap->handle, lv2key.buffer()),
  829. abstractPath,
  830. std::strlen(abstractPath)+1,
  831. urid,
  832. LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  833. if (freePath != nullptr)
  834. freePath->free_path(freePath->handle, abstractPath);
  835. #ifndef DISTRHO_OS_WINDOWS
  836. else
  837. std::free(abstractPath);
  838. #endif
  839. break;
  840. }
  841. }
  842. // some hosts need +1 for the null terminator, even though the type is string
  843. store(handle,
  844. fUridMap->map(fUridMap->handle, lv2key.buffer()),
  845. value.buffer(),
  846. value.length()+1,
  847. urid,
  848. LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE);
  849. break;
  850. }
  851. }
  852. return LV2_STATE_SUCCESS;
  853. }
  854. LV2_State_Status lv2_restore(const LV2_State_Retrieve_Function retrieve,
  855. const LV2_State_Handle handle,
  856. const LV2_Feature* const* const features)
  857. {
  858. size_t size;
  859. uint32_t type, flags;
  860. String lv2key;
  861. LV2_URID urid;
  862. for (uint32_t i=0, count=fPlugin.getStateCount(); i < count; ++i)
  863. {
  864. const String& key(fPlugin.getStateKey(i));
  865. const uint32_t hints = fPlugin.getStateHints(i);
  866. if (hints & kStateIsHostReadable)
  867. {
  868. lv2key = DISTRHO_PLUGIN_URI "#";
  869. urid = (hints & kStateIsFilenamePath) == kStateIsFilenamePath
  870. ? fURIDs.atomPath
  871. : fURIDs.atomString;
  872. }
  873. else
  874. {
  875. lv2key = DISTRHO_PLUGIN_LV2_STATE_PREFIX;
  876. urid = fURIDs.atomString;
  877. }
  878. lv2key += key;
  879. size = 0;
  880. type = 0;
  881. flags = LV2_STATE_IS_POD|LV2_STATE_IS_PORTABLE;
  882. const void* data = retrieve(handle,
  883. fUridMap->map(fUridMap->handle, lv2key.buffer()),
  884. &size, &type, &flags);
  885. if (data == nullptr || size == 0)
  886. continue;
  887. DISTRHO_SAFE_ASSERT_CONTINUE(type == urid);
  888. const char* const value = (const char*)data;
  889. const std::size_t length = std::strlen(value);
  890. DISTRHO_SAFE_ASSERT_CONTINUE(length == size || length+1 == size);
  891. if (urid == fURIDs.atomPath)
  892. {
  893. const LV2_State_Map_Path* mapPath = nullptr;
  894. const LV2_State_Free_Path* freePath = nullptr;
  895. for (int i=0; features[i] != nullptr; ++i)
  896. {
  897. if (std::strcmp(features[i]->URI, LV2_STATE__mapPath) == 0)
  898. mapPath = (const LV2_State_Map_Path*)features[i]->data;
  899. else if (std::strcmp(features[i]->URI, LV2_STATE__freePath) == 0)
  900. freePath = (const LV2_State_Free_Path*)features[i]->data;
  901. }
  902. if (char* const absolutePath = mapPath != nullptr
  903. ? mapPath->absolute_path(mapPath->handle, value)
  904. : nullptr)
  905. {
  906. setState(key, absolutePath);
  907. if (freePath != nullptr)
  908. freePath->free_path(freePath->handle, absolutePath);
  909. #ifndef DISTRHO_OS_WINDOWS
  910. else
  911. std::free(absolutePath);
  912. #endif
  913. // signal msg needed for UI
  914. fNeededUiSends[i] = true;
  915. continue;
  916. }
  917. }
  918. setState(key, value);
  919. #if DISTRHO_PLUGIN_WANT_STATE
  920. // signal msg needed for UI
  921. if ((hints & kStateIsOnlyForDSP) == 0x0)
  922. fNeededUiSends[i] = true;
  923. #endif
  924. }
  925. return LV2_STATE_SUCCESS;
  926. }
  927. // -------------------------------------------------------------------
  928. LV2_Worker_Status lv2_work(const void* const data)
  929. {
  930. const LV2_Atom* const eventBody = (const LV2_Atom*)data;
  931. if (eventBody->type == fURIDs.dpfKeyValue)
  932. {
  933. const char* const key = (const char*)(eventBody + 1);
  934. const char* const value = key + (std::strlen(key) + 1U);
  935. setState(key, value);
  936. return LV2_WORKER_SUCCESS;
  937. }
  938. if (eventBody->type == fURIDs.atomObject)
  939. {
  940. const LV2_Atom_Object* const object = (const LV2_Atom_Object*)eventBody;
  941. const LV2_Atom* property = nullptr;
  942. const LV2_Atom* value = nullptr;
  943. lv2_atom_object_get(object, fURIDs.patchProperty, &property, fURIDs.patchValue, &value, nullptr);
  944. DISTRHO_SAFE_ASSERT_RETURN(property != nullptr, LV2_WORKER_ERR_UNKNOWN);
  945. DISTRHO_SAFE_ASSERT_RETURN(property->type == fURIDs.atomURID, LV2_WORKER_ERR_UNKNOWN);
  946. DISTRHO_SAFE_ASSERT_RETURN(value != nullptr, LV2_WORKER_ERR_UNKNOWN);
  947. DISTRHO_SAFE_ASSERT_RETURN(value->type == fURIDs.atomPath ||
  948. value->type == fURIDs.atomString, LV2_WORKER_ERR_UNKNOWN);
  949. const LV2_URID urid = ((const LV2_Atom_URID*)property)->body;
  950. const char* const filename = (const char*)(value + 1);
  951. String key;
  952. try {
  953. key = fUridStateMap[urid];
  954. } DISTRHO_SAFE_EXCEPTION_RETURN("lv2_work fUridStateMap[urid]", LV2_WORKER_ERR_UNKNOWN);
  955. setState(key, filename);
  956. /* FIXME host should be responsible for updating UI side, not us
  957. for (uint32_t i=0, count=fPlugin.getStateCount(); i < count; ++i)
  958. {
  959. if (fPlugin.getStateKey(i) == key)
  960. {
  961. if ((fPlugin.getStateHints(i) & kStateIsOnlyForDSP) == 0x0)
  962. fNeededUiSends[i] = true;
  963. break;
  964. }
  965. }
  966. */
  967. return LV2_WORKER_SUCCESS;
  968. }
  969. return LV2_WORKER_ERR_UNKNOWN;
  970. }
  971. LV2_Worker_Status lv2_work_response(uint32_t, const void*)
  972. {
  973. return LV2_WORKER_SUCCESS;
  974. }
  975. #endif
  976. // -------------------------------------------------------------------
  977. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  978. void* lv2_get_instance_pointer()
  979. {
  980. return fPlugin.getInstancePointer();
  981. }
  982. #endif
  983. // -------------------------------------------------------------------
  984. private:
  985. PluginExporter fPlugin;
  986. const bool fUsingNominal; // if false use maxBlockLength
  987. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  988. uint32_t fRunCount;
  989. #endif
  990. // LV2 ports
  991. #if DISTRHO_PLUGIN_NUM_INPUTS > 0
  992. const float* fPortAudioIns[DISTRHO_PLUGIN_NUM_INPUTS];
  993. #else
  994. const float** fPortAudioIns;
  995. #endif
  996. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  997. float* fPortAudioOuts[DISTRHO_PLUGIN_NUM_OUTPUTS];
  998. #else
  999. float** fPortAudioOuts;
  1000. #endif
  1001. float** fPortControls;
  1002. #if DISTRHO_LV2_USE_EVENTS_IN
  1003. LV2_Atom_Sequence* fPortEventsIn;
  1004. #endif
  1005. #if DISTRHO_PLUGIN_WANT_LATENCY
  1006. float* fPortLatency;
  1007. #endif
  1008. // Temporary data
  1009. float* fLastControlValues;
  1010. double fSampleRate;
  1011. #if DISTRHO_PLUGIN_WANT_MIDI_INPUT
  1012. MidiEvent fMidiEvents[kMaxMidiEvents];
  1013. #endif
  1014. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  1015. TimePosition fTimePosition;
  1016. struct Lv2PositionData {
  1017. int64_t bar;
  1018. float barBeat;
  1019. uint32_t beatUnit;
  1020. float beatsPerBar;
  1021. float beatsPerMinute;
  1022. int64_t frame;
  1023. double speed;
  1024. double ticksPerBeat;
  1025. Lv2PositionData()
  1026. : bar(-1),
  1027. barBeat(-1.0f),
  1028. beatUnit(0),
  1029. beatsPerBar(0.0f),
  1030. beatsPerMinute(0.0f),
  1031. frame(-1),
  1032. speed(0.0),
  1033. ticksPerBeat(-1.0) {}
  1034. } fLastPositionData;
  1035. #endif
  1036. #if DISTRHO_LV2_USE_EVENTS_OUT
  1037. struct Lv2EventsOutData {
  1038. uint32_t capacity, offset;
  1039. LV2_Atom_Sequence* port;
  1040. Lv2EventsOutData()
  1041. : capacity(0),
  1042. offset(0),
  1043. port(nullptr) {}
  1044. void initIfNeeded(const LV2_URID uridAtomSequence)
  1045. {
  1046. if (capacity != 0)
  1047. return;
  1048. capacity = port->atom.size;
  1049. port->atom.size = sizeof(LV2_Atom_Sequence_Body);
  1050. port->atom.type = uridAtomSequence;
  1051. port->body.unit = 0;
  1052. port->body.pad = 0;
  1053. }
  1054. void growBy(const uint32_t size)
  1055. {
  1056. offset += size;
  1057. port->atom.size += size;
  1058. }
  1059. void endRun()
  1060. {
  1061. capacity = 0;
  1062. offset = 0;
  1063. }
  1064. } fEventsOutData;
  1065. #endif
  1066. // LV2 URIDs
  1067. struct URIDs {
  1068. const LV2_URID_Map* _uridMap;
  1069. LV2_URID atomBlank;
  1070. LV2_URID atomObject;
  1071. LV2_URID atomDouble;
  1072. LV2_URID atomFloat;
  1073. LV2_URID atomInt;
  1074. LV2_URID atomLong;
  1075. LV2_URID atomPath;
  1076. LV2_URID atomSequence;
  1077. LV2_URID atomString;
  1078. LV2_URID atomURID;
  1079. LV2_URID dpfKeyValue;
  1080. LV2_URID midiEvent;
  1081. LV2_URID patchSet;
  1082. LV2_URID patchProperty;
  1083. LV2_URID patchValue;
  1084. LV2_URID timePosition;
  1085. LV2_URID timeBar;
  1086. LV2_URID timeBarBeat;
  1087. LV2_URID timeBeatUnit;
  1088. LV2_URID timeBeatsPerBar;
  1089. LV2_URID timeBeatsPerMinute;
  1090. LV2_URID timeTicksPerBeat;
  1091. LV2_URID timeFrame;
  1092. LV2_URID timeSpeed;
  1093. URIDs(const LV2_URID_Map* const uridMap)
  1094. : _uridMap(uridMap),
  1095. atomBlank(map(LV2_ATOM__Blank)),
  1096. atomObject(map(LV2_ATOM__Object)),
  1097. atomDouble(map(LV2_ATOM__Double)),
  1098. atomFloat(map(LV2_ATOM__Float)),
  1099. atomInt(map(LV2_ATOM__Int)),
  1100. atomLong(map(LV2_ATOM__Long)),
  1101. atomPath(map(LV2_ATOM__Path)),
  1102. atomSequence(map(LV2_ATOM__Sequence)),
  1103. atomString(map(LV2_ATOM__String)),
  1104. atomURID(map(LV2_ATOM__URID)),
  1105. dpfKeyValue(map(DISTRHO_PLUGIN_LV2_STATE_PREFIX "KeyValueState")),
  1106. midiEvent(map(LV2_MIDI__MidiEvent)),
  1107. patchSet(map(LV2_PATCH__Set)),
  1108. patchProperty(map(LV2_PATCH__property)),
  1109. patchValue(map(LV2_PATCH__value)),
  1110. timePosition(map(LV2_TIME__Position)),
  1111. timeBar(map(LV2_TIME__bar)),
  1112. timeBarBeat(map(LV2_TIME__barBeat)),
  1113. timeBeatUnit(map(LV2_TIME__beatUnit)),
  1114. timeBeatsPerBar(map(LV2_TIME__beatsPerBar)),
  1115. timeBeatsPerMinute(map(LV2_TIME__beatsPerMinute)),
  1116. timeTicksPerBeat(map(LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat)),
  1117. timeFrame(map(LV2_TIME__frame)),
  1118. timeSpeed(map(LV2_TIME__speed)) {}
  1119. inline LV2_URID map(const char* const uri) const
  1120. {
  1121. return _uridMap->map(_uridMap->handle, uri);
  1122. }
  1123. } fURIDs;
  1124. // LV2 features
  1125. #if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST
  1126. const LV2_ControlInputPort_Change_Request* const fCtrlInPortChangeReq;
  1127. #endif
  1128. const LV2_URID_Map* const fUridMap;
  1129. const LV2_Worker_Schedule* const fWorker;
  1130. #if DISTRHO_PLUGIN_WANT_STATE
  1131. LV2_Atom_Forge fAtomForge;
  1132. StringToStringMap fStateMap;
  1133. UridToStringMap fUridStateMap;
  1134. LV2_URID* fUrids;
  1135. bool* fNeededUiSends;
  1136. void setState(const char* const key, const char* const newValue)
  1137. {
  1138. fPlugin.setState(key, newValue);
  1139. // save this key if necessary
  1140. if (fPlugin.wantStateKey(key))
  1141. updateInternalState(key, newValue, false);
  1142. }
  1143. bool updateState(const char* const key, const char* const newValue)
  1144. {
  1145. fPlugin.setState(key, newValue);
  1146. return updateInternalState(key, newValue, true);
  1147. }
  1148. bool updateInternalState(const char* const key, const char* const newValue, const bool sendToUI)
  1149. {
  1150. // key must already exist
  1151. for (StringToStringMap::iterator it=fStateMap.begin(), ite=fStateMap.end(); it != ite; ++it)
  1152. {
  1153. const String& dkey(it->first);
  1154. if (dkey == key)
  1155. {
  1156. it->second = newValue;
  1157. if (sendToUI)
  1158. {
  1159. for (uint32_t i=0, count=fPlugin.getStateCount(); i < count; ++i)
  1160. {
  1161. if (fPlugin.getStateKey(i) == key)
  1162. {
  1163. if ((fPlugin.getStateHints(i) & kStateIsOnlyForDSP) == 0x0)
  1164. fNeededUiSends[i] = true;
  1165. break;
  1166. }
  1167. }
  1168. }
  1169. return true;
  1170. }
  1171. }
  1172. d_stderr("Failed to find plugin state with key \"%s\"", key);
  1173. return false;
  1174. }
  1175. #endif
  1176. void updateParameterOutputsAndTriggers()
  1177. {
  1178. float curValue;
  1179. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  1180. {
  1181. if (fPlugin.isParameterOutput(i))
  1182. {
  1183. curValue = fLastControlValues[i] = fPlugin.getParameterValue(i);
  1184. setPortControlValue(i, curValue);
  1185. }
  1186. else if ((fPlugin.getParameterHints(i) & kParameterIsTrigger) == kParameterIsTrigger)
  1187. {
  1188. // NOTE: host is responsible for auto-updating control port buffers
  1189. }
  1190. }
  1191. #if DISTRHO_PLUGIN_WANT_LATENCY
  1192. if (fPortLatency != nullptr)
  1193. *fPortLatency = fPlugin.getLatency();
  1194. #endif
  1195. }
  1196. #if DISTRHO_PLUGIN_WANT_PARAMETER_VALUE_CHANGE_REQUEST
  1197. bool requestParameterValueChange(const uint32_t index, const float value)
  1198. {
  1199. if (fCtrlInPortChangeReq == nullptr)
  1200. return false;
  1201. return fCtrlInPortChangeReq->request_change(fCtrlInPortChangeReq->handle,
  1202. index + fPlugin.getParameterOffset(),
  1203. value);
  1204. }
  1205. static bool requestParameterValueChangeCallback(void* const ptr, const uint32_t index, const float value)
  1206. {
  1207. return (((PluginLv2*)ptr)->requestParameterValueChange(index, value) == 0);
  1208. }
  1209. #endif
  1210. #if DISTRHO_PLUGIN_WANT_STATE
  1211. static bool updateStateValueCallback(void* const ptr, const char* const key, const char* const value)
  1212. {
  1213. return ((PluginLv2*)ptr)->updateState(key, value);
  1214. }
  1215. #endif
  1216. #if DISTRHO_PLUGIN_WANT_MIDI_OUTPUT
  1217. bool writeMidi(const MidiEvent& midiEvent)
  1218. {
  1219. DISTRHO_SAFE_ASSERT_RETURN(fEventsOutData.port != nullptr, false);
  1220. fEventsOutData.initIfNeeded(fURIDs.atomSequence);
  1221. const uint32_t capacity = fEventsOutData.capacity;
  1222. const uint32_t offset = fEventsOutData.offset;
  1223. if (sizeof(LV2_Atom_Event) + midiEvent.size > capacity - offset)
  1224. return false;
  1225. LV2_Atom_Event* const aev = (LV2_Atom_Event*)(LV2_ATOM_CONTENTS(LV2_Atom_Sequence, fEventsOutData.port) + offset);
  1226. aev->time.frames = midiEvent.frame;
  1227. aev->body.type = fURIDs.midiEvent;
  1228. aev->body.size = midiEvent.size;
  1229. std::memcpy(LV2_ATOM_BODY(&aev->body),
  1230. midiEvent.size > MidiEvent::kDataSize ? midiEvent.dataExt : midiEvent.data,
  1231. midiEvent.size);
  1232. fEventsOutData.growBy(lv2_atom_pad_size(sizeof(LV2_Atom_Event) + midiEvent.size));
  1233. return true;
  1234. }
  1235. static bool writeMidiCallback(void* ptr, const MidiEvent& midiEvent)
  1236. {
  1237. return ((PluginLv2*)ptr)->writeMidi(midiEvent);
  1238. }
  1239. #endif
  1240. };
  1241. // -----------------------------------------------------------------------
  1242. static LV2_Handle lv2_instantiate(const LV2_Descriptor*, double sampleRate, const char* bundlePath, const LV2_Feature* const* features)
  1243. {
  1244. const LV2_Options_Option* options = nullptr;
  1245. const LV2_URID_Map* uridMap = nullptr;
  1246. const LV2_Worker_Schedule* worker = nullptr;
  1247. const LV2_ControlInputPort_Change_Request* ctrlInPortChangeReq = nullptr;
  1248. for (int i=0; features[i] != nullptr; ++i)
  1249. {
  1250. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  1251. options = (const LV2_Options_Option*)features[i]->data;
  1252. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  1253. uridMap = (const LV2_URID_Map*)features[i]->data;
  1254. else if (std::strcmp(features[i]->URI, LV2_WORKER__schedule) == 0)
  1255. worker = (const LV2_Worker_Schedule*)features[i]->data;
  1256. else if (std::strcmp(features[i]->URI, LV2_CONTROL_INPUT_PORT_CHANGE_REQUEST_URI) == 0)
  1257. ctrlInPortChangeReq = (const LV2_ControlInputPort_Change_Request*)features[i]->data;
  1258. }
  1259. if (options == nullptr)
  1260. {
  1261. d_stderr("Options feature missing, cannot continue!");
  1262. return nullptr;
  1263. }
  1264. if (uridMap == nullptr)
  1265. {
  1266. d_stderr("URID Map feature missing, cannot continue!");
  1267. return nullptr;
  1268. }
  1269. #if DISTRHO_PLUGIN_WANT_STATE
  1270. if (worker == nullptr)
  1271. {
  1272. d_stderr("Worker feature missing, cannot continue!");
  1273. return nullptr;
  1274. }
  1275. #endif
  1276. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  1277. mod_license_check(features, DISTRHO_PLUGIN_URI);
  1278. #endif
  1279. d_nextBufferSize = 0;
  1280. bool usingNominal = false;
  1281. for (int i=0; options[i].key != 0; ++i)
  1282. {
  1283. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__nominalBlockLength))
  1284. {
  1285. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  1286. {
  1287. d_nextBufferSize = *(const int*)options[i].value;
  1288. usingNominal = true;
  1289. }
  1290. else
  1291. {
  1292. d_stderr("Host provides nominalBlockLength but has wrong value type");
  1293. }
  1294. break;
  1295. }
  1296. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  1297. {
  1298. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  1299. d_nextBufferSize = *(const int*)options[i].value;
  1300. else
  1301. d_stderr("Host provides maxBlockLength but has wrong value type");
  1302. // no break, continue in case host supports nominalBlockLength
  1303. }
  1304. }
  1305. if (d_nextBufferSize == 0)
  1306. {
  1307. d_stderr("Host does not provide nominalBlockLength or maxBlockLength options");
  1308. d_nextBufferSize = 2048;
  1309. }
  1310. d_nextSampleRate = sampleRate;
  1311. d_nextBundlePath = bundlePath;
  1312. d_nextCanRequestParameterValueChanges = ctrlInPortChangeReq != nullptr;
  1313. if (std::getenv("RUNNING_UNDER_LV2LINT") != nullptr)
  1314. d_nextPluginIsDummy = true;
  1315. return new PluginLv2(sampleRate, uridMap, worker, ctrlInPortChangeReq, usingNominal);
  1316. }
  1317. #define instancePtr ((PluginLv2*)instance)
  1318. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  1319. {
  1320. instancePtr->lv2_connect_port(port, dataLocation);
  1321. }
  1322. static void lv2_activate(LV2_Handle instance)
  1323. {
  1324. instancePtr->lv2_activate();
  1325. }
  1326. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  1327. {
  1328. instancePtr->lv2_run(sampleCount);
  1329. }
  1330. static void lv2_deactivate(LV2_Handle instance)
  1331. {
  1332. instancePtr->lv2_deactivate();
  1333. }
  1334. static void lv2_cleanup(LV2_Handle instance)
  1335. {
  1336. delete instancePtr;
  1337. }
  1338. // -----------------------------------------------------------------------
  1339. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  1340. {
  1341. return instancePtr->lv2_get_options(options);
  1342. }
  1343. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  1344. {
  1345. return instancePtr->lv2_set_options(options);
  1346. }
  1347. // -----------------------------------------------------------------------
  1348. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  1349. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  1350. {
  1351. return instancePtr->lv2_get_program(index);
  1352. }
  1353. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  1354. {
  1355. instancePtr->lv2_select_program(bank, program);
  1356. }
  1357. #endif
  1358. // -----------------------------------------------------------------------
  1359. #if DISTRHO_PLUGIN_WANT_STATE
  1360. static LV2_State_Status lv2_save(LV2_Handle instance, LV2_State_Store_Function store, LV2_State_Handle handle, uint32_t, const LV2_Feature* const* const features)
  1361. {
  1362. return instancePtr->lv2_save(store, handle, features);
  1363. }
  1364. static LV2_State_Status lv2_restore(LV2_Handle instance, LV2_State_Retrieve_Function retrieve, LV2_State_Handle handle, uint32_t, const LV2_Feature* const* const features)
  1365. {
  1366. return instancePtr->lv2_restore(retrieve, handle, features);
  1367. }
  1368. LV2_Worker_Status lv2_work(LV2_Handle instance, LV2_Worker_Respond_Function, LV2_Worker_Respond_Handle, uint32_t, const void* data)
  1369. {
  1370. return instancePtr->lv2_work(data);
  1371. }
  1372. LV2_Worker_Status lv2_work_response(LV2_Handle instance, uint32_t size, const void* body)
  1373. {
  1374. return instancePtr->lv2_work_response(size, body);
  1375. }
  1376. #endif
  1377. // -----------------------------------------------------------------------
  1378. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  1379. static void* lv2_get_instance_pointer(LV2_Handle instance)
  1380. {
  1381. return instancePtr->lv2_get_instance_pointer();
  1382. }
  1383. #endif
  1384. // -----------------------------------------------------------------------
  1385. static const void* lv2_extension_data(const char* uri)
  1386. {
  1387. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  1388. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  1389. return &options;
  1390. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  1391. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  1392. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  1393. return &programs;
  1394. #endif
  1395. #if DISTRHO_PLUGIN_WANT_STATE
  1396. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  1397. static const LV2_Worker_Interface worker = { lv2_work, lv2_work_response, nullptr };
  1398. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  1399. return &state;
  1400. if (std::strcmp(uri, LV2_WORKER__interface) == 0)
  1401. return &worker;
  1402. #endif
  1403. #if DISTRHO_PLUGIN_WANT_DIRECT_ACCESS
  1404. struct LV2_DirectAccess_Interface {
  1405. void* (*get_instance_pointer)(LV2_Handle handle);
  1406. };
  1407. static const LV2_DirectAccess_Interface directaccess = { lv2_get_instance_pointer };
  1408. if (std::strcmp(uri, DISTRHO_PLUGIN_LV2_STATE_PREFIX "direct-access") == 0)
  1409. return &directaccess;
  1410. #endif
  1411. #ifdef DISTRHO_PLUGIN_LICENSED_FOR_MOD
  1412. return mod_license_interface(uri);
  1413. #else
  1414. return nullptr;
  1415. #endif
  1416. }
  1417. #undef instancePtr
  1418. // -----------------------------------------------------------------------
  1419. static const LV2_Descriptor sLv2Descriptor = {
  1420. DISTRHO_PLUGIN_URI,
  1421. lv2_instantiate,
  1422. lv2_connect_port,
  1423. lv2_activate,
  1424. lv2_run,
  1425. lv2_deactivate,
  1426. lv2_cleanup,
  1427. lv2_extension_data
  1428. };
  1429. // -----------------------------------------------------------------------
  1430. END_NAMESPACE_DISTRHO
  1431. DISTRHO_PLUGIN_EXPORT
  1432. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  1433. {
  1434. USE_NAMESPACE_DISTRHO
  1435. return (index == 0) ? &sLv2Descriptor : nullptr;
  1436. }
  1437. // -----------------------------------------------------------------------