Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

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

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