Part 1 of loading 2.X sessions; some things work, some things don't, hacks a-plenty.
[ardour.git] / libs / ardour / send.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/send.h"
26 #include "ardour/session.h"
27 #include "ardour/port.h"
28 #include "ardour/audio_port.h"
29 #include "ardour/buffer_set.h"
30 #include "ardour/meter.h"
31 #include "ardour/panner.h"
32 #include "ardour/io.h"
33
34 #include "i18n.h"
35
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 Send::Send (Session& s, boost::shared_ptr<MuteMaster> mm, Role r)
40         : Delivery (s, mm, string_compose (_("send %1"), (_bitslot = s.next_send_id()) + 1), r)
41         , _metering (false)
42 {
43         _amp.reset (new Amp (_session, _mute_master));
44         _meter.reset (new PeakMeter (_session));
45
46         ProcessorCreated (this); /* EMIT SIGNAL */
47 }
48
49 Send::Send (Session& s, boost::shared_ptr<MuteMaster> mm, const XMLNode& node, int version, Role r)
50         : Delivery (s, mm, "send", r)
51         , _metering (false)
52 {
53         _amp.reset (new Amp (_session, _mute_master));
54         _meter.reset (new PeakMeter (_session));
55
56         if (set_state (node, version)) {
57                 throw failed_constructor();
58         }
59
60         ProcessorCreated (this); /* EMIT SIGNAL */
61 }
62
63 Send::~Send ()
64 {
65         GoingAway ();
66 }
67
68 void
69 Send::activate ()
70 {
71         _amp->activate ();
72         _meter->activate ();
73
74         Processor::activate ();
75 }
76
77 void
78 Send::deactivate ()
79 {
80         _amp->deactivate ();
81         _meter->deactivate ();
82
83         Processor::deactivate ();
84 }
85
86 void
87 Send::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
88 {
89         if (_output->n_ports() == ChanCount::ZERO) {
90                 _meter->reset ();
91                 _active = _pending_active;
92                 return;
93         }
94
95         if (!_active && !_pending_active) {
96                 _meter->reset ();
97                 _output->silence (nframes);
98                 _active = _pending_active;
99                 return;
100         }
101
102         // we have to copy the input, because deliver_output() may alter the buffers
103         // in-place, which a send must never do.
104
105         BufferSet& sendbufs = _session.get_mix_buffers (bufs.count());
106         sendbufs.read_from (bufs, nframes);
107         assert(sendbufs.count() == bufs.count());
108
109         /* gain control */
110
111         // Can't automate gain for sends or returns yet because we need different buffers
112         // so that we don't overwrite the main automation data for the route amp
113         // _amp->setup_gain_automation (start_frame, end_frame, nframes);
114         _amp->run (sendbufs, start_frame, end_frame, nframes);
115
116         /* deliver to outputs */
117
118         Delivery::run (sendbufs, start_frame, end_frame, nframes);
119
120         /* consider metering */
121
122         if (_metering) {
123                 if (_amp->gain_control()->get_value() == 0) {
124                         _meter->reset();
125                 } else {
126                         _meter->run (*_output_buffers, start_frame, end_frame, nframes);
127                 }
128         }
129
130         /* _active was set to _pending_active by Delivery::run() */
131 }
132
133 XMLNode&
134 Send::get_state(void)
135 {
136         return state (true);
137 }
138
139 XMLNode&
140 Send::state(bool full)
141 {
142         XMLNode& node = Delivery::state(full);
143         char buf[32];
144
145         node.add_property ("type", "send");
146         snprintf (buf, sizeof (buf), "%" PRIu32, _bitslot);
147         node.add_property ("bitslot", buf);
148
149         return node;
150 }
151
152 int
153 Send::set_state (const XMLNode& node, int version)
154 {
155         XMLNodeList nlist = node.children();
156         XMLNodeIterator niter;
157         const XMLProperty* prop;
158
159         if ((prop = node.property ("bitslot")) == 0) {
160                 _bitslot = _session.next_send_id();
161         } else {
162                 sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
163                 _session.mark_send_id (_bitslot);
164         }
165
166         const XMLNode* insert_node = &node;
167
168         /* XXX need to load automation state & data for amp */
169
170         Delivery::set_state (*insert_node);
171
172         return 0;
173 }
174
175 bool
176 Send::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
177 {
178         /* sends have no impact at all on the channel configuration of the
179            streams passing through the route. so, out == in.
180         */
181
182         out = in;
183         return true;
184 }
185
186 bool
187 Send::configure_io (ChanCount in, ChanCount out)
188 {
189         if (!_amp->configure_io (in, out) || !_meter->configure_io (in, out)) {
190                 return false;
191         }
192
193         if (!Processor::configure_io (in, out)) {
194                 return false;
195         }
196
197         reset_panner ();
198
199         return true;
200 }
201
202 /** Set up the XML description of a send so that its name is unique.
203  *  @param state XML send state.
204  *  @param session Session.
205  */
206 void
207 Send::make_unique (XMLNode &state, Session &session)
208 {
209         uint32_t const bitslot = session.next_send_id() + 1;
210
211         char buf[32];
212         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
213         state.property("bitslot")->set_value (buf);
214
215         std::string const name = string_compose (_("send %1"), bitslot);
216
217         state.property("name")->set_value (name);
218
219         XMLNode* io = state.child ("IO");
220
221         if (io) {
222                 io->property("name")->set_value (name);
223         }
224 }
225
226 bool
227 Send::set_name (const std::string& new_name)
228 {
229         std::string unique_name;
230
231         if (_role == Delivery::Send) {
232                 char buf[32];
233                 snprintf (buf, sizeof (buf), "%u", _bitslot);
234                 unique_name = new_name;
235                 unique_name += buf;
236         } else {
237                 unique_name = new_name;
238         }
239
240         return Delivery::set_name (unique_name);
241 }
242
243 bool
244 Send::visible () const
245 {
246         if (_role == Listen) {
247                 return false;
248         }
249
250         return true;
251 }