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