make Gtkmm2ext::Popup::touch() thread-safe (again; it used to be in the 0.99 era)
[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 */
19
20 #include <algorithm>
21
22 #include <pbd/xml++.h>
23
24 #include <ardour/send.h>
25 #include <ardour/session.h>
26 #include <ardour/port.h>
27
28 #include "i18n.h"
29
30 using namespace ARDOUR;
31 using namespace PBD;
32
33 Send::Send (Session& s, Placement p)
34         : Redirect (s, string_compose (_("send %1"), (bitslot = s.next_send_id()) + 1), p)
35 {
36         _metering = false;
37         expected_inputs = 0;
38         RedirectCreated (this); /* EMIT SIGNAL */
39 }
40
41 Send::Send (Session& s, const XMLNode& node)
42         : Redirect (s,  "send", PreFader)
43 {
44         _metering = false;
45         expected_inputs = 0;
46
47         bitslot = 0xffffffff;
48
49         if (set_state (node)) {
50                 throw failed_constructor();
51         }
52
53         RedirectCreated (this); /* EMIT SIGNAL */
54 }
55
56 Send::Send (const Send& other)
57         : Redirect (other._session, string_compose (_("send %1"), (bitslot = other._session.next_send_id()) + 1), other.placement())
58 {
59         _metering = false;
60         expected_inputs = 0;
61
62         /* set up the same outputs, and connect them to the same places */
63         for (uint32_t i = 0; i < other.n_outputs (); ++i) {
64                 add_output_port ("", 0);
65                 Port* p = other.output (i);
66                 if (p) {
67                         /* this is what the other send's output is connected to */
68                         const char **connections = p->get_connections ();
69                         if (connections) {
70                                 for (uint32_t c = 0; connections[c]; ++c) {
71                                         connect_output (output (i), connections [c], 0);
72                                 }
73                         }
74                 }
75         }
76
77         if (other.active()) {
78                 set_active (true, this);
79         }
80
81         RedirectCreated (this); /* EMIT SIGNAL */
82 }
83
84 Send::~Send ()
85 {
86         GoingAway ();
87 }
88
89 XMLNode&
90 Send::get_state(void)
91 {
92         return state (true);
93 }
94
95 XMLNode&
96 Send::state(bool full)
97 {
98         XMLNode *node = new XMLNode("Send");
99         char buf[32];
100         node->add_child_nocopy (Redirect::state (full));
101         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
102         node->add_property ("bitslot", buf);
103         return *node;
104 }
105
106 int
107 Send::set_state(const XMLNode& node)
108 {
109         XMLNodeList nlist = node.children();
110         XMLNodeIterator niter;
111         const XMLProperty* prop;
112
113         if ((prop = node.property ("bitslot")) == 0) {
114                 bitslot = _session.next_send_id();
115         } else {
116                 uint32_t old_bitslot = bitslot;
117                 sscanf (prop->value().c_str(), "%" PRIu32, &bitslot);
118
119                 if (bitslot != old_bitslot) {
120                         _session.mark_send_id (bitslot);
121                 }
122         }
123
124         /* Send has regular IO automation (gain, pan) */
125
126         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
127                 if ((*niter)->name() == Redirect::state_node_name) {
128                         Redirect::set_state (**niter);
129                         break;
130                 } else if ((*niter)->name() == X_("Automation")) {
131                         IO::set_automation_state (*(*niter));
132                 }
133         }
134
135         if (niter == nlist.end()) {
136                 error << _("XML node describing a send is missing a Redirect node") << endmsg;
137                 return -1;
138         }
139
140         return 0;
141 }
142
143 void
144 Send::run (vector<Sample *>& bufs, uint32_t nbufs, nframes_t nframes, nframes_t offset)
145 {
146         if (active()) {
147
148                 // we have to copy the input, because IO::deliver_output may alter the buffers
149                 // in-place, which a send must never do.
150
151                 vector<Sample*>& sendbufs = _session.get_send_buffers();
152
153                 for (size_t i=0; i < nbufs; ++i) {
154                         memcpy (sendbufs[i], bufs[i], sizeof (Sample) * nframes);
155                 }
156                 
157                 
158                 IO::deliver_output (sendbufs, nbufs, nframes, offset);
159
160                 if (_metering) {
161                         uint32_t n;
162                         uint32_t no = n_outputs();
163
164                         if (_gain == 0) {
165
166                                 for (n = 0; n < no; ++n) {
167                                         _peak_power[n] = 0;
168                                 } 
169
170                         } else {
171
172                                 for (n = 0; n < no; ++n) {
173                                         _peak_power[n] = Session::compute_peak (output(n)->get_buffer(nframes) + offset, nframes, _peak_power[n]);
174                                 }
175                         }
176                 }
177
178         } else {
179                 silence (nframes, offset);
180                 
181                 if (_metering) {
182                         uint32_t n;
183                         uint32_t no = n_outputs();
184
185                         for (n = 0; n < no; ++n) {
186                                 _peak_power[n] = 0;
187                         } 
188                 }
189         }
190 }
191
192 void
193 Send::set_metering (bool yn)
194 {
195         _metering = yn;
196
197         if (!_metering) {
198                 /* XXX possible thread hazard here */
199                 reset_peak_meters ();
200         }
201 }
202
203 void
204 Send::expect_inputs (uint32_t expected)
205 {
206         if (expected != expected_inputs) {
207                 expected_inputs = expected;
208                 reset_panner ();
209         }
210 }