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.

1538 lines
51KB

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