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