libpsynth 0.2.1
/home/raskolnikov/dev/psynth/trunk/src/psynth/new_graph/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 #ifndef PSYNTH_GRAPH_BUFFER_PORT_HPP_
00032 #define PSYNTH_GRAPH_BUFFER_PORT_HPP_
00033 
00034 #include <psynth/new_graph/processor.hpp>
00035 #include <psynth/new_graph/buffers.hpp>
00036 #include <psynth/new_graph/port.hpp>
00037 #include <psynth/new_graph/buffer_port_fwd.hpp>
00038 
00039 namespace psynth
00040 {
00041 namespace graph
00042 {
00043 
00044 template <typename T>
00045 class buffer_out_port : public out_port<T>
00046 {
00047 public:
00048     buffer_out_port (std::string name, node* owner)
00049         : out_port<T> (name, owner) {}
00050 
00051     virtual typename T::const_range rt_out_range () const
00052     { return const_range (this->rt_get_out ()); }
00053 
00054     virtual typename T::range rt_out_range ()
00055     { return range (this->rt_get_out ()); }
00056     
00057     void rt_context_update (rt_process_context& ctx)
00058     { this->rt_get_out ().recreate (ctx.block_size ()); }
00059 };
00060 
00061 template <typename T>
00062 class buffer_in_port : public in_port<T>
00063 {
00064 public:
00065     virtual typename T::const_range rt_in_range () const
00066     { return const_range (this->rt_get_in ()); }
00067     
00068     buffer_in_port (std::string name, node* owner)
00069         : in_port<T> (name, owner) {}
00070 };
00071 
00072 template <typename T>
00073 class defaulting_buffer_in_port : public buffer_in_port<T>
00074 {
00075 public:
00076     typedef buffer_in_port<T> base_type;
00077 
00078     const T& rt_get_in () const
00079     {
00080         if (this->rt_in_available ())
00081             return base_type::rt_get_in ();
00082         else
00083             return _default;
00084     }
00085         
00086     defaulting_buffer_in_port (std::string name,
00087                                node* owner,
00088                                typename T::value_type defval)
00089         : base_type (name, owner)
00090         , _default_value (defval)
00091     {}
00092     
00093     void rt_context_update (rt_process_context& ctx)
00094     {
00095         base_type::rt_context_update (ctx);
00096         _default.recreate (ctx.block_size (), _default_value, 0);
00097     }
00098     
00099 private:
00100     T _default;
00101     typename T::value_type _default_value;
00102 };
00103 
00104 template <typename T>
00105 struct buffer_forward_port
00106     : public forward_port_impl<buffer_in_port<T>,
00107                                buffer_out_port<T> >
00108 {
00109     typedef forward_port_impl<buffer_in_port<T>,
00110                               buffer_out_port<T> > base_type;
00111     
00112     buffer_forward_port (std::string in_name,
00113                          std::string out_name,
00114                          node* in_owner,
00115                          node* out_owner)
00116         : base_type (in_name, out_name,
00117                      in_owner, out_owner)
00118     {}
00119 };
00120 
00121 typedef buffer_out_port<audio_buffer>    audio_out_port;
00122 typedef buffer_in_port<audio_buffer>     audio_in_port;
00123 typedef defaulting_buffer_in_port<audio_buffer>
00124 defaulting_audio_in_port;
00125 typedef buffer_forward_port<audio_buffer> audio_forward_port;
00126 
00127 typedef buffer_out_port<sample_buffer> sample_out_port;
00128 typedef buffer_in_port<sample_buffer>  sample_in_port;
00129 typedef defaulting_buffer_in_port<sample_buffer>
00130 defaulting_sample_in_port;
00131 typedef buffer_forward_port<sample_buffer> sample_forward_port;
00132 
00133 extern template class buffer_out_port<audio_buffer>;
00134 extern template class buffer_in_port<audio_buffer>;
00135 extern template class defaulting_buffer_in_port<audio_buffer>;
00136 extern template class buffer_forward_port<audio_buffer>;
00137 extern template class buffer_out_port<sample_buffer>;
00138 extern template class buffer_in_port<sample_buffer>;
00139 extern template class defaulting_buffer_in_port<sample_buffer>;
00140 extern template class buffer_forward_port<sample_buffer>;
00141 
00142 } /* namespace graph */
00143 } /* namespace psynth */
00144 
00145 #endif /* PSYNTH_GRAPH_BUFFER_PORT_HPP_ */