peak_controller_effect.cpp 4.1 KB

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