QuadratureLfo.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * quadraturelfo.h - defination of QuadratureLfo 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. #ifndef QUADRATURELFO_H
  25. #define QUADRATURELFO_H
  26. #include "lmms_math.h"
  27. class QuadratureLfo
  28. {
  29. public:
  30. QuadratureLfo( int sampleRate ) :
  31. m_frequency(0),
  32. m_phase(0),
  33. m_offset(D_PI / 2)
  34. {
  35. setSampleRate(sampleRate);
  36. }
  37. ~QuadratureLfo() = default;
  38. inline void setFrequency( double frequency )
  39. {
  40. if( frequency < 0 || frequency > m_samplerate / 2.0 || frequency == m_frequency )
  41. {
  42. return;
  43. }
  44. m_frequency = frequency;
  45. m_increment = m_frequency * m_twoPiOverSr;
  46. }
  47. inline void restart()
  48. {
  49. m_phase = 0;
  50. }
  51. inline void setSampleRate ( int samplerate )
  52. {
  53. m_samplerate = samplerate;
  54. m_twoPiOverSr = F_2PI / samplerate;
  55. m_increment = m_frequency * m_twoPiOverSr;
  56. }
  57. inline void setOffset( double offsetVal )
  58. {
  59. m_offset = offsetVal;
  60. }
  61. void tick( float *l, float *r )
  62. {
  63. *l = sinf( m_phase );
  64. *r = sinf( m_phase + m_offset );
  65. m_phase += m_increment;
  66. while (m_phase >= D_2PI)
  67. {
  68. m_phase -= D_2PI;
  69. }
  70. }
  71. private:
  72. double m_frequency;
  73. double m_phase;
  74. double m_increment;
  75. double m_twoPiOverSr;
  76. double m_offset;
  77. int m_samplerate;
  78. };
  79. #endif // QUADRATURELFO_H