libpsynth 0.2.1
/home/raskolnikov/dev/psynth/trunk/src/psynth/graph/node_output.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002  *                                                                         *
00003  *   PSYCHOSYNTH                                                           *
00004  *   ===========                                                           *
00005  *                                                                         *
00006  *   Copyright (C) 2007 Juan Pedro Bolivar Puente                          *
00007  *                                                                         *
00008  *   This program is free software: you can redistribute it and/or modify  *
00009  *   it under the terms of the GNU General Public License as published by  *
00010  *   the Free Software Foundation, either version 3 of the License, or     *
00011  *   (at your option) any later version.                                   *
00012  *                                                                         *
00013  *   This program is distributed in the hope that it will be useful,       *
00014  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00015  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00016  *   GNU General Public License for more details.                          *
00017  *                                                                         *
00018  *   You should have received a copy of the GNU General Public License     *
00019  *   along with this program.  If not, see <http://www.gnu.org/licenses/>. *
00020  *                                                                         *
00021  ***************************************************************************/
00022  
00023 #ifndef PSYNTH_NODE_OUTPUT_H
00024 #define PSYNTH_NODE_OUTPUT_H
00025 
00026 #include <list>
00027 #include <functional>
00028 
00029 #include <psynth/io/output.hpp>
00030 #include <psynth/graph/node.hpp>
00031 #include <psynth/graph/node_factory.hpp>
00032 
00033 namespace psynth
00034 {
00035 namespace graph
00036 {
00037 
00038 class node_manager;
00039 
00040 typedef io::output_ptr<audio_const_range> audio_output_ptr;
00041 typedef io::async_output_ptr<audio_const_range> audio_async_output_ptr;
00042 
00043 class node_output : public node
00044 {
00045     struct slot
00046     {
00047         audio_ring_range::position  m_ptr;
00048         audio_async_output_ptr      m_out;
00049         node_output* m_parent;
00050         audio_buffer m_buf;
00051         
00052         slot (audio_async_output_ptr out,
00053               node_output* parent,
00054               audio_ring_range::position ptr,
00055               const audio_info& info);
00056         
00057         void callback (std::size_t nframes);    
00058     };
00059         
00060     audio_ring_buffer m_buffer;
00061     
00062     node_manager* m_manager;
00063     std::list<slot*> m_slots;
00064     std::list<audio_output_ptr > m_passive_slots;
00065 
00066     /*
00067       RWLock m_buflock;
00068       Mutex m_passive_lock;
00069       Mutex m_global_lock;
00070     */
00071     
00072     void do_output (slot& slot, size_t nframes);
00073         
00074     void do_update (const node* caller, int caller_port_type, int caller_port);
00075     void do_advance () {}
00076     void on_info_change ();
00077     
00078 public:
00079     enum in_audio_socket_id {
00080         IN_A_INPUT,
00081         N_IN_A_SOCKETS
00082     } ;
00083         
00084     enum in_control_socket_id {
00085         N_IN_C_SOCKETS
00086     };
00087         
00088     enum out_audio_socket_id {
00089         N_OUT_A_SOCKETS
00090     };
00091         
00092     enum  out_control_socket_id {
00093         N_OUT_C_SOCKETS
00094     };
00095 
00096     enum param_id {
00097         N_PARAM
00098     };
00099         
00100     node_output (const audio_info& info);
00101 
00102     ~node_output ();
00103         
00104     bool set_manager (node_manager* mgr) {
00105         if (m_manager != NULL && mgr != NULL) 
00106             return false; /* Already attached */
00107                 
00108         m_manager = mgr;
00109         return true;
00110     }
00111         
00112     void attach_output (audio_async_output_ptr out)
00113     {
00114         //m_buflock.readLock();
00115         m_slots.push_back(
00116             new slot (out, this, range (m_buffer).begin_pos(), get_info()));
00117         //m_buflock.unlock();
00118     };
00119     
00120     void detach_output (audio_async_output_ptr out)
00121     {
00122         for (std::list<slot*>::iterator i = m_slots.begin(); i != m_slots.end();) {
00123             if ((*i)->m_out == out) {
00124                 (*i)->m_out->set_callback (0);
00125                 /* This should take threading into account. */
00126                 (*i)->m_out.reset ();
00127                 delete *i;
00128                 m_slots.erase(i++);
00129             } else
00130                 ++i;
00131         }
00132     };
00133 
00134     void attach_passive_output (audio_output_ptr out) {
00135         //m_passive_lock.lock();
00136         m_passive_slots.push_back (out);
00137         //m_passive_lock.unlock();
00138     };
00139 
00140     void detach_passive_output (audio_output_ptr out) {
00141         //m_passive_lock.lock();
00142         m_passive_slots.remove(out);
00143         //m_passive_lock.unlock();
00144     };
00145 };
00146 
00147 PSYNTH_DECLARE_NODE_FACTORY(node_output, "output");
00148 
00149 } /* namespace graph */
00150 } /* namespace psynth */
00151 
00152 #endif /* PSYNTH_OUBJECTOUTPUT_H */