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