merged with 1697 revision of trunk (which is post-rc1 but pre-rc2
[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
28 #include "i18n.h"
29
30 using namespace ARDOUR;
31 using namespace PBD;
32
33 Send::Send (Session& s, Placement p)
34         : Redirect (s, string_compose (_("send %1"), (bitslot = s.next_send_id()) + 1), p)
35 {
36         _metering = false;
37         expected_inputs = 0;
38         RedirectCreated (this); /* EMIT SIGNAL */
39 }
40
41 Send::Send (Session& s, const XMLNode& node)
42         : Redirect (s,  "send", PreFader)
43 {
44         _metering = false;
45         expected_inputs = 0;
46
47         if (set_state (node)) {
48                 throw failed_constructor();
49         }
50
51         RedirectCreated (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         expected_inputs = 0;
59
60         RedirectCreated (this); /* EMIT SIGNAL */
61 }
62
63 Send::~Send ()
64 {
65         GoingAway ();
66 }
67
68 XMLNode&
69 Send::get_state(void)
70 {
71         return state (true);
72 }
73
74 XMLNode&
75 Send::state(bool full)
76 {
77         XMLNode *node = new XMLNode("Send");
78         char buf[32];
79         node->add_child_nocopy (Redirect::state (full));
80         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
81         node->add_property ("bitslot", buf);
82         return *node;
83 }
84
85 int
86 Send::set_state(const XMLNode& node)
87 {
88         XMLNodeList nlist = node.children();
89         XMLNodeIterator niter;
90         const XMLProperty* prop;
91
92         if ((prop = node.property ("bitslot")) == 0) {
93                 bitslot = _session.next_send_id();
94         } else {
95                 sscanf (prop->value().c_str(), "%" PRIu32, &bitslot);
96                 _session.mark_send_id (bitslot);
97         }
98
99         /* Send has regular IO automation (gain, pan) */
100
101         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
102                 if ((*niter)->name() == Redirect::state_node_name) {
103                         Redirect::set_state (**niter);
104                         break;
105                 } else if ((*niter)->name() == X_("Automation")) {
106                         IO::set_automation_state (*(*niter));
107                 }
108         }
109
110         if (niter == nlist.end()) {
111                 error << _("XML node describing a send is missing a Redirect node") << endmsg;
112                 return -1;
113         }
114
115         return 0;
116 }
117
118 void
119 Send::run (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
120 {
121         if (active()) {
122
123                 // we have to copy the input, because IO::deliver_output may alter the buffers
124                 // in-place, which a send must never do.
125
126                 vector<Sample*>& sendbufs = _session.get_send_buffers();
127
128                 for (size_t i=0; i < nbufs; ++i) {
129                         memcpy (sendbufs[i], bufs[i], sizeof (Sample) * nframes);
130                 }
131                 
132                 
133                 IO::deliver_output (sendbufs, nbufs, nframes, offset);
134
135                 if (_metering) {
136                         uint32_t n;
137                         uint32_t no = n_outputs();
138
139                         if (_gain == 0) {
140
141                                 for (n = 0; n < no; ++n) {
142                                         _peak_power[n] = 0;
143                                 } 
144
145                         } else {
146
147                                 for (n = 0; n < no; ++n) {
148                                         _peak_power[n] = Session::compute_peak (output(n)->get_buffer(nframes) + offset, nframes, _peak_power[n]);
149                                 }
150                         }
151                 }
152
153         } else {
154                 silence (nframes, offset);
155                 
156                 if (_metering) {
157                         uint32_t n;
158                         uint32_t no = n_outputs();
159
160                         for (n = 0; n < no; ++n) {
161                                 _peak_power[n] = 0;
162                         } 
163                 }
164         }
165 }
166
167 void
168 Send::set_metering (bool yn)
169 {
170         _metering = yn;
171
172         if (!_metering) {
173                 /* XXX possible thread hazard here */
174                 reset_peak_meters ();
175         }
176 }
177
178 void
179 Send::expect_inputs (uint32_t expected)
180 {
181         if (expected != expected_inputs) {
182                 expected_inputs = expected;
183                 reset_panner ();
184         }
185 }