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

154 lines
18 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>Guitar.h</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#ifndef STK_GUITAR_H</span>
<a name="l00002"></a>00002 <span class="preprocessor"></span><span class="preprocessor">#define STK_GUITAR_H</span>
<a name="l00003"></a>00003 <span class="preprocessor"></span>
<a name="l00004"></a>00004 <span class="preprocessor">#include &quot;Stk.h&quot;</span>
<a name="l00005"></a>00005 <span class="preprocessor">#include &quot;Twang.h&quot;</span>
<a name="l00006"></a>00006 <span class="preprocessor">#include &quot;OnePole.h&quot;</span>
<a name="l00007"></a>00007 <span class="preprocessor">#include &quot;OneZero.h&quot;</span>
<a name="l00008"></a>00008
<a name="l00009"></a>00009 <span class="keyword">namespace </span>stk {
<a name="l00010"></a>00010
<a name="l00011"></a>00011 <span class="comment">/***************************************************/</span>
<a name="l00039"></a>00039 <span class="comment">/***************************************************/</span>
<a name="l00040"></a>00040
<a name="l00041"></a><a class="code" href="classstk_1_1Guitar.html">00041</a> <span class="keyword">class </span><a class="code" href="classstk_1_1Guitar.html" title="STK guitar model class.">Guitar</a> : <span class="keyword">public</span> <a class="code" href="classstk_1_1Stk.html" title="STK base class.">Stk</a>
<a name="l00042"></a>00042 {
<a name="l00043"></a>00043 <span class="keyword">public</span>:
<a name="l00045"></a>00045 <a class="code" href="classstk_1_1Guitar.html#a7eac8ce2d2d6db176770639fe547bcb2" title="Class constructor, specifying an arbitrary number of strings (default = 6).">Guitar</a>( <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> nStrings = 6, std::string bodyfile = <span class="stringliteral">&quot;&quot;</span> );
<a name="l00046"></a>00046
<a name="l00048"></a>00048 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1Guitar.html#adb7355a9bcf6e94d9db4809175cbae1b" title="Reset and clear all internal state.">clear</a>( <span class="keywordtype">void</span> );
<a name="l00049"></a>00049
<a name="l00051"></a>00051
<a name="l00056"></a>00056 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1Guitar.html#a95c212c2d6a72ee93c2000f0308df9cc" title="Set the string excitation, using either a soundfile or computed noise.">setBodyFile</a>( std::string bodyfile = <span class="stringliteral">&quot;&quot;</span> );
<a name="l00057"></a>00057
<a name="l00059"></a>00059
<a name="l00063"></a>00063 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1Guitar.html#a4a2098002e20c5aac3927e2c080c64ca" title="Set the pluck position for one or all strings.">setPluckPosition</a>( StkFloat position, <span class="keywordtype">int</span> <span class="keywordtype">string</span> = -1 );
<a name="l00064"></a>00064
<a name="l00066"></a>00066
<a name="l00070"></a>00070 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1Guitar.html#adc1d5eadc46cbcdb8ddcf8b0e4aae9db" title="Set the loop gain for one or all strings.">setLoopGain</a>( StkFloat gain, <span class="keywordtype">int</span> <span class="keywordtype">string</span> = -1 );
<a name="l00071"></a>00071
<a name="l00073"></a>00073 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1Guitar.html#a8812168a7ffc5318d42a25b1cfda7bff" title="Set instrument parameters for a particular frequency.">setFrequency</a>( StkFloat frequency, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <span class="keywordtype">string</span> = 0 );
<a name="l00074"></a>00074
<a name="l00076"></a>00076
<a name="l00080"></a>00080 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1Guitar.html#a9dec744a074dba5c869b2aec1198ada7" title="Start a note with the given frequency and amplitude.">noteOn</a>( StkFloat frequency, StkFloat amplitude, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <span class="keywordtype">string</span> = 0 );
<a name="l00081"></a>00081
<a name="l00083"></a>00083 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1Guitar.html#accf5a4b16602560591ed010245a297d4" title="Stop a note with the given amplitude (speed of decay).">noteOff</a>( StkFloat amplitude, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> <span class="keywordtype">string</span> = 0 );
<a name="l00084"></a>00084
<a name="l00086"></a>00086
<a name="l00090"></a>00090 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1Guitar.html#ae9900d35d4f5ae7a67dfcc62a1175aba" title="Perform the control change specified by number and value (0.0 - 128.0).">controlChange</a>( <span class="keywordtype">int</span> number, StkFloat value, <span class="keywordtype">int</span> <span class="keywordtype">string</span> = -1 );
<a name="l00091"></a>00091
<a name="l00093"></a><a class="code" href="classstk_1_1Guitar.html#aee53aa17d760670cc875c06f1de6af91">00093</a> StkFloat <a class="code" href="classstk_1_1Guitar.html#aee53aa17d760670cc875c06f1de6af91" title="Return the last computed output value.">lastOut</a>( <span class="keywordtype">void</span> ) { <span class="keywordflow">return</span> lastFrame_[0]; };
<a name="l00094"></a>00094
<a name="l00096"></a>00096 StkFloat <a class="code" href="classstk_1_1Guitar.html#a47223c1ef411141ae04b0971497b5fb8" title="Take an optional input sample and compute one output sample.">tick</a>( StkFloat input = 0.0 );
<a name="l00097"></a>00097
<a name="l00099"></a>00099
<a name="l00107"></a>00107 <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_1Guitar.html#a47223c1ef411141ae04b0971497b5fb8" title="Take an optional input sample and compute 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="l00108"></a>00108
<a name="l00110"></a>00110
<a name="l00118"></a>00118 <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_1Guitar.html#a47223c1ef411141ae04b0971497b5fb8" title="Take an optional input sample and compute 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; iFrames, <a class="code" href="classstk_1_1StkFrames.html" title="An STK class to handle vectorized audio data.">StkFrames</a> &amp;oFrames, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> iChannel = 0, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> oChannel = 0 );
<a name="l00119"></a>00119
<a name="l00120"></a>00120 <span class="keyword">protected</span>:
<a name="l00121"></a>00121
<a name="l00122"></a>00122 std::vector&lt; stk::Twang &gt; strings_;
<a name="l00123"></a>00123 std::vector&lt; int &gt; stringState_; <span class="comment">// 0 = off, 1 = decaying, 2 = on</span>
<a name="l00124"></a>00124 std::vector&lt; unsigned int &gt; decayCounter_;
<a name="l00125"></a>00125 std::vector&lt; unsigned int &gt; filePointer_;
<a name="l00126"></a>00126 std::vector&lt; StkFloat &gt; pluckGains_;
<a name="l00127"></a>00127
<a name="l00128"></a>00128 <a class="code" href="classstk_1_1OnePole.html" title="STK one-pole filter class.">OnePole</a> pickFilter_;
<a name="l00129"></a>00129 <a class="code" href="classstk_1_1OnePole.html" title="STK one-pole filter class.">OnePole</a> couplingFilter_;
<a name="l00130"></a>00130 StkFloat couplingGain_;
<a name="l00131"></a>00131 <a class="code" href="classstk_1_1StkFrames.html" title="An STK class to handle vectorized audio data.">StkFrames</a> excitation_;
<a name="l00132"></a>00132 <a class="code" href="classstk_1_1StkFrames.html" title="An STK class to handle vectorized audio data.">StkFrames</a> lastFrame_;
<a name="l00133"></a>00133 };
<a name="l00134"></a>00134
<a name="l00135"></a><a class="code" href="classstk_1_1Guitar.html#a47223c1ef411141ae04b0971497b5fb8">00135</a> <span class="keyword">inline</span> StkFloat <a class="code" href="classstk_1_1Guitar.html#a47223c1ef411141ae04b0971497b5fb8" title="Take an optional input sample and compute one output sample.">Guitar :: tick</a>( StkFloat input )
<a name="l00136"></a>00136 {
<a name="l00137"></a>00137 StkFloat temp, output = 0.0;
<a name="l00138"></a>00138 lastFrame_[0] /= strings_.size(); <span class="comment">// evenly spread coupling across strings</span>
<a name="l00139"></a>00139 <span class="keywordflow">for</span> ( <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i&lt;strings_.size(); i++ ) {
<a name="l00140"></a>00140 <span class="keywordflow">if</span> ( stringState_[i] ) {
<a name="l00141"></a>00141 temp = input;
<a name="l00142"></a>00142 <span class="comment">// If pluckGain &lt; 0.2, let string ring but don&#39;t pluck it.</span>
<a name="l00143"></a>00143 <span class="keywordflow">if</span> ( filePointer_[i] &lt; excitation_.<a class="code" href="classstk_1_1StkFrames.html#a05b1ab6fa750a8221a7d65c30e0cdab9" title="Return the number of sample frames represented by the data.">frames</a>() &amp;&amp; pluckGains_[i] &gt; 0.2 )
<a name="l00144"></a>00144 temp += pluckGains_[i] * excitation_[filePointer_[i]++];
<a name="l00145"></a>00145 temp += couplingGain_ * couplingFilter_.<a class="code" href="classstk_1_1OnePole.html#a3c83b6d43f76bec06f11caa9de76881e" title="Input one sample to the filter and return one output.">tick</a>( lastFrame_[0] ); <span class="comment">// bridge coupling</span>
<a name="l00146"></a>00146 output += strings_[i].tick( temp );
<a name="l00147"></a>00147 <span class="comment">// Check if string energy has decayed sufficiently to turn it off.</span>
<a name="l00148"></a>00148 <span class="keywordflow">if</span> ( stringState_[i] == 1 ) {
<a name="l00149"></a>00149 <span class="keywordflow">if</span> ( fabs( strings_[i].<a class="code" href="classstk_1_1Guitar.html#aee53aa17d760670cc875c06f1de6af91" title="Return the last computed output value.">lastOut</a>() ) &lt; 0.001 ) decayCounter_[i]++;
<a name="l00150"></a>00150 <span class="keywordflow">else</span> decayCounter_[i] = 0;
<a name="l00151"></a>00151 <span class="keywordflow">if</span> ( decayCounter_[i] &gt; (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>) floor( 0.1 * <a class="code" href="classstk_1_1Stk.html#a5fbe37000a611ce56075ee7d8936472d" title="Static method that returns the current STK sample rate.">Stk::sampleRate</a>() ) ) {
<a name="l00152"></a>00152 stringState_[i] = 0;
<a name="l00153"></a>00153 decayCounter_[i] = 0;
<a name="l00154"></a>00154 }
<a name="l00155"></a>00155 }
<a name="l00156"></a>00156 }
<a name="l00157"></a>00157 }
<a name="l00158"></a>00158
<a name="l00159"></a>00159 <span class="keywordflow">return</span> lastFrame_[0] = output;
<a name="l00160"></a>00160 }
<a name="l00161"></a>00161
<a name="l00162"></a><a class="code" href="classstk_1_1Guitar.html#aa8a2447a813f400c92ab2333c2d8a125">00162</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_1Guitar.html#a47223c1ef411141ae04b0971497b5fb8" title="Take an optional input sample and compute one output sample.">Guitar :: 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="l00163"></a>00163 {
<a name="l00164"></a>00164 <span class="preprocessor">#if defined(_STK_DEBUG_)</span>
<a name="l00165"></a>00165 <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="l00166"></a>00166 oStream_ &lt;&lt; <span class="stringliteral">&quot;Guitar::tick(): channel and StkFrames arguments are incompatible!&quot;</span>;
<a name="l00167"></a>00167 <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="l00168"></a>00168 }
<a name="l00169"></a>00169 <span class="preprocessor">#endif</span>
<a name="l00170"></a>00170 <span class="preprocessor"></span>
<a name="l00171"></a>00171 StkFloat *samples = &amp;frames[channel];
<a name="l00172"></a>00172 <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="l00173"></a>00173 <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="l00174"></a>00174 *samples = <a class="code" href="classstk_1_1Guitar.html#a47223c1ef411141ae04b0971497b5fb8" title="Take an optional input sample and compute one output sample.">tick</a>( *samples );
<a name="l00175"></a>00175
<a name="l00176"></a>00176 <span class="keywordflow">return</span> frames;
<a name="l00177"></a>00177 }
<a name="l00178"></a>00178
<a name="l00179"></a><a class="code" href="classstk_1_1Guitar.html#a8ed813db2c23a9893e7d329528d659e8">00179</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_1Guitar.html#a47223c1ef411141ae04b0971497b5fb8" title="Take an optional input sample and compute one output sample.">Guitar :: tick</a>( <a class="code" href="classstk_1_1StkFrames.html" title="An STK class to handle vectorized audio data.">StkFrames</a>&amp; iFrames, <a class="code" href="classstk_1_1StkFrames.html" title="An STK class to handle vectorized audio data.">StkFrames</a>&amp; oFrames, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> iChannel, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> oChannel )
<a name="l00180"></a>00180 {
<a name="l00181"></a>00181 <span class="preprocessor">#if defined(_STK_DEBUG_)</span>
<a name="l00182"></a>00182 <span class="preprocessor"></span> <span class="keywordflow">if</span> ( iChannel &gt;= iFrames.<a class="code" href="classstk_1_1StkFrames.html#aec7ef9c46675a24111aa6e2fda3ba870" title="Return the number of channels represented by the data.">channels</a>() || oChannel &gt;= oFrames.<a class="code" href="classstk_1_1StkFrames.html#aec7ef9c46675a24111aa6e2fda3ba870" title="Return the number of channels represented by the data.">channels</a>() ) {
<a name="l00183"></a>00183 oStream_ &lt;&lt; <span class="stringliteral">&quot;Guitar::tick(): channel and StkFrames arguments are incompatible!&quot;</span>;
<a name="l00184"></a>00184 <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="l00185"></a>00185 }
<a name="l00186"></a>00186 <span class="preprocessor">#endif</span>
<a name="l00187"></a>00187 <span class="preprocessor"></span>
<a name="l00188"></a>00188 StkFloat *iSamples = &amp;iFrames[iChannel];
<a name="l00189"></a>00189 StkFloat *oSamples = &amp;oFrames[oChannel];
<a name="l00190"></a>00190 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> iHop = iFrames.<a class="code" href="classstk_1_1StkFrames.html#aec7ef9c46675a24111aa6e2fda3ba870" title="Return the number of channels represented by the data.">channels</a>(), oHop = oFrames.<a class="code" href="classstk_1_1StkFrames.html#aec7ef9c46675a24111aa6e2fda3ba870" title="Return the number of channels represented by the data.">channels</a>();
<a name="l00191"></a>00191 <span class="keywordflow">for</span> ( <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i&lt;iFrames.<a class="code" href="classstk_1_1StkFrames.html#a05b1ab6fa750a8221a7d65c30e0cdab9" title="Return the number of sample frames represented by the data.">frames</a>(); i++, iSamples += iHop, oSamples += oHop )
<a name="l00192"></a>00192 *oSamples = <a class="code" href="classstk_1_1Guitar.html#a47223c1ef411141ae04b0971497b5fb8" title="Take an optional input sample and compute one output sample.">tick</a>( *iSamples );
<a name="l00193"></a>00193
<a name="l00194"></a>00194 <span class="keywordflow">return</span> iFrames;
<a name="l00195"></a>00195 }
<a name="l00196"></a>00196
<a name="l00197"></a>00197 } <span class="comment">// stk namespace</span>
<a name="l00198"></a>00198
<a name="l00199"></a>00199 <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>