MultitapEchoControls.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * MultitapEchoControls.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 <QDomElement>
  26. #include <QVarLengthArray>
  27. #include "MultitapEchoControls.h"
  28. #include "MultitapEcho.h"
  29. #include "lmms_math.h"
  30. #include "base64.h"
  31. MultitapEchoControls::MultitapEchoControls( MultitapEchoEffect * eff ) :
  32. EffectControls( eff ),
  33. m_effect( eff ),
  34. m_steps( 16, 4, 32, this, "Steps" ),
  35. m_stepLength( 100.0f, 1.0f, 500.0f, 0.1f, 500.0f, this, "Step length" ),
  36. m_dryGain( 0.0f, -80.0f, 20.0f, 0.1f, this, "Dry gain" ),
  37. m_swapInputs( false, this, "Swap inputs" ),
  38. m_stages( 1.0f, 1.0f, 4.0f, 1.0f, this, "Lowpass stages" ),
  39. m_ampGraph( -60.0f, 0.0f, 16, this ),
  40. m_lpGraph( 0.0f, 3.0f, 16, this )
  41. {
  42. m_stages.setStrictStepSize( true );
  43. connect( &m_ampGraph, SIGNAL( samplesChanged( int, int ) ), this, SLOT( ampSamplesChanged( int, int ) ) );
  44. connect( &m_lpGraph, SIGNAL( samplesChanged( int, int ) ), this, SLOT( lpSamplesChanged( int, int ) ) );
  45. connect( &m_steps, SIGNAL( dataChanged() ), this, SLOT( lengthChanged() ) );
  46. connect( Engine::mixer(), SIGNAL( sampleRateChanged() ), this, SLOT( sampleRateChanged() ) );
  47. setDefaultAmpShape();
  48. setDefaultLpShape();
  49. }
  50. MultitapEchoControls::~MultitapEchoControls()
  51. {
  52. }
  53. void MultitapEchoControls::saveSettings( QDomDocument & doc, QDomElement & parent )
  54. {
  55. m_steps.saveSettings( doc, parent, "steps" );
  56. m_stepLength.saveSettings( doc, parent, "steplength" );
  57. m_dryGain.saveSettings( doc, parent, "drygain" );
  58. m_swapInputs.saveSettings( doc, parent, "swapinputs" );
  59. m_stages.saveSettings( doc, parent, "stages" );
  60. QString ampString;
  61. base64::encode( (const char *) m_ampGraph.samples(), m_ampGraph.length() * sizeof(float), ampString );
  62. parent.setAttribute( "ampsteps", ampString );
  63. QString lpString;
  64. base64::encode( (const char *) m_lpGraph.samples(), m_lpGraph.length() * sizeof(float), lpString );
  65. parent.setAttribute( "lpsteps", lpString );
  66. }
  67. void MultitapEchoControls::loadSettings( const QDomElement & elem )
  68. {
  69. m_steps.loadSettings( elem, "steps" );
  70. m_stepLength.loadSettings( elem, "steplength" );
  71. m_dryGain.loadSettings( elem, "drygain" );
  72. m_swapInputs.loadSettings( elem, "swapinputs" );
  73. m_stages.loadSettings( elem, "stages" );
  74. int size = 0;
  75. char * dst = 0;
  76. base64::decode( elem.attribute( "ampsteps"), &dst, &size );
  77. m_ampGraph.setSamples( (float*) dst );
  78. base64::decode( elem.attribute( "lpsteps"), &dst, &size );
  79. m_lpGraph.setSamples( (float*) dst );
  80. delete[] dst;
  81. }
  82. void MultitapEchoControls::setDefaultAmpShape()
  83. {
  84. const int length = m_steps.value();
  85. QVarLengthArray<float> samples(length);
  86. for( int i = 0; i < length; ++i )
  87. {
  88. samples[i] = 0.0f;
  89. }
  90. m_ampGraph.setSamples( &samples[0] );
  91. }
  92. void MultitapEchoControls::setDefaultLpShape()
  93. {
  94. const int length = m_steps.value();
  95. QVarLengthArray<float> samples(length);
  96. for( int i = 0; i < length; ++i )
  97. {
  98. samples[i] = 3.0f;
  99. }
  100. m_lpGraph.setSamples( &samples[0] );
  101. }
  102. void MultitapEchoControls::ampSamplesChanged( int begin, int end )
  103. {
  104. const float * samples = m_ampGraph.samples();
  105. for( int i = begin; i <= end; ++i )
  106. {
  107. m_effect->m_amp[i] = dbfsToAmp( samples[i] );
  108. }
  109. }
  110. void MultitapEchoControls::ampResetClicked()
  111. {
  112. setDefaultAmpShape();
  113. }
  114. void MultitapEchoControls::lpSamplesChanged( int begin, int end )
  115. {
  116. //qDebug( "b/e %d - %d", begin, end );
  117. const float * samples = m_lpGraph.samples();
  118. for( int i = begin; i <= end; ++i )
  119. {
  120. m_effect->m_lpFreq[i] = 20.0f * exp10( samples[i] );
  121. }
  122. m_effect->updateFilters( begin, end );
  123. }
  124. void MultitapEchoControls::lpResetClicked()
  125. {
  126. setDefaultLpShape();
  127. }
  128. void MultitapEchoControls::lengthChanged()
  129. {
  130. const int len = m_steps.value();
  131. m_ampGraph.setLength( len );
  132. ampSamplesChanged( 0, len - 1 );
  133. m_lpGraph.setLength( len );
  134. lpSamplesChanged( 0, len - 1 );
  135. m_effect->updateFilters( 0, len - 1 );
  136. }
  137. void MultitapEchoControls::sampleRateChanged()
  138. {
  139. m_effect->m_sampleRate = Engine::mixer()->processingSampleRate();
  140. m_effect->m_sampleRatio = 1.0f / m_effect->m_sampleRate;
  141. m_effect->updateFilters( 0, 19 );
  142. }