peak_controller_effect.cpp 4.1 KB

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