ensure that the name used the control out IO for a route always uses the desired...
[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/panner.h>
28
29 #include "i18n.h"
30
31 using namespace ARDOUR;
32 using namespace PBD;
33
34 Send::Send (Session& s, Placement p)
35         : Redirect (s, string_compose (_("send %1"), (bitslot = s.next_send_id()) + 1), p)
36 {
37         _metering = false;
38         expected_inputs = 0;
39         RedirectCreated (this); /* EMIT SIGNAL */
40 }
41
42 Send::Send (Session& s, const XMLNode& node)
43         : Redirect (s,  "send", PreFader)
44 {
45         _metering = false;
46         expected_inputs = 0;
47
48         bitslot = 0xffffffff;
49
50         if (set_state (node)) {
51                 throw failed_constructor();
52         }
53
54         RedirectCreated (this); /* EMIT SIGNAL */
55 }
56
57 Send::Send (const Send& other)
58         : Redirect (other._session, string_compose (_("send %1"), (bitslot = other._session.next_send_id()) + 1), other.placement())
59 {
60         _metering = false;
61         expected_inputs = 0;
62
63         /* set up the same outputs, and connect them to the same places */
64         no_panner_reset = true;
65         for (uint32_t i = 0; i < other.n_outputs (); ++i) {
66                 add_output_port ("", 0);
67                 Port* p = other.output (i);
68                 if (p) {
69                         /* this is what the other send's output is connected to */
70                         const char **connections = p->get_connections ();
71                         if (connections) {
72                                 for (uint32_t c = 0; connections[c]; ++c) {
73                                         connect_output (output (i), connections [c], 0);
74                                 }
75                         }
76                 }
77         }
78         
79         /* setup panner */
80
81         no_panner_reset = false;
82
83         /* copy state */
84
85         XMLNode& other_state (const_cast<Send*>(&other)->_panner->get_state());
86         _panner->set_state (other_state);
87         
88         delete &other_state;
89
90         RedirectCreated (this); /* EMIT SIGNAL */
91 }
92
93 Send::~Send ()
94 {
95         GoingAway ();
96 }
97
98 XMLNode&
99 Send::get_state(void)
100 {
101         return state (true);
102 }
103
104 XMLNode&
105 Send::state(bool full)
106 {
107         XMLNode *node = new XMLNode("Send");
108         char buf[32];
109         node->add_child_nocopy (Redirect::state (full));
110         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
111         node->add_property ("bitslot", buf);
112         return *node;
113 }
114
115 int
116 Send::set_state(const XMLNode& node)
117 {
118         XMLNodeList nlist = node.children();
119         XMLNodeIterator niter;
120         const XMLProperty* prop;
121
122         if ((prop = node.property ("bitslot")) == 0) {
123                 bitslot = _session.next_send_id();
124         } else {
125                 uint32_t old_bitslot = bitslot;
126                 sscanf (prop->value().c_str(), "%" PRIu32, &bitslot);
127
128                 if (bitslot != old_bitslot) {
129                         _session.mark_send_id (bitslot);
130                 }
131         }
132
133         /* Send has regular IO automation (gain, pan) */
134
135         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
136                 if ((*niter)->name() == Redirect::state_node_name) {
137                         Redirect::set_state (**niter);
138                         break;
139                 } else if ((*niter)->name() == X_("Automation")) {
140                         IO::set_automation_state (*(*niter));
141                 }
142         }
143
144         if (niter == nlist.end()) {
145                 error << _("XML node describing a send is missing a Redirect node") << endmsg;
146                 return -1;
147         }
148
149         return 0;
150 }
151
152 void
153 Send::run (vector<Sample *>& bufs, uint32_t nbufs, 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                 vector<Sample*>& sendbufs = _session.get_send_buffers();
161
162                 for (size_t i=0; i < nbufs; ++i) {
163                         memcpy (sendbufs[i], bufs[i], sizeof (Sample) * nframes);
164                 }
165                 
166                 
167                 IO::deliver_output (sendbufs, nbufs, nframes, offset);
168
169                 if (_metering) {
170                         uint32_t n;
171                         uint32_t no = n_outputs();
172
173                         if (_gain == 0) {
174
175                                 for (n = 0; n < no; ++n) {
176                                         _peak_power[n] = 0;
177                                 } 
178
179                         } else {
180
181                                 for (n = 0; n < no; ++n) {
182                                         _peak_power[n] = Session::compute_peak (output(n)->get_buffer(nframes) + offset, nframes, _peak_power[n]);
183                                 }
184                         }
185                 }
186
187         } else {
188                 silence (nframes, offset);
189                 
190                 if (_metering) {
191                         uint32_t n;
192                         uint32_t no = n_outputs();
193
194                         for (n = 0; n < no; ++n) {
195                                 _peak_power[n] = 0;
196                         } 
197                 }
198         }
199 }
200
201 void
202 Send::set_metering (bool yn)
203 {
204         _metering = yn;
205
206         if (!_metering) {
207                 /* XXX possible thread hazard here */
208                 reset_peak_meters ();
209         }
210 }
211
212 void
213 Send::expect_inputs (uint32_t expected)
214 {
215         if (expected != expected_inputs) {
216                 expected_inputs = expected;
217                 reset_panner ();
218         }
219 }