Insert/Redirect refactoring, towards better MIDI support in mixer strip, and
[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 "i18n.h"
31
32 using namespace ARDOUR;
33 using namespace PBD;
34
35 Send::Send (Session& s, Placement p)
36         : Redirect (s, string_compose (_("send %1"), (bitslot = s.next_send_id()) + 1), p)
37 {
38         _metering = false;
39         InsertCreated (this); /* EMIT SIGNAL */
40 }
41
42 Send::Send (Session& s, const XMLNode& node)
43         : Redirect (s,  "send", PreFader)
44 {
45         _metering = false;
46
47         if (set_state (node)) {
48                 throw failed_constructor();
49         }
50
51         InsertCreated (this); /* EMIT SIGNAL */
52 }
53
54 Send::Send (const Send& other)
55         : Redirect (other._session, string_compose (_("send %1"), (bitslot = other._session.next_send_id()) + 1), other.placement())
56 {
57         _metering = false;
58         InsertCreated (this); /* EMIT SIGNAL */
59 }
60
61 Send::~Send ()
62 {
63         GoingAway ();
64 }
65
66 XMLNode&
67 Send::get_state(void)
68 {
69         return state (true);
70 }
71
72 XMLNode&
73 Send::state(bool full)
74 {
75         XMLNode& node = Redirect::state(full);
76         char buf[32];
77         node.add_property ("type", "send");
78         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
79         node.add_property ("bitslot", buf);
80
81         return node;
82 }
83
84 int
85 Send::set_state(const XMLNode& node)
86 {
87         XMLNodeList nlist = node.children();
88         XMLNodeIterator niter;
89         const XMLProperty* prop;
90
91         if ((prop = node.property ("bitslot")) == 0) {
92                 bitslot = _session.next_send_id();
93         } else {
94                 sscanf (prop->value().c_str(), "%" PRIu32, &bitslot);
95                 _session.mark_send_id (bitslot);
96         }
97
98         const XMLNode* insert_node = &node;
99
100         /* Send has regular IO automation (gain, pan) */
101
102         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
103                 if ((*niter)->name() == "Redirect") {
104                         insert_node = *niter;
105                 } else if ((*niter)->name() == X_("Automation")) {
106                         _io->set_automation_state (*(*niter));
107                 }
108         }
109         
110         Redirect::set_state (*insert_node);
111
112         if (niter == nlist.end()) {
113                 error << _("XML node describing a send is missing a Redirect node") << endmsg;
114                 return -1;
115         }
116
117         return 0;
118 }
119
120 void
121 Send::run (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, nframes_t offset)
122 {
123         if (active()) {
124
125                 // we have to copy the input, because IO::deliver_output may alter the buffers
126                 // in-place, which a send must never do.
127
128                 BufferSet& sendbufs = _session.get_send_buffers(bufs.count());
129
130                 sendbufs.read_from(bufs, nframes);
131                 assert(sendbufs.count() == bufs.count());
132
133                 _io->deliver_output (sendbufs, start_frame, end_frame, nframes, offset);
134
135                 if (_metering) {
136                         if (_io->_gain == 0) {
137                                 _io->_meter->reset();
138                         } else {
139                                 _io->_meter->run(_io->output_buffers(), start_frame, end_frame, nframes, offset);
140                         }
141                 }
142
143         } else {
144                 _io->silence (nframes, offset);
145                 
146                 if (_metering) {
147                         _io->_meter->reset();
148                 }
149         }
150 }
151
152 void
153 Send::set_metering (bool yn)
154 {
155         _metering = yn;
156
157         if (!_metering) {
158                 /* XXX possible thread hazard here */
159                 _io->peak_meter().reset();
160         }
161 }
162
163 bool
164 Send::can_support_input_configuration (ChanCount in) const
165 {
166         if (_io->input_maximum() == ChanCount::INFINITE && _io->output_maximum() == ChanCount::INFINITE) {
167
168                 /* not configured yet */
169
170                 return true; /* we can support anything the first time we're asked */
171
172         } else {
173
174                 /* the "input" config for a port insert corresponds to how
175                    many output ports it will have.
176                 */
177
178                 if (_io->output_maximum() == in) {
179
180                         return true;
181                 } 
182         }
183
184         return false;
185 }
186
187 ChanCount
188 Send::output_for_input_configuration (ChanCount in) const
189 {
190         // from the internal (Insert) perspective a Send does not modify its input whatsoever
191         return in;
192 }
193
194 bool
195 Send::configure_io (ChanCount in, ChanCount out)
196 {
197         /* we're transparent no matter what.  fight the power. */
198         if (out != in)
199                 return false;
200
201         _io->set_output_maximum (in);
202         _io->set_output_minimum (in);
203         _io->set_input_maximum (ChanCount::ZERO);
204         _io->set_input_minimum (ChanCount::ZERO);
205
206         bool success = _io->ensure_io (ChanCount::ZERO, in, false, this) == 0;
207
208         if (success) {
209                 Insert::configure_io(in, out);
210                 _io->reset_panner();
211                 return true;
212         } else {
213                 return false;
214         }
215 }
216
217 ChanCount
218 Send::output_streams() const
219 {
220         return _io->n_outputs ();
221 }
222
223 ChanCount
224 Send::input_streams() const
225 {
226         return _io->n_outputs (); // (sic)
227 }