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.cpp"
  27. extern "C"
  28. {
  29. Plugin::Descriptor PLUGIN_EXPORT amplifier_plugin_descriptor =
  30. {
  31. STRINGIFY( PLUGIN_NAME ),
  32. "Amplifier",
  33. QT_TRANSLATE_NOOP( "pluginBrowser", "A native amplifier plugin" ),
  34. "Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>",
  35. 0x0100,
  36. Plugin::Effect,
  37. new PluginPixmapLoader( "logo" ),
  38. NULL,
  39. NULL
  40. } ;
  41. }
  42. AmplifierEffect::AmplifierEffect( Model* parent, const Descriptor::SubPluginFeatures::Key* key ) :
  43. Effect( &amplifier_plugin_descriptor, parent, key ),
  44. m_ampControls( this )
  45. {
  46. }
  47. AmplifierEffect::~AmplifierEffect()
  48. {
  49. }
  50. bool AmplifierEffect::processAudioBuffer( sampleFrame* buf, const fpp_t frames )
  51. {
  52. if( !isEnabled() || !isRunning () )
  53. {
  54. return( false );
  55. }
  56. double outSum = 0.0;
  57. const float d = dryLevel();
  58. const float w = wetLevel();
  59. const ValueBuffer * volBuf = m_ampControls.m_volumeModel.valueBuffer();
  60. const ValueBuffer * panBuf = m_ampControls.m_panModel.valueBuffer();
  61. const ValueBuffer * leftBuf = m_ampControls.m_leftModel.valueBuffer();
  62. const ValueBuffer * rightBuf = m_ampControls.m_rightModel.valueBuffer();
  63. for( fpp_t f = 0; f < frames; ++f )
  64. {
  65. // qDebug( "offset %d, value %f", f, m_ampControls.m_volumeModel.value( f ) );
  66. outSum += buf[f][0]*buf[f][0] + buf[f][1]*buf[f][1];
  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. }
  101. checkGate( outSum / frames );
  102. return isRunning();
  103. }
  104. extern "C"
  105. {
  106. // necessary for getting instance out of shared lib
  107. Plugin * PLUGIN_EXPORT lmms_plugin_main( Model* parent, void* data )
  108. {
  109. return new AmplifierEffect( parent, static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>( data ) );
  110. }
  111. }