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