Do a topological sort of the route list before passing it to
[ardour.git] / libs / ardour / internal_return.cc
1 /*
2     Copyright (C) 2009 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include <glibmm/thread.h>
20
21 #include "pbd/failed_constructor.h"
22
23 #include "ardour/audio_buffer.h"
24 #include "ardour/internal_return.h"
25 #include "ardour/mute_master.h"
26 #include "ardour/session.h"
27 #include "ardour/internal_send.h"
28 #include "ardour/audioengine.h"
29
30 using namespace std;
31 using namespace ARDOUR;
32
33 PBD::Signal1<void, pframes_t> InternalReturn::CycleStart;
34
35 InternalReturn::InternalReturn (Session& s)
36         : Return (s, true)
37 {
38         CycleStart.connect_same_thread (*this, boost::bind (&InternalReturn::cycle_start, this, _1));
39         _display_to_user = false;
40 }
41
42 void
43 InternalReturn::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, pframes_t nframes, bool)
44 {
45         if (!_active && !_pending_active) {
46                 return;
47         }
48
49         /* _sends is only modified with the process lock held, so this is ok, I think */
50
51         for (list<InternalSend*>::iterator i = _sends.begin(); i != _sends.end(); ++i) {
52                 if ((*i)->active ()) {
53                         bufs.merge_from ((*i)->get_buffers(), nframes);
54                 }
55         }
56
57         _active = _pending_active;
58 }
59
60 bool
61 InternalReturn::configure_io (ChanCount in, ChanCount out)
62 {
63         IOProcessor::configure_io (in, out);
64         allocate_buffers (_session.engine().frames_per_cycle());
65         return true;
66 }
67
68 int
69 InternalReturn::set_block_size (pframes_t nframes)
70 {
71         allocate_buffers (nframes);
72         return 0;
73 }
74
75 void
76 InternalReturn::allocate_buffers (pframes_t nframes)
77 {
78         buffers.ensure_buffers (_configured_input, nframes);
79         buffers.set_count (_configured_input);
80 }
81
82 void
83 InternalReturn::add_send (InternalSend* send)
84 {
85         Glib::Mutex::Lock lm (_session.engine().process_lock());
86         _sends.push_back (send);
87 }
88
89 void
90 InternalReturn::remove_send (InternalSend* send)
91 {
92         Glib::Mutex::Lock lm (_session.engine().process_lock());
93         _sends.remove (send);
94 }
95
96 void
97 InternalReturn::cycle_start (pframes_t nframes)
98 {
99         /* called from process cycle - no lock necessary */
100         if (!_sends.empty ()) {
101                 /* don't bother with this if nobody is going to feed us anything */
102                 buffers.silence (nframes, 0);
103         }
104 }
105
106 XMLNode&
107 InternalReturn::state (bool full)
108 {
109         XMLNode& node (Return::state (full));
110         /* override type */
111         node.add_property("type", "intreturn");
112         return node;
113 }
114
115 XMLNode&
116 InternalReturn::get_state()
117 {
118         return state (true);
119 }
120
121 int
122 InternalReturn::set_state (const XMLNode& node, int version)
123 {
124         return Return::set_state (node, version);
125 }
126
127 bool
128 InternalReturn::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
129 {
130         out = in;
131         return true;
132 }
133
134