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