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.

881 lines
27KB

  1. /*
  2. * DISTRHO Plugin Framework (DPF)
  3. * Copyright (C) 2012-2014 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/midi.h"
  21. #include "lv2/options.h"
  22. #include "lv2/state.h"
  23. #include "lv2/time.h"
  24. #include "lv2/urid.h"
  25. #include "lv2/worker.h"
  26. #include "lv2/lv2_programs.h"
  27. #ifdef noexcept
  28. # undef noexcept
  29. #endif
  30. #include <map>
  31. #ifndef DISTRHO_PLUGIN_URI
  32. # error DISTRHO_PLUGIN_URI undefined!
  33. #endif
  34. #if DISTRHO_PLUGIN_WANT_STATE
  35. # warning LV2 State still TODO (working but needs final testing)
  36. #endif
  37. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  38. # warning LV2 TimePos still TODO
  39. #endif
  40. #define DISTRHO_LV2_USE_EVENTS_IN (DISTRHO_PLUGIN_IS_SYNTH || DISTRHO_PLUGIN_WANT_TIMEPOS || (DISTRHO_PLUGIN_WANT_STATE && DISTRHO_PLUGIN_HAS_UI))
  41. #define DISTRHO_LV2_USE_EVENTS_OUT (DISTRHO_PLUGIN_WANT_STATE && DISTRHO_PLUGIN_HAS_UI)
  42. typedef std::map<d_string,d_string> StringMap;
  43. START_NAMESPACE_DISTRHO
  44. // -----------------------------------------------------------------------
  45. class PluginLv2
  46. {
  47. public:
  48. PluginLv2(const LV2_URID_Map* const uridMap, const LV2_Worker_Schedule* const worker)
  49. : fPortControls(nullptr),
  50. fLastControlValues(nullptr),
  51. #if DISTRHO_LV2_USE_EVENTS_IN || DISTRHO_LV2_USE_EVENTS_OUT
  52. # if DISTRHO_PLUGIN_WANT_TIMEPOS
  53. fLastTimeSpeed(0.0f),
  54. # endif
  55. fURIDs(uridMap),
  56. #endif
  57. fUridMap(uridMap),
  58. fWorker(worker)
  59. {
  60. #if DISTRHO_PLUGIN_NUM_INPUTS > 0
  61. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_INPUTS; ++i)
  62. fPortAudioIns[i] = nullptr;
  63. #else
  64. fPortAudioIns = nullptr;
  65. #endif
  66. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  67. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  68. fPortAudioOuts[i] = nullptr;
  69. #else
  70. fPortAudioOuts = nullptr;
  71. #endif
  72. if (const uint32_t count = fPlugin.getParameterCount())
  73. {
  74. fPortControls = new float*[count];
  75. fLastControlValues = new float[count];
  76. for (uint32_t i=0; i < count; ++i)
  77. {
  78. fPortControls[i] = nullptr;
  79. fLastControlValues[i] = fPlugin.getParameterValue(i);
  80. }
  81. }
  82. else
  83. {
  84. fPortControls = nullptr;
  85. fLastControlValues = nullptr;
  86. }
  87. #if DISTRHO_LV2_USE_EVENTS_IN
  88. fPortEventsIn = nullptr;
  89. #endif
  90. #if DISTRHO_LV2_USE_EVENTS_OUT
  91. fPortEventsOut = nullptr;
  92. #endif
  93. #if DISTRHO_PLUGIN_WANT_LATENCY
  94. fPortLatency = nullptr;
  95. #endif
  96. }
  97. ~PluginLv2()
  98. {
  99. if (fPortControls != nullptr)
  100. {
  101. delete[] fPortControls;
  102. fPortControls = nullptr;
  103. }
  104. if (fLastControlValues)
  105. {
  106. delete[] fLastControlValues;
  107. fLastControlValues = nullptr;
  108. }
  109. #if DISTRHO_PLUGIN_WANT_STATE
  110. fStateMap.clear();
  111. #endif
  112. }
  113. // -------------------------------------------------------------------
  114. void lv2_activate()
  115. {
  116. fPlugin.activate();
  117. }
  118. void lv2_deactivate()
  119. {
  120. fPlugin.deactivate();
  121. }
  122. // -------------------------------------------------------------------
  123. void lv2_connect_port(const uint32_t port, void* const dataLocation)
  124. {
  125. uint32_t index = 0;
  126. #if DISTRHO_PLUGIN_NUM_INPUTS > 0
  127. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_INPUTS; ++i)
  128. {
  129. if (port == index++)
  130. {
  131. fPortAudioIns[i] = (float*)dataLocation;
  132. return;
  133. }
  134. }
  135. #endif
  136. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  137. for (uint32_t i=0; i < DISTRHO_PLUGIN_NUM_OUTPUTS; ++i)
  138. {
  139. if (port == index++)
  140. {
  141. fPortAudioOuts[i] = (float*)dataLocation;
  142. return;
  143. }
  144. }
  145. #endif
  146. #if DISTRHO_LV2_USE_EVENTS_IN
  147. if (port == index++)
  148. {
  149. fPortEventsIn = (LV2_Atom_Sequence*)dataLocation;
  150. return;
  151. }
  152. #endif
  153. #if DISTRHO_LV2_USE_EVENTS_OUT
  154. if (port == index++)
  155. {
  156. fPortEventsOut = (LV2_Atom_Sequence*)dataLocation;
  157. return;
  158. }
  159. #endif
  160. #if DISTRHO_PLUGIN_WANT_LATENCY
  161. if (port == index++)
  162. {
  163. fPortLatency = (float*)dataLocation;
  164. return;
  165. }
  166. #endif
  167. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  168. {
  169. if (port == index++)
  170. {
  171. fPortControls[i] = (float*)dataLocation;
  172. return;
  173. }
  174. }
  175. }
  176. // -------------------------------------------------------------------
  177. void lv2_run(const uint32_t sampleCount)
  178. {
  179. // pre-roll
  180. if (sampleCount == 0)
  181. return updateParameterOutputs();
  182. // Check for updated parameters
  183. float curValue;
  184. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  185. {
  186. if (fPortControls[i] == nullptr)
  187. continue;
  188. curValue = *fPortControls[i];
  189. if (fLastControlValues[i] != curValue && ! fPlugin.isParameterOutput(i))
  190. {
  191. fLastControlValues[i] = curValue;
  192. fPlugin.setParameterValue(i, curValue);
  193. }
  194. }
  195. #if DISTRHO_LV2_USE_EVENTS_IN
  196. # if DISTRHO_PLUGIN_IS_SYNTH
  197. uint32_t midiEventCount = 0;
  198. # endif
  199. # if DISTRHO_PLUGIN_WANT_TIMEPOS
  200. bool needsFrameIncrement = true;
  201. # endif
  202. LV2_ATOM_SEQUENCE_FOREACH(fPortEventsIn, event)
  203. {
  204. if (event == nullptr)
  205. break;
  206. # if DISTRHO_PLUGIN_IS_SYNTH
  207. if (event->body.type == fURIDs.midiEvent)
  208. {
  209. if (event->body.size > 4 || midiEventCount >= kMaxMidiEvents)
  210. continue;
  211. const uint8_t* const data((const uint8_t*)(event + 1));
  212. MidiEvent& midiEvent(fMidiEvents[midiEventCount]);
  213. midiEvent.frame = event->time.frames;
  214. midiEvent.size = event->body.size;
  215. uint8_t i;
  216. for (i=0; i < midiEvent.size; ++i)
  217. midiEvent.buf[i] = data[i];
  218. for (; i < 4; ++i)
  219. midiEvent.buf[i] = 0;
  220. ++midiEventCount;
  221. continue;
  222. }
  223. # endif
  224. # if DISTRHO_PLUGIN_WANT_TIMEPOS
  225. if (event->body.type == fURIDs.atomBlank)
  226. {
  227. const LV2_Atom_Object* const obj((const LV2_Atom_Object*)&event->body);
  228. if (obj->body.otype != fURIDs.timePosition)
  229. continue;
  230. LV2_Atom* bar = nullptr;
  231. LV2_Atom* barBeat = nullptr;
  232. LV2_Atom* beat = nullptr;
  233. LV2_Atom* beatUnit = nullptr;
  234. LV2_Atom* beatsPerBar = nullptr;
  235. LV2_Atom* beatsPerMinute = nullptr;
  236. LV2_Atom* frame = nullptr;
  237. LV2_Atom* speed = nullptr;
  238. lv2_atom_object_get(obj,
  239. fURIDs.timeBar, &bar,
  240. fURIDs.timeBarBeat, &barBeat,
  241. fURIDs.timeBeat, &beat,
  242. fURIDs.timeBeatUnit, &beatUnit,
  243. fURIDs.timeBeatsPerBar, &beatsPerBar,
  244. fURIDs.timeBeatsPerMinute, &beatsPerMinute,
  245. fURIDs.timeFrame, &frame,
  246. fURIDs.timeSpeed, &speed,
  247. nullptr);
  248. // TODO:
  249. // - tick
  250. // - barStartTick
  251. // - ticksPerBeat
  252. if (bar != nullptr)
  253. {
  254. if (bar->type == fURIDs.atomDouble)
  255. fTimePos.bbt.bar = ((LV2_Atom_Double*)bar)->body + 1.0f;
  256. else if (bar->type == fURIDs.atomFloat)
  257. fTimePos.bbt.bar = ((LV2_Atom_Float*)bar)->body + 1.0f;
  258. else if (bar->type == fURIDs.atomInt)
  259. fTimePos.bbt.bar = ((LV2_Atom_Int*)bar)->body + 1;
  260. else if (bar->type == fURIDs.atomLong)
  261. fTimePos.bbt.bar = ((LV2_Atom_Long*)bar)->body + 1;
  262. }
  263. /*if (barBeat != nullptr && barBeat->type == fURIDs.atomFloat)
  264. {
  265. }*/
  266. if (beat != nullptr)
  267. {
  268. if (beat->type == fURIDs.atomDouble)
  269. fTimePos.bbt.beat = ((LV2_Atom_Double*)beat)->body + 1.0f;
  270. else if (beat->type == fURIDs.atomFloat)
  271. fTimePos.bbt.beat = ((LV2_Atom_Float*)beat)->body + 1.0f;
  272. else if (beat->type == fURIDs.atomInt)
  273. fTimePos.bbt.beat = ((LV2_Atom_Int*)beat)->body + 1;
  274. else if (beat->type == fURIDs.atomLong)
  275. fTimePos.bbt.beat = ((LV2_Atom_Long*)beat)->body + 1;
  276. }
  277. if (beatUnit != nullptr)
  278. {
  279. if (beatUnit->type == fURIDs.atomDouble)
  280. fTimePos.bbt.beatType = ((LV2_Atom_Double*)beatUnit)->body;
  281. else if (beatUnit->type == fURIDs.atomFloat)
  282. fTimePos.bbt.beatType = ((LV2_Atom_Float*)beatUnit)->body;
  283. else if (beatUnit->type == fURIDs.atomInt)
  284. fTimePos.bbt.beatType = ((LV2_Atom_Int*)beatUnit)->body;
  285. else if (beatUnit->type == fURIDs.atomLong)
  286. fTimePos.bbt.beatType = ((LV2_Atom_Long*)beatUnit)->body;
  287. }
  288. if (beatsPerBar != nullptr)
  289. {
  290. if (beatsPerBar->type == fURIDs.atomDouble)
  291. fTimePos.bbt.beatsPerBar = ((LV2_Atom_Double*)beatsPerBar)->body;
  292. else if (beatsPerBar->type == fURIDs.atomFloat)
  293. fTimePos.bbt.beatsPerBar = ((LV2_Atom_Float*)beatsPerBar)->body;
  294. else if (beatsPerBar->type == fURIDs.atomInt)
  295. fTimePos.bbt.beatsPerBar = ((LV2_Atom_Int*)beatsPerBar)->body;
  296. else if (beatsPerBar->type == fURIDs.atomLong)
  297. fTimePos.bbt.beatsPerBar = ((LV2_Atom_Long*)beatsPerBar)->body;
  298. }
  299. if (beatsPerMinute != nullptr)
  300. {
  301. if (beatsPerMinute->type == fURIDs.atomDouble)
  302. fTimePos.bbt.beatsPerMinute = ((LV2_Atom_Double*)beatsPerMinute)->body;
  303. else if (beatsPerMinute->type == fURIDs.atomFloat)
  304. fTimePos.bbt.beatsPerMinute = ((LV2_Atom_Float*)beatsPerMinute)->body;
  305. else if (beatsPerMinute->type == fURIDs.atomInt)
  306. fTimePos.bbt.beatsPerMinute = ((LV2_Atom_Int*)beatsPerMinute)->body;
  307. else if (beatsPerMinute->type == fURIDs.atomLong)
  308. fTimePos.bbt.beatsPerMinute = ((LV2_Atom_Long*)beatsPerMinute)->body;
  309. }
  310. if (frame != nullptr && frame->type == fURIDs.atomLong)
  311. {
  312. fTimePos.frame = ((LV2_Atom_Long*)frame)->body;
  313. needsFrameIncrement = false;
  314. }
  315. if (speed != nullptr && speed->type == fURIDs.atomFloat)
  316. {
  317. fLastTimeSpeed = ((LV2_Atom_Float*)speed)->body;
  318. fTimePos.playing = (fLastTimeSpeed == 1.0f);
  319. }
  320. if ((! fTimePos.bbt.valid) && beatsPerMinute != nullptr && beatsPerBar != nullptr && beatUnit != nullptr)
  321. fTimePos.bbt.valid = true;
  322. continue;
  323. }
  324. # endif
  325. # if (DISTRHO_PLUGIN_WANT_STATE && DISTRHO_PLUGIN_HAS_UI)
  326. if (event->body.type == fURIDs.distrhoState && fWorker != nullptr)
  327. {
  328. const void* const data((const void*)(event + 1));
  329. fWorker->schedule_work(fWorker->handle, event->body.size, data);
  330. continue;
  331. }
  332. # endif
  333. }
  334. #endif
  335. # if DISTRHO_PLUGIN_WANT_TIMEPOS
  336. if (needsFrameIncrement && fLastTimeSpeed != 0.0f)
  337. fTimePos.frame += fLastTimeSpeed*sampleCount;
  338. fPlugin.setTimePos(fTimePos);
  339. # endif
  340. #if DISTRHO_PLUGIN_IS_SYNTH
  341. fPlugin.run(fPortAudioIns, fPortAudioOuts, sampleCount, fMidiEvents, midiEventCount);
  342. #else
  343. fPlugin.run(fPortAudioIns, fPortAudioOuts, sampleCount);
  344. #endif
  345. #if DISTRHO_LV2_USE_EVENTS_OUT
  346. #endif
  347. updateParameterOutputs();
  348. }
  349. // -------------------------------------------------------------------
  350. uint32_t lv2_get_options(LV2_Options_Option* const /*options*/)
  351. {
  352. // currently unused
  353. return LV2_OPTIONS_ERR_UNKNOWN;
  354. }
  355. uint32_t lv2_set_options(const LV2_Options_Option* const options)
  356. {
  357. for (int i=0; options[i].key != 0; ++i)
  358. {
  359. if (options[i].key == fUridMap->map(fUridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  360. {
  361. if (options[i].type == fUridMap->map(fUridMap->handle, LV2_ATOM__Int))
  362. {
  363. const int bufferSize(*(const int*)options[i].value);
  364. fPlugin.setBufferSize(bufferSize);
  365. return LV2_OPTIONS_SUCCESS;
  366. }
  367. else
  368. {
  369. d_stderr("Host changed maxBlockLength but with wrong value type");
  370. return LV2_OPTIONS_ERR_BAD_VALUE;
  371. }
  372. }
  373. else if (options[i].key == fUridMap->map(fUridMap->handle, LV2_CORE__sampleRate))
  374. {
  375. if (options[i].type == fUridMap->map(fUridMap->handle, LV2_ATOM__Double))
  376. {
  377. const double sampleRate(*(const double*)options[i].value);
  378. fPlugin.setSampleRate(sampleRate);
  379. return LV2_OPTIONS_SUCCESS;
  380. }
  381. else
  382. {
  383. d_stderr("Host changed sampleRate but with wrong value type");
  384. return LV2_OPTIONS_ERR_BAD_VALUE;
  385. }
  386. }
  387. }
  388. return LV2_OPTIONS_ERR_BAD_KEY;
  389. }
  390. // -------------------------------------------------------------------
  391. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  392. const LV2_Program_Descriptor* lv2_get_program(const uint32_t index)
  393. {
  394. if (index >= fPlugin.getProgramCount())
  395. return nullptr;
  396. static LV2_Program_Descriptor desc;
  397. desc.bank = index / 128;
  398. desc.program = index % 128;
  399. desc.name = fPlugin.getProgramName(index);
  400. return &desc;
  401. }
  402. void lv2_select_program(const uint32_t bank, const uint32_t program)
  403. {
  404. const uint32_t realProgram(bank * 128 + program);
  405. if (realProgram >= fPlugin.getProgramCount())
  406. return;
  407. fPlugin.setProgram(realProgram);
  408. // Update control inputs
  409. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  410. {
  411. if (fPlugin.isParameterOutput(i))
  412. continue;
  413. fLastControlValues[i] = fPlugin.getParameterValue(i);
  414. if (fPortControls[i] != nullptr)
  415. *fPortControls[i] = fLastControlValues[i];
  416. }
  417. }
  418. #endif
  419. // -------------------------------------------------------------------
  420. #if DISTRHO_PLUGIN_WANT_STATE
  421. LV2_State_Status lv2_save(const LV2_State_Store_Function store, const LV2_State_Handle handle, const uint32_t flags)
  422. {
  423. //flags = LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE;
  424. for (auto it = fStateMap.begin(), end = fStateMap.end(); it != end; ++it)
  425. {
  426. const d_string& key = it->first;
  427. const d_string& value = it->second;
  428. store(handle, fUridMap->map(fUridMap->handle, (const char*)key), (const char*)value, value.length()+1, fURIDs.atomString, flags);
  429. }
  430. return LV2_STATE_SUCCESS;
  431. }
  432. LV2_State_Status lv2_restore(const LV2_State_Retrieve_Function retrieve, const LV2_State_Handle handle)
  433. {
  434. size_t size = 0;
  435. uint32_t type = 0;
  436. uint32_t flags = LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE;
  437. for (uint32_t i=0, count=fPlugin.getStateCount(); i < count; ++i)
  438. {
  439. const d_string& key = fPlugin.getStateKey(i);
  440. const void* data = retrieve(handle, fUridMap->map(fUridMap->handle, (const char*)key), &size, &type, &flags);
  441. if (size == 0)
  442. continue;
  443. if (data == nullptr)
  444. continue;
  445. if (type != fURIDs.atomString)
  446. continue;
  447. const char* const value((const char*)data);
  448. if (std::strlen(value) != size)
  449. continue;
  450. setState(key, value);
  451. }
  452. return LV2_STATE_SUCCESS;
  453. }
  454. // -------------------------------------------------------------------
  455. LV2_Worker_Status lv2_work(const void* const data)
  456. {
  457. const char* const stateKey((const char*)data);
  458. const char* const stateValue(stateKey+std::strlen(stateKey)+1);
  459. setState(stateKey, stateValue);
  460. return LV2_WORKER_SUCCESS;
  461. }
  462. #endif
  463. // -------------------------------------------------------------------
  464. private:
  465. PluginExporter fPlugin;
  466. // LV2 ports
  467. #if DISTRHO_PLUGIN_NUM_INPUTS > 0
  468. float* fPortAudioIns[DISTRHO_PLUGIN_NUM_INPUTS];
  469. #else
  470. float** fPortAudioIns;
  471. #endif
  472. #if DISTRHO_PLUGIN_NUM_OUTPUTS > 0
  473. float* fPortAudioOuts[DISTRHO_PLUGIN_NUM_OUTPUTS];
  474. #else
  475. float** fPortAudioOuts;
  476. #endif
  477. float** fPortControls;
  478. #if DISTRHO_LV2_USE_EVENTS_IN
  479. LV2_Atom_Sequence* fPortEventsIn;
  480. #endif
  481. #if DISTRHO_LV2_USE_EVENTS_OUT
  482. LV2_Atom_Sequence* fPortEventsOut;
  483. #endif
  484. #if DISTRHO_PLUGIN_WANT_LATENCY
  485. float* fPortLatency;
  486. #endif
  487. // Temporary data
  488. float* fLastControlValues;
  489. #if DISTRHO_PLUGIN_IS_SYNTH
  490. MidiEvent fMidiEvents[kMaxMidiEvents];
  491. #endif
  492. #if DISTRHO_PLUGIN_WANT_TIMEPOS
  493. TimePos fTimePos;
  494. float fLastTimeSpeed;
  495. #endif
  496. // LV2 URIDs
  497. #if DISTRHO_LV2_USE_EVENTS_IN || DISTRHO_LV2_USE_EVENTS_OUT
  498. struct URIDs {
  499. LV2_URID atomBlank;
  500. LV2_URID atomDouble;
  501. LV2_URID atomFloat;
  502. LV2_URID atomInt;
  503. LV2_URID atomLong;
  504. LV2_URID atomString;
  505. LV2_URID distrhoState;
  506. LV2_URID midiEvent;
  507. LV2_URID timePosition;
  508. LV2_URID timeBar;
  509. LV2_URID timeBarBeat;
  510. LV2_URID timeBeat;
  511. LV2_URID timeBeatUnit;
  512. LV2_URID timeBeatsPerBar;
  513. LV2_URID timeBeatsPerMinute;
  514. LV2_URID timeFrame;
  515. LV2_URID timeSpeed;
  516. URIDs(const LV2_URID_Map* const uridMap)
  517. : atomBlank(uridMap->map(uridMap->handle, LV2_ATOM__Blank)),
  518. atomDouble(uridMap->map(uridMap->handle, LV2_ATOM__Double)),
  519. atomFloat(uridMap->map(uridMap->handle, LV2_ATOM__Float)),
  520. atomInt(uridMap->map(uridMap->handle, LV2_ATOM__Int)),
  521. atomLong(uridMap->map(uridMap->handle, LV2_ATOM__Long)),
  522. atomString(uridMap->map(uridMap->handle, LV2_ATOM__String)),
  523. distrhoState(uridMap->map(uridMap->handle, "urn:distrho:keyValueState")),
  524. midiEvent(uridMap->map(uridMap->handle, LV2_MIDI__MidiEvent)),
  525. timePosition(uridMap->map(uridMap->handle, LV2_TIME__Position)),
  526. timeBar(uridMap->map(uridMap->handle, LV2_TIME__bar)),
  527. timeBarBeat(uridMap->map(uridMap->handle, LV2_TIME__barBeat)),
  528. timeBeat(uridMap->map(uridMap->handle, LV2_TIME__beat)),
  529. timeBeatUnit(uridMap->map(uridMap->handle, LV2_TIME__beatUnit)),
  530. timeBeatsPerBar(uridMap->map(uridMap->handle, LV2_TIME__beatsPerBar)),
  531. timeBeatsPerMinute(uridMap->map(uridMap->handle, LV2_TIME__beatsPerMinute)),
  532. timeFrame(uridMap->map(uridMap->handle, LV2_TIME__frame)),
  533. timeSpeed(uridMap->map(uridMap->handle, LV2_TIME__speed)) {}
  534. } fURIDs;
  535. #endif
  536. // LV2 features
  537. const LV2_URID_Map* const fUridMap;
  538. const LV2_Worker_Schedule* const fWorker;
  539. #if DISTRHO_PLUGIN_WANT_STATE
  540. StringMap fStateMap;
  541. void setState(const char* const key, const char* const newValue)
  542. {
  543. fPlugin.setState(key, newValue);
  544. // check if we want to save this key
  545. if (! fPlugin.wantsStateKey(key))
  546. return;
  547. // check if key already exists
  548. for (auto it = fStateMap.begin(), end = fStateMap.end(); it != end; ++it)
  549. {
  550. const d_string& d_key = it->first;
  551. if (d_key == key)
  552. {
  553. it->second = newValue;
  554. return;
  555. }
  556. }
  557. // nope, add a new one then
  558. d_string d_key(key);
  559. fStateMap[d_key] = newValue;
  560. }
  561. #endif
  562. void updateParameterOutputs()
  563. {
  564. for (uint32_t i=0, count=fPlugin.getParameterCount(); i < count; ++i)
  565. {
  566. if (! fPlugin.isParameterOutput(i))
  567. continue;
  568. fLastControlValues[i] = fPlugin.getParameterValue(i);
  569. if (fPortControls[i] != nullptr)
  570. *fPortControls[i] = fLastControlValues[i];
  571. }
  572. #if DISTRHO_PLUGIN_WANT_LATENCY
  573. if (fPortLatency != nullptr)
  574. *fPortLatency = fPlugin.getLatency();
  575. #endif
  576. }
  577. };
  578. // -----------------------------------------------------------------------
  579. static LV2_Handle lv2_instantiate(const LV2_Descriptor*, double sampleRate, const char*, const LV2_Feature* const* features)
  580. {
  581. const LV2_Options_Option* options = nullptr;
  582. const LV2_URID_Map* uridMap = nullptr;
  583. const LV2_Worker_Schedule* worker = nullptr;
  584. for (int i=0; features[i] != nullptr; ++i)
  585. {
  586. if (std::strcmp(features[i]->URI, LV2_OPTIONS__options) == 0)
  587. options = (const LV2_Options_Option*)features[i]->data;
  588. else if (std::strcmp(features[i]->URI, LV2_URID__map) == 0)
  589. uridMap = (const LV2_URID_Map*)features[i]->data;
  590. else if (std::strcmp(features[i]->URI, LV2_WORKER__schedule) == 0)
  591. worker = (const LV2_Worker_Schedule*)features[i]->data;
  592. }
  593. if (options == nullptr)
  594. {
  595. d_stderr("Options feature missing, cannot continue!");
  596. return nullptr;
  597. }
  598. if (uridMap == nullptr)
  599. {
  600. d_stderr("URID Map feature missing, cannot continue!");
  601. return nullptr;
  602. }
  603. #if DISTRHO_PLUGIN_WANT_STATE
  604. if (worker == nullptr)
  605. {
  606. d_stderr("Worker feature missing, cannot continue!");
  607. return nullptr;
  608. }
  609. #endif
  610. d_lastBufferSize = 0;
  611. for (int i=0; options[i].key != 0; ++i)
  612. {
  613. if (options[i].key == uridMap->map(uridMap->handle, LV2_BUF_SIZE__maxBlockLength))
  614. {
  615. if (options[i].type == uridMap->map(uridMap->handle, LV2_ATOM__Int))
  616. d_lastBufferSize = *(const int*)options[i].value;
  617. else
  618. d_stderr("Host provides maxBlockLength but has wrong value type");
  619. break;
  620. }
  621. }
  622. if (d_lastBufferSize == 0)
  623. {
  624. d_stderr("Host does not provide maxBlockLength option");
  625. d_lastBufferSize = 2048;
  626. }
  627. d_lastSampleRate = sampleRate;
  628. return new PluginLv2(uridMap, worker);
  629. }
  630. #define instancePtr ((PluginLv2*)instance)
  631. static void lv2_connect_port(LV2_Handle instance, uint32_t port, void* dataLocation)
  632. {
  633. instancePtr->lv2_connect_port(port, dataLocation);
  634. }
  635. static void lv2_activate(LV2_Handle instance)
  636. {
  637. instancePtr->lv2_activate();
  638. }
  639. static void lv2_run(LV2_Handle instance, uint32_t sampleCount)
  640. {
  641. instancePtr->lv2_run(sampleCount);
  642. }
  643. static void lv2_deactivate(LV2_Handle instance)
  644. {
  645. instancePtr->lv2_deactivate();
  646. }
  647. static void lv2_cleanup(LV2_Handle instance)
  648. {
  649. delete instancePtr;
  650. }
  651. // -----------------------------------------------------------------------
  652. static uint32_t lv2_get_options(LV2_Handle instance, LV2_Options_Option* options)
  653. {
  654. return instancePtr->lv2_get_options(options);
  655. }
  656. static uint32_t lv2_set_options(LV2_Handle instance, const LV2_Options_Option* options)
  657. {
  658. return instancePtr->lv2_set_options(options);
  659. }
  660. // -----------------------------------------------------------------------
  661. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  662. static const LV2_Program_Descriptor* lv2_get_program(LV2_Handle instance, uint32_t index)
  663. {
  664. return instancePtr->lv2_get_program(index);
  665. }
  666. static void lv2_select_program(LV2_Handle instance, uint32_t bank, uint32_t program)
  667. {
  668. instancePtr->lv2_select_program(bank, program);
  669. }
  670. #endif
  671. // -----------------------------------------------------------------------
  672. #if DISTRHO_PLUGIN_WANT_STATE
  673. static LV2_State_Status lv2_save(LV2_Handle instance, LV2_State_Store_Function store, LV2_State_Handle handle, uint32_t flags, const LV2_Feature* const*)
  674. {
  675. return instancePtr->lv2_save(store, handle, flags);
  676. }
  677. static LV2_State_Status lv2_restore(LV2_Handle instance, LV2_State_Retrieve_Function retrieve, LV2_State_Handle handle, uint32_t, const LV2_Feature* const*)
  678. {
  679. return instancePtr->lv2_restore(retrieve, handle);
  680. }
  681. LV2_Worker_Status lv2_work(LV2_Handle instance, LV2_Worker_Respond_Function, LV2_Worker_Respond_Handle, uint32_t, const void* data)
  682. {
  683. return instancePtr->lv2_work(data);
  684. }
  685. #endif
  686. // -----------------------------------------------------------------------
  687. static const void* lv2_extension_data(const char* uri)
  688. {
  689. static const LV2_Options_Interface options = { lv2_get_options, lv2_set_options };
  690. if (std::strcmp(uri, LV2_OPTIONS__interface) == 0)
  691. return &options;
  692. #if DISTRHO_PLUGIN_WANT_PROGRAMS
  693. static const LV2_Programs_Interface programs = { lv2_get_program, lv2_select_program };
  694. if (std::strcmp(uri, LV2_PROGRAMS__Interface) == 0)
  695. return &programs;
  696. #endif
  697. #if DISTRHO_PLUGIN_WANT_STATE
  698. static const LV2_State_Interface state = { lv2_save, lv2_restore };
  699. static const LV2_Worker_Interface worker = { lv2_work, nullptr, nullptr };
  700. if (std::strcmp(uri, LV2_STATE__interface) == 0)
  701. return &state;
  702. if (std::strcmp(uri, LV2_WORKER__interface) == 0)
  703. return &worker;
  704. #endif
  705. return nullptr;
  706. }
  707. #undef instancePtr
  708. // -----------------------------------------------------------------------
  709. static const LV2_Descriptor sLv2Descriptor = {
  710. DISTRHO_PLUGIN_URI,
  711. lv2_instantiate,
  712. lv2_connect_port,
  713. lv2_activate,
  714. lv2_run,
  715. lv2_deactivate,
  716. lv2_cleanup,
  717. lv2_extension_data
  718. };
  719. // -----------------------------------------------------------------------
  720. END_NAMESPACE_DISTRHO
  721. DISTRHO_PLUGIN_EXPORT
  722. const LV2_Descriptor* lv2_descriptor(uint32_t index)
  723. {
  724. USE_NAMESPACE_DISTRHO
  725. return (index == 0) ? &sLv2Descriptor : nullptr;
  726. }
  727. // -----------------------------------------------------------------------