KickerOsc.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * KickerOsc.h - alternative sweeping oscillator
  3. *
  4. * Copyright (c) 2006-2014 Tobias Doerffel <tobydox/at/users.sourceforge.net>
  5. * Copyright (c) 2014 grejppi <grejppi/at/gmail.com>
  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. #ifndef KICKER_OSC_H
  26. #define KICKER_OSC_H
  27. #include "DspEffectLibrary.h"
  28. #include "Oscillator.h"
  29. #include "lmms_math.h"
  30. #include "interpolation.h"
  31. #include "MemoryManager.h"
  32. template<class FX = DspEffectLibrary::StereoBypass>
  33. class KickerOsc
  34. {
  35. MM_OPERATORS
  36. public:
  37. KickerOsc( const FX & fx, const float start, const float end, const float noise, const float offset,
  38. const float slope, const float env, const float diststart, const float distend, const float length ) :
  39. m_phase( offset ),
  40. m_startFreq( start ),
  41. m_endFreq( end ),
  42. m_noise( noise ),
  43. m_slope( slope ),
  44. m_env( env ),
  45. m_distStart( diststart ),
  46. m_distEnd( distend ),
  47. m_hasDistEnv( diststart != distend ),
  48. m_length( length ),
  49. m_FX( fx ),
  50. m_counter( 0 ),
  51. m_freq( start )
  52. {
  53. }
  54. virtual ~KickerOsc()
  55. {
  56. }
  57. void update( sampleFrame* buf, const fpp_t frames, const float sampleRate )
  58. {
  59. for( fpp_t frame = 0; frame < frames; ++frame )
  60. {
  61. const double gain = ( 1 - fastPow( ( m_counter < m_length ) ? m_counter / m_length : 1, m_env ) );
  62. const sample_t s = ( Oscillator::sinSample( m_phase ) * ( 1 - m_noise ) ) + ( Oscillator::noiseSample( 0 ) * gain * gain * m_noise );
  63. buf[frame][0] = s * gain;
  64. buf[frame][1] = s * gain;
  65. // update distortion envelope if necessary
  66. if( m_hasDistEnv && m_counter < m_length )
  67. {
  68. float thres = linearInterpolate( m_distStart, m_distEnd, m_counter / m_length );
  69. m_FX.leftFX().setThreshold( thres );
  70. m_FX.rightFX().setThreshold( thres );
  71. }
  72. m_FX.nextSample( buf[frame][0], buf[frame][1] );
  73. m_phase += m_freq / sampleRate;
  74. const double change = ( m_counter < m_length ) ? ( ( m_startFreq - m_endFreq ) * ( 1 - fastPow( m_counter / m_length, m_slope ) ) ) : 0;
  75. m_freq = m_endFreq + change;
  76. ++m_counter;
  77. }
  78. }
  79. private:
  80. float m_phase;
  81. const float m_startFreq;
  82. const float m_endFreq;
  83. const float m_noise;
  84. const float m_slope;
  85. const float m_env;
  86. const float m_distStart;
  87. const float m_distEnd;
  88. const bool m_hasDistEnv;
  89. const float m_length;
  90. FX m_FX;
  91. unsigned long m_counter;
  92. double m_freq;
  93. };
  94. #endif