StereoDelay.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * stereodelay.cpp - defination of StereoDelay 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 "StereoDelay.h"
  25. #include <cstdlib>
  26. #include "lmms_basics.h"
  27. #include "interpolation.h"
  28. #include "lmms_math.h"
  29. StereoDelay::StereoDelay( int maxTime, int sampleRate )
  30. {
  31. m_buffer = 0;
  32. m_maxTime = maxTime;
  33. m_maxLength = maxTime * sampleRate;
  34. m_length = m_maxLength;
  35. m_writeIndex = 0;
  36. m_feedback = 0.0f;
  37. setSampleRate( sampleRate );
  38. }
  39. StereoDelay::~StereoDelay()
  40. {
  41. if( m_buffer )
  42. {
  43. delete[] m_buffer;
  44. }
  45. }
  46. void StereoDelay::tick( sampleFrame& frame )
  47. {
  48. m_writeIndex = ( m_writeIndex + 1 ) % ( int )m_maxLength;
  49. int readIndex = m_writeIndex - m_length;
  50. if (readIndex < 0 ) { readIndex += m_maxLength; }
  51. float lOut = m_buffer[ readIndex ][ 0 ];
  52. float rOut = m_buffer[ readIndex ] [1 ];
  53. m_buffer[ m_writeIndex ][ 0 ] = frame[ 0 ] + ( lOut * m_feedback );
  54. m_buffer[ m_writeIndex ][ 1 ] = frame[ 1 ] + ( rOut * m_feedback );
  55. frame[ 0 ] = lOut;
  56. frame[ 1 ] = rOut;
  57. }
  58. void StereoDelay::setSampleRate( int sampleRate )
  59. {
  60. if( m_buffer )
  61. {
  62. delete[] m_buffer;
  63. }
  64. int bufferSize = ( int )( sampleRate * m_maxTime );
  65. m_buffer = new sampleFrame[bufferSize];
  66. for( int i = 0 ; i < bufferSize ; i++)
  67. {
  68. m_buffer[i][0] = 0.0;
  69. m_buffer[i][1] = 0.0;
  70. }
  71. }