f@0
|
1 /***************************************************/
|
f@0
|
2 /*! \class SineWave
|
f@0
|
3 \brief STK sinusoid oscillator class.
|
f@0
|
4
|
f@0
|
5 This class computes and saves a static sine "table" that can be
|
f@0
|
6 shared by multiple instances. It has an interface similar to the
|
f@0
|
7 WaveLoop class but inherits from the Generator class. Output
|
f@0
|
8 values are computed using linear interpolation.
|
f@0
|
9
|
f@0
|
10 The "table" length, set in SineWave.h, is 2048 samples by default.
|
f@0
|
11
|
f@0
|
12 by Perry R. Cook and Gary P. Scavone, 1995--2014.
|
f@0
|
13 */
|
f@0
|
14 /***************************************************/
|
f@0
|
15
|
f@0
|
16 #include "../include/SineWave.h"
|
f@0
|
17 #include <cmath>
|
f@0
|
18
|
f@0
|
19 namespace stk {
|
f@0
|
20
|
f@0
|
21 StkFrames SineWave :: table_;
|
f@0
|
22
|
f@0
|
23 SineWave :: SineWave( void )
|
f@0
|
24 : time_(0.0), rate_(1.0), phaseOffset_(0.0)
|
f@0
|
25 {
|
f@0
|
26 if ( table_.empty() ) {
|
f@0
|
27 table_.resize( TABLE_SIZE + 1, 1 );
|
f@0
|
28 StkFloat temp = 1.0 / TABLE_SIZE;
|
f@0
|
29 for ( unsigned long i=0; i<=TABLE_SIZE; i++ )
|
f@0
|
30 table_[i] = sin( TWO_PI * i * temp );
|
f@0
|
31 }
|
f@0
|
32
|
f@0
|
33 Stk::addSampleRateAlert( this );
|
f@0
|
34 }
|
f@0
|
35
|
f@0
|
36 SineWave :: ~SineWave()
|
f@0
|
37 {
|
f@0
|
38 Stk::removeSampleRateAlert( this );
|
f@0
|
39 }
|
f@0
|
40
|
f@0
|
41 void SineWave :: sampleRateChanged( StkFloat newRate, StkFloat oldRate )
|
f@0
|
42 {
|
f@0
|
43 if ( !ignoreSampleRateChange_ )
|
f@0
|
44 this->setRate( oldRate * rate_ / newRate );
|
f@0
|
45 }
|
f@0
|
46
|
f@0
|
47 void SineWave :: reset( void )
|
f@0
|
48 {
|
f@0
|
49 time_ = 0.0;
|
f@0
|
50 lastFrame_[0] = 0;
|
f@0
|
51 }
|
f@0
|
52
|
f@0
|
53 void SineWave :: setFrequency( StkFloat frequency )
|
f@0
|
54 {
|
f@0
|
55 // This is a looping frequency.
|
f@0
|
56 this->setRate( TABLE_SIZE * frequency / Stk::sampleRate() );
|
f@0
|
57 }
|
f@0
|
58
|
f@0
|
59 void SineWave :: addTime( StkFloat time )
|
f@0
|
60 {
|
f@0
|
61 // Add an absolute time in samples.
|
f@0
|
62 time_ += time;
|
f@0
|
63 }
|
f@0
|
64
|
f@0
|
65 void SineWave :: addPhase( StkFloat phase )
|
f@0
|
66 {
|
f@0
|
67 // Add a time in cycles (one cycle = TABLE_SIZE).
|
f@0
|
68 time_ += TABLE_SIZE * phase;
|
f@0
|
69 }
|
f@0
|
70
|
f@0
|
71 void SineWave :: addPhaseOffset( StkFloat phaseOffset )
|
f@0
|
72 {
|
f@0
|
73 // Add a phase offset relative to any previous offset value.
|
f@0
|
74 time_ += ( phaseOffset - phaseOffset_ ) * TABLE_SIZE;
|
f@0
|
75 phaseOffset_ = phaseOffset;
|
f@0
|
76 }
|
f@0
|
77
|
f@0
|
78 } // stk namespace
|