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.

1796 lines
66KB

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