a4d0de0958d72965afaef9e410d0461042cf6cbe
[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 (Evoral::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<Evoral::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                 Evoral::Parameter param(PluginAutomation, port);
114                 /* FIXME: this is legacy and only used for plugin inserts?  I think? */
115                 boost::shared_ptr<Evoral::Control> c = control (param, true);
116                 c->list()->add (when, value);
117                 tosave.insert (param);
118         }
119         
120         return 0;
121
122   bad:
123         error << string_compose(_("cannot load automation data from %2"), fullpath) << endmsg;
124         controls().clear ();
125         return -1;
126 }
127
128 void
129 Automatable::add_control(boost::shared_ptr<Evoral::Control> ac)
130 {
131         Evoral::Parameter param = ac->parameter();
132         
133         ControlSet::add_control(ac);
134         _can_automate_list.insert(param);
135         auto_state_changed(param); // sync everything up
136 }
137
138 void
139 Automatable::what_has_visible_data(set<Evoral::Parameter>& s) const
140 {
141         Glib::Mutex::Lock lm (control_lock());
142         set<Evoral::Parameter>::const_iterator li;
143         
144         for (li = _visible_controls.begin(); li != _visible_controls.end(); ++li) {
145                 s.insert  (*li);
146         }
147 }
148
149 string
150 Automatable::describe_parameter (Evoral::Parameter param)
151 {
152         /* derived classes like PluginInsert should override this */
153
154         if (param == Evoral::Parameter(GainAutomation)) {
155                 return _("Fader");
156         } else if (param.type() == PanAutomation) {
157                 /* ID's are zero-based, present them as 1-based */
158                 return (string_compose(_("Pan %1"), param.id() + 1));
159         } else if (param.type() == MidiCCAutomation) {
160                 return string_compose("CC %1 (%2) [%3]",
161                                 param.id() + 1, midi_name(param.id()), int(param.channel()) + 1);                       
162         } else if (param.type() == MidiPgmChangeAutomation) {
163                 return string_compose("Program [%1]", int(param.channel()) + 1);
164         } else if (param.type() == MidiPitchBenderAutomation) {
165                 return string_compose("Bender [%1]", int(param.channel()) + 1);
166         } else if (param.type() == MidiChannelPressureAutomation) {
167                 return string_compose("Pressure [%1]", int(param.channel()) + 1);
168         } else {
169                 return EventTypeMap::instance().to_symbol(param);
170         }
171 }
172
173 void
174 Automatable::can_automate (Evoral::Parameter what)
175 {
176         _can_automate_list.insert (what);
177 }
178
179 void
180 Automatable::mark_automation_visible (Evoral::Parameter what, bool yn)
181 {
182         if (yn) {
183                 _visible_controls.insert (what);
184         } else {
185                 set<Evoral::Parameter>::iterator i;
186
187                 if ((i = _visible_controls.find (what)) != _visible_controls.end()) {
188                         _visible_controls.erase (i);
189                 }
190         }
191 }
192
193 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
194  * had a single automation parameter, with it's type implicit.  Derived objects should
195  * pass that type and it will be used for the untyped AutomationList found.
196  */
197 int
198 Automatable::set_automation_state (const XMLNode& node, Evoral::Parameter legacy_param)
199 {       
200         Glib::Mutex::Lock lm (control_lock());
201
202         /* Don't clear controls, since some may be special derived Controllable classes */
203
204         _visible_controls.clear ();
205
206         XMLNodeList nlist = node.children();
207         XMLNodeIterator niter;
208         
209         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
210
211                 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, &param) != 1) {
212                   error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
213                   continue;
214                   }*/
215
216                 if ((*niter)->name() == "AutomationList") {
217
218                         const XMLProperty* id_prop = (*niter)->property("automation-id");
219
220                         Evoral::Parameter param = (id_prop
221                                         ? EventTypeMap::instance().new_parameter(id_prop->value())
222                                         : legacy_param);
223
224                         if (param.type() == NullAutomation) {
225                                 warning << "Automation has null type" << endl;
226                                 continue;
227                         }
228                         
229                         boost::shared_ptr<AutomationList> al (new AutomationList(**niter, param));
230                         
231                         if (!id_prop) {
232                                 warning << "AutomationList node without automation-id property, "
233                                         << "using default: " << EventTypeMap::instance().to_symbol(legacy_param) << endmsg;
234                         }
235
236                         boost::shared_ptr<Evoral::Control> existing = control(param);
237                         if (existing)
238                                 existing->set_list(al);
239                         else
240                                 add_control(control_factory(param));
241
242                 } else {
243                         error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
244                 }
245         }
246
247         _last_automation_snapshot = 0;
248
249         return 0;
250 }
251
252 XMLNode&
253 Automatable::get_automation_state ()
254 {
255         Glib::Mutex::Lock lm (control_lock());
256         XMLNode* node = new XMLNode (X_("Automation"));
257         
258         if (controls().empty()) {
259                 return *node;
260         }
261
262         for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
263                 boost::shared_ptr<AutomationList> l
264                                 = boost::dynamic_pointer_cast<AutomationList>(li->second->list());
265                 node->add_child_nocopy (l->get_state ());
266         }
267
268         return *node;
269 }
270
271 void
272 Automatable::set_parameter_automation_state (Evoral::Parameter param, AutoState s)
273 {
274         Glib::Mutex::Lock lm (control_lock());
275         
276         boost::shared_ptr<Evoral::Control> c = control (param, true);
277         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
278
279         if (s != l->automation_state()) {
280                 l->set_automation_state (s);
281                 _a_session.set_dirty ();
282         }
283 }
284
285 AutoState
286 Automatable::get_parameter_automation_state (Evoral::Parameter param, bool lock)
287 {
288         AutoState result = Off;
289
290         if (lock)
291                 control_lock().lock();
292
293         boost::shared_ptr<Evoral::Control> c = control(param);
294         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
295
296         if (c)
297                 result = l->automation_state();
298         
299         if (lock)
300                 control_lock().unlock();
301
302         return result;
303 }
304
305 void
306 Automatable::set_parameter_automation_style (Evoral::Parameter param, AutoStyle s)
307 {
308         Glib::Mutex::Lock lm (control_lock());
309         
310         boost::shared_ptr<Evoral::Control> c = control(param, true);
311         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
312
313         if (s != l->automation_style()) {
314                 l->set_automation_style (s);
315                 _a_session.set_dirty ();
316         }
317 }
318
319 AutoStyle
320 Automatable::get_parameter_automation_style (Evoral::Parameter param)
321 {
322         Glib::Mutex::Lock lm (control_lock());
323
324         boost::shared_ptr<Evoral::Control> c = control(param);
325         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
326
327         if (c) {
328                 return l->automation_style();
329         } else {
330                 return Absolute; // whatever
331         }
332 }
333
334 void
335 Automatable::protect_automation ()
336 {
337         typedef set<Evoral::Parameter> ParameterSet;
338         ParameterSet automated_params;
339
340         what_has_data(automated_params);
341
342         for (ParameterSet::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
343
344                 boost::shared_ptr<Evoral::Control> c = control(*i);
345                 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
346
347                 switch (l->automation_state()) {
348                 case Write:
349                         l->set_automation_state (Off);
350                         break;
351                 case Touch:
352                         l->set_automation_state (Play);
353                         break;
354                 default:
355                         break;
356                 }
357         }
358 }
359
360 void
361 Automatable::automation_snapshot (nframes_t now, bool force)
362 {
363         if (force || _last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
364
365                 for (Controls::iterator i = controls().begin(); i != controls().end(); ++i) {
366                         boost::shared_ptr<AutomationControl> c
367                                         = boost::dynamic_pointer_cast<AutomationControl>(i->second);
368                         if (c->automation_write()) {
369                                 c->list()->rt_add (now, i->second->user_float());
370                         }
371                 }
372                 
373                 _last_automation_snapshot = now;
374         }
375 }
376
377 void
378 Automatable::transport_stopped (nframes_t now)
379 {
380         for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
381                 
382                 boost::shared_ptr<AutomationControl> c
383                                 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
384                 boost::shared_ptr<AutomationList> l
385                                 = boost::dynamic_pointer_cast<AutomationList>(c->list());
386                 
387                 c->list()->reposition_for_rt_add (now);
388
389                 if (c->automation_state() != Off) {
390                         c->set_value(c->list()->eval(now));
391                 }
392         }
393 }
394
395 boost::shared_ptr<Evoral::Control>
396 Automatable::control_factory(const Evoral::Parameter& param)
397 {
398         boost::shared_ptr<AutomationList> list(new AutomationList(param));
399         Evoral::Control* control = NULL;
400         if (param.type() >= MidiCCAutomation && param.type() <= MidiChannelPressureAutomation) {
401                 control = new MidiTrack::MidiControl((MidiTrack*)this, param);
402         } else if (param.type() == PluginAutomation) {
403                 control = new PluginInsert::PluginControl((PluginInsert*)this, param);
404         } else {
405                 control = new AutomationControl(_a_session, param);
406         }
407         control->set_list(list);
408         return boost::shared_ptr<Evoral::Control>(control);
409 }
410