MultitapEcho.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * MultitapEcho.cpp - a multitap echo delay plugin
  3. *
  4. * Copyright (c) 2014 Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>
  5. * Copyright (c) 2008-2014 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 "MultitapEcho.h"
  26. #include "embed.cpp"
  27. extern "C"
  28. {
  29. Plugin::Descriptor PLUGIN_EXPORT multitapecho_plugin_descriptor =
  30. {
  31. STRINGIFY( PLUGIN_NAME ),
  32. "Multitap Echo",
  33. QT_TRANSLATE_NOOP( "pluginBrowser", "A multitap echo delay plugin" ),
  34. "Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>",
  35. 0x0100,
  36. Plugin::Effect,
  37. new PluginPixmapLoader( "logo" ),
  38. NULL,
  39. NULL
  40. } ;
  41. }
  42. MultitapEchoEffect::MultitapEchoEffect( Model* parent, const Descriptor::SubPluginFeatures::Key* key ) :
  43. Effect( &multitapecho_plugin_descriptor, parent, key ),
  44. m_stages( 1 ),
  45. m_controls( this ),
  46. m_buffer( 16100.0f ),
  47. m_sampleRate( Engine::mixer()->processingSampleRate() ),
  48. m_sampleRatio( 1.0f / m_sampleRate )
  49. {
  50. m_work = MM_ALLOC( sampleFrame, Engine::mixer()->framesPerPeriod() );
  51. m_buffer.reset();
  52. m_stages = static_cast<int>( m_controls.m_stages.value() );
  53. updateFilters( 0, 19 );
  54. }
  55. MultitapEchoEffect::~MultitapEchoEffect()
  56. {
  57. MM_FREE( m_work );
  58. }
  59. void MultitapEchoEffect::updateFilters( int begin, int end )
  60. {
  61. for( int i = begin; i <= end; ++i )
  62. {
  63. for( int s = 0; s < m_stages; ++s )
  64. {
  65. setFilterFreq( m_lpFreq[i] * m_sampleRatio, m_filter[i][s] );
  66. }
  67. }
  68. }
  69. void MultitapEchoEffect::runFilter( sampleFrame * dst, sampleFrame * src, StereoOnePole & filter, const fpp_t frames )
  70. {
  71. for( int f = 0; f < frames; ++f )
  72. {
  73. dst[f][0] = filter.update( src[f][0], 0 );
  74. dst[f][1] = filter.update( src[f][1], 1 );
  75. }
  76. }
  77. bool MultitapEchoEffect::processAudioBuffer( sampleFrame * buf, const fpp_t frames )
  78. {
  79. if( !isEnabled() || !isRunning () )
  80. {
  81. return( false );
  82. }
  83. double outSum = 0.0;
  84. const float d = dryLevel();
  85. const float w = wetLevel();
  86. // get processing vars
  87. const int steps = m_controls.m_steps.value();
  88. const float stepLength = m_controls.m_stepLength.value();
  89. const float dryGain = dbfsToAmp( m_controls.m_dryGain.value() );
  90. const bool swapInputs = m_controls.m_swapInputs.value();
  91. // check if number of stages has changed
  92. if( m_controls.m_stages.isValueChanged() )
  93. {
  94. m_stages = static_cast<int>( m_controls.m_stages.value() );
  95. updateFilters( 0, steps - 1 );
  96. }
  97. // add dry buffer - never swap inputs for dry
  98. m_buffer.writeAddingMultiplied( buf, 0, frames, dryGain );
  99. // swapped inputs?
  100. if( swapInputs )
  101. {
  102. float offset = stepLength;
  103. for( int i = 0; i < steps; ++i ) // add all steps swapped
  104. {
  105. for( int s = 0; s < m_stages; ++s )
  106. {
  107. runFilter( m_work, buf, m_filter[i][s], frames );
  108. }
  109. m_buffer.writeSwappedAddingMultiplied( m_work, offset, frames, m_amp[i] );
  110. offset += stepLength;
  111. }
  112. }
  113. else
  114. {
  115. float offset = stepLength;
  116. for( int i = 0; i < steps; ++i ) // add all steps
  117. {
  118. for( int s = 0; s < m_stages; ++s )
  119. {
  120. runFilter( m_work, buf, m_filter[i][s], frames );
  121. }
  122. m_buffer.writeAddingMultiplied( m_work, offset, frames, m_amp[i] );
  123. offset += stepLength;
  124. }
  125. }
  126. // pop the buffer and mix it into output
  127. m_buffer.pop( m_work );
  128. for( int f = 0; f < frames; ++f )
  129. {
  130. buf[f][0] = d * buf[f][0] + w * m_work[f][0];
  131. buf[f][1] = d * buf[f][1] + w * m_work[f][1];
  132. outSum += buf[f][0]*buf[f][0] + buf[f][1]*buf[f][1];
  133. }
  134. checkGate( outSum / frames );
  135. return isRunning();
  136. }
  137. extern "C"
  138. {
  139. // necessary for getting instance out of shared lib
  140. Plugin * PLUGIN_EXPORT lmms_plugin_main( Model* parent, void* data )
  141. {
  142. return new MultitapEchoEffect( parent, static_cast<const Plugin::Descriptor::SubPluginFeatures::Key *>( data ) );
  143. }
  144. }