Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   FAQ   Tutorial


BlitSquare.h
1 #ifndef STK_BLITSQUARE_H
2 #define STK_BLITSQUARE_H
3 
4 #include "Generator.h"
5 #include <cmath>
6 #include <limits>
7 
8 namespace stk {
9 
10 /***************************************************/
40 /***************************************************/
41 
42 class BlitSquare: public Generator
43 {
44  public:
46  BlitSquare( StkFloat frequency = 220.0 );
47 
49  ~BlitSquare();
50 
52  void reset();
53 
55 
58  void setPhase( StkFloat phase ) { phase_ = PI * phase; };
59 
61 
64  StkFloat getPhase() const { return phase_ / PI; };
65 
67  void setFrequency( StkFloat frequency );
68 
70 
82  void setHarmonics( unsigned int nHarmonics = 0 );
83 
85  StkFloat lastOut( void ) const { return lastFrame_[0]; };
86 
88  StkFloat tick( void );
89 
91 
98  StkFrames& tick( StkFrames& frames, unsigned int channel = 0 );
99 
100  protected:
101 
102  void updateHarmonics( void );
103 
104  unsigned int nHarmonics_;
105  unsigned int m_;
106  StkFloat rate_;
107  StkFloat phase_;
108  StkFloat p_;
109  StkFloat a_;
110  StkFloat lastBlitOutput_;
111  StkFloat dcbState_;
112 };
113 
114 inline StkFloat BlitSquare :: tick( void )
115 {
116  StkFloat temp = lastBlitOutput_;
117 
118  // A fully optimized version of this would replace the two sin calls
119  // with a pair of fast sin oscillators, for which stable fast
120  // two-multiply algorithms are well known. In the spirit of STK,
121  // which favors clarity over performance, the optimization has
122  // not been made here.
123 
124  // Avoid a divide by zero, or use of a denomralized divisor
125  // at the sinc peak, which has a limiting value of 1.0.
126  StkFloat denominator = sin( phase_ );
127  if ( fabs( denominator ) < std::numeric_limits<StkFloat>::epsilon() ) {
128  // Inexact comparison safely distinguishes betwen *close to zero*, and *close to PI*.
129  if ( phase_ < 0.1f || phase_ > TWO_PI - 0.1f )
130  lastBlitOutput_ = a_;
131  else
132  lastBlitOutput_ = -a_;
133  }
134  else {
135  lastBlitOutput_ = sin( m_ * phase_ );
136  lastBlitOutput_ /= p_ * denominator;
137  }
138 
139  lastBlitOutput_ += temp;
140 
141  // Now apply DC blocker.
142  lastFrame_[0] = lastBlitOutput_ - dcbState_ + 0.999 * lastFrame_[0];
143  dcbState_ = lastBlitOutput_;
144 
145  phase_ += rate_;
146  if ( phase_ >= TWO_PI ) phase_ -= TWO_PI;
147 
148  return lastFrame_[0];
149 }
150 
151 inline StkFrames& BlitSquare :: tick( StkFrames& frames, unsigned int channel )
152 {
153 #if defined(_STK_DEBUG_)
154  if ( channel >= frames.channels() ) {
155  oStream_ << "BlitSquare::tick(): channel and StkFrames arguments are incompatible!";
156  handleError( StkError::FUNCTION_ARGUMENT );
157  }
158 #endif
159 
160  StkFloat *samples = &frames[channel];
161  unsigned int hop = frames.channels();
162  for ( unsigned int i=0; i<frames.frames(); i++, samples += hop )
163  *samples = BlitSquare::tick();
164 
165  return frames;
166 }
167 
168 } // stk namespace
169 
170 #endif

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