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