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