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.

1757 lines
63KB

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