Make DnD copy processors using their XML representations. Remove unused
[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/send.h>
25 #include <ardour/session.h>
26 #include <ardour/port.h>
27 #include <ardour/audio_port.h>
28 #include <ardour/buffer_set.h>
29 #include <ardour/meter.h>
30 #include <ardour/panner.h>
31 #include <ardour/io.h>
32
33 #include "i18n.h"
34
35 using namespace ARDOUR;
36 using namespace PBD;
37
38 Send::Send (Session& s, Placement p)
39         : IOProcessor (s, string_compose (_("send %1"), (bitslot = s.next_send_id()) + 1), p)
40 {
41         _metering = false;
42         ProcessorCreated (this); /* EMIT SIGNAL */
43 }
44
45 Send::Send (Session& s, const XMLNode& node)
46         : IOProcessor (s, "send", PreFader)
47 {
48         _metering = false;
49
50         if (set_state (node)) {
51                 throw failed_constructor();
52         }
53
54         ProcessorCreated (this); /* EMIT SIGNAL */
55 }
56
57 Send::~Send ()
58 {
59         GoingAway ();
60 }
61
62 XMLNode&
63 Send::get_state(void)
64 {
65         return state (true);
66 }
67
68 XMLNode&
69 Send::state(bool full)
70 {
71         XMLNode& node = IOProcessor::state(full);
72         char buf[32];
73         node.add_property ("type", "send");
74         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
75         node.add_property ("bitslot", buf);
76
77         return node;
78 }
79
80 int
81 Send::set_state(const XMLNode& node)
82 {
83         XMLNodeList nlist = node.children();
84         XMLNodeIterator niter;
85         const XMLProperty* prop;
86
87         if ((prop = node.property ("bitslot")) == 0) {
88                 bitslot = _session.next_send_id();
89         } else {
90                 sscanf (prop->value().c_str(), "%" PRIu32, &bitslot);
91                 _session.mark_send_id (bitslot);
92         }
93
94         const XMLNode* insert_node = &node;
95
96         /* Send has regular IO automation (gain, pan) */
97
98         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
99                 if ((*niter)->name() == "IOProcessor") {
100                         insert_node = *niter;
101                 } else if ((*niter)->name() == X_("Automation")) {
102                         // _io->set_automation_state (*(*niter), Evoral::Parameter(GainAutomation));
103                 }
104         }
105         
106         IOProcessor::set_state (*insert_node);
107
108         return 0;
109 }
110
111 void
112 Send::run_in_place (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
113 {
114         if (active()) {
115
116                 // we have to copy the input, because IO::deliver_output may alter the buffers
117                 // in-place, which a send must never do.
118
119                 BufferSet& sendbufs = _session.get_mix_buffers(bufs.count());
120
121                 sendbufs.read_from(bufs, nframes);
122                 assert(sendbufs.count() == bufs.count());
123
124                 _io->deliver_output (sendbufs, start_frame, end_frame, nframes, offset);
125
126                 if (_metering) {
127                         if (_io->effective_gain() == 0) {
128                                 _io->peak_meter().reset();
129                         } else {
130                                 _io->peak_meter().run_in_place(_io->output_buffers(), start_frame, end_frame, nframes, offset);
131                         }
132                 }
133
134         } else {
135                 _io->silence (nframes, offset);
136                 
137                 if (_metering) {
138                         _io->peak_meter().reset();
139                 }
140         }
141 }
142
143 void
144 Send::set_metering (bool yn)
145 {
146         _metering = yn;
147
148         if (!_metering) {
149                 /* XXX possible thread hazard here */
150                 _io->peak_meter().reset();
151         }
152 }
153
154 bool
155 Send::can_support_io_configuration (const ChanCount& in, ChanCount& out_is_ignored) const
156 {
157         if (_io->input_maximum() == ChanCount::INFINITE && _io->output_maximum() == ChanCount::INFINITE) {
158
159                 /* not configured yet */
160
161                 return 1; /* we can support anything the first time we're asked */
162
163         } else {
164
165                 /* the "input" config for a port insert corresponds to how
166                    many output ports it will have.
167                 */
168
169                 if (_io->output_maximum() == in) {
170                         return 1;
171                 } 
172         }
173
174         return -1;
175 }
176
177 bool
178 Send::configure_io (ChanCount in, ChanCount out)
179 {
180         /* we're transparent no matter what.  fight the power. */
181
182         if (out != in) {
183                 return false;
184         }
185
186         _io->set_output_maximum (in);
187         _io->set_output_minimum (in);
188         _io->set_input_maximum (ChanCount::ZERO);
189         _io->set_input_minimum (ChanCount::ZERO);
190
191         if (_io->ensure_io (ChanCount::ZERO, in, false, this) != 0) {
192                 return false;
193         }
194
195         Processor::configure_io(in, out);
196         _io->reset_panner();
197
198         return true;
199 }
200
201 ChanCount
202 Send::output_streams() const
203 {
204         // this method reflects the idea that from the perspective of the Route's ProcessorList, 
205         // a send is just a passthrough. that doesn't match what the Send actually does with its 
206         // data, but since what it does is invisible to the Route, it appears to be a passthrough.
207         
208         return _configured_input;
209 }
210
211 ChanCount
212 Send::input_streams() const
213 {
214         return _configured_input;
215 }
216
217
218 void
219 Send::expect_inputs (const ChanCount& expected)
220 {
221         if (expected != expected_inputs) {
222                 expected_inputs = expected;
223                 _io->reset_panner ();
224         }
225 }
226
227 /** Set up the XML description of a send so that its name is unique.
228  *  @param state XML send state.
229  *  @param session Session.
230  */
231 void
232 Send::make_unique (XMLNode &state, Session &session)
233 {
234         uint32_t const bitslot = session.next_send_id() + 1;
235
236         char buf[32];
237         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
238         state.property("bitslot")->set_value (buf);
239
240         std::string const name = string_compose (_("send %1"), bitslot);
241         
242         state.property("name")->set_value (name);
243
244         XMLNode* io = state.child ("IO");
245         if (io) {
246                 io->property("name")->set_value (name);
247         }
248 }