FlangerEffect.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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.h"
  27. #include "plugin_export.h"
  28. extern "C"
  29. {
  30. Plugin::Descriptor PLUGIN_EXPORT flanger_plugin_descriptor =
  31. {
  32. STRINGIFY( PLUGIN_NAME ),
  33. "Flanger",
  34. QT_TRANSLATE_NOOP( "pluginBrowser", "A native flanger plugin" ),
  35. "Dave French <contact/dot/dave/dot/french3/at/googlemail/dot/com>",
  36. 0x0100,
  37. Plugin::Effect,
  38. new PluginPixmapLoader("logo"),
  39. NULL,
  40. NULL
  41. } ;
  42. FlangerEffect::FlangerEffect( Model *parent, const Plugin::Descriptor::SubPluginFeatures::Key *key ) :
  43. Effect( &flanger_plugin_descriptor, parent, key ),
  44. m_flangerControls( this )
  45. {
  46. m_lfo = new QuadratureLfo( Engine::mixer()->processingSampleRate() );
  47. m_lDelay = new MonoDelay( 1, Engine::mixer()->processingSampleRate() );
  48. m_rDelay = new MonoDelay( 1, Engine::mixer()->processingSampleRate() );
  49. m_noise = new Noise;
  50. }
  51. FlangerEffect::~FlangerEffect()
  52. {
  53. if(m_lDelay )
  54. {
  55. delete m_lDelay;
  56. }
  57. if( m_rDelay )
  58. {
  59. delete m_rDelay;
  60. }
  61. if( m_lfo )
  62. {
  63. delete m_lfo;
  64. }
  65. if(m_noise)
  66. {
  67. delete m_noise;
  68. }
  69. }
  70. bool FlangerEffect::processAudioBuffer( sampleFrame *buf, const fpp_t frames )
  71. {
  72. if( !isEnabled() || !isRunning () )
  73. {
  74. return( false );
  75. }
  76. double outSum = 0.0;
  77. const float d = dryLevel();
  78. const float w = wetLevel();
  79. const float length = m_flangerControls.m_delayTimeModel.value() * Engine::mixer()->processingSampleRate();
  80. const float noise = m_flangerControls.m_whiteNoiseAmountModel.value();
  81. float amplitude = m_flangerControls.m_lfoAmountModel.value() * Engine::mixer()->processingSampleRate();
  82. bool invertFeedback = m_flangerControls.m_invertFeedbackModel.value();
  83. m_lfo->setFrequency( 1.0/m_flangerControls.m_lfoFrequencyModel.value() );
  84. m_lDelay->setFeedback( m_flangerControls.m_feedbackModel.value() );
  85. m_rDelay->setFeedback( m_flangerControls.m_feedbackModel.value() );
  86. sample_t dryS[2];
  87. float leftLfo;
  88. float rightLfo;
  89. for( fpp_t f = 0; f < frames; ++f )
  90. {
  91. buf[f][0] += m_noise->tick() * noise;
  92. buf[f][1] += m_noise->tick() * noise;
  93. dryS[0] = buf[f][0];
  94. dryS[1] = buf[f][1];
  95. m_lfo->tick(&leftLfo, &rightLfo);
  96. m_lDelay->setLength( ( float )length + amplitude * (leftLfo+1.0) );
  97. m_rDelay->setLength( ( float )length + amplitude * (rightLfo+1.0) );
  98. if(invertFeedback)
  99. {
  100. m_lDelay->tick( &buf[f][1] );
  101. m_rDelay->tick(&buf[f][0] );
  102. } else
  103. {
  104. m_lDelay->tick( &buf[f][0] );
  105. m_rDelay->tick( &buf[f][1] );
  106. }
  107. buf[f][0] = ( d * dryS[0] ) + ( w * buf[f][0] );
  108. buf[f][1] = ( d * dryS[1] ) + ( w * buf[f][1] );
  109. outSum += buf[f][0]*buf[f][0] + buf[f][1]*buf[f][1];
  110. }
  111. checkGate( outSum / frames );
  112. return isRunning();
  113. }
  114. void FlangerEffect::changeSampleRate()
  115. {
  116. m_lfo->setSampleRate( Engine::mixer()->processingSampleRate() );
  117. m_lDelay->setSampleRate( Engine::mixer()->processingSampleRate() );
  118. m_rDelay->setSampleRate( Engine::mixer()->processingSampleRate() );
  119. }
  120. void FlangerEffect::restartLFO()
  121. {
  122. m_lfo->restart();
  123. }
  124. extern "C"
  125. {
  126. //needed for getting plugin out of shared lib
  127. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model* parent, void* data )
  128. {
  129. return new FlangerEffect( parent , static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>( data ) );
  130. }
  131. }}