dynamics_processor.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * dynamics_processor.cpp - dynamics_processor effect-plugin
  3. *
  4. * Copyright (c) 2014 Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>
  5. * Copyright (c) 2006-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 "dynamics_processor.h"
  26. #include "lmms_math.h"
  27. #include "interpolation.h"
  28. #include "embed.h"
  29. #include "plugin_export.h"
  30. extern "C"
  31. {
  32. Plugin::Descriptor PLUGIN_EXPORT dynamicsprocessor_plugin_descriptor =
  33. {
  34. STRINGIFY( PLUGIN_NAME ),
  35. "Dynamics Processor",
  36. QT_TRANSLATE_NOOP( "PluginBrowser",
  37. "plugin for processing dynamics in a flexible way" ),
  38. "Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>",
  39. 0x0100,
  40. Plugin::Effect,
  41. new PluginPixmapLoader("logo"),
  42. NULL,
  43. NULL
  44. } ;
  45. }
  46. const float DYN_NOISE_FLOOR = 0.00001f; // -100dBFS noise floor
  47. const double DNF_LOG = 5.0;
  48. dynProcEffect::dynProcEffect( Model * _parent,
  49. const Descriptor::SubPluginFeatures::Key * _key ) :
  50. Effect( &dynamicsprocessor_plugin_descriptor, _parent, _key ),
  51. m_dpControls( this )
  52. {
  53. m_currentPeak[0] = m_currentPeak[1] = DYN_NOISE_FLOOR;
  54. m_rms[0] = new RmsHelper( 64 * Engine::mixer()->processingSampleRate() / 44100 );
  55. m_rms[1] = new RmsHelper( 64 * Engine::mixer()->processingSampleRate() / 44100 );
  56. calcAttack();
  57. calcRelease();
  58. }
  59. dynProcEffect::~dynProcEffect()
  60. {
  61. delete m_rms[0];
  62. delete m_rms[1];
  63. }
  64. inline void dynProcEffect::calcAttack()
  65. {
  66. m_attCoeff = exp10( ( DNF_LOG / ( m_dpControls.m_attackModel.value() * 0.001 ) ) / Engine::mixer()->processingSampleRate() );
  67. }
  68. inline void dynProcEffect::calcRelease()
  69. {
  70. m_relCoeff = exp10( ( -DNF_LOG / ( m_dpControls.m_releaseModel.value() * 0.001 ) ) / Engine::mixer()->processingSampleRate() );
  71. }
  72. bool dynProcEffect::processAudioBuffer( sampleFrame * _buf,
  73. const fpp_t _frames )
  74. {
  75. if( !isEnabled() || !isRunning () )
  76. {
  77. //apparently we can't keep running after the decay value runs out so we'll just set the peaks to zero
  78. m_currentPeak[0] = m_currentPeak[1] = DYN_NOISE_FLOOR;
  79. return( false );
  80. }
  81. //qDebug( "%f %f", m_currentPeak[0], m_currentPeak[1] );
  82. // variables for effect
  83. int i = 0;
  84. float sm_peak[2] = { 0.0f, 0.0f };
  85. float gain;
  86. double out_sum = 0.0;
  87. const float d = dryLevel();
  88. const float w = wetLevel();
  89. const int stereoMode = m_dpControls.m_stereomodeModel.value();
  90. const float inputGain = m_dpControls.m_inputModel.value();
  91. const float outputGain = m_dpControls.m_outputModel.value();
  92. const float * samples = m_dpControls.m_wavegraphModel.samples();
  93. // debug code
  94. // qDebug( "peaks %f %f", m_currentPeak[0], m_currentPeak[1] );
  95. if( m_needsUpdate )
  96. {
  97. m_rms[0]->setSize( 64 * Engine::mixer()->processingSampleRate() / 44100 );
  98. m_rms[1]->setSize( 64 * Engine::mixer()->processingSampleRate() / 44100 );
  99. calcAttack();
  100. calcRelease();
  101. m_needsUpdate = false;
  102. }
  103. else
  104. {
  105. if( m_dpControls.m_attackModel.isValueChanged() )
  106. {
  107. calcAttack();
  108. }
  109. if( m_dpControls.m_releaseModel.isValueChanged() )
  110. {
  111. calcRelease();
  112. }
  113. }
  114. for( fpp_t f = 0; f < _frames; ++f )
  115. {
  116. double s[2] = { _buf[f][0], _buf[f][1] };
  117. // apply input gain
  118. s[0] *= inputGain;
  119. s[1] *= inputGain;
  120. // update peak values
  121. for ( i=0; i <= 1; i++ )
  122. {
  123. const double t = m_rms[i]->update( s[i] );
  124. if( t > m_currentPeak[i] )
  125. {
  126. m_currentPeak[i] = qMin( m_currentPeak[i] * m_attCoeff, t );
  127. }
  128. else
  129. if( t < m_currentPeak[i] )
  130. {
  131. m_currentPeak[i] = qMax( m_currentPeak[i] * m_relCoeff, t );
  132. }
  133. m_currentPeak[i] = qBound( DYN_NOISE_FLOOR, m_currentPeak[i], 10.0f );
  134. }
  135. // account for stereo mode
  136. switch( stereoMode )
  137. {
  138. case dynProcControls::SM_Maximum:
  139. {
  140. sm_peak[0] = sm_peak[1] = qMax( m_currentPeak[0], m_currentPeak[1] );
  141. break;
  142. }
  143. case dynProcControls::SM_Average:
  144. {
  145. sm_peak[0] = sm_peak[1] = ( m_currentPeak[0] + m_currentPeak[1] ) * 0.5;
  146. break;
  147. }
  148. case dynProcControls::SM_Unlinked:
  149. {
  150. sm_peak[0] = m_currentPeak[0];
  151. sm_peak[1] = m_currentPeak[1];
  152. break;
  153. }
  154. }
  155. // start effect
  156. for ( i=0; i <= 1; i++ )
  157. {
  158. const int lookup = static_cast<int>( sm_peak[i] * 200.0f );
  159. const float frac = fraction( sm_peak[i] * 200.0f );
  160. if( sm_peak[i] > DYN_NOISE_FLOOR )
  161. {
  162. if ( lookup < 1 )
  163. {
  164. gain = frac * samples[0];
  165. }
  166. else
  167. if ( lookup < 200 )
  168. {
  169. gain = linearInterpolate( samples[ lookup - 1 ],
  170. samples[ lookup ], frac );
  171. }
  172. else
  173. {
  174. gain = samples[199];
  175. };
  176. s[i] *= gain;
  177. s[i] /= sm_peak[i];
  178. }
  179. }
  180. // apply output gain
  181. s[0] *= outputGain;
  182. s[1] *= outputGain;
  183. // mix wet/dry signals
  184. _buf[f][0] = d * _buf[f][0] + w * s[0];
  185. _buf[f][1] = d * _buf[f][1] + w * s[1];
  186. out_sum += _buf[f][0] * _buf[f][0] + _buf[f][1] * _buf[f][1];
  187. }
  188. checkGate( out_sum / _frames );
  189. return( isRunning() );
  190. }
  191. extern "C"
  192. {
  193. // necessary for getting instance out of shared lib
  194. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model * _parent, void * _data )
  195. {
  196. return( new dynProcEffect( _parent,
  197. static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>(
  198. _data ) ) );
  199. }
  200. }