Lv2ControlBase.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Lv2ControlBase.h - Lv2 control base class
  3. *
  4. * Copyright (c) 2018-2020 Johannes Lorenz <jlsf2013$users.sourceforge.net, $=@>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #ifndef LV2_CONTROL_BASE_H
  25. #define LV2_CONTROL_BASE_H
  26. #include "lmmsconfig.h"
  27. #ifdef LMMS_HAVE_LV2
  28. #include <lilv/lilv.h>
  29. #include "DataFile.h"
  30. #include "LinkedModelGroups.h"
  31. #include "lmms_export.h"
  32. #include "Plugin.h"
  33. class Lv2Proc;
  34. class PluginIssue;
  35. /**
  36. Common base class for Lv2 plugins
  37. This class contains a vector of Lv2Proc, usually 1 (for stereo plugins) or
  38. 2 (for mono plugins). Most of the logic is done there, this class primarily
  39. forwards work to the Lv2Proc and collects the results.
  40. This class provides everything Lv2 plugins have in common. It's not
  41. named Lv2Plugin, because
  42. * it does not inherit Instrument
  43. * the Plugin subclass Effect does not inherit this class
  44. This class would usually be a Model subclass. However, Qt doesn't allow
  45. this:
  46. * inheriting only from Model will cause diamond inheritance for QObject,
  47. which will cause errors with Q_OBJECT
  48. * making this a direct subclass of Instrument resp. EffectControls would
  49. require CRTP, which would make this class a template class, which would
  50. conflict with Q_OBJECT
  51. The consequence is that this class can neither inherit QObject or Model, nor
  52. Instrument or EffectControls, which means in fact:
  53. * this class contains no signals or slots, but it offers stubs for slots
  54. that shall be called by child classes
  55. * this class can not override virtuals of Instrument or EffectControls, so
  56. it will offer functions that must be called by virtuals in its child class
  57. */
  58. class LMMS_EXPORT Lv2ControlBase : public LinkedModelGroups
  59. {
  60. public:
  61. static Plugin::PluginTypes check(const LilvPlugin* m_plugin,
  62. std::vector<PluginIssue> &issues);
  63. const LilvPlugin* getPlugin() const { return m_plugin; }
  64. Lv2Proc *control(std::size_t idx) { return m_procs[idx].get(); }
  65. const Lv2Proc *control(std::size_t idx) const { return m_procs[idx].get(); }
  66. bool hasGui() const { return m_hasGUI; }
  67. void setHasGui(bool val) { m_hasGUI = val; }
  68. protected:
  69. /*
  70. ctor/dtor
  71. */
  72. //! @param that the class inheriting this class and inheriting Model;
  73. //! this is the same pointer as this, but a different type
  74. //! @param uri the Lv2 URI telling this class what plugin to construct
  75. Lv2ControlBase(class Model *that, const QString& uri);
  76. Lv2ControlBase(const Lv2ControlBase&) = delete;
  77. ~Lv2ControlBase() override;
  78. Lv2ControlBase& operator=(const Lv2ControlBase&) = delete;
  79. //! Must be checked after ctor or reload
  80. bool isValid() const { return m_valid; }
  81. /*
  82. overrides
  83. */
  84. LinkedModelGroup* getGroup(std::size_t idx) override;
  85. const LinkedModelGroup* getGroup(std::size_t idx) const override;
  86. /*
  87. utils for the run thread
  88. */
  89. //! Copy values from the LMMS core (connected models, MIDI events, ...) into
  90. //! the respective ports
  91. void copyModelsFromLmms();
  92. //! Bring values from all ports to the LMMS core
  93. void copyModelsToLmms() const;
  94. //! Copy buffer passed by LMMS into our ports
  95. void copyBuffersFromLmms(const sampleFrame *buf, fpp_t frames);
  96. //! Copy our ports into buffers passed by LMMS
  97. void copyBuffersToLmms(sampleFrame *buf, fpp_t frames) const;
  98. //! Run the Lv2 plugin instance for @param frames frames
  99. void run(fpp_t frames);
  100. /*
  101. load/save, must be called from virtuals
  102. */
  103. void saveSettings(QDomDocument &doc, QDomElement &that);
  104. void loadSettings(const QDomElement &that);
  105. void loadFile(const QString &file);
  106. //! TODO: not implemented
  107. void reloadPlugin();
  108. /*
  109. more functions that must be called from virtuals
  110. */
  111. std::size_t controlCount() const;
  112. QString nodeName() const { return "lv2controls"; }
  113. bool hasNoteInput() const;
  114. void handleMidiInputEvent(const class MidiEvent &event,
  115. const class TimePos &time, f_cnt_t offset);
  116. private:
  117. //! Return the DataFile settings type
  118. virtual DataFile::Types settingsType() = 0;
  119. //! Inform the plugin about a file name change
  120. virtual void setNameFromFile(const QString &fname) = 0;
  121. //! Independent processors
  122. //! If this is a mono effect, the vector will have size 2 in order to
  123. //! fulfill LMMS' requirement of having stereo input and output
  124. std::vector<std::unique_ptr<Lv2Proc>> m_procs;
  125. bool m_valid = true;
  126. bool m_hasGUI = false;
  127. unsigned m_channelsPerProc;
  128. const LilvPlugin* m_plugin;
  129. };
  130. #endif // LMMS_HAVE_LV2
  131. #endif // LV2_CONTROL_BASE_H