waveshaper.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * waveshaper.cpp - waveshaper 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 "waveshaper.h"
  26. #include "lmms_math.h"
  27. #include "embed.cpp"
  28. #include "interpolation.h"
  29. extern "C"
  30. {
  31. Plugin::Descriptor PLUGIN_EXPORT waveshaper_plugin_descriptor =
  32. {
  33. STRINGIFY( PLUGIN_NAME ),
  34. "Waveshaper Effect",
  35. QT_TRANSLATE_NOOP( "pluginBrowser",
  36. "plugin for waveshaping" ),
  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. waveShaperEffect::waveShaperEffect( Model * _parent,
  46. const Descriptor::SubPluginFeatures::Key * _key ) :
  47. Effect( &waveshaper_plugin_descriptor, _parent, _key ),
  48. m_wsControls( this )
  49. {
  50. }
  51. waveShaperEffect::~waveShaperEffect()
  52. {
  53. }
  54. bool waveShaperEffect::processAudioBuffer( sampleFrame * _buf,
  55. const fpp_t _frames )
  56. {
  57. if( !isEnabled() || !isRunning () )
  58. {
  59. return( false );
  60. }
  61. // variables for effect
  62. int i = 0;
  63. double out_sum = 0.0;
  64. const float d = dryLevel();
  65. const float w = wetLevel();
  66. float input = m_wsControls.m_inputModel.value();
  67. float output = m_wsControls.m_outputModel.value();
  68. const float * samples = m_wsControls.m_wavegraphModel.samples();
  69. const bool clip = m_wsControls.m_clipModel.value();
  70. ValueBuffer *inputBuffer = m_wsControls.m_inputModel.valueBuffer();
  71. ValueBuffer *outputBufer = m_wsControls.m_outputModel.valueBuffer();
  72. int inputInc = inputBuffer ? 1 : 0;
  73. int outputInc = outputBufer ? 1 : 0;
  74. const float *inputPtr = inputBuffer ? &( inputBuffer->values()[ 0 ] ) : &input;
  75. const float *outputPtr = outputBufer ? &( outputBufer->values()[ 0 ] ) : &output;
  76. for( fpp_t f = 0; f < _frames; ++f )
  77. {
  78. float s[2] = { _buf[f][0], _buf[f][1] };
  79. // apply input gain
  80. s[0] *= *inputPtr;
  81. s[1] *= *inputPtr;
  82. // clip if clip enabled
  83. if( clip )
  84. {
  85. s[0] = qBound( -1.0f, s[0], 1.0f );
  86. s[1] = qBound( -1.0f, s[1], 1.0f );
  87. }
  88. // start effect
  89. for( i=0; i <= 1; ++i )
  90. {
  91. const int lookup = static_cast<int>( qAbs( s[i] ) * 200.0f );
  92. const float frac = fraction( qAbs( s[i] ) * 200.0f );
  93. const float posneg = s[i] < 0 ? -1.0f : 1.0f;
  94. if( lookup < 1 )
  95. {
  96. s[i] = frac * samples[0] * posneg;
  97. }
  98. else if( lookup < 200 )
  99. {
  100. s[i] = linearInterpolate( samples[ lookup - 1 ],
  101. samples[ lookup ], frac )
  102. * posneg;
  103. }
  104. else
  105. {
  106. s[i] *= samples[199];
  107. }
  108. }
  109. // apply output gain
  110. s[0] *= *outputPtr;
  111. s[1] *= *outputPtr;
  112. out_sum += _buf[f][0]*_buf[f][0] + _buf[f][1]*_buf[f][1];
  113. // mix wet/dry signals
  114. _buf[f][0] = d * _buf[f][0] + w * s[0];
  115. _buf[f][1] = d * _buf[f][1] + w * s[1];
  116. outputPtr += outputInc;
  117. inputPtr += inputInc;
  118. }
  119. checkGate( out_sum / _frames );
  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 waveShaperEffect( _parent,
  128. static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>(
  129. _data ) ) );
  130. }
  131. }