dynamics_processor.cpp 5.7 KB

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