Lv2Effect.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Lv2Effect.cpp - implementation of LV2 effect
  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 "Lv2Effect.h"
  25. #include <QDebug>
  26. #include <lv2.h>
  27. #include "Lv2SubPluginFeatures.h"
  28. #include "embed.h"
  29. #include "plugin_export.h"
  30. extern "C"
  31. {
  32. Plugin::Descriptor PLUGIN_EXPORT lv2effect_plugin_descriptor =
  33. {
  34. STRINGIFY(PLUGIN_NAME),
  35. "LV2",
  36. QT_TRANSLATE_NOOP("PluginBrowser",
  37. "plugin for using arbitrary LV2-effects inside LMMS."),
  38. "Johannes Lorenz <jlsf2013$$$users.sourceforge.net, $$$=@>",
  39. 0x0100,
  40. Plugin::Effect,
  41. new PluginPixmapLoader("logo"),
  42. nullptr,
  43. new Lv2SubPluginFeatures(Plugin::Effect)
  44. };
  45. }
  46. Lv2Effect::Lv2Effect(Model* parent, const Descriptor::SubPluginFeatures::Key *key) :
  47. Effect(&lv2effect_plugin_descriptor, parent, key),
  48. m_controls(this, key->attributes["uri"]),
  49. m_tmpOutputSmps(Engine::mixer()->framesPerPeriod())
  50. {
  51. }
  52. bool Lv2Effect::processAudioBuffer(sampleFrame *buf, const fpp_t frames)
  53. {
  54. if (!isEnabled() || !isRunning()) { return false; }
  55. Q_ASSERT(frames <= static_cast<fpp_t>(m_tmpOutputSmps.size()));
  56. m_controls.copyBuffersFromLmms(buf, frames);
  57. m_controls.copyModelsFromLmms();
  58. // m_pluginMutex.lock();
  59. m_controls.run(frames);
  60. // m_pluginMutex.unlock();
  61. m_controls.copyModelsToLmms();
  62. m_controls.copyBuffersToLmms(m_tmpOutputSmps.data(), frames);
  63. double outSum = .0;
  64. bool corrupt = wetLevel() < 0; // #3261 - if w < 0, bash w := 0, d := 1
  65. const float d = corrupt ? 1 : dryLevel();
  66. const float w = corrupt ? 0 : wetLevel();
  67. for(fpp_t f = 0; f < frames; ++f)
  68. {
  69. buf[f][0] = d * buf[f][0] + w * m_tmpOutputSmps[f][0];
  70. buf[f][1] = d * buf[f][1] + w * m_tmpOutputSmps[f][1];
  71. double l = static_cast<double>(buf[f][0]);
  72. double r = static_cast<double>(buf[f][1]);
  73. outSum += l*l + r*r;
  74. }
  75. checkGate(outSum / frames);
  76. return isRunning();
  77. }
  78. extern "C"
  79. {
  80. // necessary for getting instance out of shared lib
  81. PLUGIN_EXPORT Plugin *lmms_plugin_main(Model *_parent, void *_data)
  82. {
  83. using KeyType = Plugin::Descriptor::SubPluginFeatures::Key;
  84. Lv2Effect* eff = new Lv2Effect(_parent, static_cast<const KeyType*>(_data));
  85. if (!eff->isValid()) { delete eff; eff = nullptr; }
  86. return eff;
  87. }
  88. }