remove StateManager code entirely and more debugging output cruft
[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     $Id$
19 */
20
21 #include <algorithm>
22
23 #include <pbd/xml++.h>
24
25 #include <ardour/send.h>
26 #include <ardour/session.h>
27 #include <ardour/port.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, s.next_send_name(), 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         if (set_state (node)) {
49                 throw failed_constructor();
50         }
51
52         RedirectCreated (this); /* EMIT SIGNAL */
53 }
54
55 Send::Send (const Send& other)
56         : Redirect (other._session, other._session.next_send_name(), other.placement())
57 {
58         _metering = false;
59         expected_inputs = 0;
60
61         RedirectCreated (this); /* EMIT SIGNAL */
62 }
63
64 Send::~Send ()
65 {
66         GoingAway ();
67 }
68
69 XMLNode&
70 Send::get_state(void)
71 {
72         return state (true);
73 }
74
75 XMLNode&
76 Send::state(bool full)
77 {
78         XMLNode *node = new XMLNode("Send");
79         node->add_child_nocopy (Redirect::state(full));
80         return *node;
81 }
82
83 int
84 Send::set_state(const XMLNode& node)
85 {
86         XMLNodeList nlist = node.children();
87         XMLNodeIterator niter;
88         
89         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
90                 if ((*niter)->name() == Redirect::state_node_name) {
91                         Redirect::set_state (**niter);
92                         break;
93                 }
94         }
95
96         if (niter == nlist.end()) {
97                 error << _("XML node describing a send is missing a Redirect node") << endmsg;
98                 return -1;
99         }
100
101         return 0;
102 }
103
104 void
105 Send::run (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
106 {
107         if (active()) {
108
109                 // we have to copy the input, because IO::deliver_output may alter the buffers
110                 // in-place, which a send must never do.
111
112                 vector<Sample*>& sendbufs = _session.get_send_buffers();
113
114                 for (size_t i=0; i < nbufs; ++i) {
115                         memcpy (sendbufs[i], bufs[i], sizeof (Sample) * nframes);
116                 }
117                 
118                 
119                 IO::deliver_output (sendbufs, nbufs, nframes, offset);
120
121                 if (_metering) {
122                         uint32_t n;
123                         uint32_t no = n_outputs();
124
125                         if (_gain == 0) {
126
127                                 for (n = 0; n < no; ++n) {
128                                         _peak_power[n] = 0;
129                                 } 
130
131                         } else {
132
133                                 for (n = 0; n < no; ++n) {
134                                         _peak_power[n] = Session::compute_peak (output(n)->get_buffer(nframes) + offset, nframes, _peak_power[n]);
135                                 }
136                         }
137                 }
138
139         } else {
140                 silence (nframes, offset);
141                 
142                 if (_metering) {
143                         uint32_t n;
144                         uint32_t no = n_outputs();
145
146                         for (n = 0; n < no; ++n) {
147                                 _peak_power[n] = 0;
148                         } 
149                 }
150         }
151 }
152
153 void
154 Send::set_metering (bool yn)
155 {
156         _metering = yn;
157
158         if (!_metering) {
159                 /* XXX possible thread hazard here */
160                 reset_peak_meters ();
161         }
162 }
163
164 void
165 Send::expect_inputs (uint32_t expected)
166 {
167         if (expected != expected_inputs) {
168                 expected_inputs = expected;
169                 reset_panner ();
170         }
171 }