fix clicking when processors become active/inactive; reduce crazy 2.5sec delay for...
[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)) {
62                 throw failed_constructor();
63         }
64
65         ProcessorCreated (this); /* EMIT SIGNAL */
66 }
67
68 Return::~Return ()
69 {
70         GoingAway ();
71 }
72
73 XMLNode&
74 Return::get_state(void)
75 {
76         return state (true);
77 }
78
79 XMLNode&
80 Return::state(bool full)
81 {
82         XMLNode& node = IOProcessor::state(full);
83         char buf[32];
84         node.add_property ("type", "return");
85         snprintf (buf, sizeof (buf), "%" PRIu32, _bitslot);
86         node.add_property ("bitslot", buf);
87
88         return node;
89 }
90
91 int
92 Return::set_state(const XMLNode& node)
93 {
94         XMLNodeList nlist = node.children();
95         XMLNodeIterator niter;
96         const XMLProperty* prop;
97
98         if ((prop = node.property ("bitslot")) == 0) {
99                 _bitslot = _session.next_return_id();
100         } else {
101                 sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
102                 _session.mark_return_id (_bitslot);
103         }
104
105         const XMLNode* insert_node = &node;
106
107         /* Return has regular IO automation (gain, pan) */
108
109         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
110                 if ((*niter)->name() == "IOProcessor") {
111                         insert_node = *niter;
112                 } else if ((*niter)->name() == X_("Automation")) {
113                         // _io->set_automation_state (*(*niter), Evoral::Parameter(GainAutomation));
114                 }
115         }
116         
117         IOProcessor::set_state (*insert_node);
118
119         return 0;
120 }
121
122 void
123 Return::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
124 {
125         if ((!_active && !_pending_active) || _input->n_ports() == ChanCount::ZERO) {
126                 return;
127         }
128         
129         _input->collect_input (bufs, nframes, _configured_input);
130         bufs.set_count(_configured_output);
131
132         // Can't automate gain for sends or returns yet because we need different buffers
133         // so that we don't overwrite the main automation data for the route amp
134         // _amp->setup_gain_automation (start_frame, end_frame, nframes);
135         _amp->run (bufs, start_frame, end_frame, nframes);
136         
137         if (_metering) {
138                 if (_amp->gain_control()->get_value() == 0) {
139                         _meter->reset();
140                 } else {
141                         _meter->run (bufs, start_frame, end_frame, nframes);
142                 }
143         }
144
145         _active = _pending_active;
146 }
147
148 bool
149 Return::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
150 {
151         out = in + _input->n_ports();
152         return true;
153 }
154
155 bool
156 Return::configure_io (ChanCount in, ChanCount out)
157 {
158         if (out != in + _input->n_ports()) {
159                 return false;
160         }
161
162         // Ensure there are enough buffers (since we add some)
163         if (_session.get_scratch_buffers(in).count() < out) {
164                 Glib::Mutex::Lock em (_session.engine().process_lock());
165                 IO::PortCountChanged(out);
166         }
167
168         Processor::configure_io(in, out);
169
170         return true;
171 }
172
173 /** Set up the XML description of a return so that its name is unique.
174  *  @param state XML return state.
175  *  @param session Session.
176  */
177 void
178 Return::make_unique (XMLNode &state, Session &session)
179 {
180         uint32_t const bitslot = session.next_return_id() + 1;
181
182         char buf[32];
183         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
184         state.property("bitslot")->set_value (buf);
185
186         std::string const name = string_compose (_("return %1"), bitslot);
187         
188         state.property("name")->set_value (name);
189
190         XMLNode* io = state.child ("IO");
191         if (io) {
192                 io->property("name")->set_value (name);
193         }
194 }
195
196