cleanup up cleanup at session destruction; clarify the meaning of 3 signals (DropRefe...
[ardour.git] / libs / ardour / return.cc
1 /*
2     Copyright (C) 2000 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
20 #include <algorithm>
21
22 #include "pbd/xml++.h"
23
24 #include "ardour/amp.h"
25 #include "ardour/audio_port.h"
26 #include "ardour/buffer_set.h"
27 #include "ardour/io.h"
28 #include "ardour/meter.h"
29 #include "ardour/panner.h"
30 #include "ardour/port.h"
31 #include "ardour/return.h"
32 #include "ardour/session.h"
33
34 #include "i18n.h"
35
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 Return::Return (Session& s, bool internal)
40         : IOProcessor (s, (internal ? false : true), false,
41                        string_compose (_("return %1"), (_bitslot = s.next_return_id()) + 1))
42         , _metering (false)
43 {
44         /* never muted */
45
46         _amp.reset (new Amp (_session, boost::shared_ptr<MuteMaster>()));
47         _meter.reset (new PeakMeter (_session));
48
49         ProcessorCreated (this); /* EMIT SIGNAL */
50 }
51
52 Return::Return (Session& s, const XMLNode& node, bool internal)
53         : IOProcessor (s, (internal ? false : true), false, "return")
54         , _metering (false)
55 {
56         /* never muted */
57
58         _amp.reset (new Amp (_session, boost::shared_ptr<MuteMaster>()));
59         _meter.reset (new PeakMeter (_session));
60
61         if (set_state (node, Stateful::loading_state_version)) {
62                 throw failed_constructor();
63         }
64
65         ProcessorCreated (this); /* EMIT SIGNAL */
66 }
67
68 Return::~Return ()
69 {
70 }
71
72 XMLNode&
73 Return::get_state(void)
74 {
75         return state (true);
76 }
77
78 XMLNode&
79 Return::state(bool full)
80 {
81         XMLNode& node = IOProcessor::state(full);
82         char buf[32];
83         node.add_property ("type", "return");
84         snprintf (buf, sizeof (buf), "%" PRIu32, _bitslot);
85         node.add_property ("bitslot", buf);
86
87         return node;
88 }
89
90 int
91 Return::set_state (const XMLNode& node, int version)
92 {
93         XMLNodeList nlist = node.children();
94         XMLNodeIterator niter;
95         const XMLProperty* prop;
96
97         if ((prop = node.property ("bitslot")) == 0) {
98                 _bitslot = _session.next_return_id();
99         } else {
100                 sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
101                 _session.mark_return_id (_bitslot);
102         }
103
104         const XMLNode* insert_node = &node;
105
106         /* Return has regular IO automation (gain, pan) */
107
108         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
109                 if ((*niter)->name() == "IOProcessor") {
110                         insert_node = *niter;
111                 } else if ((*niter)->name() == X_("Automation")) {
112                         // _io->set_automation_state (*(*niter), Evoral::Parameter(GainAutomation));
113                 }
114         }
115
116         IOProcessor::set_state (*insert_node, version);
117
118         return 0;
119 }
120
121 void
122 Return::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes, bool)
123 {
124         if ((!_active && !_pending_active) || _input->n_ports() == ChanCount::ZERO) {
125                 return;
126         }
127
128         _input->collect_input (bufs, nframes, _configured_input);
129         bufs.set_count(_configured_output);
130
131         // Can't automate gain for sends or returns yet because we need different buffers
132         // so that we don't overwrite the main automation data for the route amp
133         // _amp->setup_gain_automation (start_frame, end_frame, nframes);
134         _amp->run (bufs, start_frame, end_frame, nframes, true);
135
136         if (_metering) {
137                 if (_amp->gain_control()->get_value() == 0) {
138                         _meter->reset();
139                 } else {
140                         _meter->run (bufs, start_frame, end_frame, nframes, true);
141                 }
142         }
143
144         _active = _pending_active;
145 }
146
147 bool
148 Return::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
149 {
150         out = in + _input->n_ports();
151         return true;
152 }
153
154 bool
155 Return::configure_io (ChanCount in, ChanCount out)
156 {
157         if (out != in + _input->n_ports()) {
158                 return false;
159         }
160
161         // Ensure there are enough buffers (since we add some)
162         if (_session.get_scratch_buffers(in).count() < out) {
163                 Glib::Mutex::Lock em (_session.engine().process_lock());
164                 IO::PortCountChanged(out);
165         }
166
167         Processor::configure_io(in, out);
168
169         return true;
170 }
171
172 /** Set up the XML description of a return so that its name is unique.
173  *  @param state XML return state.
174  *  @param session Session.
175  */
176 void
177 Return::make_unique (XMLNode &state, Session &session)
178 {
179         uint32_t const bitslot = session.next_return_id() + 1;
180
181         char buf[32];
182         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
183         state.property("bitslot")->set_value (buf);
184
185         std::string const name = string_compose (_("return %1"), bitslot);
186
187         state.property("name")->set_value (name);
188
189         XMLNode* io = state.child ("IO");
190         if (io) {
191                 io->property("name")->set_value (name);
192         }
193 }
194
195