Files
stk/doc/html/BlitSquare_8h_source.html
2013-09-29 23:51:11 +02:00

135 lines
12 KiB
HTML

<HTML>
<HEAD>
<TITLE>The Synthesis ToolKit in C++ (STK)</TITLE>
<LINK HREF="doxygen.css" REL="stylesheet" TYPE="text/css">
</HEAD>
<BODY BGCOLOR="#FFFFFF">
<CENTER>
<img src="princeton.gif"> &nbsp; <img src="ccrma.gif"> &nbsp; <img src="mcgill.gif"><P>
<a class="qindex" href="index.html">Home</a> &nbsp; <a class="qindex" href="information.html">Information</a> &nbsp; <a class="qindex" href="classes.html">Classes</a> &nbsp; <a class="qindex" href="download.html">Download</a> &nbsp; <a class="qindex" href="usage.html">Usage</a> &nbsp; <a class="qindex" href="maillist.html">Mail List</a> &nbsp; <a class="qindex" href="system.html">Requirements</a> &nbsp; <a class="qindex" href="links.html">Links</a> &nbsp; <a class="qindex" href="faq.html">FAQ</a> &nbsp; <a class="qindex" href="tutorial.html">Tutorial</a></CENTER>
<HR>
<!-- Generated by Doxygen 1.6.2 -->
<div class="navpath"><a class="el" href="dir_221deb9bdff892d63ed1e409450a60d2.html">include</a>
</div>
<div class="contents">
<h1>BlitSquare.h</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#ifndef STK_BLITSQUARE_H</span>
<a name="l00002"></a>00002 <span class="preprocessor"></span><span class="preprocessor">#define STK_BLITSQUARE_H</span>
<a name="l00003"></a>00003 <span class="preprocessor"></span>
<a name="l00004"></a>00004 <span class="preprocessor">#include &quot;Generator.h&quot;</span>
<a name="l00005"></a>00005 <span class="preprocessor">#include &lt;cmath&gt;</span>
<a name="l00006"></a>00006 <span class="preprocessor">#include &lt;limits&gt;</span>
<a name="l00007"></a>00007
<a name="l00008"></a>00008 <span class="keyword">namespace </span>stk {
<a name="l00009"></a>00009
<a name="l00010"></a>00010 <span class="comment">/***************************************************/</span>
<a name="l00040"></a>00040 <span class="comment">/***************************************************/</span>
<a name="l00041"></a>00041
<a name="l00042"></a><a class="code" href="classstk_1_1BlitSquare.html">00042</a> <span class="keyword">class </span><a class="code" href="classstk_1_1BlitSquare.html" title="STK band-limited square wave class.">BlitSquare</a>: <span class="keyword">public</span> <a class="code" href="classstk_1_1Generator.html" title="STK abstract unit generator parent class.">Generator</a>
<a name="l00043"></a>00043 {
<a name="l00044"></a>00044 <span class="keyword">public</span>:
<a name="l00046"></a>00046 <a class="code" href="classstk_1_1BlitSquare.html#a2cb3aee415a6ee75fa85b01e4e85d70b" title="Default constructor that initializes BLIT frequency to 220 Hz.">BlitSquare</a>( StkFloat frequency = 220.0 );
<a name="l00047"></a>00047
<a name="l00049"></a>00049 <a class="code" href="classstk_1_1BlitSquare.html#a4bc6e424ca1631f353f55846084634ef" title="Class destructor.">~BlitSquare</a>();
<a name="l00050"></a>00050
<a name="l00052"></a>00052 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1BlitSquare.html#a8f6dbe07d7f64b24cc9a41f6208dd6c4" title="Resets the oscillator state and phase to 0.">reset</a>();
<a name="l00053"></a>00053
<a name="l00055"></a>00055
<a name="l00058"></a><a class="code" href="classstk_1_1BlitSquare.html#aeface58980a3ec865131f3cad59bbbef">00058</a> <span class="keywordtype">void</span> <a class="code" href="classstk_1_1BlitSquare.html#aeface58980a3ec865131f3cad59bbbef" title="Set the phase of the signal.">setPhase</a>( StkFloat phase ) { phase_ = PI * phase; };
<a name="l00059"></a>00059
<a name="l00061"></a>00061
<a name="l00064"></a><a class="code" href="classstk_1_1BlitSquare.html#abbda8cb84bcd36fc6d96130e9832a6af">00064</a> StkFloat <a class="code" href="classstk_1_1BlitSquare.html#abbda8cb84bcd36fc6d96130e9832a6af" title="Get the current phase of the signal.">getPhase</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> phase_ / PI; };
<a name="l00065"></a>00065
<a name="l00067"></a>00067 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1BlitSquare.html#a8d0d8e0a12987ebccc56c09c7e7dbd6a" title="Set the impulse train rate in terms of a frequency in Hz.">setFrequency</a>( StkFloat frequency );
<a name="l00068"></a>00068
<a name="l00070"></a>00070
<a name="l00082"></a>00082 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1BlitSquare.html#a94521b4224d0873f5e783c721c123b21" title="Set the number of harmonics generated in the signal.">setHarmonics</a>( <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> nHarmonics = 0 );
<a name="l00083"></a>00083
<a name="l00085"></a><a class="code" href="classstk_1_1BlitSquare.html#a77972e4e1c53600bf80cf692601ccae1">00085</a> StkFloat <a class="code" href="classstk_1_1BlitSquare.html#a77972e4e1c53600bf80cf692601ccae1" title="Return the last computed output value.">lastOut</a>( <span class="keywordtype">void</span> )<span class="keyword"> const </span>{ <span class="keywordflow">return</span> lastFrame_[0]; };
<a name="l00086"></a>00086
<a name="l00088"></a>00088 StkFloat <a class="code" href="classstk_1_1BlitSquare.html#a04366d55cecb4c8a726ea0519683d817" title="Compute and return one output sample.">tick</a>( <span class="keywordtype">void</span> );
<a name="l00089"></a>00089
<a name="l00091"></a>00091
<a name="l00098"></a>00098 <a class="code" href="classstk_1_1StkFrames.html" title="An STK class to handle vectorized audio data.">StkFrames</a>&amp; <a class="code" href="classstk_1_1BlitSquare.html#a04366d55cecb4c8a726ea0519683d817" title="Compute and return one output sample.">tick</a>( <a class="code" href="classstk_1_1StkFrames.html" title="An STK class to handle vectorized audio data.">StkFrames</a>&amp; frames, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> channel = 0 );
<a name="l00099"></a>00099
<a name="l00100"></a>00100 <span class="keyword">protected</span>:
<a name="l00101"></a>00101
<a name="l00102"></a>00102 <span class="keywordtype">void</span> updateHarmonics( <span class="keywordtype">void</span> );
<a name="l00103"></a>00103
<a name="l00104"></a>00104 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> nHarmonics_;
<a name="l00105"></a>00105 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> m_;
<a name="l00106"></a>00106 StkFloat rate_;
<a name="l00107"></a>00107 StkFloat phase_;
<a name="l00108"></a>00108 StkFloat p_;
<a name="l00109"></a>00109 StkFloat a_;
<a name="l00110"></a>00110 StkFloat lastBlitOutput_;
<a name="l00111"></a>00111 StkFloat dcbState_;
<a name="l00112"></a>00112 };
<a name="l00113"></a>00113
<a name="l00114"></a><a class="code" href="classstk_1_1BlitSquare.html#a04366d55cecb4c8a726ea0519683d817">00114</a> <span class="keyword">inline</span> StkFloat <a class="code" href="classstk_1_1BlitSquare.html#a04366d55cecb4c8a726ea0519683d817" title="Compute and return one output sample.">BlitSquare :: tick</a>( <span class="keywordtype">void</span> )
<a name="l00115"></a>00115 {
<a name="l00116"></a>00116 StkFloat temp = lastBlitOutput_;
<a name="l00117"></a>00117
<a name="l00118"></a>00118 <span class="comment">// A fully optimized version of this would replace the two sin calls</span>
<a name="l00119"></a>00119 <span class="comment">// with a pair of fast sin oscillators, for which stable fast </span>
<a name="l00120"></a>00120 <span class="comment">// two-multiply algorithms are well known. In the spirit of STK,</span>
<a name="l00121"></a>00121 <span class="comment">// which favors clarity over performance, the optimization has </span>
<a name="l00122"></a>00122 <span class="comment">// not been made here.</span>
<a name="l00123"></a>00123
<a name="l00124"></a>00124 <span class="comment">// Avoid a divide by zero, or use of a denomralized divisor</span>
<a name="l00125"></a>00125 <span class="comment">// at the sinc peak, which has a limiting value of 1.0.</span>
<a name="l00126"></a>00126 StkFloat denominator = sin( phase_ );
<a name="l00127"></a>00127 <span class="keywordflow">if</span> ( fabs( denominator ) &lt; std::numeric_limits&lt;StkFloat&gt;::epsilon() ) {
<a name="l00128"></a>00128 <span class="comment">// Inexact comparison safely distinguishes betwen *close to zero*, and *close to PI*.</span>
<a name="l00129"></a>00129 <span class="keywordflow">if</span> ( phase_ &lt; 0.1f || phase_ &gt; TWO_PI - 0.1f )
<a name="l00130"></a>00130 lastBlitOutput_ = a_;
<a name="l00131"></a>00131 <span class="keywordflow">else</span>
<a name="l00132"></a>00132 lastBlitOutput_ = -a_;
<a name="l00133"></a>00133 }
<a name="l00134"></a>00134 <span class="keywordflow">else</span> {
<a name="l00135"></a>00135 lastBlitOutput_ = sin( m_ * phase_ );
<a name="l00136"></a>00136 lastBlitOutput_ /= p_ * denominator;
<a name="l00137"></a>00137 }
<a name="l00138"></a>00138
<a name="l00139"></a>00139 lastBlitOutput_ += temp;
<a name="l00140"></a>00140
<a name="l00141"></a>00141 <span class="comment">// Now apply DC blocker.</span>
<a name="l00142"></a>00142 lastFrame_[0] = lastBlitOutput_ - dcbState_ + 0.999 * lastFrame_[0];
<a name="l00143"></a>00143 dcbState_ = lastBlitOutput_;
<a name="l00144"></a>00144
<a name="l00145"></a>00145 phase_ += rate_;
<a name="l00146"></a>00146 <span class="keywordflow">if</span> ( phase_ &gt;= TWO_PI ) phase_ -= TWO_PI;
<a name="l00147"></a>00147
<a name="l00148"></a>00148 <span class="keywordflow">return</span> lastFrame_[0];
<a name="l00149"></a>00149 }
<a name="l00150"></a>00150
<a name="l00151"></a><a class="code" href="classstk_1_1BlitSquare.html#a7df894d40b3d6effbb2a09819356258d">00151</a> <span class="keyword">inline</span> <a class="code" href="classstk_1_1StkFrames.html" title="An STK class to handle vectorized audio data.">StkFrames</a>&amp; <a class="code" href="classstk_1_1BlitSquare.html#a04366d55cecb4c8a726ea0519683d817" title="Compute and return one output sample.">BlitSquare :: tick</a>( <a class="code" href="classstk_1_1StkFrames.html" title="An STK class to handle vectorized audio data.">StkFrames</a>&amp; frames, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> channel )
<a name="l00152"></a>00152 {
<a name="l00153"></a>00153 <span class="preprocessor">#if defined(_STK_DEBUG_)</span>
<a name="l00154"></a>00154 <span class="preprocessor"></span> <span class="keywordflow">if</span> ( channel &gt;= frames.<a class="code" href="classstk_1_1StkFrames.html#aec7ef9c46675a24111aa6e2fda3ba870" title="Return the number of channels represented by the data.">channels</a>() ) {
<a name="l00155"></a>00155 oStream_ &lt;&lt; <span class="stringliteral">&quot;BlitSquare::tick(): channel and StkFrames arguments are incompatible!&quot;</span>;
<a name="l00156"></a>00156 <a class="code" href="classstk_1_1Stk.html#a48ac73a0d8ca28445ba1a054e1f061ff" title="Static function for error reporting and handling using c-strings.">handleError</a>( StkError::FUNCTION_ARGUMENT );
<a name="l00157"></a>00157 }
<a name="l00158"></a>00158 <span class="preprocessor">#endif</span>
<a name="l00159"></a>00159 <span class="preprocessor"></span>
<a name="l00160"></a>00160 StkFloat *samples = &amp;frames[channel];
<a name="l00161"></a>00161 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> hop = frames.<a class="code" href="classstk_1_1StkFrames.html#aec7ef9c46675a24111aa6e2fda3ba870" title="Return the number of channels represented by the data.">channels</a>();
<a name="l00162"></a>00162 <span class="keywordflow">for</span> ( <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i&lt;frames.<a class="code" href="classstk_1_1StkFrames.html#a05b1ab6fa750a8221a7d65c30e0cdab9" title="Return the number of sample frames represented by the data.">frames</a>(); i++, samples += hop )
<a name="l00163"></a>00163 *samples = <a class="code" href="classstk_1_1BlitSquare.html#a04366d55cecb4c8a726ea0519683d817" title="Compute and return one output sample.">BlitSquare::tick</a>();
<a name="l00164"></a>00164
<a name="l00165"></a>00165 <span class="keywordflow">return</span> frames;
<a name="l00166"></a>00166 }
<a name="l00167"></a>00167
<a name="l00168"></a>00168 } <span class="comment">// stk namespace</span>
<a name="l00169"></a>00169
<a name="l00170"></a>00170 <span class="preprocessor">#endif</span>
</pre></div></div>
<HR>
<table>
<tr><td><A HREF="http://ccrma.stanford.edu/software/stk/"><I>The Synthesis ToolKit in C++ (STK)</I></A></td></tr>
<tr><td>&copy;1995-2012 Perry R. Cook and Gary P. Scavone. All Rights Reserved.</td></tr>
</table>
</BODY>
</HTML>