Arduino STK  4.6.2
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
STK abstract unit generator parent class.
Definition: Generator.h:21
STK periodic/random modulator.
Definition: Modulate.h:24
void setVibratoGain(StkFloat gain)
Set the periodic (vibrato) gain.
Definition: Modulate.h:42
StkFloat tick(void)
Compute and return one output sample.
Definition: Modulate.h:80
void setVibratoRate(StkFloat rate)
Set the periodic (vibrato) rate or frequency in Hz.
Definition: Modulate.h:39
void reset(void)
Reset internal state.
Definition: Modulate.h:36
void setRandomRate(StkFloat rate)
Set the periodic (vibrato) rate or frequency in Hz.
Definition: Modulate.h:45
Modulate(void)
Class constructor.
StkFloat lastOut(void) const
Return the last computed output value.
Definition: Modulate.h:51
void setRandomGain(StkFloat gain)
Set the random modulation gain.
~Modulate(void)
Class destructor.
STK noise generator.
Definition: Noise.h:22
StkFloat lastOut(void) const
Return the last computed output value.
Definition: Noise.h:40
StkFloat tick(void)
Compute and return one output sample.
Definition: Noise.h:59
STK one-pole filter class.
Definition: OnePole.h:21
StkFloat tick(StkFloat input)
Input one sample to the filter and return one output.
Definition: OnePole.h:80
STK sinusoid oscillator class.
Definition: SineWave.h:26
void setFrequency(StkFloat frequency)
Set the data interpolation rate based on a looping frequency.
StkFloat tick(void)
Compute and return one output sample.
Definition: SineWave.h:99
An STK class to handle vectorized audio data.
Definition: Stk.h:287
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition: Stk.h:415
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition: Stk.h:418
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
static StkFloat sampleRate(void)
Static method that returns the current STK sample rate.
Definition: Stk.h:156
The STK namespace.
Definition: ADSR.h:8