BassBooster.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. * BassBooster.cpp - bass booster effect plugin
  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 "BassBooster.h"
  25. #include "embed.h"
  26. #include "plugin_export.h"
  27. extern "C"
  28. {
  29. Plugin::Descriptor PLUGIN_EXPORT bassbooster_plugin_descriptor =
  30. {
  31. STRINGIFY( PLUGIN_NAME ),
  32. "BassBooster",
  33. QT_TRANSLATE_NOOP( "PluginBrowser", "Boost your bass the fast and simple way" ),
  34. "Tobias Doerffel <tobydox/at/users.sf.net>",
  35. 0x0100,
  36. Plugin::Effect,
  37. new PluginPixmapLoader("logo"),
  38. NULL,
  39. NULL
  40. } ;
  41. }
  42. BassBoosterEffect::BassBoosterEffect( Model* parent, const Descriptor::SubPluginFeatures::Key* key ) :
  43. Effect( &bassbooster_plugin_descriptor, parent, key ),
  44. m_frequencyChangeNeeded( false ),
  45. m_bbFX( DspEffectLibrary::FastBassBoost( 70.0f, 1.0f, 2.8f ) ),
  46. m_bbControls( this )
  47. {
  48. changeFrequency();
  49. changeGain();
  50. changeRatio();
  51. }
  52. BassBoosterEffect::~BassBoosterEffect()
  53. {
  54. }
  55. bool BassBoosterEffect::processAudioBuffer( sampleFrame* buf, const fpp_t frames )
  56. {
  57. if( !isEnabled() || !isRunning () )
  58. {
  59. return( false );
  60. }
  61. // check out changed controls
  62. if( m_frequencyChangeNeeded || m_bbControls.m_freqModel.isValueChanged() )
  63. {
  64. changeFrequency();
  65. m_frequencyChangeNeeded = false;
  66. }
  67. if( m_bbControls.m_gainModel.isValueChanged() ) { changeGain(); }
  68. if( m_bbControls.m_ratioModel.isValueChanged() ) { changeRatio(); }
  69. const float const_gain = m_bbControls.m_gainModel.value();
  70. const ValueBuffer *gainBuffer = m_bbControls.m_gainModel.valueBuffer();
  71. double outSum = 0.0;
  72. const float d = dryLevel();
  73. const float w = wetLevel();
  74. for( fpp_t f = 0; f < frames; ++f )
  75. {
  76. float gain = const_gain;
  77. if (gainBuffer) {
  78. //process period using sample exact data
  79. gain = gainBuffer->value( f );
  80. }
  81. //float gain = gainBuffer ? gainBuffer[f] : gain;
  82. m_bbFX.leftFX().setGain( gain );
  83. m_bbFX.rightFX().setGain( gain);
  84. sample_t s[2] = { buf[f][0], buf[f][1] };
  85. m_bbFX.nextSample( s[0], s[1] );
  86. buf[f][0] = d * buf[f][0] + w * s[0];
  87. buf[f][1] = d * buf[f][1] + w * s[1];
  88. outSum += buf[f][0] * buf[f][0] + buf[f][1] * buf[f][1];
  89. }
  90. checkGate( outSum / frames );
  91. return isRunning();
  92. }
  93. inline void BassBoosterEffect::changeFrequency()
  94. {
  95. const sample_t fac = Engine::mixer()->processingSampleRate() / 44100.0f;
  96. m_bbFX.leftFX().setFrequency( m_bbControls.m_freqModel.value() * fac );
  97. m_bbFX.rightFX().setFrequency( m_bbControls.m_freqModel.value() * fac );
  98. }
  99. inline void BassBoosterEffect::changeGain()
  100. {
  101. m_bbFX.leftFX().setGain( m_bbControls.m_gainModel.value() );
  102. m_bbFX.rightFX().setGain( m_bbControls.m_gainModel.value() );
  103. }
  104. inline void BassBoosterEffect::changeRatio()
  105. {
  106. m_bbFX.leftFX().setRatio( m_bbControls.m_ratioModel.value() );
  107. m_bbFX.rightFX().setRatio( m_bbControls.m_ratioModel.value() );
  108. }
  109. extern "C"
  110. {
  111. // necessary for getting instance out of shared lib
  112. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model* parent, void* data )
  113. {
  114. return new BassBoosterEffect( parent, static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>( data ) );
  115. }
  116. }