Merged with trunk R1141
[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 #include <ardour/buffer_set.h>
30 #include <ardour/meter.h>
31 #include "i18n.h"
32
33 using namespace ARDOUR;
34 using namespace PBD;
35
36 Send::Send (Session& s, Placement p)
37         : Redirect (s, s.next_send_name(), p)
38 {
39         _metering = false;
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
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         RedirectCreated (this); /* EMIT SIGNAL */
60 }
61
62 Send::~Send ()
63 {
64         GoingAway ();
65 }
66
67 XMLNode&
68 Send::get_state(void)
69 {
70         return state (true);
71 }
72
73 XMLNode&
74 Send::state(bool full)
75 {
76         XMLNode *node = new XMLNode("Send");
77         node->add_child_nocopy (Redirect::state (full));
78         return *node;
79 }
80
81 int
82 Send::set_state(const XMLNode& node)
83 {
84         XMLNodeList nlist = node.children();
85         XMLNodeIterator niter;
86
87         /* Send has regular IO automation (gain, pan) */
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                 } else if ((*niter)->name() == X_("Automation")) {
94                         IO::set_automation_state (*(*niter));
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 (BufferSet& bufs, nframes_t start_frame, nframes_t end_frame, nframes_t nframes, 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                 BufferSet& sendbufs = _session.get_send_buffers(bufs.count());
115
116                 sendbufs.read_from(bufs, nframes);
117                 assert(sendbufs.count() == bufs.count());
118
119                 IO::deliver_output (sendbufs, start_frame, end_frame, nframes, offset);
120
121                 if (_metering) {
122                         if (_gain == 0) {
123                                 _meter->reset();
124                         } else {
125                                 _meter->run(output_buffers(), nframes, offset);
126                         }
127                 }
128
129         } else {
130                 silence (nframes, offset);
131                 
132                 if (_metering) {
133                         _meter->reset();
134                 }
135         }
136 }
137
138 void
139 Send::set_metering (bool yn)
140 {
141         _metering = yn;
142
143         if (!_metering) {
144                 /* XXX possible thread hazard here */
145                 peak_meter().reset();
146         }
147 }
148
149 void
150 Send::expect_inputs (const ChanCount& expected)
151 {
152         if (expected != _expected_inputs) {
153                 _expected_inputs = expected;
154                 reset_panner ();
155         }
156 }