Lfo.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * lfo.h - declaration of Lfo class, a simple sine lfo
  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 LFO_H
  25. #define LFO_H
  26. #include "lmms_math.h"
  27. class Lfo
  28. {
  29. public:
  30. Lfo( int samplerate );
  31. ~Lfo()
  32. {
  33. }
  34. inline void setFrequency( double frequency )
  35. {
  36. if( frequency < 0 || frequency > ( m_samplerate / 2.0 ) || frequency == m_frequency )
  37. {
  38. return;
  39. }
  40. m_frequency = frequency;
  41. m_increment = m_frequency * m_twoPiOverSr;
  42. if( m_phase >= F_2PI )
  43. {
  44. m_phase -= F_2PI;
  45. }
  46. }
  47. inline void setSampleRate ( int samplerate )
  48. {
  49. m_samplerate = samplerate;
  50. m_twoPiOverSr = F_2PI / samplerate;
  51. m_increment = m_frequency * m_twoPiOverSr;
  52. }
  53. float tick();
  54. private:
  55. double m_frequency;
  56. double m_phase;
  57. double m_increment;
  58. double m_twoPiOverSr;
  59. int m_samplerate;
  60. };
  61. #endif // LFO_H