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.

914 lines
28KB

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