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