Amplifier.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Amplifier.cpp - A native amplifier effect plugin with sample-exact amplification
  3. *
  4. * Copyright (c) 2014 Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>
  5. * Copyright (c) 2006-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  6. *
  7. * This file is part of LMMS - https://lmms.io
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public
  20. * License along with this program (see COPYING); if not, write to the
  21. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  22. * Boston, MA 02110-1301 USA.
  23. *
  24. */
  25. #include "Amplifier.h"
  26. #include "embed.h"
  27. #include "plugin_export.h"
  28. extern "C"
  29. {
  30. Plugin::Descriptor PLUGIN_EXPORT amplifier_plugin_descriptor =
  31. {
  32. STRINGIFY( PLUGIN_NAME ),
  33. "Amplifier",
  34. QT_TRANSLATE_NOOP( "PluginBrowser", "A native amplifier plugin" ),
  35. "Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>",
  36. 0x0100,
  37. Plugin::Effect,
  38. new PluginPixmapLoader("logo"),
  39. nullptr,
  40. nullptr,
  41. } ;
  42. }
  43. AmplifierEffect::AmplifierEffect( Model* parent, const Descriptor::SubPluginFeatures::Key* key ) :
  44. Effect( &amplifier_plugin_descriptor, parent, key ),
  45. m_ampControls( this )
  46. {
  47. }
  48. AmplifierEffect::~AmplifierEffect()
  49. {
  50. }
  51. bool AmplifierEffect::processAudioBuffer( sampleFrame* buf, const fpp_t frames )
  52. {
  53. if( !isEnabled() || !isRunning () )
  54. {
  55. return( false );
  56. }
  57. double outSum = 0.0;
  58. const float d = dryLevel();
  59. const float w = wetLevel();
  60. const ValueBuffer * volBuf = m_ampControls.m_volumeModel.valueBuffer();
  61. const ValueBuffer * panBuf = m_ampControls.m_panModel.valueBuffer();
  62. const ValueBuffer * leftBuf = m_ampControls.m_leftModel.valueBuffer();
  63. const ValueBuffer * rightBuf = m_ampControls.m_rightModel.valueBuffer();
  64. for( fpp_t f = 0; f < frames; ++f )
  65. {
  66. // qDebug( "offset %d, value %f", f, m_ampControls.m_volumeModel.value( f ) );
  67. sample_t s[2] = { buf[f][0], buf[f][1] };
  68. // vol knob
  69. if( volBuf )
  70. {
  71. s[0] *= volBuf->value( f ) * 0.01f;
  72. s[1] *= volBuf->value( f ) * 0.01f;
  73. }
  74. else
  75. {
  76. s[0] *= m_ampControls.m_volumeModel.value() * 0.01f;
  77. s[1] *= m_ampControls.m_volumeModel.value() * 0.01f;
  78. }
  79. // convert pan values to left/right values
  80. const float pan = panBuf
  81. ? panBuf->value( f )
  82. : m_ampControls.m_panModel.value();
  83. const float left1 = pan <= 0
  84. ? 1.0
  85. : 1.0 - pan * 0.01f;
  86. const float right1 = pan >= 0
  87. ? 1.0
  88. : 1.0 + pan * 0.01f;
  89. // second stage amplification
  90. const float left2 = leftBuf
  91. ? leftBuf->value( f )
  92. : m_ampControls.m_leftModel.value();
  93. const float right2 = rightBuf
  94. ? rightBuf->value( f )
  95. : m_ampControls.m_rightModel.value();
  96. s[0] *= left1 * left2 * 0.01;
  97. s[1] *= right1 * right2 * 0.01;
  98. buf[f][0] = d * buf[f][0] + w * s[0];
  99. buf[f][1] = d * buf[f][1] + w * s[1];
  100. outSum += buf[f][0] * buf[f][0] + buf[f][1] * buf[f][1];
  101. }
  102. checkGate( outSum / frames );
  103. return isRunning();
  104. }
  105. extern "C"
  106. {
  107. // necessary for getting instance out of shared lib
  108. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model* parent, void* data )
  109. {
  110. return new AmplifierEffect( parent, static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>( data ) );
  111. }
  112. }