Plugin automation fixes from torbenh.
[ardour.git] / libs / ardour / automatable.cc
1 /*
2     Copyright (C) 2001,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 <ardour/ardour.h>
21 #include <fstream>
22 #include <inttypes.h>
23 #include <cstdio>
24 #include <errno.h>
25 #include <pbd/error.h>
26 #include <pbd/enumwriter.h>
27 #include <midi++/names.h>
28 #include <ardour/session.h>
29 #include <ardour/automatable.h>
30 #include <ardour/midi_track.h>
31 #include <ardour/plugin_insert.h>
32
33 #include "i18n.h"
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 nframes_t Automatable::_automation_interval = 0;
40
41 Automatable::Automatable(Session& session)
42         : _a_session(session)
43         , _last_automation_snapshot(0)
44 {
45 }
46
47 int
48 Automatable::old_set_automation_state (const XMLNode& node)
49 {
50         const XMLProperty *prop;
51                         
52         if ((prop = node.property ("path")) != 0) {
53                 load_automation (prop->value());
54         } else {
55                 warning << _("Automation node has no path property") << endmsg;
56         }
57         
58         if ((prop = node.property ("visible")) != 0) {
59                 uint32_t what;
60                 stringstream sstr;
61                 
62                 _visible_controls.clear ();
63                 
64                 sstr << prop->value();
65                 while (1) {
66                         sstr >> what;
67                         if (sstr.fail()) {
68                                 break;
69                         }
70                         mark_automation_visible (Parameter(PluginAutomation, what), true);
71                 }
72         }
73         
74         _last_automation_snapshot = 0;
75
76         return 0;
77 }
78
79 int
80 Automatable::load_automation (const string& path)
81 {
82         string fullpath;
83
84         if (path[0] == '/') { // legacy
85                 fullpath = path;
86         } else {
87                 fullpath = _a_session.automation_dir();
88                 fullpath += path;
89         }
90         ifstream in (fullpath.c_str());
91
92         if (!in) {
93                 warning << string_compose(_("cannot open %2 to load automation data (%3)")
94                                 , fullpath, strerror (errno)) << endmsg;
95                 return 1;
96         }
97
98         Glib::Mutex::Lock lm (control_lock());
99         set<Parameter> tosave;
100         controls().clear ();
101         
102         _last_automation_snapshot = 0;
103
104         while (in) {
105                 double when;
106                 double value;
107                 uint32_t port;
108
109                 in >> port;  if (!in) break;
110                 in >> when;  if (!in) goto bad;
111                 in >> value; if (!in) goto bad;
112                 
113                 /* FIXME: this is legacy and only used for plugin inserts?  I think? */
114                 boost::shared_ptr<Evoral::Control> c = control (Parameter(PluginAutomation, port), true);
115                 c->list()->add (when, value);
116                 tosave.insert (Parameter(PluginAutomation, port));
117         }
118         
119         return 0;
120
121   bad:
122         error << string_compose(_("cannot load automation data from %2"), fullpath) << endmsg;
123         controls().clear ();
124         return -1;
125 }
126
127 void
128 Automatable::add_control(boost::shared_ptr<Evoral::Control> ac)
129 {
130         Parameter param = ac->parameter();
131         
132         ControlSet::add_control(ac);
133         _can_automate_list.insert(param);
134         auto_state_changed(param); // sync everything up
135 }
136
137 void
138 Automatable::what_has_visible_data(set<Parameter>& s) const
139 {
140         Glib::Mutex::Lock lm (control_lock());
141         set<Parameter>::const_iterator li;
142         
143         for (li = _visible_controls.begin(); li != _visible_controls.end(); ++li) {
144                 s.insert  (*li);
145         }
146 }
147
148 string
149 Automatable::describe_parameter (Parameter param)
150 {
151         /* derived classes like PluginInsert should override this */
152
153         if (param == Parameter(GainAutomation)) {
154                 return _("Fader");
155         } else if (param.type() == PanAutomation) {
156                 /* ID's are zero-based, present them as 1-based */
157                 return (string_compose(_("Pan %1"), param.id() + 1));
158         } else if (param.type() == MidiCCAutomation) {
159                 return string_compose("CC %1 (%2) [%3]",
160                                 param.id() + 1, midi_name(param.id()), int(param.channel()) + 1);                       
161         } else if (param.type() == MidiPgmChangeAutomation) {
162                 return string_compose("Program [%1]", int(param.channel()) + 1);
163         } else if (param.type() == MidiPitchBenderAutomation) {
164                 return string_compose("Bender [%1]", int(param.channel()) + 1);
165         } else if (param.type() == MidiChannelPressureAutomation) {
166                 return string_compose("Pressure [%1]", int(param.channel()) + 1);
167         } else {
168                 return param.symbol();
169         }
170 }
171
172 void
173 Automatable::can_automate (Parameter what)
174 {
175         _can_automate_list.insert (what);
176 }
177
178 void
179 Automatable::mark_automation_visible (Parameter what, bool yn)
180 {
181         if (yn) {
182                 _visible_controls.insert (what);
183         } else {
184                 set<Parameter>::iterator i;
185
186                 if ((i = _visible_controls.find (what)) != _visible_controls.end()) {
187                         _visible_controls.erase (i);
188                 }
189         }
190 }
191
192 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
193  * had a single automation parameter, with it's type implicit.  Derived objects should
194  * pass that type and it will be used for the untyped AutomationList found.
195  */
196 int
197 Automatable::set_automation_state (const XMLNode& node, Parameter legacy_param)
198 {       
199         Glib::Mutex::Lock lm (control_lock());
200
201         /* Don't clear controls, since some may be special derived Controllable classes */
202
203         _visible_controls.clear ();
204
205         XMLNodeList nlist = node.children();
206         XMLNodeIterator niter;
207         
208         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
209
210                 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, &param) != 1) {
211                   error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
212                   continue;
213                   }*/
214
215                 if ((*niter)->name() == "AutomationList") {
216
217                         const XMLProperty* id_prop = (*niter)->property("automation-id");
218
219                         Parameter param = (id_prop ? Parameter(id_prop->value()) : legacy_param);
220                         if (param.type() == NullAutomation) {
221                                 warning << "Automation has null type" << endl;
222                                 continue;
223                         }
224                         
225                         boost::shared_ptr<AutomationList> al (new AutomationList(**niter, param));
226                         
227                         if (!id_prop) {
228                                 warning << "AutomationList node without automation-id property, "
229                                         << "using default: " << legacy_param.symbol() << endmsg;
230                         }
231
232                         boost::shared_ptr<Evoral::Control> existing = control(param);
233                         if (existing)
234                                 existing->set_list(al);
235                         else
236                                 add_control(control_factory(param));
237
238                 } else {
239                         error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
240                 }
241         }
242
243         _last_automation_snapshot = 0;
244
245         return 0;
246 }
247
248 XMLNode&
249 Automatable::get_automation_state ()
250 {
251         Glib::Mutex::Lock lm (control_lock());
252         XMLNode* node = new XMLNode (X_("Automation"));
253         
254         if (controls().empty()) {
255                 return *node;
256         }
257
258         for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
259                 boost::shared_ptr<AutomationList> l
260                                 = boost::dynamic_pointer_cast<AutomationList>(li->second->list());
261                 node->add_child_nocopy (l->get_state ());
262         }
263
264         return *node;
265 }
266
267 void
268 Automatable::set_parameter_automation_state (Parameter param, AutoState s)
269 {
270         Glib::Mutex::Lock lm (control_lock());
271         
272         boost::shared_ptr<Evoral::Control> c = control (param, true);
273         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
274
275         if (s != l->automation_state()) {
276                 l->set_automation_state (s);
277                 _a_session.set_dirty ();
278         }
279 }
280
281 AutoState
282 Automatable::get_parameter_automation_state (Parameter param, bool lock)
283 {
284         AutoState result = Off;
285
286         if (lock)
287                 control_lock().lock();
288
289         boost::shared_ptr<Evoral::Control> c = control(param);
290         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
291
292         if (c)
293                 result = l->automation_state();
294         
295         if (lock)
296                 control_lock().unlock();
297
298         return result;
299 }
300
301 void
302 Automatable::set_parameter_automation_style (Parameter param, AutoStyle s)
303 {
304         Glib::Mutex::Lock lm (control_lock());
305         
306         boost::shared_ptr<Evoral::Control> c = control(param, true);
307         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
308
309         if (s != l->automation_style()) {
310                 l->set_automation_style (s);
311                 _a_session.set_dirty ();
312         }
313 }
314
315 AutoStyle
316 Automatable::get_parameter_automation_style (Parameter param)
317 {
318         Glib::Mutex::Lock lm (control_lock());
319
320         boost::shared_ptr<Evoral::Control> c = control(param);
321         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
322
323         if (c) {
324                 return l->automation_style();
325         } else {
326                 return Absolute; // whatever
327         }
328 }
329
330 void
331 Automatable::protect_automation ()
332 {
333         typedef set<Evoral::Parameter> ParameterSet;
334         ParameterSet automated_params;
335
336         what_has_data(automated_params);
337
338         for (ParameterSet::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
339
340                 boost::shared_ptr<Evoral::Control> c = control(*i);
341                 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
342
343                 switch (l->automation_state()) {
344                 case Write:
345                         l->set_automation_state (Off);
346                         break;
347                 case Touch:
348                         l->set_automation_state (Play);
349                         break;
350                 default:
351                         break;
352                 }
353         }
354 }
355
356 void
357 Automatable::automation_snapshot (nframes_t now, bool force)
358 {
359         if (force || _last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
360
361                 for (Controls::iterator i = controls().begin(); i != controls().end(); ++i) {
362                         boost::shared_ptr<AutomationControl> c
363                                         = boost::dynamic_pointer_cast<AutomationControl>(i->second);
364                         if (c->automation_write()) {
365                                 c->list()->rt_add (now, i->second->user_float());
366                         }
367                 }
368                 
369                 _last_automation_snapshot = now;
370         }
371 }
372
373 void
374 Automatable::transport_stopped (nframes_t now)
375 {
376         for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
377                 
378                 boost::shared_ptr<AutomationControl> c
379                                 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
380                 boost::shared_ptr<AutomationList> l
381                                 = boost::dynamic_pointer_cast<AutomationList>(c->list());
382                 
383                 c->list()->reposition_for_rt_add (now);
384
385                 if (c->automation_state() != Off) {
386                         c->set_value(c->list()->eval(now));
387                 }
388         }
389 }
390
391 boost::shared_ptr<Evoral::Control>
392 Automatable::control_factory(const Evoral::Parameter& param)
393 {
394         boost::shared_ptr<AutomationList> list(new AutomationList(param));
395         Evoral::Control* control = NULL;
396         if (param.type() >= MidiCCAutomation && param.type() <= MidiChannelPressureAutomation) {
397                 control = new MidiTrack::MidiControl((MidiTrack*)this, param);
398         } else if (param.type() == PluginAutomation) {
399                 control = new PluginInsert::PluginControl((PluginInsert*)this, param);
400         } else {
401                 control = new AutomationControl(_a_session, param);
402         }
403         control->set_list(list);
404         return boost::shared_ptr<Evoral::Control>(control);
405 }
406