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