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