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