comparison stk/src/SineWave.cpp @ 0:4606bd505630 tip

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