Lv2Instrument.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Lv2Instrument.cpp - implementation of LV2 instrument
  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. #include "Lv2Instrument.h"
  25. #include <QDebug>
  26. #include <QDragEnterEvent>
  27. #include "Engine.h"
  28. #include "InstrumentPlayHandle.h"
  29. #include "InstrumentTrack.h"
  30. #include "Lv2SubPluginFeatures.h"
  31. #include "Mixer.h"
  32. #include "StringPairDrag.h"
  33. #include "Clipboard.h"
  34. #include "embed.h"
  35. #include "plugin_export.h"
  36. extern "C"
  37. {
  38. Plugin::Descriptor PLUGIN_EXPORT lv2instrument_plugin_descriptor =
  39. {
  40. STRINGIFY(PLUGIN_NAME),
  41. "LV2",
  42. QT_TRANSLATE_NOOP("PluginBrowser",
  43. "plugin for using arbitrary LV2 instruments inside LMMS."),
  44. "Johannes Lorenz <jlsf2013$$$users.sourceforge.net, $$$=@>",
  45. 0x0100,
  46. Plugin::Instrument,
  47. new PluginPixmapLoader("logo"),
  48. nullptr,
  49. new Lv2SubPluginFeatures(Plugin::Instrument)
  50. };
  51. }
  52. /*
  53. Lv2Instrument
  54. */
  55. Lv2Instrument::Lv2Instrument(InstrumentTrack *instrumentTrackArg,
  56. Descriptor::SubPluginFeatures::Key *key) :
  57. Instrument(instrumentTrackArg, &lv2instrument_plugin_descriptor, key),
  58. Lv2ControlBase(this, key->attributes["uri"])
  59. {
  60. if (Lv2ControlBase::isValid())
  61. {
  62. #ifdef LV2_INSTRUMENT_USE_MIDI
  63. for (int i = 0; i < NumKeys; ++i) { m_runningNotes[i] = 0; }
  64. #endif
  65. connect(instrumentTrack()->pitchRangeModel(), SIGNAL(dataChanged()),
  66. this, SLOT(updatePitchRange()), Qt::DirectConnection);
  67. connect(Engine::mixer(), &Mixer::sampleRateChanged,
  68. this, [this](){Lv2ControlBase::reloadPlugin();});
  69. // now we need a play-handle which cares for calling play()
  70. InstrumentPlayHandle *iph =
  71. new InstrumentPlayHandle(this, instrumentTrackArg);
  72. Engine::mixer()->addPlayHandle(iph);
  73. }
  74. }
  75. Lv2Instrument::~Lv2Instrument()
  76. {
  77. Engine::mixer()->removePlayHandlesOfTypes(instrumentTrack(),
  78. PlayHandle::TypeNotePlayHandle |
  79. PlayHandle::TypeInstrumentPlayHandle);
  80. }
  81. bool Lv2Instrument::isValid() const { return Lv2ControlBase::isValid(); }
  82. void Lv2Instrument::saveSettings(QDomDocument &doc, QDomElement &that)
  83. {
  84. Lv2ControlBase::saveSettings(doc, that);
  85. }
  86. void Lv2Instrument::loadSettings(const QDomElement &that)
  87. {
  88. Lv2ControlBase::loadSettings(that);
  89. }
  90. void Lv2Instrument::loadFile(const QString &file)
  91. {
  92. Lv2ControlBase::loadFile(file);
  93. }
  94. #ifdef LV2_INSTRUMENT_USE_MIDI
  95. bool Lv2Instrument::handleMidiEvent(
  96. const MidiEvent &event, const TimePos &time, f_cnt_t offset)
  97. {
  98. // this function can be called from GUI threads while the plugin is running
  99. // handleMidiInputEvent will use a thread-safe ringbuffer
  100. handleMidiInputEvent(event, time, offset);
  101. return true;
  102. }
  103. #endif
  104. // not yet working
  105. #ifndef LV2_INSTRUMENT_USE_MIDI
  106. void Lv2Instrument::playNote(NotePlayHandle *nph, sampleFrame *)
  107. {
  108. }
  109. #endif
  110. void Lv2Instrument::play(sampleFrame *buf)
  111. {
  112. copyModelsFromLmms();
  113. fpp_t fpp = Engine::mixer()->framesPerPeriod();
  114. run(fpp);
  115. copyModelsToLmms();
  116. copyBuffersToLmms(buf, fpp);
  117. instrumentTrack()->processAudioBuffer(buf, fpp, nullptr);
  118. }
  119. PluginView *Lv2Instrument::instantiateView(QWidget *parent)
  120. {
  121. return new Lv2InsView(this, parent);
  122. }
  123. void Lv2Instrument::updatePitchRange()
  124. {
  125. qDebug() << "Lmms: Cannot update pitch range for lv2 plugin:"
  126. "not implemented yet";
  127. }
  128. QString Lv2Instrument::nodeName() const
  129. {
  130. return Lv2ControlBase::nodeName();
  131. }
  132. DataFile::Types Lv2Instrument::settingsType()
  133. {
  134. return DataFile::InstrumentTrackSettings;
  135. }
  136. void Lv2Instrument::setNameFromFile(const QString &name)
  137. {
  138. instrumentTrack()->setName(name);
  139. }
  140. /*
  141. Lv2InsView
  142. */
  143. Lv2InsView::Lv2InsView(Lv2Instrument *_instrument, QWidget *_parent) :
  144. InstrumentView(_instrument, _parent),
  145. Lv2ViewBase(this, _instrument)
  146. {
  147. setAutoFillBackground(true);
  148. if (m_reloadPluginButton) {
  149. connect(m_reloadPluginButton, &QPushButton::clicked,
  150. this, [this](){ this->castModel<Lv2Instrument>()->reloadPlugin();} );
  151. }
  152. if (m_toggleUIButton) {
  153. connect(m_toggleUIButton, &QPushButton::toggled,
  154. this, [this](){ toggleUI(); });
  155. }
  156. if (m_helpButton) {
  157. connect(m_helpButton, &QPushButton::toggled,
  158. this, [this](bool visible){ toggleHelp(visible); });
  159. }
  160. }
  161. void Lv2InsView::dragEnterEvent(QDragEnterEvent *_dee)
  162. {
  163. // For mimeType() and MimeType enum class
  164. using namespace Clipboard;
  165. void (QDragEnterEvent::*reaction)(void) = &QDragEnterEvent::ignore;
  166. if (_dee->mimeData()->hasFormat( mimeType( MimeType::StringPair )))
  167. {
  168. const QString txt =
  169. _dee->mimeData()->data( mimeType( MimeType::StringPair ) );
  170. if (txt.section(':', 0, 0) == "pluginpresetfile") {
  171. reaction = &QDragEnterEvent::acceptProposedAction;
  172. }
  173. }
  174. (_dee->*reaction)();
  175. }
  176. void Lv2InsView::dropEvent(QDropEvent *_de)
  177. {
  178. const QString type = StringPairDrag::decodeKey(_de);
  179. const QString value = StringPairDrag::decodeValue(_de);
  180. if (type == "pluginpresetfile")
  181. {
  182. castModel<Lv2Instrument>()->loadFile(value);
  183. _de->accept();
  184. return;
  185. }
  186. _de->ignore();
  187. }
  188. void Lv2InsView::modelChanged()
  189. {
  190. Lv2ViewBase::modelChanged(castModel<Lv2Instrument>());
  191. }
  192. extern "C"
  193. {
  194. // necessary for getting instance out of shared lib
  195. PLUGIN_EXPORT Plugin *lmms_plugin_main(Model *_parent, void *_data)
  196. {
  197. using KeyType = Plugin::Descriptor::SubPluginFeatures::Key;
  198. Lv2Instrument* ins = new Lv2Instrument(
  199. static_cast<InstrumentTrack*>(_parent),
  200. static_cast<KeyType*>(_data ));
  201. if (!ins->isValid()) { delete ins; ins = nullptr; }
  202. return ins;
  203. }
  204. }