The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

1779 lines
64KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. #if JucePlugin_Build_LV2 && (! (JUCE_ANDROID || JUCE_IOS))
  19. #ifndef _SCL_SECURE_NO_WARNINGS
  20. #define _SCL_SECURE_NO_WARNINGS
  21. #endif
  22. #ifndef _CRT_SECURE_NO_WARNINGS
  23. #define _CRT_SECURE_NO_WARNINGS
  24. #endif
  25. #define JUCE_CORE_INCLUDE_NATIVE_HEADERS 1
  26. #define JUCE_CORE_INCLUDE_OBJC_HELPERS 1
  27. #define JUCE_GUI_BASICS_INCLUDE_XHEADERS 1
  28. #include <juce_audio_plugin_client/juce_audio_plugin_client.h>
  29. #include <juce_audio_plugin_client/utility/juce_CheckSettingMacros.h>
  30. #include <juce_audio_plugin_client/utility/juce_LinuxMessageThread.h>
  31. #include <juce_audio_processors/utilities/juce_FlagCache.h>
  32. #include <juce_audio_processors/format_types/juce_LegacyAudioParameter.cpp>
  33. #include "JuceLV2Defines.h"
  34. #include <juce_audio_processors/format_types/juce_LV2Common.h>
  35. #include <fstream>
  36. #define JUCE_TURTLE_RECALL_URI "https://lv2-extensions.juce.com/turtle_recall"
  37. #ifndef JucePlugin_LV2URI
  38. #error "You need to define the JucePlugin_LV2URI value! If you're using the Projucer/CMake, the definition will be written into JuceLV2Defines.h automatically."
  39. #endif
  40. namespace juce
  41. {
  42. namespace lv2_client
  43. {
  44. constexpr auto uriSeparator = ":";
  45. const auto JucePluginLV2UriUi = String (JucePlugin_LV2URI) + uriSeparator + "UI";
  46. const auto JucePluginLV2UriState = String (JucePlugin_LV2URI) + uriSeparator + "StateString";
  47. const auto JucePluginLV2UriProgram = String (JucePlugin_LV2URI) + uriSeparator + "Program";
  48. static const LV2_Feature* findMatchingFeature (const LV2_Feature* const* features, const char* uri)
  49. {
  50. for (auto feature = features; *feature != nullptr; ++feature)
  51. if (std::strcmp ((*feature)->URI, uri) == 0)
  52. return *feature;
  53. return nullptr;
  54. }
  55. static bool hasFeature (const LV2_Feature* const* features, const char* uri)
  56. {
  57. return findMatchingFeature (features, uri) != nullptr;
  58. }
  59. template <typename Data>
  60. Data findMatchingFeatureData (const LV2_Feature* const* features, const char* uri)
  61. {
  62. if (const auto* feature = findMatchingFeature (features, uri))
  63. return static_cast<Data> (feature->data);
  64. return {};
  65. }
  66. static const LV2_Options_Option* findMatchingOption (const LV2_Options_Option* options, LV2_URID urid)
  67. {
  68. for (auto option = options; option->value != nullptr; ++option)
  69. if (option->key == urid)
  70. return option;
  71. return nullptr;
  72. }
  73. class ParameterStorage : private AudioProcessorListener
  74. {
  75. public:
  76. ParameterStorage (AudioProcessor& proc, LV2_URID_Map map)
  77. : processor (proc),
  78. mapFeature (map),
  79. legacyParameters (proc, false)
  80. {
  81. processor.addListener (this);
  82. }
  83. ~ParameterStorage() override
  84. {
  85. processor.removeListener (this);
  86. }
  87. /* This is the string that will be used to uniquely identify the parameter.
  88. This string will be written into the plugin's manifest as an IRI, so it must be
  89. syntactically valid.
  90. We escape this string rather than writing the user-defined parameter ID directly to avoid
  91. writing a malformed manifest in the case that user IDs contain spaces or other reserved
  92. characters. This should allow users to keep the same param IDs for all plugin formats.
  93. */
  94. static String getIri (const AudioProcessorParameter& param)
  95. {
  96. return URL::addEscapeChars (LegacyAudioParameter::getParamID (&param, false), true);
  97. }
  98. void setValueFromHost (LV2_URID urid, float value) noexcept
  99. {
  100. const auto it = uridToIndexMap.find (urid);
  101. if (it == uridToIndexMap.end())
  102. {
  103. // No such parameter.
  104. jassertfalse;
  105. return;
  106. }
  107. if (auto* param = legacyParameters.getParamForIndex ((int) it->second))
  108. {
  109. const auto scaledValue = [&]
  110. {
  111. if (auto* rangedParam = dynamic_cast<RangedAudioParameter*> (param))
  112. return rangedParam->convertTo0to1 (value);
  113. return value;
  114. }();
  115. if (scaledValue != param->getValue())
  116. {
  117. ScopedValueSetter<bool> scope (ignoreCallbacks, true);
  118. param->setValueNotifyingHost (scaledValue);
  119. }
  120. }
  121. }
  122. struct Options
  123. {
  124. bool parameterValue, gestureBegin, gestureEnd;
  125. };
  126. static constexpr auto newClientValue = 1 << 0,
  127. gestureBegan = 1 << 1,
  128. gestureEnded = 1 << 2;
  129. template <typename Callback>
  130. void forEachChangedParameter (Callback&& callback)
  131. {
  132. stateCache.ifSet ([this, &callback] (size_t parameterIndex, float, uint32_t bits)
  133. {
  134. const Options options { (bits & newClientValue) != 0,
  135. (bits & gestureBegan) != 0,
  136. (bits & gestureEnded) != 0 };
  137. callback (*legacyParameters.getParamForIndex ((int) parameterIndex),
  138. indexToUridMap[parameterIndex],
  139. options);
  140. });
  141. }
  142. private:
  143. void audioProcessorParameterChanged (AudioProcessor*, int parameterIndex, float value) override
  144. {
  145. if (! ignoreCallbacks)
  146. stateCache.setValueAndBits ((size_t) parameterIndex, value, newClientValue);
  147. }
  148. void audioProcessorParameterChangeGestureBegin (AudioProcessor*, int parameterIndex) override
  149. {
  150. if (! ignoreCallbacks)
  151. stateCache.setBits ((size_t) parameterIndex, gestureBegan);
  152. }
  153. void audioProcessorParameterChangeGestureEnd (AudioProcessor*, int parameterIndex) override
  154. {
  155. if (! ignoreCallbacks)
  156. stateCache.setBits ((size_t) parameterIndex, gestureEnded);
  157. }
  158. void audioProcessorChanged (AudioProcessor*, const ChangeDetails&) override {}
  159. AudioProcessor& processor;
  160. const LV2_URID_Map mapFeature;
  161. const LegacyAudioParametersWrapper legacyParameters;
  162. const std::vector<LV2_URID> indexToUridMap = [&]
  163. {
  164. std::vector<LV2_URID> result;
  165. for (auto* param : legacyParameters)
  166. {
  167. jassert ((size_t) param->getParameterIndex() == result.size());
  168. const auto uri = JucePlugin_LV2URI + String (uriSeparator) + getIri (*param);
  169. const auto urid = mapFeature.map (mapFeature.handle, uri.toRawUTF8());
  170. result.push_back (urid);
  171. }
  172. return result;
  173. }();
  174. const std::map<LV2_URID, size_t> uridToIndexMap = [&]
  175. {
  176. std::map<LV2_URID, size_t> result;
  177. size_t index = 0;
  178. for (const auto& urid : indexToUridMap)
  179. result.emplace (urid, index++);
  180. return result;
  181. }();
  182. FlaggedFloatCache<3> stateCache { (size_t) legacyParameters.getNumParameters() };
  183. bool ignoreCallbacks = false;
  184. JUCE_LEAK_DETECTOR (ParameterStorage)
  185. };
  186. enum class PortKind { seqInput, seqOutput, latencyOutput, freeWheelingInput, enabledInput };
  187. struct PortIndices
  188. {
  189. PortIndices (int numInputsIn, int numOutputsIn)
  190. : numInputs (numInputsIn), numOutputs (numOutputsIn) {}
  191. int getPortIndexForAudioInput (int audioIndex) const noexcept
  192. {
  193. return audioIndex;
  194. }
  195. int getPortIndexForAudioOutput (int audioIndex) const noexcept
  196. {
  197. return audioIndex + numInputs;
  198. }
  199. int getPortIndexFor (PortKind p) const noexcept { return getMaxAudioPortIndex() + (int) p; }
  200. // Audio ports are numbered from 0 to numInputs + numOutputs
  201. int getMaxAudioPortIndex() const noexcept { return numInputs + numOutputs; }
  202. int numInputs, numOutputs;
  203. };
  204. //==============================================================================
  205. class PlayHead : public AudioPlayHead
  206. {
  207. public:
  208. PlayHead (LV2_URID_Map mapFeatureIn, double sampleRateIn)
  209. : parser (mapFeatureIn), sampleRate (sampleRateIn)
  210. {
  211. }
  212. void invalidate() { info = nullopt; }
  213. void readNewInfo (const LV2_Atom_Event* event)
  214. {
  215. if (event->body.type != mLV2_ATOM__Object && event->body.type != mLV2_ATOM__Blank)
  216. return;
  217. const auto* object = reinterpret_cast<const LV2_Atom_Object*> (&event->body);
  218. if (object->body.otype != mLV2_TIME__Position)
  219. return;
  220. const LV2_Atom* atomFrame = nullptr;
  221. const LV2_Atom* atomSpeed = nullptr;
  222. const LV2_Atom* atomBar = nullptr;
  223. const LV2_Atom* atomBeat = nullptr;
  224. const LV2_Atom* atomBeatUnit = nullptr;
  225. const LV2_Atom* atomBeatsPerBar = nullptr;
  226. const LV2_Atom* atomBeatsPerMinute = nullptr;
  227. LV2_Atom_Object_Query query[] { { mLV2_TIME__frame, &atomFrame },
  228. { mLV2_TIME__speed, &atomSpeed },
  229. { mLV2_TIME__bar, &atomBar },
  230. { mLV2_TIME__beat, &atomBeat },
  231. { mLV2_TIME__beatUnit, &atomBeatUnit },
  232. { mLV2_TIME__beatsPerBar, &atomBeatsPerBar },
  233. { mLV2_TIME__beatsPerMinute, &atomBeatsPerMinute },
  234. LV2_ATOM_OBJECT_QUERY_END };
  235. lv2_atom_object_query (object, query);
  236. info.emplace();
  237. // Carla always seems to give us an integral 'beat' even though I'd expect
  238. // it to be a floating-point value
  239. const auto numerator = parser.parseNumericAtom<float> (atomBeatsPerBar);
  240. const auto denominator = parser.parseNumericAtom<int32_t> (atomBeatUnit);
  241. if (numerator.hasValue() && denominator.hasValue())
  242. info->setTimeSignature (TimeSignature { (int) *numerator, (int) *denominator });
  243. info->setBpm (parser.parseNumericAtom<float> (atomBeatsPerMinute));
  244. info->setPpqPosition (parser.parseNumericAtom<double> (atomBeat));
  245. info->setIsPlaying (parser.parseNumericAtom<float> (atomSpeed).orFallback (0.0f) != 0.0f);
  246. info->setBarCount (parser.parseNumericAtom<int64_t> (atomBar));
  247. if (const auto parsed = parser.parseNumericAtom<int64_t> (atomFrame))
  248. {
  249. info->setTimeInSamples (*parsed);
  250. info->setTimeInSeconds ((double) *parsed / sampleRate);
  251. }
  252. }
  253. Optional<PositionInfo> getPosition() const override
  254. {
  255. return info;
  256. }
  257. private:
  258. lv2_shared::NumericAtomParser parser;
  259. Optional<PositionInfo> info;
  260. double sampleRate;
  261. #define X(str) const LV2_URID m##str = parser.map (str);
  262. X (LV2_ATOM__Blank)
  263. X (LV2_ATOM__Object)
  264. X (LV2_TIME__Position)
  265. X (LV2_TIME__beat)
  266. X (LV2_TIME__beatUnit)
  267. X (LV2_TIME__beatsPerBar)
  268. X (LV2_TIME__beatsPerMinute)
  269. X (LV2_TIME__frame)
  270. X (LV2_TIME__speed)
  271. X (LV2_TIME__bar)
  272. #undef X
  273. JUCE_LEAK_DETECTOR (PlayHead)
  274. };
  275. //==============================================================================
  276. class Ports
  277. {
  278. public:
  279. Ports (LV2_URID_Map map, int numInputsIn, int numOutputsIn)
  280. : forge (map),
  281. indices (numInputsIn, numOutputsIn),
  282. mLV2_ATOM__Sequence (map.map (map.handle, LV2_ATOM__Sequence))
  283. {
  284. audioBuffers.resize (static_cast<size_t> (numInputsIn + numOutputsIn), nullptr);
  285. }
  286. void connect (int port, void* data)
  287. {
  288. // The following is not UB _if_ data really points to an object with the expected type.
  289. if (port == indices.getPortIndexFor (PortKind::seqInput))
  290. {
  291. inputData = static_cast<const LV2_Atom_Sequence*> (data);
  292. }
  293. else if (port == indices.getPortIndexFor (PortKind::seqOutput))
  294. {
  295. outputData = static_cast<LV2_Atom_Sequence*> (data);
  296. }
  297. else if (port == indices.getPortIndexFor (PortKind::latencyOutput))
  298. {
  299. latency = static_cast<float*> (data);
  300. }
  301. else if (port == indices.getPortIndexFor (PortKind::freeWheelingInput))
  302. {
  303. freeWheeling = static_cast<float*> (data);
  304. }
  305. else if (port == indices.getPortIndexFor (PortKind::enabledInput))
  306. {
  307. enabled = static_cast<float*> (data);
  308. }
  309. else if (isPositiveAndBelow (port, indices.getMaxAudioPortIndex()))
  310. {
  311. audioBuffers[(size_t) port] = static_cast<float*> (data);
  312. }
  313. else
  314. {
  315. // This port was not declared!
  316. jassertfalse;
  317. }
  318. }
  319. template <typename Callback>
  320. void forEachInputEvent (Callback&& callback)
  321. {
  322. if (inputData != nullptr && inputData->atom.type == mLV2_ATOM__Sequence)
  323. for (const auto* event : lv2_shared::SequenceIterator { lv2_shared::SequenceWithSize { inputData } })
  324. callback (event);
  325. }
  326. void prepareToWrite()
  327. {
  328. // Note: Carla seems to have a bug (verified with the eg-fifths plugin) where
  329. // the output buffer size is incorrect on alternate calls.
  330. forge.setBuffer (reinterpret_cast<char*> (outputData), outputData->atom.size);
  331. }
  332. void writeLatency (int value)
  333. {
  334. if (latency != nullptr)
  335. *latency = (float) value;
  336. }
  337. const float* getBufferForAudioInput (int index) const noexcept
  338. {
  339. return audioBuffers[(size_t) indices.getPortIndexForAudioInput (index)];
  340. }
  341. float* getBufferForAudioOutput (int index) const noexcept
  342. {
  343. return audioBuffers[(size_t) indices.getPortIndexForAudioOutput (index)];
  344. }
  345. bool isFreeWheeling() const noexcept
  346. {
  347. if (freeWheeling != nullptr)
  348. return *freeWheeling > 0.5f;
  349. return false;
  350. }
  351. bool isEnabled() const noexcept
  352. {
  353. if (enabled != nullptr)
  354. return *enabled > 0.5f;
  355. return true;
  356. }
  357. lv2_shared::AtomForge forge;
  358. PortIndices indices;
  359. private:
  360. static constexpr auto numParamPorts = 3;
  361. const LV2_Atom_Sequence* inputData = nullptr;
  362. LV2_Atom_Sequence* outputData = nullptr;
  363. float* latency = nullptr;
  364. float* freeWheeling = nullptr;
  365. float* enabled = nullptr;
  366. std::vector<float*> audioBuffers;
  367. const LV2_URID mLV2_ATOM__Sequence;
  368. JUCE_LEAK_DETECTOR (Ports)
  369. };
  370. class LV2PluginInstance : private AudioProcessorListener
  371. {
  372. public:
  373. LV2PluginInstance (double sampleRate,
  374. int64_t maxBlockSize,
  375. const char*,
  376. LV2_URID_Map mapFeatureIn)
  377. : mapFeature (mapFeatureIn),
  378. playHead (mapFeature, sampleRate)
  379. {
  380. processor->addListener (this);
  381. processor->setPlayHead (&playHead);
  382. prepare (sampleRate, (int) maxBlockSize);
  383. }
  384. void connect (uint32_t port, void* data)
  385. {
  386. ports.connect ((int) port, data);
  387. }
  388. void activate() {}
  389. template<typename UnaryFunction>
  390. static void iterateAudioBuffer (AudioBuffer<float>& ab, UnaryFunction fn)
  391. {
  392. float** sampleData = ab.getArrayOfWritePointers();
  393. for (int c = ab.getNumChannels(); --c >= 0;)
  394. for (int s = ab.getNumSamples(); --s >= 0;)
  395. fn (sampleData[c][s]);
  396. }
  397. static int countNaNs (AudioBuffer<float>& ab) noexcept
  398. {
  399. int count = 0;
  400. iterateAudioBuffer (ab, [&count] (float s)
  401. {
  402. if (std::isnan (s))
  403. ++count;
  404. });
  405. return count;
  406. }
  407. void run (uint32_t numSteps)
  408. {
  409. midi.clear();
  410. playHead.invalidate();
  411. ports.forEachInputEvent ([&] (const LV2_Atom_Event* event)
  412. {
  413. struct Callback
  414. {
  415. explicit Callback (LV2PluginInstance& s) : self (s) {}
  416. void setParameter (LV2_URID property, float value) const noexcept
  417. {
  418. self.parameters.setValueFromHost (property, value);
  419. }
  420. // The host probably shouldn't send us 'touched' messages.
  421. void gesture (LV2_URID, bool) const noexcept {}
  422. LV2PluginInstance& self;
  423. };
  424. patchSetHelper.processPatchSet (event, Callback { *this });
  425. playHead.readNewInfo (event);
  426. if (event->body.type == mLV2_MIDI__MidiEvent)
  427. midi.addEvent (event + 1, static_cast<int> (event->body.size), static_cast<int> (event->time.frames));
  428. });
  429. processor->setNonRealtime (ports.isFreeWheeling());
  430. for (auto i = 0, end = processor->getTotalNumInputChannels(); i < end; ++i)
  431. audio.copyFrom (i, 0, ports.getBufferForAudioInput (i), (int) numSteps);
  432. jassert (countNaNs (audio) == 0);
  433. {
  434. const ScopedLock lock { processor->getCallbackLock() };
  435. if (processor->isSuspended())
  436. {
  437. for (auto i = 0, end = processor->getTotalNumOutputChannels(); i < end; ++i)
  438. {
  439. const auto ptr = ports.getBufferForAudioOutput (i);
  440. std::fill (ptr, ptr + numSteps, 0.0f);
  441. }
  442. }
  443. else
  444. {
  445. const auto isEnabled = ports.isEnabled();
  446. if (auto* param = processor->getBypassParameter())
  447. {
  448. param->setValueNotifyingHost (isEnabled ? 0.0f : 1.0f);
  449. processor->processBlock (audio, midi);
  450. }
  451. else if (isEnabled)
  452. {
  453. processor->processBlock (audio, midi);
  454. }
  455. else
  456. {
  457. processor->processBlockBypassed (audio, midi);
  458. }
  459. }
  460. }
  461. for (auto i = 0, end = processor->getTotalNumOutputChannels(); i < end; ++i)
  462. {
  463. const auto src = audio.getReadPointer (i);
  464. const auto dst = ports.getBufferForAudioOutput (i);
  465. if (dst != nullptr)
  466. std::copy (src, src + numSteps, dst);
  467. }
  468. ports.prepareToWrite();
  469. auto* forge = ports.forge.get();
  470. lv2_shared::SequenceFrame sequence { forge, (uint32_t) 0 };
  471. parameters.forEachChangedParameter ([&] (const AudioProcessorParameter& param,
  472. LV2_URID paramUrid,
  473. const ParameterStorage::Options& options)
  474. {
  475. const auto sendTouched = [&] (bool state)
  476. {
  477. // TODO Implement begin/end change gesture support once it's supported by LV2
  478. ignoreUnused (state);
  479. };
  480. if (options.gestureBegin)
  481. sendTouched (true);
  482. if (options.parameterValue)
  483. {
  484. lv2_atom_forge_frame_time (forge, 0);
  485. lv2_shared::ObjectFrame object { forge, (uint32_t) 0, patchSetHelper.mLV2_PATCH__Set };
  486. lv2_atom_forge_key (forge, patchSetHelper.mLV2_PATCH__property);
  487. lv2_atom_forge_urid (forge, paramUrid);
  488. lv2_atom_forge_key (forge, patchSetHelper.mLV2_PATCH__value);
  489. lv2_atom_forge_float (forge, [&]
  490. {
  491. if (auto* rangedParam = dynamic_cast<const RangedAudioParameter*> (&param))
  492. return rangedParam->convertFrom0to1 (rangedParam->getValue());
  493. return param.getValue();
  494. }());
  495. }
  496. if (options.gestureEnd)
  497. sendTouched (false);
  498. });
  499. if (shouldSendStateChange.exchange (false))
  500. {
  501. lv2_atom_forge_frame_time (forge, 0);
  502. lv2_shared::ObjectFrame { forge, (uint32_t) 0, mLV2_STATE__StateChanged };
  503. }
  504. for (const auto meta : midi)
  505. {
  506. const auto bytes = static_cast<uint32_t> (meta.numBytes);
  507. lv2_atom_forge_frame_time (forge, meta.samplePosition);
  508. lv2_atom_forge_atom (forge, bytes, mLV2_MIDI__MidiEvent);
  509. lv2_atom_forge_write (forge, meta.data, bytes);
  510. }
  511. ports.writeLatency (processor->getLatencySamples());
  512. }
  513. void deactivate() {}
  514. LV2_State_Status store (LV2_State_Store_Function storeFn,
  515. LV2_State_Handle handle,
  516. uint32_t,
  517. const LV2_Feature* const*)
  518. {
  519. MemoryBlock block;
  520. processor->getStateInformation (block);
  521. const auto text = block.toBase64Encoding();
  522. storeFn (handle,
  523. mJucePluginLV2UriState,
  524. text.toRawUTF8(),
  525. text.getNumBytesAsUTF8() + 1,
  526. mLV2_ATOM__String,
  527. LV2_STATE_IS_POD | LV2_STATE_IS_PORTABLE);
  528. return LV2_STATE_SUCCESS;
  529. }
  530. LV2_State_Status retrieve (LV2_State_Retrieve_Function retrieveFn,
  531. LV2_State_Handle handle,
  532. uint32_t,
  533. const LV2_Feature* const*)
  534. {
  535. size_t size = 0;
  536. uint32_t type = 0;
  537. uint32_t dataFlags = 0;
  538. // Try retrieving a port index (if this is a 'program' preset).
  539. const auto* programData = retrieveFn (handle, mJucePluginLV2UriProgram, &size, &type, &dataFlags);
  540. if (programData != nullptr && type == mLV2_ATOM__Int && size == sizeof (int32_t))
  541. {
  542. const auto programIndex = readUnaligned<int32_t> (programData);
  543. processor->setCurrentProgram (programIndex);
  544. return LV2_STATE_SUCCESS;
  545. }
  546. // This doesn't seem to be a 'program' preset, try setting the full state from a string instead.
  547. const auto* data = retrieveFn (handle, mJucePluginLV2UriState, &size, &type, &dataFlags);
  548. if (data == nullptr)
  549. return LV2_STATE_ERR_NO_PROPERTY;
  550. if (type != mLV2_ATOM__String)
  551. return LV2_STATE_ERR_BAD_TYPE;
  552. String text (static_cast<const char*> (data), (size_t) size);
  553. MemoryBlock block;
  554. block.fromBase64Encoding (text);
  555. processor->setStateInformation (block.getData(), (int) block.getSize());
  556. return LV2_STATE_SUCCESS;
  557. }
  558. std::unique_ptr<AudioProcessorEditor> createEditor()
  559. {
  560. return std::unique_ptr<AudioProcessorEditor> (processor->createEditorIfNeeded());
  561. }
  562. void editorBeingDeleted (AudioProcessorEditor* editor)
  563. {
  564. processor->editorBeingDeleted (editor);
  565. }
  566. static std::unique_ptr<AudioProcessor> createProcessorInstance()
  567. {
  568. std::unique_ptr<AudioProcessor> result { createPluginFilterOfType (AudioProcessor::wrapperType_LV2) };
  569. #if defined (JucePlugin_PreferredChannelConfigurations)
  570. constexpr short channelConfigurations[][2] { JucePlugin_PreferredChannelConfigurations };
  571. static_assert (numElementsInArray (channelConfigurations) > 0,
  572. "JucePlugin_PreferredChannelConfigurations must contain at least one entry");
  573. static_assert (channelConfigurations[0][0] > 0 || channelConfigurations[0][1] > 0,
  574. "JucePlugin_PreferredChannelConfigurations must have at least one input or output channel");
  575. result->setPlayConfigDetails (channelConfigurations[0][0], channelConfigurations[0][1], 44100.0, 1024);
  576. const auto desiredChannels = std::make_tuple (channelConfigurations[0][0], channelConfigurations[0][1]);
  577. const auto actualChannels = std::make_tuple (result->getTotalNumInputChannels(), result->getTotalNumOutputChannels());
  578. if (desiredChannels != actualChannels)
  579. Logger::outputDebugString ("Failed to apply requested channel configuration!");
  580. #else
  581. result->enableAllBuses();
  582. #endif
  583. return result;
  584. }
  585. private:
  586. void audioProcessorParameterChanged (AudioProcessor*, int, float) override {}
  587. void audioProcessorChanged (AudioProcessor*, const ChangeDetails& details) override
  588. {
  589. // Only check for non-parameter state here because:
  590. // - Latency is automatically written every block.
  591. // - There's no way for an LV2 plugin to report an internal program change.
  592. // - Parameter info is hard-coded in the plugin's turtle description.
  593. if (details.nonParameterStateChanged)
  594. shouldSendStateChange = true;
  595. }
  596. void prepare (double sampleRate, int maxBlockSize)
  597. {
  598. jassert (processor != nullptr);
  599. processor->setRateAndBufferSizeDetails (sampleRate, maxBlockSize);
  600. processor->prepareToPlay (sampleRate, maxBlockSize);
  601. const auto numChannels = jmax (processor->getTotalNumInputChannels(),
  602. processor->getTotalNumOutputChannels());
  603. midi.ensureSize (8192);
  604. audio.setSize (numChannels, maxBlockSize);
  605. audio.clear();
  606. }
  607. LV2_URID map (StringRef uri) const { return mapFeature.map (mapFeature.handle, uri); }
  608. ScopedJuceInitialiser_GUI scopedJuceInitialiser;
  609. #if JUCE_LINUX || JUCE_BSD
  610. SharedResourcePointer<MessageThread> messageThread;
  611. #endif
  612. std::unique_ptr<AudioProcessor> processor = createProcessorInstance();
  613. const LV2_URID_Map mapFeature;
  614. ParameterStorage parameters { *processor, mapFeature };
  615. Ports ports { mapFeature,
  616. processor->getTotalNumInputChannels(),
  617. processor->getTotalNumOutputChannels() };
  618. lv2_shared::PatchSetHelper patchSetHelper { mapFeature, JucePlugin_LV2URI };
  619. PlayHead playHead;
  620. MidiBuffer midi;
  621. AudioBuffer<float> audio;
  622. std::atomic<bool> shouldSendStateChange { false };
  623. #define X(str) const LV2_URID m##str = map (str);
  624. X (JucePluginLV2UriProgram)
  625. X (JucePluginLV2UriState)
  626. X (LV2_ATOM__Int)
  627. X (LV2_ATOM__String)
  628. X (LV2_BUF_SIZE__maxBlockLength)
  629. X (LV2_BUF_SIZE__sequenceSize)
  630. X (LV2_MIDI__MidiEvent)
  631. X (LV2_PATCH__Set)
  632. X (LV2_STATE__StateChanged)
  633. #undef X
  634. JUCE_LEAK_DETECTOR (LV2PluginInstance)
  635. };
  636. //==============================================================================
  637. struct RecallFeature
  638. {
  639. int (*doRecall) (const char*) = [] (const char* libraryPath) -> int
  640. {
  641. const ScopedJuceInitialiser_GUI scope;
  642. const auto processor = LV2PluginInstance::createProcessorInstance();
  643. const File absolutePath { CharPointer_UTF8 { libraryPath } };
  644. processor->enableAllBuses();
  645. for (auto* fn : { writeManifestTtl, writeDspTtl, writeUiTtl })
  646. {
  647. const auto result = fn (*processor, absolutePath);
  648. if (result.wasOk())
  649. continue;
  650. std::cerr << result.getErrorMessage() << '\n';
  651. return 1;
  652. }
  653. return 0;
  654. };
  655. private:
  656. static String getPresetUri (int index)
  657. {
  658. return JucePlugin_LV2URI + String (uriSeparator) + "preset" + String (index + 1);
  659. }
  660. static std::ofstream openStream (const File& libraryPath, StringRef name)
  661. {
  662. return std::ofstream { libraryPath.getSiblingFile (name + ".ttl").getFullPathName().toRawUTF8() };
  663. }
  664. static Result writeManifestTtl (AudioProcessor& proc, const File& libraryPath)
  665. {
  666. auto os = openStream (libraryPath, "manifest");
  667. os << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n"
  668. "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"
  669. "@prefix pset: <http://lv2plug.in/ns/ext/presets#> .\n"
  670. "@prefix state: <http://lv2plug.in/ns/ext/state#> .\n"
  671. "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n"
  672. "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n"
  673. "\n"
  674. "<" JucePlugin_LV2URI ">\n"
  675. "\ta lv2:Plugin ;\n"
  676. "\tlv2:binary <" << URL::addEscapeChars (libraryPath.getFileName(), false) << "> ;\n"
  677. "\trdfs:seeAlso <dsp.ttl> .\n";
  678. if (proc.hasEditor())
  679. {
  680. #if JUCE_MAC
  681. #define JUCE_LV2_UI_KIND "CocoaUI"
  682. #elif JUCE_WINDOWS
  683. #define JUCE_LV2_UI_KIND "WindowsUI"
  684. #elif JUCE_LINUX
  685. #define JUCE_LV2_UI_KIND "X11UI"
  686. #else
  687. #error "LV2 UI is unsupported on this platform"
  688. #endif
  689. os << "\n"
  690. "<" << JucePluginLV2UriUi << ">\n"
  691. "\ta ui:" JUCE_LV2_UI_KIND " ;\n"
  692. "\tlv2:binary <" << URL::addEscapeChars (libraryPath.getFileName(), false) << "> ;\n"
  693. "\trdfs:seeAlso <ui.ttl> .\n"
  694. "\n";
  695. }
  696. for (auto i = 0, end = proc.getNumPrograms(); i < end; ++i)
  697. {
  698. os << "<" << getPresetUri (i) << ">\n"
  699. "\ta pset:Preset ;\n"
  700. "\tlv2:appliesTo <" JucePlugin_LV2URI "> ;\n"
  701. "\trdfs:label \"" << proc.getProgramName (i) << "\" ;\n"
  702. "\tstate:state [ <" << JucePluginLV2UriProgram << "> \"" << i << "\"^^xsd:int ; ] .\n"
  703. "\n";
  704. }
  705. return Result::ok();
  706. }
  707. static std::vector<const AudioProcessorParameterGroup*> findAllSubgroupsDepthFirst (const AudioProcessorParameterGroup& group,
  708. std::vector<const AudioProcessorParameterGroup*> foundSoFar = {})
  709. {
  710. foundSoFar.push_back (&group);
  711. for (auto* node : group)
  712. {
  713. if (auto* subgroup = node->getGroup())
  714. foundSoFar = findAllSubgroupsDepthFirst (*subgroup, std::move (foundSoFar));
  715. }
  716. return foundSoFar;
  717. }
  718. using GroupSymbolMap = std::map<const AudioProcessorParameterGroup*, String>;
  719. static String getFlattenedGroupSymbol (const AudioProcessorParameterGroup& group, String symbol = "")
  720. {
  721. if (auto* parent = group.getParent())
  722. return getFlattenedGroupSymbol (*parent, group.getID() + (symbol.isEmpty() ? "" : group.getSeparator() + symbol));
  723. return symbol;
  724. }
  725. static String getSymbolForGroup (const AudioProcessorParameterGroup& group)
  726. {
  727. const String allowedCharacters ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_0123456789");
  728. const auto base = getFlattenedGroupSymbol (group);
  729. if (base.isEmpty())
  730. return {};
  731. String copy;
  732. for (const auto character : base)
  733. copy << String::charToString (allowedCharacters.containsChar (character) ? character : (juce_wchar) '_');
  734. return "paramgroup_" + copy;
  735. }
  736. static GroupSymbolMap getGroupsAndSymbols (const std::vector<const AudioProcessorParameterGroup*>& groups)
  737. {
  738. std::set<String> symbols;
  739. GroupSymbolMap result;
  740. for (const auto& group : groups)
  741. {
  742. const auto symbol = [&]
  743. {
  744. const auto idealSymbol = getSymbolForGroup (*group);
  745. if (symbols.find (idealSymbol) == symbols.cend())
  746. return idealSymbol;
  747. for (auto i = 2; i < std::numeric_limits<int>::max(); ++i)
  748. {
  749. const auto toTest = idealSymbol + "_" + String (i);
  750. if (symbols.find (toTest) == symbols.cend())
  751. return toTest;
  752. }
  753. jassertfalse;
  754. return String();
  755. }();
  756. symbols.insert (symbol);
  757. result.emplace (group, symbol);
  758. }
  759. return result;
  760. }
  761. template <typename Fn>
  762. static void visitAllParameters (const GroupSymbolMap& groups, Fn&& fn)
  763. {
  764. for (const auto& group : groups)
  765. for (const auto* node : *group.first)
  766. if (auto* param = node->getParameter())
  767. fn (group.second, *param);
  768. }
  769. static Result writeDspTtl (AudioProcessor& proc, const File& libraryPath)
  770. {
  771. auto os = openStream (libraryPath, "dsp");
  772. os << "@prefix atom: <http://lv2plug.in/ns/ext/atom#> .\n"
  773. "@prefix bufs: <http://lv2plug.in/ns/ext/buf-size#> .\n"
  774. "@prefix doap: <http://usefulinc.com/ns/doap#> .\n"
  775. "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n"
  776. "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n"
  777. "@prefix midi: <http://lv2plug.in/ns/ext/midi#> .\n"
  778. "@prefix opts: <http://lv2plug.in/ns/ext/options#> .\n"
  779. "@prefix param: <http://lv2plug.in/ns/ext/parameters#> .\n"
  780. "@prefix patch: <http://lv2plug.in/ns/ext/patch#> .\n"
  781. "@prefix pg: <http://lv2plug.in/ns/ext/port-groups#> .\n"
  782. "@prefix plug: <" JucePlugin_LV2URI << uriSeparator << "> .\n"
  783. "@prefix pprop: <http://lv2plug.in/ns/ext/port-props#> .\n"
  784. "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n"
  785. "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n"
  786. "@prefix rsz: <http://lv2plug.in/ns/ext/resize-port#> .\n"
  787. "@prefix state: <http://lv2plug.in/ns/ext/state#> .\n"
  788. "@prefix time: <http://lv2plug.in/ns/ext/time#> .\n"
  789. "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n"
  790. "@prefix units: <http://lv2plug.in/ns/extensions/units#> .\n"
  791. "@prefix urid: <http://lv2plug.in/ns/ext/urid#> .\n"
  792. "@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n"
  793. "\n";
  794. LegacyAudioParametersWrapper legacyParameters (proc, false);
  795. const auto allGroups = findAllSubgroupsDepthFirst (legacyParameters.getGroup());
  796. const auto groupsAndSymbols = getGroupsAndSymbols (allGroups);
  797. const auto parameterVisitor = [&] (const String& symbol,
  798. const AudioProcessorParameter& param)
  799. {
  800. os << "plug:" << ParameterStorage::getIri (param) << "\n"
  801. "\ta lv2:Parameter ;\n"
  802. "\trdfs:label \"" << param.getName (1024) << "\" ;\n";
  803. if (symbol.isNotEmpty())
  804. os << "\tpg:group plug:" << symbol << " ;\n";
  805. os << "\trdfs:range atom:Float ;\n";
  806. if (auto* rangedParam = dynamic_cast<const RangedAudioParameter*> (&param))
  807. {
  808. os << "\tlv2:default " << rangedParam->convertFrom0to1 (rangedParam->getDefaultValue()) << " ;\n"
  809. "\tlv2:minimum " << rangedParam->getNormalisableRange().start << " ;\n"
  810. "\tlv2:maximum " << rangedParam->getNormalisableRange().end;
  811. }
  812. else
  813. {
  814. os << "\tlv2:default " << param.getDefaultValue() << " ;\n"
  815. "\tlv2:minimum 0.0 ;\n"
  816. "\tlv2:maximum 1.0";
  817. }
  818. // Avoid writing out loads of scale points for parameters with lots of steps
  819. constexpr auto stepLimit = 1000;
  820. const auto numSteps = param.getNumSteps();
  821. if (param.isDiscrete() && 2 <= numSteps && numSteps < stepLimit)
  822. {
  823. os << "\t ;\n"
  824. "\tlv2:portProperty lv2:enumeration " << (param.isBoolean() ? ", lv2:toggled " : "") << ";\n"
  825. "\tlv2:scalePoint ";
  826. const auto maxIndex = numSteps - 1;
  827. for (int i = 0; i < numSteps; ++i)
  828. {
  829. const auto value = (float) i / (float) maxIndex;
  830. const auto text = param.getText (value, 1024);
  831. os << (i != 0 ? ", " : "") << "[\n"
  832. "\t\trdfs:label \"" << text << "\" ;\n"
  833. "\t\trdf:value " << value << " ;\n"
  834. "\t]";
  835. }
  836. }
  837. os << " .\n\n";
  838. };
  839. visitAllParameters (groupsAndSymbols, parameterVisitor);
  840. for (const auto& groupInfo : groupsAndSymbols)
  841. {
  842. if (groupInfo.second.isEmpty())
  843. continue;
  844. os << "plug:" << groupInfo.second << "\n"
  845. "\ta pg:Group ;\n";
  846. if (auto* parent = groupInfo.first->getParent())
  847. {
  848. if (parent->getParent() != nullptr)
  849. {
  850. const auto it = groupsAndSymbols.find (parent);
  851. if (it != groupsAndSymbols.cend())
  852. os << "\tpg:subGroupOf plug:" << it->second << " ;\n";
  853. }
  854. }
  855. os << "\tlv2:symbol \"" << groupInfo.second << "\" ;\n"
  856. "\tlv2:name \"" << groupInfo.first->getName() << "\" .\n\n";
  857. }
  858. const auto getBaseBusName = [] (bool isInput) { return isInput ? "input_group_" : "output_group_"; };
  859. for (const auto isInput : { true, false })
  860. {
  861. const auto baseBusName = getBaseBusName (isInput);
  862. const auto groupKind = isInput ? "InputGroup" : "OutputGroup";
  863. const auto busCount = proc.getBusCount (isInput);
  864. for (auto i = 0; i < busCount; ++i)
  865. {
  866. if (const auto* bus = proc.getBus (isInput, i))
  867. {
  868. os << "plug:" << baseBusName << (i + 1) << "\n"
  869. "\ta pg:" << groupKind << " ;\n"
  870. "\tlv2:name \"" << bus->getName() << "\" ;\n"
  871. "\tlv2:symbol \"" << baseBusName << (i + 1) << "\" .\n\n";
  872. }
  873. }
  874. }
  875. os << "<" JucePlugin_LV2URI ">\n";
  876. if (proc.hasEditor())
  877. os << "\tui:ui <" << JucePluginLV2UriUi << "> ;\n";
  878. const auto versionParts = StringArray::fromTokens (JucePlugin_VersionString, ".", "");
  879. const auto getVersionOrZero = [&] (int indexFromBack)
  880. {
  881. const auto str = versionParts[versionParts.size() - indexFromBack];
  882. return str.isEmpty() ? 0 : str.getIntValue();
  883. };
  884. const auto minorVersion = getVersionOrZero (2);
  885. const auto microVersion = getVersionOrZero (1);
  886. os << "\ta "
  887. #if JucePlugin_IsSynth
  888. "lv2:InstrumentPlugin"
  889. #else
  890. "lv2:Plugin"
  891. #endif
  892. " ;\n"
  893. "\tdoap:name \"" JucePlugin_Name "\" ;\n"
  894. "\tdoap:description \"" JucePlugin_Desc "\" ;\n"
  895. "\tlv2:minorVersion " << minorVersion << " ;\n"
  896. "\tlv2:microVersion " << microVersion << " ;\n"
  897. "\tdoap:maintainer [\n"
  898. "\t\ta foaf:Person ;\n"
  899. "\t\tfoaf:name \"" JucePlugin_Manufacturer "\" ;\n"
  900. "\t\tfoaf:homepage <" JucePlugin_ManufacturerWebsite "> ;\n"
  901. "\t\tfoaf:mbox <" JucePlugin_ManufacturerEmail "> ;\n"
  902. "\t] ;\n"
  903. "\tdoap:release [\n"
  904. "\t\ta doap:Version ;\n"
  905. "\t\tdoap:revision \"" JucePlugin_VersionString "\" ;\n"
  906. "\t] ;\n"
  907. "\tlv2:optionalFeature\n"
  908. "\t\tlv2:hardRTCapable ;\n"
  909. "\tlv2:extensionData\n"
  910. "\t\tstate:interface ;\n"
  911. "\tlv2:requiredFeature\n"
  912. "\t\turid:map ,\n"
  913. "\t\topts:options ,\n"
  914. "\t\tbufs:boundedBlockLength ;\n";
  915. for (const auto isInput : { true, false })
  916. {
  917. const auto kind = isInput ? "mainInput" : "mainOutput";
  918. if (proc.getBusCount (isInput) > 0)
  919. os << "\tpg:" << kind << " plug:" << getBaseBusName (isInput) << "1 ;\n";
  920. }
  921. if (legacyParameters.size() != 0)
  922. {
  923. for (const auto header : { "writable", "readable" })
  924. {
  925. os << "\tpatch:" << header;
  926. bool isFirst = true;
  927. for (const auto* param : legacyParameters)
  928. {
  929. os << (isFirst ? "" : " ,") << "\n\t\tplug:" << ParameterStorage::getIri (*param);
  930. isFirst = false;
  931. }
  932. os << " ;\n";
  933. }
  934. }
  935. os << "\tlv2:port [\n";
  936. const PortIndices indices (proc.getTotalNumInputChannels(),
  937. proc.getTotalNumOutputChannels());
  938. const auto designationMap = [&]
  939. {
  940. std::map<AudioChannelSet::ChannelType, String> result;
  941. for (const auto& pair : lv2_shared::channelDesignationMap)
  942. result.emplace (pair.second, pair.first);
  943. return result;
  944. }();
  945. // TODO add support for specific audio group kinds
  946. for (const auto isInput : { true, false })
  947. {
  948. const auto baseBusName = getBaseBusName (isInput);
  949. const auto portKind = isInput ? "InputPort" : "OutputPort";
  950. const auto portName = isInput ? "Audio In " : "Audio Out ";
  951. const auto portSymbol = isInput ? "audio_in_" : "audio_out_";
  952. const auto busCount = proc.getBusCount (isInput);
  953. auto channelCounter = 0;
  954. for (auto busIndex = 0; busIndex < busCount; ++busIndex)
  955. {
  956. if (const auto* bus = proc.getBus (isInput, busIndex))
  957. {
  958. const auto channelCount = bus->getNumberOfChannels();
  959. const auto optionalBus = ! bus->isEnabledByDefault();
  960. for (auto channelIndex = 0; channelIndex < channelCount; ++channelIndex, ++channelCounter)
  961. {
  962. const auto portIndex = isInput ? indices.getPortIndexForAudioInput (channelCounter)
  963. : indices.getPortIndexForAudioOutput (channelCounter);
  964. os << "\t\ta lv2:" << portKind << " , lv2:AudioPort ;\n"
  965. "\t\tlv2:index " << portIndex << " ;\n"
  966. "\t\tlv2:symbol \"" << portSymbol << (channelCounter + 1) << "\" ;\n"
  967. "\t\tlv2:name \"" << portName << (channelCounter + 1) << "\" ;\n"
  968. "\t\tpg:group plug:" << baseBusName << (busIndex + 1) << " ;\n";
  969. if (optionalBus)
  970. os << "\t\tlv2:portProperty lv2:connectionOptional ;\n";
  971. const auto designation = bus->getCurrentLayout().getTypeOfChannel (channelIndex);
  972. const auto it = designationMap.find (designation);
  973. if (it != designationMap.end())
  974. os << "\t\tlv2:designation <" << it->second << "> ;\n";
  975. os << "\t] , [\n";
  976. }
  977. }
  978. }
  979. }
  980. // In the event that the plugin decides to send all of its parameters in one go,
  981. // we should ensure that the output buffer is large enough to accommodate, with some
  982. // extra room for the sequence header, MIDI messages etc..
  983. const auto patchSetSizeBytes = 72;
  984. const auto additionalSize = 8192;
  985. const auto atomPortMinSize = proc.getParameters().size() * patchSetSizeBytes + additionalSize;
  986. os << "\t\ta lv2:InputPort , atom:AtomPort ;\n"
  987. "\t\trsz:minimumSize " << atomPortMinSize << " ;\n"
  988. "\t\tatom:bufferType atom:Sequence ;\n"
  989. "\t\tatom:supports\n";
  990. #if ! JucePlugin_IsSynth && ! JucePlugin_IsMidiEffect
  991. if (proc.acceptsMidi())
  992. #endif
  993. os << "\t\t\tmidi:MidiEvent ,\n";
  994. os << "\t\t\tpatch:Message ,\n"
  995. "\t\t\ttime:Position ;\n"
  996. "\t\tlv2:designation lv2:control ;\n"
  997. "\t\tlv2:index " << indices.getPortIndexFor (PortKind::seqInput) << " ;\n"
  998. "\t\tlv2:symbol \"in\" ;\n"
  999. "\t\tlv2:name \"In\" ;\n"
  1000. "\t] , [\n"
  1001. "\t\ta lv2:OutputPort , atom:AtomPort ;\n"
  1002. "\t\trsz:minimumSize " << atomPortMinSize << " ;\n"
  1003. "\t\tatom:bufferType atom:Sequence ;\n"
  1004. "\t\tatom:supports\n";
  1005. #if ! JucePlugin_IsMidiEffect
  1006. if (proc.producesMidi())
  1007. #endif
  1008. os << "\t\t\tmidi:MidiEvent ,\n";
  1009. os << "\t\t\tpatch:Message ;\n"
  1010. "\t\tlv2:designation lv2:control ;\n"
  1011. "\t\tlv2:index " << indices.getPortIndexFor (PortKind::seqOutput) << " ;\n"
  1012. "\t\tlv2:symbol \"out\" ;\n"
  1013. "\t\tlv2:name \"Out\" ;\n"
  1014. "\t] , [\n"
  1015. "\t\ta lv2:OutputPort , lv2:ControlPort ;\n"
  1016. "\t\tlv2:designation lv2:latency ;\n"
  1017. "\t\tlv2:symbol \"latency\" ;\n"
  1018. "\t\tlv2:name \"Latency\" ;\n"
  1019. "\t\tlv2:index " << indices.getPortIndexFor (PortKind::latencyOutput) << " ;\n"
  1020. "\t\tlv2:portProperty lv2:reportsLatency , lv2:integer , lv2:connectionOptional , pprop:notOnGUI ;\n"
  1021. "\t\tunits:unit units:frame ;\n"
  1022. "\t] , [\n"
  1023. "\t\ta lv2:InputPort , lv2:ControlPort ;\n"
  1024. "\t\tlv2:designation lv2:freeWheeling ;\n"
  1025. "\t\tlv2:symbol \"freeWheeling\" ;\n"
  1026. "\t\tlv2:name \"Free Wheeling\" ;\n"
  1027. "\t\tlv2:default 0.0 ;\n"
  1028. "\t\tlv2:minimum 0.0 ;\n"
  1029. "\t\tlv2:maximum 1.0 ;\n"
  1030. "\t\tlv2:index " << indices.getPortIndexFor (PortKind::freeWheelingInput) << " ;\n"
  1031. "\t\tlv2:portProperty lv2:toggled , lv2:connectionOptional , pprop:notOnGUI ;\n"
  1032. "\t] , [\n"
  1033. "\t\ta lv2:InputPort , lv2:ControlPort ;\n"
  1034. "\t\tlv2:designation lv2:enabled ;\n"
  1035. "\t\tlv2:symbol \"enabled\" ;\n"
  1036. "\t\tlv2:name \"Enabled\" ;\n"
  1037. "\t\tlv2:default 1.0 ;\n"
  1038. "\t\tlv2:minimum 0.0 ;\n"
  1039. "\t\tlv2:maximum 1.0 ;\n"
  1040. "\t\tlv2:index " << indices.getPortIndexFor (PortKind::enabledInput) << " ;\n"
  1041. "\t\tlv2:portProperty lv2:toggled , lv2:connectionOptional , pprop:notOnGUI ;\n"
  1042. "\t] ;\n"
  1043. "\topts:supportedOption\n"
  1044. "\t\tbufs:maxBlockLength .\n";
  1045. return Result::ok();
  1046. }
  1047. static Result writeUiTtl (AudioProcessor& proc, const File& libraryPath)
  1048. {
  1049. if (! proc.hasEditor())
  1050. return Result::ok();
  1051. auto os = openStream (libraryPath, "ui");
  1052. const auto editorInstance = rawToUniquePtr (proc.createEditor());
  1053. const auto resizeFeatureString = editorInstance->isResizable() ? "ui:resize" : "ui:noUserResize";
  1054. os << "@prefix lv2: <http://lv2plug.in/ns/lv2core#> .\n"
  1055. "@prefix opts: <http://lv2plug.in/ns/ext/options#> .\n"
  1056. "@prefix param: <http://lv2plug.in/ns/ext/parameters#> .\n"
  1057. "@prefix ui: <http://lv2plug.in/ns/extensions/ui#> .\n"
  1058. "@prefix urid: <http://lv2plug.in/ns/ext/urid#> .\n"
  1059. "\n"
  1060. "<" << JucePluginLV2UriUi << ">\n"
  1061. "\tlv2:extensionData\n"
  1062. #if JUCE_LINUX || JUCE_BSD
  1063. "\t\tui:idleInterface ,\n"
  1064. #endif
  1065. "\t\topts:interface ,\n"
  1066. "\t\tui:noUserResize ,\n" // resize and noUserResize are always present in the extension data array
  1067. "\t\tui:resize ;\n"
  1068. "\n"
  1069. "\tlv2:requiredFeature\n"
  1070. #if JUCE_LINUX || JUCE_BSD
  1071. "\t\tui:idleInterface ,\n"
  1072. #endif
  1073. "\t\turid:map ,\n"
  1074. "\t\tui:parent ,\n"
  1075. "\t\t<http://lv2plug.in/ns/ext/instance-access> ;\n"
  1076. "\n"
  1077. "\tlv2:optionalFeature\n"
  1078. "\t\t" << resizeFeatureString << " ,\n"
  1079. "\t\topts:interface ,\n"
  1080. "\t\topts:options ;\n\n"
  1081. "\topts:supportedOption\n"
  1082. "\t\tui:scaleFactor ,\n"
  1083. "\t\tparam:sampleRate .\n";
  1084. return Result::ok();
  1085. }
  1086. JUCE_LEAK_DETECTOR (RecallFeature)
  1087. };
  1088. //==============================================================================
  1089. LV2_SYMBOL_EXPORT const LV2_Descriptor* lv2_descriptor (uint32_t index)
  1090. {
  1091. PluginHostType::jucePlugInClientCurrentWrapperType = AudioProcessor::wrapperType_LV2;
  1092. if (index != 0)
  1093. return nullptr;
  1094. static const LV2_Descriptor descriptor
  1095. {
  1096. JucePlugin_LV2URI, // TODO some constexpr check that this is a valid URI in terms of RFC 3986
  1097. [] (const LV2_Descriptor*,
  1098. double sampleRate,
  1099. const char* pathToBundle,
  1100. const LV2_Feature* const* features) -> LV2_Handle
  1101. {
  1102. const auto* mapFeature = findMatchingFeatureData<const LV2_URID_Map*> (features, LV2_URID__map);
  1103. if (mapFeature == nullptr)
  1104. {
  1105. // The host doesn't provide the 'urid map' feature
  1106. jassertfalse;
  1107. return nullptr;
  1108. }
  1109. const auto boundedBlockLength = hasFeature (features, LV2_BUF_SIZE__boundedBlockLength);
  1110. if (! boundedBlockLength)
  1111. {
  1112. // The host doesn't provide the 'bounded block length' feature
  1113. jassertfalse;
  1114. return nullptr;
  1115. }
  1116. const auto* options = findMatchingFeatureData<const LV2_Options_Option*> (features, LV2_OPTIONS__options);
  1117. if (options == nullptr)
  1118. {
  1119. // The host doesn't provide the 'options' feature
  1120. jassertfalse;
  1121. return nullptr;
  1122. }
  1123. const lv2_shared::NumericAtomParser parser { *mapFeature };
  1124. const auto blockLengthUrid = mapFeature->map (mapFeature->handle, LV2_BUF_SIZE__maxBlockLength);
  1125. const auto blockSize = parser.parseNumericOption<int64_t> (findMatchingOption (options, blockLengthUrid));
  1126. if (! blockSize.hasValue())
  1127. {
  1128. // The host doesn't specify a maximum block size
  1129. jassertfalse;
  1130. return nullptr;
  1131. }
  1132. return new LV2PluginInstance { sampleRate, *blockSize, pathToBundle, *mapFeature };
  1133. },
  1134. [] (LV2_Handle instance, uint32_t port, void* data)
  1135. {
  1136. static_cast<LV2PluginInstance*> (instance)->connect (port, data);
  1137. },
  1138. [] (LV2_Handle instance) { static_cast<LV2PluginInstance*> (instance)->activate(); },
  1139. [] (LV2_Handle instance, uint32_t sampleCount)
  1140. {
  1141. static_cast<LV2PluginInstance*> (instance)->run (sampleCount);
  1142. },
  1143. [] (LV2_Handle instance) { static_cast<LV2PluginInstance*> (instance)->deactivate(); },
  1144. [] (LV2_Handle instance)
  1145. {
  1146. JUCE_AUTORELEASEPOOL
  1147. {
  1148. delete static_cast<LV2PluginInstance*> (instance);
  1149. }
  1150. },
  1151. [] (const char* uri) -> const void*
  1152. {
  1153. const auto uriMatches = [&] (const LV2_Feature& f) { return std::strcmp (f.URI, uri) == 0; };
  1154. static RecallFeature recallFeature;
  1155. static LV2_State_Interface stateInterface
  1156. {
  1157. [] (LV2_Handle instance,
  1158. LV2_State_Store_Function store,
  1159. LV2_State_Handle handle,
  1160. uint32_t flags,
  1161. const LV2_Feature* const* features) -> LV2_State_Status
  1162. {
  1163. return static_cast<LV2PluginInstance*> (instance)->store (store, handle, flags, features);
  1164. },
  1165. [] (LV2_Handle instance,
  1166. LV2_State_Retrieve_Function retrieve,
  1167. LV2_State_Handle handle,
  1168. uint32_t flags,
  1169. const LV2_Feature* const* features) -> LV2_State_Status
  1170. {
  1171. return static_cast<LV2PluginInstance*> (instance)->retrieve (retrieve, handle, flags, features);
  1172. }
  1173. };
  1174. static const LV2_Feature features[] { { JUCE_TURTLE_RECALL_URI, &recallFeature },
  1175. { LV2_STATE__interface, &stateInterface } };
  1176. const auto it = std::find_if (std::begin (features), std::end (features), uriMatches);
  1177. return it != std::end (features) ? it->data : nullptr;
  1178. }
  1179. };
  1180. return &descriptor;
  1181. }
  1182. static Optional<float> findScaleFactor (const LV2_URID_Map* symap, const LV2_Options_Option* options)
  1183. {
  1184. if (options == nullptr || symap == nullptr)
  1185. return {};
  1186. const lv2_shared::NumericAtomParser parser { *symap };
  1187. const auto scaleFactorUrid = symap->map (symap->handle, LV2_UI__scaleFactor);
  1188. const auto* scaleFactorOption = findMatchingOption (options, scaleFactorUrid);
  1189. return parser.parseNumericOption<float> (scaleFactorOption);
  1190. }
  1191. class LV2UIInstance : private Component,
  1192. private ComponentListener
  1193. {
  1194. public:
  1195. LV2UIInstance (const char*,
  1196. const char*,
  1197. LV2UI_Write_Function writeFunctionIn,
  1198. LV2UI_Controller controllerIn,
  1199. LV2UI_Widget* widget,
  1200. LV2PluginInstance* pluginIn,
  1201. LV2UI_Widget parentIn,
  1202. const LV2_URID_Map* symapIn,
  1203. const LV2UI_Resize* resizeFeatureIn,
  1204. Optional<float> scaleFactorIn)
  1205. : writeFunction (writeFunctionIn),
  1206. controller (controllerIn),
  1207. plugin (pluginIn),
  1208. parent (parentIn),
  1209. symap (symapIn),
  1210. resizeFeature (resizeFeatureIn),
  1211. scaleFactor (scaleFactorIn),
  1212. editor (plugin->createEditor())
  1213. {
  1214. jassert (plugin != nullptr);
  1215. jassert (parent != nullptr);
  1216. jassert (editor != nullptr);
  1217. if (editor == nullptr)
  1218. return;
  1219. const auto bounds = getSizeToContainChild();
  1220. setSize (bounds.getWidth(), bounds.getHeight());
  1221. addAndMakeVisible (*editor);
  1222. setBroughtToFrontOnMouseClick (true);
  1223. setOpaque (true);
  1224. setVisible (false);
  1225. removeFromDesktop();
  1226. addToDesktop (0, parent);
  1227. editor->addComponentListener (this);
  1228. *widget = getWindowHandle();
  1229. setVisible (true);
  1230. editor->setScaleFactor (getScaleFactor());
  1231. requestResize();
  1232. }
  1233. ~LV2UIInstance() override
  1234. {
  1235. plugin->editorBeingDeleted (editor.get());
  1236. }
  1237. // This is called by the host when a parameter changes.
  1238. // We don't care, our UI will listen to the processor directly.
  1239. void portEvent (uint32_t, uint32_t, uint32_t, const void*) {}
  1240. // Called when the host requests a resize
  1241. int resize (int width, int height)
  1242. {
  1243. const ScopedValueSetter<bool> scope (hostRequestedResize, true);
  1244. setSize (width, height);
  1245. return 0;
  1246. }
  1247. // Called by the host to give us an opportunity to process UI events
  1248. void idleCallback()
  1249. {
  1250. #if JUCE_LINUX || JUCE_BSD
  1251. messageThread->processPendingEvents();
  1252. #endif
  1253. }
  1254. void resized() override
  1255. {
  1256. const ScopedValueSetter<bool> scope (hostRequestedResize, true);
  1257. if (editor != nullptr)
  1258. {
  1259. const auto localArea = editor->getLocalArea (this, getLocalBounds());
  1260. editor->setBoundsConstrained ({ localArea.getWidth(), localArea.getHeight() });
  1261. }
  1262. }
  1263. void paint (Graphics& g) override { g.fillAll (Colours::black); }
  1264. uint32_t getOptions (LV2_Options_Option* options)
  1265. {
  1266. const auto scaleFactorUrid = symap->map (symap->handle, LV2_UI__scaleFactor);
  1267. const auto floatUrid = symap->map (symap->handle, LV2_ATOM__Float);;
  1268. for (auto* opt = options; opt->key != 0; ++opt)
  1269. {
  1270. if (opt->context != LV2_OPTIONS_INSTANCE || opt->subject != 0 || opt->key != scaleFactorUrid)
  1271. continue;
  1272. if (scaleFactor.hasValue())
  1273. {
  1274. opt->type = floatUrid;
  1275. opt->size = sizeof (float);
  1276. opt->value = &(*scaleFactor);
  1277. }
  1278. }
  1279. return LV2_OPTIONS_SUCCESS;
  1280. }
  1281. uint32_t setOptions (const LV2_Options_Option* options)
  1282. {
  1283. const auto scaleFactorUrid = symap->map (symap->handle, LV2_UI__scaleFactor);
  1284. const auto floatUrid = symap->map (symap->handle, LV2_ATOM__Float);;
  1285. for (auto* opt = options; opt->key != 0; ++opt)
  1286. {
  1287. if (opt->context != LV2_OPTIONS_INSTANCE
  1288. || opt->subject != 0
  1289. || opt->key != scaleFactorUrid
  1290. || opt->type != floatUrid
  1291. || opt->size != sizeof (float))
  1292. {
  1293. continue;
  1294. }
  1295. scaleFactor = *static_cast<const float*> (opt->value);
  1296. updateScale();
  1297. }
  1298. return LV2_OPTIONS_SUCCESS;
  1299. }
  1300. private:
  1301. void updateScale()
  1302. {
  1303. editor->setScaleFactor (getScaleFactor());
  1304. requestResize();
  1305. }
  1306. Rectangle<int> getSizeToContainChild() const
  1307. {
  1308. if (editor != nullptr)
  1309. return getLocalArea (editor.get(), editor->getLocalBounds());
  1310. return {};
  1311. }
  1312. float getScaleFactor() const noexcept
  1313. {
  1314. return scaleFactor.hasValue() ? *scaleFactor : 1.0f;
  1315. }
  1316. void componentMovedOrResized (Component&, bool, bool wasResized) override
  1317. {
  1318. if (! hostRequestedResize && wasResized)
  1319. requestResize();
  1320. }
  1321. void write (uint32_t portIndex, uint32_t bufferSize, uint32_t portProtocol, const void* data)
  1322. {
  1323. writeFunction (controller, portIndex, bufferSize, portProtocol, data);
  1324. }
  1325. void requestResize()
  1326. {
  1327. if (editor == nullptr)
  1328. return;
  1329. const auto bounds = getSizeToContainChild();
  1330. if (resizeFeature == nullptr)
  1331. return;
  1332. if (auto* fn = resizeFeature->ui_resize)
  1333. fn (resizeFeature->handle, bounds.getWidth(), bounds.getHeight());
  1334. setSize (bounds.getWidth(), bounds.getHeight());
  1335. repaint();
  1336. }
  1337. #if JUCE_LINUX || JUCE_BSD
  1338. SharedResourcePointer<HostDrivenEventLoop> messageThread;
  1339. #endif
  1340. LV2UI_Write_Function writeFunction;
  1341. LV2UI_Controller controller;
  1342. LV2PluginInstance* plugin;
  1343. LV2UI_Widget parent;
  1344. const LV2_URID_Map* symap = nullptr;
  1345. const LV2UI_Resize* resizeFeature = nullptr;
  1346. Optional<float> scaleFactor;
  1347. std::unique_ptr<AudioProcessorEditor> editor;
  1348. bool hostRequestedResize = false;
  1349. JUCE_LEAK_DETECTOR (LV2UIInstance)
  1350. };
  1351. LV2_SYMBOL_EXPORT const LV2UI_Descriptor* lv2ui_descriptor (uint32_t index)
  1352. {
  1353. if (index != 0)
  1354. return nullptr;
  1355. static const LV2UI_Descriptor descriptor
  1356. {
  1357. JucePluginLV2UriUi.toRawUTF8(), // TODO some constexpr check that this is a valid URI in terms of RFC 3986
  1358. [] (const LV2UI_Descriptor*,
  1359. const char* pluginUri,
  1360. const char* bundlePath,
  1361. LV2UI_Write_Function writeFunction,
  1362. LV2UI_Controller controller,
  1363. LV2UI_Widget* widget,
  1364. const LV2_Feature* const* features) -> LV2UI_Handle
  1365. {
  1366. #if JUCE_LINUX || JUCE_BSD
  1367. SharedResourcePointer<HostDrivenEventLoop> messageThread;
  1368. #endif
  1369. auto* plugin = findMatchingFeatureData<LV2PluginInstance*> (features, LV2_INSTANCE_ACCESS_URI);
  1370. if (plugin == nullptr)
  1371. {
  1372. // No instance access
  1373. jassertfalse;
  1374. return nullptr;
  1375. }
  1376. auto* parent = findMatchingFeatureData<LV2UI_Widget> (features, LV2_UI__parent);
  1377. if (parent == nullptr)
  1378. {
  1379. // No parent access
  1380. jassertfalse;
  1381. return nullptr;
  1382. }
  1383. auto* resizeFeature = findMatchingFeatureData<const LV2UI_Resize*> (features, LV2_UI__resize);
  1384. const auto* symap = findMatchingFeatureData<const LV2_URID_Map*> (features, LV2_URID__map);
  1385. const auto scaleFactor = findScaleFactor (symap, findMatchingFeatureData<const LV2_Options_Option*> (features, LV2_OPTIONS__options));
  1386. return new LV2UIInstance { pluginUri,
  1387. bundlePath,
  1388. writeFunction,
  1389. controller,
  1390. widget,
  1391. plugin,
  1392. parent,
  1393. symap,
  1394. resizeFeature,
  1395. scaleFactor };
  1396. },
  1397. [] (LV2UI_Handle ui)
  1398. {
  1399. #if JUCE_LINUX || JUCE_BSD
  1400. SharedResourcePointer<HostDrivenEventLoop> messageThread;
  1401. #endif
  1402. JUCE_AUTORELEASEPOOL
  1403. {
  1404. delete static_cast<LV2UIInstance*> (ui);
  1405. }
  1406. },
  1407. [] (LV2UI_Handle ui, uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  1408. {
  1409. JUCE_ASSERT_MESSAGE_THREAD
  1410. static_cast<LV2UIInstance*> (ui)->portEvent (portIndex, bufferSize, format, buffer);
  1411. },
  1412. [] (const char* uri) -> const void*
  1413. {
  1414. const auto uriMatches = [&] (const LV2_Feature& f) { return std::strcmp (f.URI, uri) == 0; };
  1415. static LV2UI_Resize resize { nullptr, [] (LV2UI_Feature_Handle handle, int width, int height) -> int
  1416. {
  1417. JUCE_ASSERT_MESSAGE_THREAD
  1418. return static_cast<LV2UIInstance*> (handle)->resize (width, height);
  1419. } };
  1420. static LV2UI_Idle_Interface idle { [] (LV2UI_Handle handle)
  1421. {
  1422. static_cast<LV2UIInstance*> (handle)->idleCallback();
  1423. return 0;
  1424. } };
  1425. static LV2_Options_Interface options
  1426. {
  1427. [] (LV2_Handle handle, LV2_Options_Option* optionsIn)
  1428. {
  1429. return static_cast<LV2UIInstance*> (handle)->getOptions (optionsIn);
  1430. },
  1431. [] (LV2_Handle handle, const LV2_Options_Option* optionsIn)
  1432. {
  1433. return static_cast<LV2UIInstance*> (handle)->setOptions (optionsIn);
  1434. }
  1435. };
  1436. // We'll always define noUserResize and idle in the extension data array, but we'll
  1437. // only declare them in the ui.ttl if the UI is actually non-resizable, or requires
  1438. // idle callbacks.
  1439. // Well-behaved hosts should check the ttl before trying to search the
  1440. // extension-data array.
  1441. static const LV2_Feature features[] { { LV2_UI__resize, &resize },
  1442. { LV2_UI__noUserResize, nullptr },
  1443. { LV2_UI__idleInterface, &idle },
  1444. { LV2_OPTIONS__interface, &options } };
  1445. const auto it = std::find_if (std::begin (features), std::end (features), uriMatches);
  1446. return it != std::end (features) ? it->data : nullptr;
  1447. }
  1448. };
  1449. return &descriptor;
  1450. }
  1451. }
  1452. }
  1453. #endif