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