Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1226 lines
41KB

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