libpsynth 0.2.1
/home/raskolnikov/dev/psynth/trunk/src/psynth/new_graph/soft_buffer_port.hpp
Go to the documentation of this file.
00001 
00011 /*
00012  *  Copyright (C) 2011 Juan Pedro BolĂ­var Puente
00013  *
00014  *  This file is part of Psychosynth.
00015  *   
00016  *  Psychosynth is free software: you can redistribute it and/or modify
00017  *  it under the terms of the GNU General Public License as published by
00018  *  the Free Software Foundation, either version 3 of the License, or
00019  *  (at your option) any later version.
00020  *
00021  *  Psychosynth is distributed in the hope that it will be useful,
00022  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024  *  GNU General Public License for more details.
00025  *
00026  *  You should have received a copy of the GNU General Public License
00027  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00028  *
00029  */
00030 
00031 #include <iostream>
00032 
00033 #include <psynth/sound/output.hpp>
00034 #include <psynth/synth/simple_envelope.hpp>
00035 #include <psynth/synth/util.hpp>
00036 #include <psynth/new_graph/buffer_port.hpp>
00037 
00038 namespace psynth
00039 {
00040 namespace graph
00041 {
00042 
00043 constexpr float default_soft_port_duration = 0.004f; // Seconds
00044 
00045 template <class Buffer>
00046 class soft_buffer_in_port : public buffer_in_port<Buffer>
00047 {
00048     typedef buffer_in_port<Buffer> base_type;
00049     
00050 public:
00051     soft_buffer_in_port (
00052         std::string name,
00053         node* owner,
00054         audio_sample stable_value = 0.0f, 
00055         float duration  = default_soft_port_duration);
00056 
00057     const Buffer& rt_get_in () const
00058     { return _local_buffer; }
00059     
00060     void disconnect ();
00061     void connect (out_port_base& dest);
00062     void rt_process (rt_process_context& ctx);
00063     void rt_context_update (rt_process_context& ctx);
00064     
00065 private:
00066     typedef synth::simple_envelope<sample_range> envelope_type;
00067 
00068     audio_sample   _stable_value;
00069     out_port_base* _request_source;
00070     bool           _requested;
00071     envelope_type  _envelope;
00072     float          _duration;
00073     Buffer         _local_buffer;
00074 };
00075 
00076 template <class B>
00077 soft_buffer_in_port<B>::soft_buffer_in_port (std::string name,
00078                                              node* owner,
00079                                              audio_sample stable_value, 
00080                                              float duration)
00081     : base_type (name, owner)
00082     , _stable_value (stable_value)
00083     , _request_source (0)
00084     , _requested (false)
00085     , _duration (duration)
00086     , _local_buffer ()
00087 {
00088 }
00089 
00090 template <typename T>
00091 struct soft_buffer_forward_port
00092     : public forward_port_impl<soft_buffer_in_port<T>,
00093                                buffer_out_port<T> >
00094 {
00095     typedef forward_port_impl<soft_buffer_in_port<T>,
00096                               buffer_out_port<T> > base_type;
00097     
00098     soft_buffer_forward_port (std::string in_name,
00099                               std::string out_name,
00100                               node* in_owner,
00101                               node* out_owner)
00102         : base_type (in_name, out_name,
00103                      in_owner, out_owner)
00104     {}
00105 };
00106 
00107 typedef soft_buffer_in_port<audio_buffer> soft_audio_in_port;
00108 typedef soft_buffer_in_port<sample_buffer> soft_sample_in_port;
00109 typedef soft_buffer_forward_port<audio_buffer> soft_audio_forward_port;
00110 typedef soft_buffer_forward_port<sample_buffer> soft_sample_forward_port;
00111 
00112 extern template class soft_buffer_in_port<audio_buffer>;
00113 extern template class soft_buffer_in_port<sample_buffer>;
00114 extern template class soft_buffer_forward_port<audio_buffer>;
00115 extern template class soft_buffer_forward_port<sample_buffer>;
00116 
00117 } /* namespace graph */
00118 } /* namespace psynth */