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