switch to using boost::signals2 instead of sigc++, at least for libardour. not finish...
[ardour.git] / libs / ardour / port_insert.cc
1 /*
2     Copyright (C) 2000,2007 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 <string>
21
22
23 #include "pbd/failed_constructor.h"
24 #include "pbd/xml++.h"
25
26 #include "ardour/delivery.h"
27 #include "ardour/port_insert.h"
28 #include "ardour/plugin.h"
29 #include "ardour/port.h"
30 #include "ardour/route.h"
31 #include "ardour/buffer_set.h"
32
33 #include "ardour/audioengine.h"
34 #include "ardour/session.h"
35 #include "ardour/types.h"
36
37 #include "i18n.h"
38
39 using namespace std;
40 using namespace ARDOUR;
41 using namespace PBD;
42
43 PortInsert::PortInsert (Session& s, boost::shared_ptr<MuteMaster> mm)
44         : IOProcessor (s, true, true, string_compose (_("insert %1"), (bitslot = s.next_insert_id()) + 1), "")
45         , _out (new Delivery (s, _output, mm, _name, Delivery::Insert))
46 {
47         ProcessorCreated (this); /* EMIT SIGNAL */
48 }
49
50 PortInsert::PortInsert (Session& s, boost::shared_ptr<MuteMaster> mm, const XMLNode& node)
51         : IOProcessor (s, true, true, "unnamed port insert")
52         , _out (new Delivery (s, _output, mm, _name, Delivery::Insert))
53
54 {
55         if (set_state (node, Stateful::loading_state_version)) {
56                 throw failed_constructor();
57         }
58
59         ProcessorCreated (this); /* EMIT SIGNAL */
60 }
61
62 PortInsert::~PortInsert ()
63 {
64         drop_references ();
65 }
66
67 void
68 PortInsert::run (BufferSet& bufs, sframes_t start_frame, sframes_t end_frame, nframes_t nframes, bool)
69 {
70         if (_output->n_ports().n_total() == 0) {
71                 return;
72         }
73
74         if (!_active && !_pending_active) {
75                 /* deliver silence */
76                 silence (nframes);
77                 goto out;
78         }
79
80         _out->run (bufs, start_frame, end_frame, nframes, true);
81         _input->collect_input (bufs, nframes, ChanCount::ZERO);
82
83   out:
84         _active = _pending_active;
85 }
86
87 XMLNode&
88 PortInsert::get_state(void)
89 {
90         return state (true);
91 }
92
93 XMLNode&
94 PortInsert::state (bool full)
95 {
96         XMLNode& node = Processor::state(full);
97         char buf[32];
98         node.add_property ("type", "port");
99         snprintf (buf, sizeof (buf), "%" PRIu32, bitslot);
100         node.add_property ("bitslot", buf);
101
102         return node;
103 }
104
105 int
106 PortInsert::set_state (const XMLNode& node, int version)
107 {
108         XMLNodeList nlist = node.children();
109         XMLNodeIterator niter;
110         XMLPropertyList plist;
111         const XMLProperty *prop;
112
113         if ((prop = node.property ("type")) == 0) {
114                 error << _("XML node describing port insert is missing the `type' field") << endmsg;
115                 return -1;
116         }
117
118         if (prop->value() != "port") {
119                 error << _("non-port insert XML used for port plugin insert") << endmsg;
120                 return -1;
121         }
122
123         if ((prop = node.property ("bitslot")) == 0) {
124                 bitslot = _session.next_insert_id();
125         } else {
126                 sscanf (prop->value().c_str(), "%" PRIu32, &bitslot);
127                 _session.mark_insert_id (bitslot);
128         }
129
130         const XMLNode* insert_node = &node;
131
132         // legacy sessions: search for child IOProcessor node
133         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
134                 if ((*niter)->name() == "IOProcessor") {
135                         insert_node = *niter;
136                         break;
137                 }
138         }
139
140         Processor::set_state (*insert_node, version);
141
142         return 0;
143 }
144
145 ARDOUR::nframes_t
146 PortInsert::signal_latency() const
147 {
148         /* because we deliver and collect within the same cycle,
149            all I/O is necessarily delayed by at least frames_per_cycle().
150
151            if the return port for insert has its own latency, we
152            need to take that into account too.
153         */
154
155         return _session.engine().frames_per_cycle() + _input->signal_latency();
156 }
157
158 bool
159 PortInsert::configure_io (ChanCount in, ChanCount out)
160 {
161         /* for an insert, processor input corresponds to IO output, and vice versa */
162
163         if (_input->ensure_io (in, false, this) != 0) {
164                 return false;
165         }
166
167         if (_output->ensure_io (out, false, this) != 0) {
168                 return false;
169         }
170
171         return Processor::configure_io (in, out);
172 }
173
174 bool
175 PortInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out) const
176 {
177         out = in;
178         return true;
179 }
180
181 bool
182 PortInsert::set_name (const std::string& name)
183 {
184         bool ret = Processor::set_name (name);
185
186         ret = (ret && _input->set_name (name) && _output->set_name (name));
187
188         return ret;
189 }
190
191 void
192 PortInsert::activate ()
193 {
194         IOProcessor::activate ();
195
196         _out->activate ();
197 }
198
199 void
200 PortInsert::deactivate ()
201 {
202         IOProcessor::deactivate ();
203
204         _out->deactivate ();
205 }