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