processor naming tweaks, processor visibility tweaks and more, trying to finish this...
[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, 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)) {
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::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
70 {
71         if (!_active || _output->n_ports() == ChanCount::ZERO) {
72                 _meter->reset ();
73                 return;
74         }
75
76         // we have to copy the input, because deliver_output() may alter the buffers
77         // in-place, which a send must never do.
78         
79         BufferSet& sendbufs = _session.get_mix_buffers (bufs.count());
80         sendbufs.read_from (bufs, nframes);
81         assert(sendbufs.count() == bufs.count());
82
83         /* gain control */
84
85         // Can't automate gain for sends or returns yet because we need different buffers
86         // so that we don't overwrite the main automation data for the route amp
87         // _amp->setup_gain_automation (start_frame, end_frame, nframes);
88         _amp->run (sendbufs, start_frame, end_frame, nframes);
89
90         /* deliver to outputs */
91
92         Delivery::run (sendbufs, start_frame, end_frame, nframes);
93
94         /* consider metering */
95         
96         if (_metering) {
97                 if (_amp->gain_control()->get_value() == 0) {
98                         _meter->reset();
99                 } else {
100                         _meter->run (*_output_buffers, start_frame, end_frame, nframes);
101                 }
102         }
103 }
104
105 XMLNode&
106 Send::get_state(void)
107 {
108         return state (true);
109 }
110
111 XMLNode&
112 Send::state(bool full)
113 {
114         XMLNode& node = Delivery::state(full);
115         char buf[32];
116
117         node.add_property ("type", "send");
118         snprintf (buf, sizeof (buf), "%" PRIu32, _bitslot);
119         node.add_property ("bitslot", buf);
120
121         return node;
122 }
123
124 int
125 Send::set_state(const XMLNode& node)
126 {
127         XMLNodeList nlist = node.children();
128         XMLNodeIterator niter;
129         const XMLProperty* prop;
130
131         if ((prop = node.property ("bitslot")) == 0) {
132                 _bitslot = _session.next_send_id();
133         } else {
134                 sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
135                 _session.mark_send_id (_bitslot);
136         }
137
138         const XMLNode* insert_node = &node;
139
140         /* XXX need to load automation state & data for amp */
141         
142         Delivery::set_state (*insert_node);
143
144         return 0;
145 }
146
147 bool
148 Send::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
149 {
150         if (_output->n_ports() == ChanCount::ZERO) {
151
152                 /* not configured yet, we can support anything */
153
154                 out = in;
155                 return true; /* we can support anything the first time we're asked */
156
157         } else {
158
159                 /* for a send, processor input corresponds to IO output */
160
161                 out = in;
162                 return true;
163         }
164
165         return false;
166 }
167
168 /** Set up the XML description of a send so that its name is unique.
169  *  @param state XML send state.
170  *  @param session Session.
171  */
172 void
173 Send::make_unique (XMLNode &state, Session &session)
174 {
175         uint32_t const bitslot = session.next_send_id() + 1;
176
177         char buf[32];
178         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
179         state.property("bitslot")->set_value (buf);
180
181         std::string const name = string_compose (_("send %1"), bitslot);
182         
183         state.property("name")->set_value (name);
184
185         XMLNode* io = state.child ("IO");
186
187         if (io) {
188                 io->property("name")->set_value (name);
189         }
190 }
191
192 bool
193 Send::set_name (const std::string& new_name)
194 {
195         std::string unique_name;
196
197         if (_role == Delivery::Send) {
198                 char buf[32];
199                 snprintf (buf, sizeof (buf), "%u", _bitslot);
200                 unique_name = new_name;
201                 unique_name += buf;
202         } else {
203                 unique_name = new_name;
204         }
205
206         return Delivery::set_name (unique_name);
207 }
208
209 bool
210 Send::visible () const
211 {
212         if (_role == Listen) {
213                 return false;
214         }
215
216         return true;
217 }