DelayEffect.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * delayeffect.cpp - definition of the DelayEffect class. The Delay Plugin
  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 "DelayEffect.h"
  25. #include "Engine.h"
  26. #include "embed.h"
  27. #include "interpolation.h"
  28. #include "plugin_export.h"
  29. extern "C"
  30. {
  31. Plugin::Descriptor PLUGIN_EXPORT delay_plugin_descriptor =
  32. {
  33. STRINGIFY( PLUGIN_NAME ),
  34. "Delay",
  35. QT_TRANSLATE_NOOP( "PluginBrowser", "A native delay plugin" ),
  36. "Dave French <contact/dot/dave/dot/french3/at/googlemail/dot/com>",
  37. 0x0100,
  38. Plugin::Effect,
  39. new PluginPixmapLoader("logo"),
  40. NULL,
  41. NULL
  42. } ;
  43. DelayEffect::DelayEffect( Model* parent, const Plugin::Descriptor::SubPluginFeatures::Key* key ) :
  44. Effect( &delay_plugin_descriptor, parent, key ),
  45. m_delayControls( this )
  46. {
  47. m_delay = 0;
  48. m_delay = new StereoDelay( 20, Engine::mixer()->processingSampleRate() );
  49. m_lfo = new Lfo( Engine::mixer()->processingSampleRate() );
  50. m_outGain = 1.0;
  51. }
  52. DelayEffect::~DelayEffect()
  53. {
  54. if( m_delay )
  55. {
  56. delete m_delay;
  57. }
  58. if( m_lfo )
  59. {
  60. delete m_lfo;
  61. }
  62. }
  63. bool DelayEffect::processAudioBuffer( sampleFrame* buf, const fpp_t frames )
  64. {
  65. if( !isEnabled() || !isRunning () )
  66. {
  67. return( false );
  68. }
  69. double outSum = 0.0;
  70. const float sr = Engine::mixer()->processingSampleRate();
  71. const float d = dryLevel();
  72. const float w = wetLevel();
  73. sample_t dryS[2];
  74. float lPeak = 0.0;
  75. float rPeak = 0.0;
  76. float length = m_delayControls.m_delayTimeModel.value();
  77. float amplitude = m_delayControls.m_lfoAmountModel.value() * sr;
  78. float lfoTime = 1.0 / m_delayControls.m_lfoTimeModel.value();
  79. float feedback = m_delayControls.m_feedbackModel.value();
  80. ValueBuffer *lengthBuffer = m_delayControls.m_delayTimeModel.valueBuffer();
  81. ValueBuffer *feedbackBuffer = m_delayControls.m_feedbackModel.valueBuffer();
  82. ValueBuffer *lfoTimeBuffer = m_delayControls.m_lfoTimeModel.valueBuffer();
  83. ValueBuffer *lfoAmountBuffer = m_delayControls.m_lfoAmountModel.valueBuffer();
  84. int lengthInc = lengthBuffer ? 1 : 0;
  85. int amplitudeInc = lfoAmountBuffer ? 1 : 0;
  86. int lfoTimeInc = lfoTimeBuffer ? 1 : 0;
  87. int feedbackInc = feedbackBuffer ? 1 : 0;
  88. float *lengthPtr = lengthBuffer ? &( lengthBuffer->values()[ 0 ] ) : &length;
  89. float *amplitudePtr = lfoAmountBuffer ? &( lfoAmountBuffer->values()[ 0 ] ) : &amplitude;
  90. float *lfoTimePtr = lfoTimeBuffer ? &( lfoTimeBuffer->values()[ 0 ] ) : &lfoTime;
  91. float *feedbackPtr = feedbackBuffer ? &( feedbackBuffer->values()[ 0 ] ) : &feedback;
  92. if( m_delayControls.m_outGainModel.isValueChanged() )
  93. {
  94. m_outGain = dbfsToAmp( m_delayControls.m_outGainModel.value() );
  95. }
  96. int sampleLength;
  97. for( fpp_t f = 0; f < frames; ++f )
  98. {
  99. dryS[0] = buf[f][0];
  100. dryS[1] = buf[f][1];
  101. m_delay->setFeedback( *feedbackPtr );
  102. m_lfo->setFrequency( *lfoTimePtr );
  103. sampleLength = *lengthPtr * Engine::mixer()->processingSampleRate();
  104. m_currentLength = sampleLength;
  105. m_delay->setLength( m_currentLength + ( *amplitudePtr * ( float )m_lfo->tick() ) );
  106. m_delay->tick( buf[f] );
  107. buf[f][0] *= m_outGain;
  108. buf[f][1] *= m_outGain;
  109. lPeak = buf[f][0] > lPeak ? buf[f][0] : lPeak;
  110. rPeak = buf[f][1] > rPeak ? buf[f][1] : rPeak;
  111. buf[f][0] = ( d * dryS[0] ) + ( w * buf[f][0] );
  112. buf[f][1] = ( d * dryS[1] ) + ( w * buf[f][1] );
  113. outSum += buf[f][0]*buf[f][0] + buf[f][1]*buf[f][1];
  114. lengthPtr += lengthInc;
  115. amplitudePtr += amplitudeInc;
  116. lfoTimePtr += lfoTimeInc;
  117. feedbackPtr += feedbackInc;
  118. }
  119. checkGate( outSum / frames );
  120. m_delayControls.m_outPeakL = lPeak;
  121. m_delayControls.m_outPeakR = rPeak;
  122. return isRunning();
  123. }
  124. void DelayEffect::changeSampleRate()
  125. {
  126. m_lfo->setSampleRate( Engine::mixer()->processingSampleRate() );
  127. m_delay->setSampleRate( Engine::mixer()->processingSampleRate() );
  128. }
  129. extern "C"
  130. {
  131. //needed for getting plugin out of shared lib
  132. PLUGIN_EXPORT Plugin * lmms_plugin_main( Model* parent, void* data )
  133. {
  134. return new DelayEffect( parent , static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>( data ) );
  135. }
  136. }}