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