Arduino STK 4.6.2
SineWave.h
1#ifndef STK_SINEWAVE_H
2#define STK_SINEWAVE_H
3
4const unsigned long TABLE_SIZE = 2048;
5
6#include "Generator.h"
7
8namespace stk {
9
10/***************************************************/
23/***************************************************/
24
25class SineWave : public Generator
26{
27public:
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
86protected:
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
99inline StkFloat SineWave :: tick( void )
100{
101 // Prevent that we crash if we call tick if it has not been set up yet
102 if (table_.size()==0) return 0.0f;
103
104 // Check limits of time address ... if necessary, recalculate modulo
105 // TABLE_SIZE.
106 while ( time_ < 0.0f )
107 time_ += TABLE_SIZE;
108 while ( time_ >= TABLE_SIZE )
109 time_ -= TABLE_SIZE;
110
111 iIndex_ = (unsigned int) time_;
112 alpha_ = time_ - iIndex_;
113 StkFloat tmp = table_[ iIndex_ ];
114 tmp += ( alpha_ * ( table_[ iIndex_ + 1 ] - tmp ) );
115
116 // Increment time, which can be negative.
117 time_ += rate_;
118
119 lastFrame_[0] = tmp;
120 return lastFrame_[0];
121}
122
123inline StkFrames& SineWave :: tick( StkFrames& frames, unsigned int channel )
124{
125#if defined(_STK_DEBUG_)
126 if ( channel >= frames.channels() ) {
127 oStream_ << "SineWave::tick(): channel and StkFrames arguments are incompatible!";
128 handleError( StkError::FUNCTION_ARGUMENT );
129 }
130#endif
131
132 StkFloat *samples = &frames[channel];
133 StkFloat tmp = 0.0;
134
135 unsigned int hop = frames.channels();
136 for ( unsigned int i=0; i<frames.frames(); i++, samples += hop ) {
137
138 // Check limits of time address ... if necessary, recalculate modulo
139 // TABLE_SIZE.
140 while ( time_ < 0.0f )
141 time_ += TABLE_SIZE;
142 while ( time_ >= TABLE_SIZE )
143 time_ -= TABLE_SIZE;
144
145 iIndex_ = (unsigned int) time_;
146 alpha_ = time_ - iIndex_;
147 tmp = table_[ iIndex_ ];
148 tmp += ( alpha_ * ( table_[ iIndex_ + 1 ] - tmp ) );
149 *samples = tmp;
150
151 // Increment time, which can be negative.
152 time_ += rate_;
153 }
154
155 lastFrame_[0] = tmp;
156 return frames;
157}
158
159} // stk namespace
160
161#endif
162
Generator(void)
Class constructor.
Definition Generator.h:25
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