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.

1021 lines
32KB

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