Arduino STK  4.6.2
SineWave.h
1 #ifndef STK_SINEWAVE_H
2 #define STK_SINEWAVE_H
3 
4 const unsigned long TABLE_SIZE = 2048;
5 
6 #include "Generator.h"
7 
8 namespace stk {
9 
10 /***************************************************/
23 /***************************************************/
24 
25 class SineWave : public Generator
26 {
27 public:
29  SineWave( void );
30 
32  ~SineWave( void );
33 
35  void reset( void );
36 
38 
41  void setRate( StkFloat rate ) { rate_ = rate; };
42 
44 
50  void setFrequency( StkFloat frequency );
51 
53  void addTime( StkFloat time );
54 
56 
61  void addPhase( StkFloat phase );
62 
64 
68  void addPhaseOffset( StkFloat phaseOffset );
69 
71  StkFloat lastOut( void ) const { return lastFrame_[0]; };
72 
74  StkFloat tick( void );
75 
77 
84  StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
85 
86 protected:
87 
88  void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
89 
90  static StkFrames table_;
91  StkFloat time_;
92  StkFloat rate_;
93  StkFloat phaseOffset_;
94  unsigned int iIndex_;
95  StkFloat alpha_;
96 
97 };
98 
99 inline StkFloat SineWave :: tick( void )
100 {
101  // Check limits of time address ... if necessary, recalculate modulo
102  // TABLE_SIZE.
103  while ( time_ < 0.0 )
104  time_ += TABLE_SIZE;
105  while ( time_ >= TABLE_SIZE )
106  time_ -= TABLE_SIZE;
107 
108  iIndex_ = (unsigned int) time_;
109  alpha_ = time_ - iIndex_;
110  StkFloat tmp = table_[ iIndex_ ];
111  tmp += ( alpha_ * ( table_[ iIndex_ + 1 ] - tmp ) );
112 
113  // Increment time, which can be negative.
114  time_ += rate_;
115 
116  lastFrame_[0] = tmp;
117  return lastFrame_[0];
118 }
119 
120 inline StkFrames& SineWave :: tick( StkFrames& frames, unsigned int channel )
121 {
122 #if defined(_STK_DEBUG_)
123  if ( channel >= frames.channels() ) {
124  oStream_ << "SineWave::tick(): channel and StkFrames arguments are incompatible!";
125  handleError( StkError::FUNCTION_ARGUMENT );
126  }
127 #endif
128 
129  StkFloat *samples = &frames[channel];
130  StkFloat tmp = 0.0;
131 
132  unsigned int hop = frames.channels();
133  for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
134 
135  // Check limits of time address ... if necessary, recalculate modulo
136  // TABLE_SIZE.
137  while ( time_ < 0.0 )
138  time_ += TABLE_SIZE;
139  while ( time_ >= TABLE_SIZE )
140  time_ -= TABLE_SIZE;
141 
142  iIndex_ = (unsigned int) time_;
143  alpha_ = time_ - iIndex_;
144  tmp = table_[ iIndex_ ];
145  tmp += ( alpha_ * ( table_[ iIndex_ + 1 ] - tmp ) );
146  *samples = tmp;
147 
148  // Increment time, which can be negative.
149  time_ += rate_;
150  }
151 
152  lastFrame_[0] = tmp;
153  return frames;
154 }
155 
156 } // stk namespace
157 
158 #endif
159 
STK abstract unit generator parent class.
Definition: Generator.h:21
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
StkFloat lastOut(void) const
Return the last computed output value.
Definition: SineWave.h:71
void addPhase(StkFloat phase)
Increment the read pointer by a normalized phase value.
void reset(void)
Clear output and reset time pointer to zero.
void addPhaseOffset(StkFloat phaseOffset)
Add a normalized phase offset to the read pointer.
SineWave(void)
Default constructor.
void addTime(StkFloat time)
Increment the read pointer by time in samples, modulo the table size.
~SineWave(void)
Class destructor.
void setRate(StkFloat rate)
Set the data read rate in samples. The rate can be negative.
Definition: SineWave.h:41
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.
The STK namespace.
Definition: ADSR.h:8