Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   FAQ   Tutorial


Modulate.h
1 #ifndef STK_MODULATE_H
2 #define STK_MODULATE_H
3 
4 #include "Generator.h"
5 #include "SineWave.h"
6 #include "Noise.h"
7 #include "OnePole.h"
8 
9 namespace stk {
10 
11 /***************************************************/
21 /***************************************************/
22 
23 class Modulate : public Generator
24 {
25  public:
27 
30  Modulate( void );
31 
33  ~Modulate( void );
34 
36  void reset( void ) { lastFrame_[0] = 0.0; };
37 
39  void setVibratoRate( StkFloat rate ) { vibrato_.setFrequency( rate ); };
40 
42  void setVibratoGain( StkFloat gain ) { vibratoGain_ = gain; };
43 
45  void setRandomRate( StkFloat rate ) { noiseRate_ = (unsigned int) ( rate * Stk::sampleRate() / 22050.0 ); };
46 
48  void setRandomGain( StkFloat gain );
49 
51  StkFloat lastOut( void ) const { return lastFrame_[0]; };
52 
54  StkFloat tick( void );
55 
57 
64  StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
65 
66  protected:
67 
68  void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
69 
70  SineWave vibrato_;
71  Noise noise_;
72  OnePole filter_;
73  StkFloat vibratoGain_;
74  StkFloat randomGain_;
75  unsigned int noiseRate_;
76  unsigned int noiseCounter_;
77 
78 };
79 
80 inline StkFloat Modulate :: tick( void )
81 {
82  // Compute periodic and random modulations.
83  lastFrame_[0] = vibratoGain_ * vibrato_.tick();
84  if ( noiseCounter_++ >= noiseRate_ ) {
85  noise_.tick();
86  noiseCounter_ = 0;
87  }
88  lastFrame_[0] += filter_.tick( noise_.lastOut() );
89  return lastFrame_[0];
90 }
91 
92 inline StkFrames& Modulate :: tick( StkFrames& frames, unsigned int channel )
93 {
94 #if defined(_STK_DEBUG_)
95  if ( channel >= frames.channels() ) {
96  oStream_ << "Modulate::tick(): channel and StkFrames arguments are incompatible!";
97  handleError( StkError::FUNCTION_ARGUMENT );
98  }
99 #endif
100 
101  StkFloat *samples = &frames[channel];
102  unsigned int hop = frames.channels();
103  for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
104  *samples = Modulate::tick();
105 
106  return frames;
107 }
108 
109 } // stk namespace
110 
111 #endif

The Synthesis ToolKit in C++ (STK)
©1995--2019 Perry R. Cook and Gary P. Scavone. All Rights Reserved.