VstEffect.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * VstEffect.cpp - class for handling VST effect plugins
  3. *
  4. * Copyright (c) 2006-2014 Tobias Doerffel <tobydox/at/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 <QMessageBox>
  25. #include "VstEffect.h"
  26. #include "GuiApplication.h"
  27. #include "Song.h"
  28. #include "TextFloat.h"
  29. #include "VstSubPluginFeatures.h"
  30. #include "embed.h"
  31. #include "plugin_export.h"
  32. extern "C"
  33. {
  34. Plugin::Descriptor PLUGIN_EXPORT vsteffect_plugin_descriptor =
  35. {
  36. STRINGIFY( PLUGIN_NAME ),
  37. "VST",
  38. QT_TRANSLATE_NOOP( "PluginBrowser",
  39. "plugin for using arbitrary VST effects inside LMMS." ),
  40. "Tobias Doerffel <tobydox/at/users.sf.net>",
  41. 0x0200,
  42. Plugin::Effect,
  43. new PluginPixmapLoader("logo"),
  44. NULL,
  45. new VstSubPluginFeatures( Plugin::Effect )
  46. } ;
  47. }
  48. VstEffect::VstEffect( Model * _parent,
  49. const Descriptor::SubPluginFeatures::Key * _key ) :
  50. Effect( &vsteffect_plugin_descriptor, _parent, _key ),
  51. m_pluginMutex(),
  52. m_key( *_key ),
  53. m_vstControls( this )
  54. {
  55. if( !m_key.attributes["file"].isEmpty() )
  56. {
  57. openPlugin( m_key.attributes["file"] );
  58. }
  59. setDisplayName( m_key.attributes["file"].section( ".dll", 0, 0 ).isEmpty()
  60. ? m_key.name : m_key.attributes["file"].section( ".dll", 0, 0 ) );
  61. }
  62. VstEffect::~VstEffect()
  63. {
  64. }
  65. bool VstEffect::processAudioBuffer( sampleFrame * _buf, const fpp_t _frames )
  66. {
  67. if( !isEnabled() || !isRunning () )
  68. {
  69. return false;
  70. }
  71. if( m_plugin )
  72. {
  73. const float d = dryLevel();
  74. #ifdef __GNUC__
  75. sampleFrame buf[_frames];
  76. #else
  77. sampleFrame * buf = new sampleFrame[_frames];
  78. #endif
  79. memcpy( buf, _buf, sizeof( sampleFrame ) * _frames );
  80. if (m_pluginMutex.tryLock(Engine::getSong()->isExporting() ? -1 : 0))
  81. {
  82. m_plugin->process( buf, buf );
  83. m_pluginMutex.unlock();
  84. }
  85. double out_sum = 0.0;
  86. const float w = wetLevel();
  87. for( fpp_t f = 0; f < _frames; ++f )
  88. {
  89. _buf[f][0] = w*buf[f][0] + d*_buf[f][0];
  90. _buf[f][1] = w*buf[f][1] + d*_buf[f][1];
  91. }
  92. for( fpp_t f = 0; f < _frames; ++f )
  93. {
  94. out_sum += _buf[f][0]*_buf[f][0] + _buf[f][1]*_buf[f][1];
  95. }
  96. #ifndef __GNUC__
  97. delete[] buf;
  98. #endif
  99. checkGate( out_sum / _frames );
  100. }
  101. return isRunning();
  102. }
  103. void VstEffect::openPlugin( const QString & _plugin )
  104. {
  105. TextFloat * tf = NULL;
  106. if( gui )
  107. {
  108. tf = TextFloat::displayMessage(
  109. VstPlugin::tr( "Loading plugin" ),
  110. VstPlugin::tr( "Please wait while loading VST plugin..." ),
  111. PLUGIN_NAME::getIconPixmap( "logo", 24, 24 ), 0 );
  112. }
  113. QMutexLocker ml( &m_pluginMutex ); Q_UNUSED( ml );
  114. m_plugin = QSharedPointer<VstPlugin>(new VstPlugin( _plugin ));
  115. if( m_plugin->failed() )
  116. {
  117. m_plugin.clear();
  118. delete tf;
  119. collectErrorForUI( VstPlugin::tr( "The VST plugin %1 could not be loaded." ).arg( _plugin ) );
  120. return;
  121. }
  122. delete tf;
  123. m_key.attributes["file"] = _plugin;
  124. }
  125. extern "C"
  126. {
  127. // necessary for getting instance out of shared lib
  128. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model * _parent, void * _data )
  129. {
  130. return new VstEffect( _parent,
  131. static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>(
  132. _data ) );
  133. }
  134. }