MonoDelay.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * monodelay.cpp - defination of MonoDelay class.
  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 "MonoDelay.h"
  25. #include "interpolation.h"
  26. #include "lmms_math.h"
  27. #include "string.h"
  28. MonoDelay::MonoDelay( int maxTime , int sampleRate )
  29. {
  30. m_buffer = 0;
  31. m_maxTime = maxTime;
  32. m_maxLength = maxTime * sampleRate;
  33. m_length = m_maxLength;
  34. m_writeIndex = 0;
  35. m_feedback = 0.0f;
  36. setSampleRate( sampleRate );
  37. }
  38. MonoDelay::~MonoDelay()
  39. {
  40. if( m_buffer )
  41. {
  42. delete m_buffer;
  43. }
  44. }
  45. void MonoDelay::tick( sample_t* sample )
  46. {
  47. m_writeIndex = ( m_writeIndex + 1 ) % ( int )m_maxLength;
  48. int readIndex = m_writeIndex - m_length;
  49. if (readIndex < 0 ) { readIndex += m_maxLength; }
  50. float out = m_buffer[ readIndex ];
  51. m_buffer[ m_writeIndex ] = *sample + ( out * m_feedback );
  52. *sample = out;
  53. }
  54. void MonoDelay::setSampleRate( int sampleRate )
  55. {
  56. if( m_buffer )
  57. {
  58. delete m_buffer;
  59. }
  60. m_buffer = new sample_t[( int )( sampleRate * m_maxTime ) ];
  61. memset( m_buffer, 0, sizeof(float) * ( int )( sampleRate * m_maxTime ) );
  62. }