FlangerEffect.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * flangereffect.cpp - defination of FlangerEffect class.
  3. *
  4. * Copyright (c) 2014 David French <dave/dot/french3/at/googlemail/dot/com>
  5. *
  6. * This file is part of LMMS - https://lmms.io
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public
  19. * License along with this program (see COPYING); if not, write to the
  20. * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. * Boston, MA 02110-1301 USA.
  22. *
  23. */
  24. #include "FlangerEffect.h"
  25. #include "Engine.h"
  26. #include "embed.cpp"
  27. extern "C"
  28. {
  29. Plugin::Descriptor PLUGIN_EXPORT flanger_plugin_descriptor =
  30. {
  31. STRINGIFY( PLUGIN_NAME ),
  32. "Flanger",
  33. QT_TRANSLATE_NOOP( "pluginBrowser", "A native flanger plugin" ),
  34. "Dave French <contact/dot/dave/dot/french3/at/googlemail/dot/com>",
  35. 0x0100,
  36. Plugin::Effect,
  37. new PluginPixmapLoader( "logo" ),
  38. NULL,
  39. NULL
  40. } ;
  41. FlangerEffect::FlangerEffect( Model *parent, const Plugin::Descriptor::SubPluginFeatures::Key *key ) :
  42. Effect( &flanger_plugin_descriptor, parent, key ),
  43. m_flangerControls( this )
  44. {
  45. m_lfo = new QuadratureLfo( Engine::mixer()->processingSampleRate() );
  46. m_lDelay = new MonoDelay( 1, Engine::mixer()->processingSampleRate() );
  47. m_rDelay = new MonoDelay( 1, Engine::mixer()->processingSampleRate() );
  48. m_noise = new Noise;
  49. }
  50. FlangerEffect::~FlangerEffect()
  51. {
  52. if(m_lDelay )
  53. {
  54. delete m_lDelay;
  55. }
  56. if( m_rDelay )
  57. {
  58. delete m_rDelay;
  59. }
  60. if( m_lfo )
  61. {
  62. delete m_lfo;
  63. }
  64. if(m_noise)
  65. {
  66. delete m_noise;
  67. }
  68. }
  69. bool FlangerEffect::processAudioBuffer( sampleFrame *buf, const fpp_t frames )
  70. {
  71. if( !isEnabled() || !isRunning () )
  72. {
  73. return( false );
  74. }
  75. double outSum = 0.0;
  76. const float d = dryLevel();
  77. const float w = wetLevel();
  78. const float length = m_flangerControls.m_delayTimeModel.value() * Engine::mixer()->processingSampleRate();
  79. const float noise = m_flangerControls.m_whiteNoiseAmountModel.value();
  80. float amplitude = m_flangerControls.m_lfoAmountModel.value() * Engine::mixer()->processingSampleRate();
  81. bool invertFeedback = m_flangerControls.m_invertFeedbackModel.value();
  82. m_lfo->setFrequency( 1.0/m_flangerControls.m_lfoFrequencyModel.value() );
  83. m_lDelay->setFeedback( m_flangerControls.m_feedbackModel.value() );
  84. m_rDelay->setFeedback( m_flangerControls.m_feedbackModel.value() );
  85. sample_t dryS[2];
  86. float leftLfo;
  87. float rightLfo;
  88. for( fpp_t f = 0; f < frames; ++f )
  89. {
  90. buf[f][0] += m_noise->tick() * noise;
  91. buf[f][1] += m_noise->tick() * noise;
  92. dryS[0] = buf[f][0];
  93. dryS[1] = buf[f][1];
  94. m_lfo->tick(&leftLfo, &rightLfo);
  95. m_lDelay->setLength( ( float )length + amplitude * (leftLfo+1.0) );
  96. m_rDelay->setLength( ( float )length + amplitude * (rightLfo+1.0) );
  97. if(invertFeedback)
  98. {
  99. m_lDelay->tick( &buf[f][1] );
  100. m_rDelay->tick(&buf[f][0] );
  101. } else
  102. {
  103. m_lDelay->tick( &buf[f][0] );
  104. m_rDelay->tick( &buf[f][1] );
  105. }
  106. buf[f][0] = ( d * dryS[0] ) + ( w * buf[f][0] );
  107. buf[f][1] = ( d * dryS[1] ) + ( w * buf[f][1] );
  108. outSum += buf[f][0]*buf[f][0] + buf[f][1]*buf[f][1];
  109. }
  110. checkGate( outSum / frames );
  111. return isRunning();
  112. }
  113. void FlangerEffect::changeSampleRate()
  114. {
  115. m_lfo->setSampleRate( Engine::mixer()->processingSampleRate() );
  116. m_lDelay->setSampleRate( Engine::mixer()->processingSampleRate() );
  117. m_rDelay->setSampleRate( Engine::mixer()->processingSampleRate() );
  118. }
  119. void FlangerEffect::restartLFO()
  120. {
  121. m_lfo->restart();
  122. }
  123. extern "C"
  124. {
  125. //needed for getting plugin out of shared lib
  126. Plugin * PLUGIN_EXPORT lmms_plugin_main( Model* parent, void* data )
  127. {
  128. return new FlangerEffect( parent , static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>( data ) );
  129. }
  130. }}