libpsynth 0.2.1
/home/raskolnikov/dev/psynth/trunk/src/psynth/synth/simple_envelope.hpp
Go to the documentation of this file.
00001 
00013 /*
00014  *  Copyright (C) 2011 Juan Pedro BolĂ­var Puente
00015  *
00016  *  This file is part of Psychosynth.
00017  *   
00018  *  Psychosynth is free software: you can redistribute it and/or modify
00019  *  it under the terms of the GNU General Public License as published by
00020  *  the Free Software Foundation, either version 3 of the License, or
00021  *  (at your option) any later version.
00022  *
00023  *  Psychosynth is distributed in the hope that it will be useful,
00024  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00025  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00026  *  GNU General Public License for more details.
00027  *
00028  *  You should have received a copy of the GNU General Public License
00029  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00030  *
00031  */
00032 
00033 #ifndef PSYNTH_SYNTH_SIMPLE_ENVELOPE_H
00034 #define PSYNTH_SYNTH_SIMPLE_ENVELOPE_H
00035 
00036 #include <psynth/sound/forwards.hpp>
00037 #include <psynth/sound/algorithm.hpp>
00038 #include <psynth/synth/envelope.hpp>
00039 
00040 namespace psynth
00041 {
00042 namespace synth
00043 {
00044 
00048 template <class Range>
00049 class simple_envelope : public envelope<Range>
00050 {
00051 public:
00052     typedef Range range;
00053     typedef typename range::value_type               value_type;
00054     typedef typename sound::sample_type<range>::type sample_type;
00055 
00056     simple_envelope ()
00057         : _rise_dt (0.0f)
00058         , _fall_dt (0.0f)
00059         , _curr_dt (0.0f)
00060         , _val (0.0f)
00061         {}
00062     
00063     simple_envelope (float rise_dt, float fall_dt)
00064         : _rise_dt (rise_dt)
00065         , _fall_dt (fall_dt)
00066         , _curr_dt (0.0f)
00067         , _val (0.0f)
00068         {}
00069 
00070     void set_deltas (float rise_dt, float fall_dt)
00071     {
00072         _rise_dt = rise_dt;
00073         _fall_dt = fall_dt;
00074     }
00075 
00076     void set (sample_type value)
00077     { _val = value; }
00078     
00079     value_type update ()
00080     {
00081         const sample_type one  =
00082             sound::sample_traits<sample_type>::max_value ();
00083         const sample_type zero =
00084             sound::sample_traits<sample_type>::zero_value ();
00085         sample_type val = _val;
00086         
00087         _val = _val + _curr_dt;
00088         if (_val > one)
00089             _val = one;
00090         else if (_val < zero)
00091             _val = zero;
00092         return value_type { val };
00093     }
00094 
00095     value_type update (std::size_t sample)
00096     {
00097         const sample_type one  =
00098             sound::sample_traits<sample_type>::max_value ();
00099         const sample_type zero =
00100             sound::sample_traits<sample_type>::zero_value ();
00101         sample_type val = _val;
00102         
00103         _val = _val + _curr_dt * sample;
00104         if (_val > one)
00105             _val = one;
00106         else if (_val < zero)
00107             _val = zero;
00108         return value_type { val };
00109     }
00110 
00111     void update (const range& samples)
00112     {
00113         sound::generate_frames (samples, [&] () -> value_type {
00114                 return this->simple_envelope::update ();
00115             });
00116     }
00117 
00118     template <class Range2>
00119     void update (const Range2& samples)
00120     {
00121         sound::generate_frames (samples, [&] () {
00122                 return typename Range2::value_type (
00123                     this->simple_envelope::update ());
00124             });
00125     }
00126 
00127     void press ()
00128     { _curr_dt = _rise_dt; }
00129 
00130     void release ()
00131     { _curr_dt = _fall_dt; }
00132 
00133     bool finished ()
00134     { return _val <= 0.0f; }
00135     
00136 private:
00137     float       _rise_dt;
00138     float       _fall_dt;
00139     float       _curr_dt;
00140     sample_type _val;
00141 };
00142 
00143 } /* namespace synth */
00144 } /* namespace psynth */
00145 
00146 #endif /* PSYNTH_SIMPLE_ENVELOPE_H */