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