peak_controller_effect.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * peak_controller_effect.cpp - PeakController effect plugin
  3. *
  4. * Copyright (c) 2008 Paul Giblock <drfaygo/at/gmail/dot/com>
  5. * Copyright (c) 2009 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 "Controller.h"
  26. #include "Song.h"
  27. #include "PeakController.h"
  28. #include "peak_controller_effect.h"
  29. #include "lmms_math.h"
  30. #include "embed.cpp"
  31. extern "C"
  32. {
  33. Plugin::Descriptor PLUGIN_EXPORT peakcontrollereffect_plugin_descriptor =
  34. {
  35. STRINGIFY( PLUGIN_NAME ),
  36. "Peak Controller",
  37. QT_TRANSLATE_NOOP( "pluginBrowser",
  38. "Plugin for controlling knobs with sound peaks" ),
  39. "Paul Giblock <drfaygo/at/gmail.com>",
  40. 0x0100,
  41. Plugin::Effect,
  42. new PluginPixmapLoader( "logo" ),
  43. NULL,
  44. NULL
  45. } ;
  46. }
  47. // We have to keep a list of all the PeakController effects so that we can save
  48. // an peakEffect-ID to the project. This ID is referenced in the PeakController
  49. // settings and is used to set the PeakControllerEffect pointer upon load
  50. //QVector<PeakControllerEffect *> PeakControllerEffect::s_effects;
  51. PeakControllerEffect::PeakControllerEffect(
  52. Model * _parent,
  53. const Descriptor::SubPluginFeatures::Key * _key ) :
  54. Effect( &peakcontrollereffect_plugin_descriptor, _parent, _key ),
  55. m_effectId( rand() ),
  56. m_peakControls( this ),
  57. m_lastSample( 0 ),
  58. m_autoController( NULL )
  59. {
  60. m_autoController = new PeakController( Engine::getSong(), this );
  61. if( !Engine::getSong()->isLoadingProject() )
  62. {
  63. Engine::getSong()->addController( m_autoController );
  64. }
  65. PeakController::s_effects.append( this );
  66. }
  67. PeakControllerEffect::~PeakControllerEffect()
  68. {
  69. int idx = PeakController::s_effects.indexOf( this );
  70. if( idx >= 0 )
  71. {
  72. PeakController::s_effects.remove( idx );
  73. Engine::getSong()->removeController( m_autoController );
  74. }
  75. }
  76. bool PeakControllerEffect::processAudioBuffer( sampleFrame * _buf,
  77. const fpp_t _frames )
  78. {
  79. PeakControllerEffectControls & c = m_peakControls;
  80. // This appears to be used for determining whether or not to continue processing
  81. // audio with this effect
  82. if( !isEnabled() || !isRunning() )
  83. {
  84. return false;
  85. }
  86. // RMS:
  87. double sum = 0;
  88. if( c.m_absModel.value() )
  89. {
  90. for( int i = 0; i < _frames; ++i )
  91. {
  92. // absolute value is achieved because the squares are > 0
  93. sum += _buf[i][0]*_buf[i][0] + _buf[i][1]*_buf[i][1];
  94. }
  95. }
  96. else
  97. {
  98. for( int i = 0; i < _frames; ++i )
  99. {
  100. // the value is absolute because of squaring,
  101. // so we need to correct it
  102. sum += _buf[i][0] * _buf[i][0] * sign( _buf[i][0] )
  103. + _buf[i][1] * _buf[i][1] * sign( _buf[i][1] );
  104. }
  105. }
  106. // TODO: flipping this might cause clipping
  107. // this will mute the output after the values were measured
  108. if( c.m_muteModel.value() )
  109. {
  110. for( int i = 0; i < _frames; ++i )
  111. {
  112. _buf[i][0] = _buf[i][1] = 0.0f;
  113. }
  114. }
  115. float curRMS = sqrt_neg( sum / _frames );
  116. const float tres = c.m_tresholdModel.value();
  117. const float amount = c.m_amountModel.value() * c.m_amountMultModel.value();
  118. curRMS = qAbs( curRMS ) < tres ? 0.0f : curRMS;
  119. m_lastSample = qBound( 0.0f, c.m_baseModel.value() + amount * curRMS, 1.0f );
  120. return isRunning();
  121. }
  122. extern "C"
  123. {
  124. // necessary for getting instance out of shared lib
  125. Plugin * PLUGIN_EXPORT lmms_plugin_main( Model * _parent, void * _data )
  126. {
  127. return new PeakControllerEffect( _parent,
  128. static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>( _data ) );
  129. }
  130. }