waveshaper_controls.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * waveshaper_controls.cpp - controls for waveshaper-effect
  3. *
  4. * Copyright (c) 2014 Vesa Kivimäki <contact/dot/diizy/at/nbl/dot/fi>
  5. * Copyright (c) 2008 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 "waveshaper_controls.h"
  27. #include "waveshaper.h"
  28. #include "base64.h"
  29. #include "Graph.h"
  30. #include "Engine.h"
  31. #include "Song.h"
  32. #define onedB 1.1220184543019633f
  33. waveShaperControls::waveShaperControls( waveShaperEffect * _eff ) :
  34. EffectControls( _eff ),
  35. m_effect( _eff ),
  36. m_inputModel( 1.0f, 0.0f, 5.0f, 0.01f, this, tr( "Input gain" ) ),
  37. m_outputModel( 1.0f, 0.0f, 5.0f, 0.01f, this, tr( "Output gain" ) ),
  38. m_wavegraphModel( 0.0f, 1.0f, 200, this ),
  39. m_clipModel( false, this )
  40. {
  41. connect( &m_wavegraphModel, SIGNAL( samplesChanged( int, int ) ),
  42. this, SLOT( samplesChanged( int, int ) ) );
  43. setDefaultShape();
  44. }
  45. void waveShaperControls::samplesChanged( int _begin, int _end)
  46. {
  47. Engine::getSong()->setModified();
  48. }
  49. void waveShaperControls::loadSettings( const QDomElement & _this )
  50. {
  51. //load input, output knobs
  52. m_inputModel.loadSettings( _this, "inputGain" );
  53. m_outputModel.loadSettings( _this, "outputGain" );
  54. m_clipModel.loadSettings( _this, "clipInput" );
  55. //load waveshape
  56. int size = 0;
  57. char * dst = 0;
  58. base64::decode( _this.attribute( "waveShape"), &dst, &size );
  59. m_wavegraphModel.setSamples( (float*) dst );
  60. delete[] dst;
  61. }
  62. void waveShaperControls::saveSettings( QDomDocument & _doc,
  63. QDomElement & _this )
  64. {
  65. //save input, output knobs
  66. m_inputModel.saveSettings( _doc, _this, "inputGain" );
  67. m_outputModel.saveSettings( _doc, _this, "outputGain" );
  68. m_clipModel.saveSettings( _doc, _this, "clipInput" );
  69. //save waveshape
  70. QString sampleString;
  71. base64::encode( (const char *)m_wavegraphModel.samples(),
  72. m_wavegraphModel.length() * sizeof(float), sampleString );
  73. _this.setAttribute( "waveShape", sampleString );
  74. }
  75. void waveShaperControls::setDefaultShape()
  76. {
  77. float shp [200] = { };
  78. for ( int i = 0; i<200; i++)
  79. {
  80. shp[i] = ((float)i + 1.0f) / 200.0f;
  81. }
  82. m_wavegraphModel.setLength( 200 );
  83. m_wavegraphModel.setSamples( (float*)&shp );
  84. }
  85. void waveShaperControls::resetClicked()
  86. {
  87. setDefaultShape();
  88. Engine::getSong()->setModified();
  89. }
  90. void waveShaperControls::smoothClicked()
  91. {
  92. m_wavegraphModel.smoothNonCyclic();
  93. Engine::getSong()->setModified();
  94. }
  95. void waveShaperControls::addOneClicked()
  96. {
  97. for( int i=0; i<200; i++ )
  98. {
  99. m_wavegraphModel.setSampleAt( i, qBound( 0.0f, m_wavegraphModel.samples()[i] * onedB, 1.0f ) );
  100. }
  101. Engine::getSong()->setModified();
  102. }
  103. void waveShaperControls::subOneClicked()
  104. {
  105. for( int i=0; i<200; i++ )
  106. {
  107. m_wavegraphModel.setSampleAt( i, qBound( 0.0f, m_wavegraphModel.samples()[i] / onedB, 1.0f ) );
  108. }
  109. Engine::getSong()->setModified();
  110. }