waveshaper.cpp 3.9 KB

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