Renamed Insert to Processor and Redirect to IOProcessor.
[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 parameter " << al->param_id().to_string() << endl;
126
127         // FIXME: sane default behaviour?
128         _visible_parameter_automation.insert(al->param_id());
129         _can_automate_list.insert(al->param_id());
130 }
131
132 void
133 Automatable::what_has_automation (set<ParamID>& s) const
134 {
135         Glib::Mutex::Lock lm (_automation_lock);
136         map<ParamID,AutomationList*>::const_iterator li;
137         
138         for (li = _parameter_automation.begin(); li != _parameter_automation.end(); ++li) {
139                 s.insert  ((*li).first);
140         }
141 }
142
143 void
144 Automatable::what_has_visible_automation (set<ParamID>& s) const
145 {
146         Glib::Mutex::Lock lm (_automation_lock);
147         set<ParamID>::const_iterator li;
148         
149         for (li = _visible_parameter_automation.begin(); li != _visible_parameter_automation.end(); ++li) {
150                 s.insert  (*li);
151         }
152 }
153
154 /** Returns NULL if we don't have an AutomationList for \a parameter.
155  */
156 AutomationList*
157 Automatable::automation_list (ParamID parameter, bool create_if_missing)
158 {
159         std::map<ParamID,AutomationList*>::iterator i = _parameter_automation.find(parameter);
160
161         if (i != _parameter_automation.end()) {
162                 return i->second;
163
164         } else if (create_if_missing) {
165                 AutomationList* al = new AutomationList (parameter, FLT_MIN, FLT_MAX, default_parameter_value (parameter));
166                 add_automation_parameter(al);
167                 return al;
168
169         } else {
170                 //warning << "AutomationList " << parameter.to_string() << " not found for " << _name << endmsg;
171                 return NULL;
172         }
173 }
174
175 const AutomationList*
176 Automatable::automation_list (ParamID parameter) const
177 {
178         std::map<ParamID,AutomationList*>::const_iterator i = _parameter_automation.find(parameter);
179
180         if (i != _parameter_automation.end()) {
181                 return i->second;
182         } else {
183                 //warning << "AutomationList " << parameter.to_string() << " not found for " << _name << endmsg;
184                 return NULL;
185         }
186 }
187
188
189 string
190 Automatable::describe_parameter (ParamID param)
191 {
192         /* derived classes like PluginInsert should override this */
193
194         if (param == ParamID(GainAutomation))
195                 return _("Fader");
196         else if (param == ParamID(PanAutomation))
197                 return _("Pan");
198         else if (param.type() == MidiCCAutomation)
199                 return string_compose("MIDI CC %1", param.id());
200         else
201                 return param.to_string();
202 }
203
204 void
205 Automatable::can_automate (ParamID what)
206 {
207         _can_automate_list.insert (what);
208 }
209
210 void
211 Automatable::mark_automation_visible (ParamID what, bool yn)
212 {
213         if (yn) {
214                 _visible_parameter_automation.insert (what);
215         } else {
216                 set<ParamID>::iterator i;
217
218                 if ((i = _visible_parameter_automation.find (what)) != _visible_parameter_automation.end()) {
219                         _visible_parameter_automation.erase (i);
220                 }
221         }
222 }
223
224 bool
225 Automatable::find_next_event (nframes_t now, nframes_t end, ControlEvent& next_event) const
226 {
227         map<ParamID,AutomationList*>::const_iterator li;        
228         AutomationList::TimeComparator cmp;
229
230         next_event.when = max_frames;
231         
232         for (li = _parameter_automation.begin(); li != _parameter_automation.end(); ++li) {
233                 
234                 AutomationList::const_iterator i;
235                 const AutomationList& alist (*((*li).second));
236                 ControlEvent cp (now, 0.0f);
237                 
238                 for (i = lower_bound (alist.const_begin(), alist.const_end(), &cp, cmp); i != alist.const_end() && (*i)->when < end; ++i) {
239                         if ((*i)->when > now) {
240                                 break; 
241                         }
242                 }
243                 
244                 if (i != alist.const_end() && (*i)->when < end) {
245                         
246                         if ((*i)->when < next_event.when) {
247                                 next_event.when = (*i)->when;
248                         }
249                 }
250         }
251
252         return next_event.when != max_frames;
253 }
254
255 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
256  * had a single automation parameter, with it's type implicit.  Derived objects should
257  * pass that type and it will be used for the untyped AutomationList found.
258  */
259 int
260 Automatable::set_automation_state (const XMLNode& node, ParamID legacy_param)
261 {       
262         Glib::Mutex::Lock lm (_automation_lock);
263
264         _parameter_automation.clear ();
265         _visible_parameter_automation.clear ();
266
267         XMLNodeList nlist = node.children();
268         XMLNodeIterator niter;
269         
270         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
271
272                 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, &param) != 1) {
273                   error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
274                   continue;
275                   }*/
276
277                 if ((*niter)->name() == "AutomationList") {
278
279                         const XMLProperty* id_prop = (*niter)->property("automation-id");
280
281                         ParamID param = (id_prop ? ParamID(id_prop->value()) : legacy_param);
282                         
283                         AutomationList* al = new AutomationList(**niter, param);
284                         
285                         if (!id_prop) {
286                                 warning << "AutomationList node without automation-id property, "
287                                         << "using default: " << legacy_param.to_string() << endmsg;
288                                 al->set_param_id(legacy_param);
289                         }
290
291                         add_automation_parameter(al);
292
293                 } else {
294                         error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
295                 }
296         }
297
298         return 0;
299 }
300
301 XMLNode&
302 Automatable::get_automation_state ()
303 {
304         Glib::Mutex::Lock lm (_automation_lock);
305         XMLNode* node = new XMLNode (X_("Automation"));
306         
307         cerr << "'" << _name << "'->get_automation_state, # params = " << _parameter_automation.size() << endl;
308
309         if (_parameter_automation.empty()) {
310                 return *node;
311         }
312
313         map<ParamID,AutomationList*>::iterator li;
314         
315         for (li = _parameter_automation.begin(); li != _parameter_automation.end(); ++li) {
316                 node->add_child_nocopy (li->second->get_state ());
317         }
318
319         return *node;
320 }
321
322 void
323 Automatable::clear_automation ()
324 {
325         Glib::Mutex::Lock lm (_automation_lock);
326
327         map<ParamID,AutomationList*>::iterator li;
328
329         for (li = _parameter_automation.begin(); li != _parameter_automation.end(); ++li)
330                 li->second->clear();
331 }
332         
333 void
334 Automatable::set_parameter_automation_state (ParamID param, AutoState s)
335 {
336         Glib::Mutex::Lock lm (_automation_lock);
337         
338         AutomationList* al = automation_list (param, true);
339
340         if (s != al->automation_state()) {
341                 al->set_automation_state (s);
342                 _session.set_dirty ();
343         }
344 }
345
346 AutoState
347 Automatable::get_parameter_automation_state (ParamID param)
348 {
349         Glib::Mutex::Lock lm (_automation_lock);
350
351         AutomationList* al = automation_list(param);
352
353         if (al) {
354                 return al->automation_state();
355         } else {
356                 return Off;
357         }
358 }
359
360 void
361 Automatable::set_parameter_automation_style (ParamID param, AutoStyle s)
362 {
363         Glib::Mutex::Lock lm (_automation_lock);
364         
365         AutomationList* al = automation_list (param, true);
366
367         if (s != al->automation_style()) {
368                 al->set_automation_style (s);
369                 _session.set_dirty ();
370         }
371 }
372
373 AutoStyle
374 Automatable::get_parameter_automation_style (ParamID param)
375 {
376         Glib::Mutex::Lock lm (_automation_lock);
377
378         AutomationList* al = automation_list(param);
379
380         if (al) {
381                 return al->automation_style();
382         } else {
383                 return Absolute; // whatever
384         }
385 }
386
387 void
388 Automatable::protect_automation ()
389 {
390         set<ParamID> automated_params;
391
392         what_has_automation (automated_params);
393
394         for (set<ParamID>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
395
396                 AutomationList* al = automation_list (*i);
397
398                 switch (al->automation_state()) {
399                 case Write:
400                         al->set_automation_state (Off);
401                         break;
402                 case Touch:
403                         al->set_automation_state (Play);
404                         break;
405                 default:
406                         break;
407                 }
408         }
409 }
410