| 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 - 2005. |
| 13 |
*/ |
| 14 |
/***************************************************/ |
| 15 |
|
| 16 |
#include "SineWave.h" |
| 17 |
#include <cmath> |
| 18 |
|
| 19 |
using namespace Nyq; |
| 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 |
|
| 34 |
SineWave :: ~SineWave() |
| 35 |
{ |
| 36 |
} |
| 37 |
|
| 38 |
void SineWave :: reset(void) |
| 39 |
{ |
| 40 |
time_ = 0.0; |
| 41 |
lastOutput_ = 0; |
| 42 |
} |
| 43 |
|
| 44 |
void SineWave :: setFrequency( StkFloat frequency ) |
| 45 |
{ |
| 46 |
// This is a looping frequency. |
| 47 |
this->setRate( TABLE_SIZE * frequency / Stk::sampleRate() ); |
| 48 |
} |
| 49 |
|
| 50 |
void SineWave :: addTime( StkFloat time ) |
| 51 |
{ |
| 52 |
// Add an absolute time in samples. |
| 53 |
time_ += time; |
| 54 |
|
| 55 |
while ( time_ < 0.0 ) |
| 56 |
time_ += TABLE_SIZE; |
| 57 |
while ( time_ >= TABLE_SIZE ) |
| 58 |
time_ -= TABLE_SIZE; |
| 59 |
} |
| 60 |
|
| 61 |
void SineWave :: addPhase( StkFloat angle ) |
| 62 |
{ |
| 63 |
// Add a time in cycles (one cycle = TABLE_SIZE). |
| 64 |
time_ += TABLE_SIZE * angle; |
| 65 |
|
| 66 |
while ( time_ < 0.0 ) |
| 67 |
time_ += TABLE_SIZE; |
| 68 |
while ( time_ >= TABLE_SIZE ) |
| 69 |
time_ -= TABLE_SIZE; |
| 70 |
} |
| 71 |
|
| 72 |
void SineWave :: addPhaseOffset( StkFloat angle ) |
| 73 |
{ |
| 74 |
// Add a phase offset in cycles, where 1.0 = TABLE_SIZE. |
| 75 |
phaseOffset_ = TABLE_SIZE * angle; |
| 76 |
} |
| 77 |
|
| 78 |
StkFloat SineWave :: computeSample( void ) |
| 79 |
{ |
| 80 |
// Check limits of time address ... if necessary, recalculate modulo |
| 81 |
// TABLE_SIZE. |
| 82 |
while ( time_ < 0.0 ) |
| 83 |
time_ += TABLE_SIZE; |
| 84 |
while ( time_ >= TABLE_SIZE ) |
| 85 |
time_ -= TABLE_SIZE; |
| 86 |
|
| 87 |
StkFloat tyme; |
| 88 |
if ( phaseOffset_ ) { |
| 89 |
tyme = time_ + phaseOffset_; |
| 90 |
while ( tyme < 0.0 ) |
| 91 |
tyme += TABLE_SIZE; |
| 92 |
while ( tyme >= TABLE_SIZE ) |
| 93 |
tyme -= TABLE_SIZE; |
| 94 |
} |
| 95 |
else { |
| 96 |
tyme = time_; |
| 97 |
} |
| 98 |
|
| 99 |
lastOutput_ = table_.interpolate( tyme ); |
| 100 |
|
| 101 |
// Increment time, which can be negative. |
| 102 |
time_ += rate_; |
| 103 |
|
| 104 |
return lastOutput_; |
| 105 |
} |