mirror of
https://github.com/thestk/stk
synced 2026-01-11 20:11:52 +00:00
165 lines
16 KiB
HTML
165 lines
16 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"> <img src="ccrma.gif"> <img src="mcgill.gif"><P>
|
|
<a class="qindex" href="index.html">Home</a> <a class="qindex" href="information.html">Information</a> <a class="qindex" href="classes.html">Classes</a> <a class="qindex" href="download.html">Download</a> <a class="qindex" href="usage.html">Usage</a> <a class="qindex" href="maillist.html">Mail List</a> <a class="qindex" href="system.html">Requirements</a> <a class="qindex" href="links.html">Links</a> <a class="qindex" href="faq.html">FAQ</a> <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>ADSR.h</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#ifndef STK_ADSR_H</span>
|
|
<a name="l00002"></a>00002 <span class="preprocessor"></span><span class="preprocessor">#define STK_ADSR_H</span>
|
|
<a name="l00003"></a>00003 <span class="preprocessor"></span>
|
|
<a name="l00004"></a>00004 <span class="preprocessor">#include "Generator.h"</span>
|
|
<a name="l00005"></a>00005
|
|
<a name="l00006"></a><a class="code" href="namespacestk.html">00006</a> <span class="keyword">namespace </span>stk {
|
|
<a name="l00007"></a>00007
|
|
<a name="l00008"></a>00008 <span class="comment">/***************************************************/</span>
|
|
<a name="l00022"></a>00022 <span class="comment">/***************************************************/</span>
|
|
<a name="l00023"></a>00023
|
|
<a name="l00024"></a><a class="code" href="classstk_1_1ADSR.html">00024</a> <span class="keyword">class </span><a class="code" href="classstk_1_1ADSR.html" title="STK ADSR envelope class.">ADSR</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="l00025"></a>00025 {
|
|
<a name="l00026"></a>00026 <span class="keyword">public</span>:
|
|
<a name="l00027"></a>00027
|
|
<a name="l00029"></a>00029 <span class="keyword">enum</span> {
|
|
<a name="l00030"></a><a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa9518c737b76795fa49f997c09e074c8e">00030</a> <a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa9518c737b76795fa49f997c09e074c8e">ATTACK</a>,
|
|
<a name="l00031"></a><a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa75e1deeb861fe2989250b1977018cacf">00031</a> <a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa75e1deeb861fe2989250b1977018cacf">DECAY</a>,
|
|
<a name="l00032"></a><a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa139dc35648a28ddc99a7a606e185f6ad">00032</a> <a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa139dc35648a28ddc99a7a606e185f6ad">SUSTAIN</a>,
|
|
<a name="l00033"></a><a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa7efc189b0b53e581201fd64f357a1756">00033</a> <a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa7efc189b0b53e581201fd64f357a1756">RELEASE</a>,
|
|
<a name="l00034"></a><a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa1ed3238253daecbdba14da71759392d8">00034</a> <a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa1ed3238253daecbdba14da71759392d8">IDLE</a>
|
|
<a name="l00035"></a>00035 };
|
|
<a name="l00036"></a>00036
|
|
<a name="l00038"></a>00038 <a class="code" href="classstk_1_1ADSR.html#ac7473272021307b0dcf1ef602143e144" title="Default constructor.">ADSR</a>( <span class="keywordtype">void</span> );
|
|
<a name="l00039"></a>00039
|
|
<a name="l00041"></a>00041 <a class="code" href="classstk_1_1ADSR.html#a714f0b9deb325050eba819bc1fe22127" title="Class destructor.">~ADSR</a>( <span class="keywordtype">void</span> );
|
|
<a name="l00042"></a>00042
|
|
<a name="l00044"></a>00044 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1ADSR.html#aad03e82009a20cfb49c1f04bae791631" title="Set target = 1, state = ADSR::ATTACK.">keyOn</a>( <span class="keywordtype">void</span> );
|
|
<a name="l00045"></a>00045
|
|
<a name="l00047"></a>00047 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1ADSR.html#a9400e04b313ab8900a3ba9b693f448e6" title="Set target = 0, state = ADSR::RELEASE.">keyOff</a>( <span class="keywordtype">void</span> );
|
|
<a name="l00048"></a>00048
|
|
<a name="l00050"></a>00050 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1ADSR.html#aff18347235fd52ce307c4b5de18a9c62" title="Set the attack rate (gain / sample).">setAttackRate</a>( StkFloat rate );
|
|
<a name="l00051"></a>00051
|
|
<a name="l00053"></a>00053 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1ADSR.html#ae0af300e39f83f79776c04a3e83a85b1" title="Set the target value for the attack (default = 1.0).">setAttackTarget</a>( StkFloat target );
|
|
<a name="l00054"></a>00054
|
|
<a name="l00056"></a>00056 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1ADSR.html#a285f500b1e149aed48dba2a155b95cdd" title="Set the decay rate (gain / sample).">setDecayRate</a>( StkFloat rate );
|
|
<a name="l00057"></a>00057
|
|
<a name="l00059"></a>00059 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1ADSR.html#a85138fb6bbcc7b7cd85ec8449d68f7d8" title="Set the sustain level.">setSustainLevel</a>( StkFloat level );
|
|
<a name="l00060"></a>00060
|
|
<a name="l00062"></a>00062 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1ADSR.html#a29da04231df73c2968c0e9387b43be6e" title="Set the release rate (gain / sample).">setReleaseRate</a>( StkFloat rate );
|
|
<a name="l00063"></a>00063
|
|
<a name="l00065"></a>00065 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1ADSR.html#a48885319b8175cd4d6312fdf7980f116" title="Set the attack rate based on a time duration (seconds).">setAttackTime</a>( StkFloat time );
|
|
<a name="l00066"></a>00066
|
|
<a name="l00068"></a>00068 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1ADSR.html#a450703c45384ae27eb766551843ce82d" title="Set the decay rate based on a time duration (seconds).">setDecayTime</a>( StkFloat time );
|
|
<a name="l00069"></a>00069
|
|
<a name="l00071"></a>00071 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1ADSR.html#a91c14442557cb65286a327f8b297237a" title="Set the release rate based on a time duration (seconds).">setReleaseTime</a>( StkFloat time );
|
|
<a name="l00072"></a>00072
|
|
<a name="l00074"></a>00074 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1ADSR.html#a3567b02a0e8ab82d18eb949d362f9708" title="Set sustain level and attack, decay, and release time durations (seconds).">setAllTimes</a>( StkFloat aTime, StkFloat dTime, StkFloat sLevel, StkFloat rTime );
|
|
<a name="l00075"></a>00075
|
|
<a name="l00077"></a>00077 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1ADSR.html#a428eebd510e464ccd7bb4e5728f80d63" title="Set a sustain target value and attack or decay from current value to target.">setTarget</a>( StkFloat target );
|
|
<a name="l00078"></a>00078
|
|
<a name="l00080"></a><a class="code" href="classstk_1_1ADSR.html#a87f9918cfa4104a841dc5b44f26f8904">00080</a> <span class="keywordtype">int</span> <a class="code" href="classstk_1_1ADSR.html#a87f9918cfa4104a841dc5b44f26f8904" title="Return the current envelope state (ATTACK, DECAY, SUSTAIN, RELEASE, IDLE).">getState</a>( <span class="keywordtype">void</span> )<span class="keyword"> const </span>{ <span class="keywordflow">return</span> state_; };
|
|
<a name="l00081"></a>00081
|
|
<a name="l00083"></a>00083 <span class="keywordtype">void</span> <a class="code" href="classstk_1_1ADSR.html#aa99bf5fca3885bf8d99a3edc4072a4b5" title="Set to state = ADSR::SUSTAIN with current and target values of value.">setValue</a>( StkFloat value );
|
|
<a name="l00084"></a>00084
|
|
<a name="l00086"></a><a class="code" href="classstk_1_1ADSR.html#aedbcb85346c354139f139b3232f36f39">00086</a> StkFloat <a class="code" href="classstk_1_1ADSR.html#aedbcb85346c354139f139b3232f36f39" 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="l00087"></a>00087
|
|
<a name="l00089"></a>00089 StkFloat <a class="code" href="classstk_1_1ADSR.html#af2c0869f86210a55f4f668172373c975" title="Compute and return one output sample.">tick</a>( <span class="keywordtype">void</span> );
|
|
<a name="l00090"></a>00090
|
|
<a name="l00092"></a>00092
|
|
<a name="l00099"></a>00099 <a class="code" href="classstk_1_1StkFrames.html" title="An STK class to handle vectorized audio data.">StkFrames</a>& <a class="code" href="classstk_1_1ADSR.html#af2c0869f86210a55f4f668172373c975" 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>& frames, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> channel = 0 );
|
|
<a name="l00100"></a>00100
|
|
<a name="l00101"></a>00101 <span class="keyword">protected</span>:
|
|
<a name="l00102"></a>00102
|
|
<a name="l00103"></a>00103 <span class="keywordtype">void</span> sampleRateChanged( StkFloat newRate, StkFloat oldRate );
|
|
<a name="l00104"></a>00104
|
|
<a name="l00105"></a>00105 <span class="keywordtype">int</span> state_;
|
|
<a name="l00106"></a>00106 StkFloat value_;
|
|
<a name="l00107"></a>00107 StkFloat target_;
|
|
<a name="l00108"></a>00108 StkFloat attackRate_;
|
|
<a name="l00109"></a>00109 StkFloat decayRate_;
|
|
<a name="l00110"></a>00110 StkFloat releaseRate_;
|
|
<a name="l00111"></a>00111 StkFloat releaseTime_;
|
|
<a name="l00112"></a>00112 StkFloat sustainLevel_;
|
|
<a name="l00113"></a>00113 };
|
|
<a name="l00114"></a>00114
|
|
<a name="l00115"></a><a class="code" href="classstk_1_1ADSR.html#af2c0869f86210a55f4f668172373c975">00115</a> <span class="keyword">inline</span> StkFloat <a class="code" href="classstk_1_1ADSR.html#af2c0869f86210a55f4f668172373c975" title="Compute and return one output sample.">ADSR :: tick</a>( <span class="keywordtype">void</span> )
|
|
<a name="l00116"></a>00116 {
|
|
<a name="l00117"></a>00117 <span class="keywordflow">switch</span> ( state_ ) {
|
|
<a name="l00118"></a>00118
|
|
<a name="l00119"></a>00119 <span class="keywordflow">case</span> <a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa9518c737b76795fa49f997c09e074c8e">ATTACK</a>:
|
|
<a name="l00120"></a>00120 value_ += attackRate_;
|
|
<a name="l00121"></a>00121 <span class="keywordflow">if</span> ( value_ >= target_ ) {
|
|
<a name="l00122"></a>00122 value_ = target_;
|
|
<a name="l00123"></a>00123 target_ = sustainLevel_;
|
|
<a name="l00124"></a>00124 state_ = <a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa75e1deeb861fe2989250b1977018cacf">DECAY</a>;
|
|
<a name="l00125"></a>00125 }
|
|
<a name="l00126"></a>00126 lastFrame_[0] = value_;
|
|
<a name="l00127"></a>00127 <span class="keywordflow">break</span>;
|
|
<a name="l00128"></a>00128
|
|
<a name="l00129"></a>00129 <span class="keywordflow">case</span> <a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa75e1deeb861fe2989250b1977018cacf">DECAY</a>:
|
|
<a name="l00130"></a>00130 <span class="keywordflow">if</span> ( value_ > sustainLevel_ ) {
|
|
<a name="l00131"></a>00131 value_ -= decayRate_;
|
|
<a name="l00132"></a>00132 <span class="keywordflow">if</span> ( value_ <= sustainLevel_ ) {
|
|
<a name="l00133"></a>00133 value_ = sustainLevel_;
|
|
<a name="l00134"></a>00134 state_ = <a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa139dc35648a28ddc99a7a606e185f6ad">SUSTAIN</a>;
|
|
<a name="l00135"></a>00135 }
|
|
<a name="l00136"></a>00136 }
|
|
<a name="l00137"></a>00137 <span class="keywordflow">else</span> {
|
|
<a name="l00138"></a>00138 value_ += decayRate_; <span class="comment">// attack target < sustain level</span>
|
|
<a name="l00139"></a>00139 <span class="keywordflow">if</span> ( value_ >= sustainLevel_ ) {
|
|
<a name="l00140"></a>00140 value_ = sustainLevel_;
|
|
<a name="l00141"></a>00141 state_ = <a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa139dc35648a28ddc99a7a606e185f6ad">SUSTAIN</a>;
|
|
<a name="l00142"></a>00142 }
|
|
<a name="l00143"></a>00143 }
|
|
<a name="l00144"></a>00144 lastFrame_[0] = value_;
|
|
<a name="l00145"></a>00145 <span class="keywordflow">break</span>;
|
|
<a name="l00146"></a>00146
|
|
<a name="l00147"></a>00147 <span class="keywordflow">case</span> <a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa7efc189b0b53e581201fd64f357a1756">RELEASE</a>:
|
|
<a name="l00148"></a>00148 value_ -= releaseRate_;
|
|
<a name="l00149"></a>00149 <span class="keywordflow">if</span> ( value_ <= 0.0 ) {
|
|
<a name="l00150"></a>00150 value_ = 0.0;
|
|
<a name="l00151"></a>00151 state_ = <a class="code" href="classstk_1_1ADSR.html#ae1cdd1f2886d69a76fcd5b1e0e9154baa1ed3238253daecbdba14da71759392d8">IDLE</a>;
|
|
<a name="l00152"></a>00152 }
|
|
<a name="l00153"></a>00153 lastFrame_[0] = value_;
|
|
<a name="l00154"></a>00154
|
|
<a name="l00155"></a>00155 }
|
|
<a name="l00156"></a>00156
|
|
<a name="l00157"></a>00157 <span class="keywordflow">return</span> value_;
|
|
<a name="l00158"></a>00158 }
|
|
<a name="l00159"></a>00159
|
|
<a name="l00160"></a><a class="code" href="classstk_1_1ADSR.html#a97a617c65e62aadb4f88bd10cf437a6a">00160</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>& <a class="code" href="classstk_1_1ADSR.html#af2c0869f86210a55f4f668172373c975" title="Compute and return one output sample.">ADSR :: tick</a>( <a class="code" href="classstk_1_1StkFrames.html" title="An STK class to handle vectorized audio data.">StkFrames</a>& frames, <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> channel )
|
|
<a name="l00161"></a>00161 {
|
|
<a name="l00162"></a>00162 <span class="preprocessor">#if defined(_STK_DEBUG_)</span>
|
|
<a name="l00163"></a>00163 <span class="preprocessor"></span> <span class="keywordflow">if</span> ( channel >= frames.<a class="code" href="classstk_1_1StkFrames.html#aec7ef9c46675a24111aa6e2fda3ba870" title="Return the number of channels represented by the data.">channels</a>() ) {
|
|
<a name="l00164"></a>00164 oStream_ << <span class="stringliteral">"ADSR::tick(): channel and StkFrames arguments are incompatible!"</span>;
|
|
<a name="l00165"></a>00165 <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="l00166"></a>00166 }
|
|
<a name="l00167"></a>00167 <span class="preprocessor">#endif</span>
|
|
<a name="l00168"></a>00168 <span class="preprocessor"></span>
|
|
<a name="l00169"></a>00169 StkFloat *samples = &frames[channel];
|
|
<a name="l00170"></a>00170 <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="l00171"></a>00171 <span class="keywordflow">for</span> ( <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> i=0; i<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="l00172"></a>00172 *samples = <a class="code" href="classstk_1_1ADSR.html#af2c0869f86210a55f4f668172373c975" title="Compute and return one output sample.">ADSR::tick</a>();
|
|
<a name="l00173"></a>00173
|
|
<a name="l00174"></a>00174 <span class="keywordflow">return</span> frames;
|
|
<a name="l00175"></a>00175 }
|
|
<a name="l00176"></a>00176
|
|
<a name="l00177"></a>00177 } <span class="comment">// stk namespace</span>
|
|
<a name="l00178"></a>00178
|
|
<a name="l00179"></a>00179 <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>©1995-2012 Perry R. Cook and Gary P. Scavone. All Rights Reserved.</td></tr>
|
|
</table>
|
|
|
|
</BODY>
|
|
</HTML>
|