nick m's fix for markers etc ; several tweaks for mute/solo ; rename run_in_place...
[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)
40         : IOProcessor (s, true, false, string_compose (_("return %1"), (_bitslot = s.next_return_id()) + 1))
41         , _metering (false)
42 {
43         /* never muted */
44
45         _amp.reset (new Amp (_session, boost::shared_ptr<MuteMaster>()));
46         _meter.reset (new PeakMeter (_session));
47
48         ProcessorCreated (this); /* EMIT SIGNAL */
49 }
50
51 Return::Return (Session& s, const XMLNode& node)
52         : IOProcessor (s, true, false, "return")
53         , _metering (false)
54 {
55         /* never muted */
56
57         _amp.reset (new Amp (_session, boost::shared_ptr<MuteMaster>()));
58         _meter.reset (new PeakMeter (_session));
59
60         if (set_state (node)) {
61                 throw failed_constructor();
62         }
63
64         ProcessorCreated (this); /* EMIT SIGNAL */
65 }
66
67 Return::~Return ()
68 {
69         GoingAway ();
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)
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);
117
118         return 0;
119 }
120
121 void
122 Return::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
123 {
124         if (!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);
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);
141                 }
142         }
143 }
144
145 bool
146 Return::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
147 {
148         out = in + _input->n_ports();
149         return true;
150 }
151
152 bool
153 Return::configure_io (ChanCount in, ChanCount out)
154 {
155         if (out != in + _input->n_ports()) {
156                 return false;
157         }
158
159         // Ensure there are enough buffers (since we add some)
160         if (_session.get_scratch_buffers(in).count() < out) {
161                 Glib::Mutex::Lock em (_session.engine().process_lock());
162                 IO::PortCountChanged(out);
163         }
164
165         Processor::configure_io(in, out);
166
167         return true;
168 }
169
170 /** Set up the XML description of a return so that its name is unique.
171  *  @param state XML return state.
172  *  @param session Session.
173  */
174 void
175 Return::make_unique (XMLNode &state, Session &session)
176 {
177         uint32_t const bitslot = session.next_return_id() + 1;
178
179         char buf[32];
180         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
181         state.property("bitslot")->set_value (buf);
182
183         std::string const name = string_compose (_("return %1"), bitslot);
184         
185         state.property("name")->set_value (name);
186
187         XMLNode* io = state.child ("IO");
188         if (io) {
189                 io->property("name")->set_value (name);
190         }
191 }
192
193