use a note tracker to resolve notes cut off during render by the end of the region
[ardour.git] / libs / ardour / processor.cc
1 /*
2  * Copyright (C) 2007-2012 David Robillard <d@drobilla.net>
3  * Copyright (C) 2008-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2016 Tim Mayberry <mojofunk@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #ifdef WAF_BUILD
24 #include "libardour-config.h"
25 #endif
26
27 #include <string>
28
29 #include "pbd/xml++.h"
30 #include "pbd/types_convert.h"
31
32 #include "ardour/automatable.h"
33 #include "ardour/chan_count.h"
34 #include "ardour/debug.h"
35 #include "ardour/processor.h"
36 #include "ardour/types.h"
37
38 #ifdef WINDOWS_VST_SUPPORT
39 #include "ardour/windows_vst_plugin.h"
40 #endif
41
42 #ifdef AUDIOUNIT_SUPPORT
43 #include "ardour/audio_unit.h"
44 #endif
45
46 #include "ardour/audioengine.h"
47 #include "ardour/session.h"
48 #include "ardour/types.h"
49
50 #include "pbd/i18n.h"
51
52 using namespace std;
53 using namespace ARDOUR;
54 using namespace PBD;
55
56 namespace ARDOUR { class Session; }
57
58 // Always saved as Processor, but may be IOProcessor or Send in legacy sessions
59 const string Processor::state_node_name = "Processor";
60
61 Processor::Processor(Session& session, const string& name)
62         : SessionObject(session, name)
63         , Automatable (session)
64         , _pending_active(false)
65         , _active(false)
66         , _next_ab_is_active(false)
67         , _configured(false)
68         , _display_to_user (true)
69         , _pre_fader (false)
70         , _ui_pointer (0)
71         , _window_proxy (0)
72         , _pinmgr_proxy (0)
73         , _owner (0)
74         , _input_latency (0)
75         , _output_latency (0)
76         , _capture_offset (0)
77         , _playback_offset (0)
78         , _loop_location (0)
79 {
80 }
81
82 Processor::Processor (const Processor& other)
83         : Evoral::ControlSet (other)
84         , SessionObject (other.session(), other.name())
85         , Automatable (other.session())
86         , Latent (other)
87         , _pending_active(other._pending_active)
88         , _active(other._active)
89         , _next_ab_is_active(false)
90         , _configured(false)
91         , _display_to_user (true)
92         , _pre_fader (false)
93         , _ui_pointer (0)
94         , _window_proxy (0)
95         , _pinmgr_proxy (0)
96         , _owner (0)
97         , _input_latency (0)
98         , _output_latency (0)
99         , _capture_offset (0)
100         , _playback_offset (0)
101         , _loop_location (other._loop_location)
102 {
103 }
104
105 Processor::~Processor ()
106 {
107         DEBUG_TRACE (DEBUG::Destruction, string_compose ("processor %1 destructor\n", _name));
108 }
109
110 XMLNode&
111 Processor::get_state (void)
112 {
113         return state ();
114 }
115
116 /* NODE STRUCTURE
117
118     <Automation [optionally with visible="...." ]>
119        <parameter-N>
120          <AutomationList id=N>
121            <events>
122            X1 Y1
123            X2 Y2
124            ....
125            </events>
126        </parameter-N>
127     <Automation>
128 */
129
130 XMLNode&
131 Processor::state ()
132 {
133         XMLNode* node = new XMLNode (state_node_name);
134
135         node->set_property("id", id());
136         node->set_property("name", name());
137         node->set_property("active", active());
138
139         if (_extra_xml){
140                 node->add_child_copy (*_extra_xml);
141         }
142
143         if (!skip_saving_automation) {
144                 XMLNode& automation = Automatable::get_automation_xml_state();
145                 if (!automation.children().empty() || !automation.properties().empty()) {
146                         node->add_child_nocopy (automation);
147                 } else {
148                         delete &automation;
149                 }
150         }
151
152         Latent::add_state (node);
153
154         return *node;
155 }
156
157 int
158 Processor::set_state_2X (const XMLNode & node, int /*version*/)
159 {
160         XMLProperty const * prop;
161
162         XMLNodeList children = node.children ();
163
164         for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
165
166                 if ((*i)->name() == X_("IO")) {
167
168                         if ((prop = (*i)->property ("name")) != 0) {
169                                 set_name (prop->value ());
170                         }
171
172                         set_id (**i);
173
174                         //note:  in A2, active state was stored in the Redirect node, not the child IO node
175                         /*
176                          * if ((prop = (*i)->property ("active")) != 0) {
177                                 bool const a = string_is_affirmative (prop->value ());
178                                 if (_active != a) {
179                                         if (a) {
180                                                 activate ();
181                                         } else {
182                                                 deactivate ();
183                                         }
184                                 }
185                         }*/
186
187                 }
188         }
189
190         return 0;
191 }
192
193 int
194 Processor::set_state (const XMLNode& node, int version)
195 {
196         if (version < 3000) {
197                 return set_state_2X (node, version);
198         }
199
200         bool ignore_name;
201         // Only testing for the presence of the property not value
202         if (!node.get_property("ignore-name", ignore_name)) {
203                 string name;
204                 // may not exist for legacy 3.0 sessions
205                 if (node.get_property ("name", name)) {
206                         /* don't let derived classes have a crack at set_name,
207                            as some (like Send) will screw with the one we suggest.
208                         */
209                         Processor::set_name (name);
210                 }
211
212                 set_id (node);
213         }
214
215         XMLNodeList nlist = node.children();
216         XMLNodeIterator niter;
217
218         Stateful::save_extra_xml (node);
219
220         XMLProperty const * prop = 0;
221         XMLProperty const * legacy_active = 0;
222
223         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
224
225                 if ((*niter)->name() == X_("Automation")) {
226
227                         if ((prop = (*niter)->property ("path")) != 0) {
228                                 old_set_automation_state (*(*niter));
229                         } else {
230                                 set_automation_xml_state (*(*niter), Evoral::Parameter(PluginAutomation));
231                         }
232
233                 } else if ((*niter)->name() == "Redirect") {
234                         if ( !(legacy_active = (*niter)->property("active"))) {
235                                 error << string_compose(_("No %1 property flag in element %2"), "active", (*niter)->name()) << endl;
236                         }
237                 }
238         }
239
240         if ((prop = node.property ("active")) == 0) {
241                 if (legacy_active) {
242                         prop = legacy_active;
243                 } else {
244                         error << _("No child node with active property") << endmsg;
245                         return -1;
246                 }
247         }
248
249         bool const a = string_to<bool> (prop->value ()) && !_session.get_bypass_all_loaded_plugins();
250         if (_active != a) {
251                 if (a) {
252                         activate ();
253                 } else {
254                         deactivate ();
255                 }
256         }
257
258         Latent::set_state (node, version);
259
260         return 0;
261 }
262
263 /** @pre Caller must hold process lock */
264 bool
265 Processor::configure_io (ChanCount in, ChanCount out)
266 {
267         /* This class assumes 1:1 input:output.static output stream count.
268            Derived classes must override and set _configured_output appropriately
269            if this is not the case
270         */
271
272         _configured_input = in;
273         _configured_output = out;
274         _configured = true;
275
276         ConfigurationChanged (in, out); /* EMIT SIGNAL */
277
278         return true;
279 }
280
281 bool
282 Processor::map_loop_range (samplepos_t& start, samplepos_t& end) const
283 {
284         if (!_loop_location) {
285                 return false;
286         }
287         if (start >= end) {
288                 /* no backwards looping */
289                 return false;
290         }
291
292         const samplepos_t loop_end = _loop_location->end ();
293         if (start < loop_end) {
294                 return false;
295         }
296
297         const samplepos_t loop_start   = _loop_location->start ();
298         const samplecnt_t looplen      = loop_end - loop_start;
299         const sampleoffset_t start_off = (start - loop_start) % looplen;
300         const samplepos_t start_pos    = loop_start + start_off;
301
302         assert (start >= start_pos);
303         end -= start - start_pos;
304         start = start_pos;
305         assert (end > start);
306         return true;
307 }
308
309 void
310 Processor::set_display_to_user (bool yn)
311 {
312         _display_to_user = yn;
313 }
314
315 void
316 Processor::set_pre_fader (bool p)
317 {
318         _pre_fader = p;
319 }
320
321 void
322 Processor::set_owner (SessionObject* o)
323 {
324         _owner = o;
325 }
326
327 SessionObject*
328 Processor::owner() const
329 {
330         return _owner;
331 }