first pass (ok, third really) at internal send+return - audio routing inside ardour...
[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, bool internal)
40         : Delivery (s, mm, string_compose (_("send %1"), (_bitslot = s.next_send_id()) + 1), 
41                     (internal ? Delivery::Listen : Delivery::Send))
42         , _metering (false)
43 {
44         _amp.reset (new Amp (_session, _mute_master));
45         _meter.reset (new PeakMeter (_session));
46
47         ProcessorCreated (this); /* EMIT SIGNAL */
48 }
49
50 Send::Send (Session& s, boost::shared_ptr<MuteMaster> mm, const XMLNode& node, bool internal)
51         : Delivery (s, mm, "send", (internal ? Delivery::Listen : Delivery::Send))
52         , _metering (false)
53 {
54         _amp.reset (new Amp (_session, _mute_master));
55         _meter.reset (new PeakMeter (_session));
56
57         if (set_state (node)) {
58                 throw failed_constructor();
59         }
60
61         ProcessorCreated (this); /* EMIT SIGNAL */
62 }
63
64 Send::~Send ()
65 {
66         GoingAway ();
67 }
68
69 void
70 Send::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes)
71 {
72         if (!_active || _output->n_ports() == ChanCount::ZERO) {
73                 _meter->reset ();
74                 return;
75         }
76
77         // we have to copy the input, because deliver_output() may alter the buffers
78         // in-place, which a send must never do.
79         
80         BufferSet& sendbufs = _session.get_mix_buffers (bufs.count());
81         sendbufs.read_from (bufs, nframes);
82         assert(sendbufs.count() == bufs.count());
83
84         /* gain control */
85
86         // Can't automate gain for sends or returns yet because we need different buffers
87         // so that we don't overwrite the main automation data for the route amp
88         // _amp->setup_gain_automation (start_frame, end_frame, nframes);
89         _amp->run (sendbufs, start_frame, end_frame, nframes);
90
91         /* deliver to outputs */
92
93         Delivery::run (sendbufs, start_frame, end_frame, nframes);
94
95         /* consider metering */
96         
97         if (_metering) {
98                 if (_amp->gain_control()->get_value() == 0) {
99                         _meter->reset();
100                 } else {
101                         _meter->run (*_output_buffers, start_frame, end_frame, nframes);
102                 }
103         }
104 }
105
106 XMLNode&
107 Send::get_state(void)
108 {
109         return state (true);
110 }
111
112 XMLNode&
113 Send::state(bool full)
114 {
115         XMLNode& node = IOProcessor::state(full);
116         char buf[32];
117
118         node.add_property ("type", "send");
119         snprintf (buf, sizeof (buf), "%" PRIu32, _bitslot);
120         node.add_property ("bitslot", buf);
121
122         return node;
123 }
124
125 int
126 Send::set_state(const XMLNode& node)
127 {
128         XMLNodeList nlist = node.children();
129         XMLNodeIterator niter;
130         const XMLProperty* prop;
131
132         if ((prop = node.property ("bitslot")) == 0) {
133                 _bitslot = _session.next_send_id();
134         } else {
135                 sscanf (prop->value().c_str(), "%" PRIu32, &_bitslot);
136                 _session.mark_send_id (_bitslot);
137         }
138
139         const XMLNode* insert_node = &node;
140
141         /* XXX need to load automation state & data for amp */
142         
143         Delivery::set_state (*insert_node);
144
145         return 0;
146 }
147
148 bool
149 Send::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
150 {
151         if (_output->n_ports() == ChanCount::ZERO) {
152
153                 /* not configured yet, we can support anything */
154
155                 out = in;
156                 return true; /* we can support anything the first time we're asked */
157
158         } else {
159
160                 /* for a send, processor input corresponds to IO output */
161
162                 out = in;
163                 return true;
164         }
165
166         return false;
167 }
168
169 /** Set up the XML description of a send so that its name is unique.
170  *  @param state XML send state.
171  *  @param session Session.
172  */
173 void
174 Send::make_unique (XMLNode &state, Session &session)
175 {
176         uint32_t const bitslot = session.next_send_id() + 1;
177
178         char buf[32];
179         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
180         state.property("bitslot")->set_value (buf);
181
182         std::string const name = string_compose (_("send %1"), bitslot);
183         
184         state.property("name")->set_value (name);
185
186         XMLNode* io = state.child ("IO");
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         char buf[32];
196         std::string unique_name;
197
198         snprintf (buf, sizeof (buf), "%u", _bitslot);
199         unique_name = new_name;
200         unique_name += buf;
201
202         return Delivery::set_name (unique_name);
203 }