Note modes: note, percussion.
[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 #include <ardour/midi_track.h>
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace ARDOUR;
35 using namespace PBD;
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_controls.clear ();
58                 
59                 sstr << prop->value();
60                 while (1) {
61                         sstr >> what;
62                         if (sstr.fail()) {
63                                 break;
64                         }
65                         mark_automation_visible (Parameter(PluginAutomation, what), true);
66                 }
67         }
68         
69         _last_automation_snapshot = 0;
70
71         return 0;
72 }
73
74 int
75 Automatable::load_automation (const string& path)
76 {
77         string fullpath;
78
79         if (path[0] == '/') { // legacy
80                 fullpath = path;
81         } else {
82                 fullpath = _session.automation_dir();
83                 fullpath += path;
84         }
85         ifstream in (fullpath.c_str());
86
87         if (!in) {
88                 warning << string_compose(_("%1: cannot open %2 to load automation data (%3)"), _name, fullpath, strerror (errno)) << endmsg;
89                 return 1;
90         }
91
92         Glib::Mutex::Lock lm (_automation_lock);
93         set<Parameter> tosave;
94         _controls.clear ();
95         
96         _last_automation_snapshot = 0;
97
98         while (in) {
99                 double when;
100                 double value;
101                 uint32_t port;
102
103                 in >> port;  if (!in) break;
104                 in >> when;  if (!in) goto bad;
105                 in >> value; if (!in) goto bad;
106                 
107                 /* FIXME: this is legacy and only used for plugin inserts?  I think? */
108                 boost::shared_ptr<AutomationControl> c = control (Parameter(PluginAutomation, port), true);
109                 c->list()->add (when, value);
110                 tosave.insert (Parameter(PluginAutomation, port));
111         }
112         
113         return 0;
114
115   bad:
116         error << string_compose(_("%1: cannot load automation data from %2"), _name, fullpath) << endmsg;
117         _controls.clear ();
118         return -1;
119 }
120
121 void
122 Automatable::add_control(boost::shared_ptr<AutomationControl> ac)
123 {
124         Parameter param = ac->parameter();
125
126         _controls[param] = ac;
127         
128         cerr << _name << ": added parameter " << param.to_string() << endl;
129
130         _can_automate_list.insert(param);
131
132         // Sync everything (derived classes) up to initial values
133         auto_state_changed(param);
134 }
135
136 void
137 Automatable::what_has_automation (set<Parameter>& s) const
138 {
139         Glib::Mutex::Lock lm (_automation_lock);
140         Controls::const_iterator li;
141         
142         // FIXME: correct semantics?
143         for (li = _controls.begin(); li != _controls.end(); ++li) {
144                 s.insert  ((*li).first);
145         }
146 }
147
148 void
149 Automatable::what_has_visible_automation (set<Parameter>& s) const
150 {
151         Glib::Mutex::Lock lm (_automation_lock);
152         set<Parameter>::const_iterator li;
153         
154         for (li = _visible_controls.begin(); li != _visible_controls.end(); ++li) {
155                 s.insert  (*li);
156         }
157 }
158
159 /** Returns NULL if we don't have an AutomationList for \a parameter.
160  */
161 boost::shared_ptr<AutomationControl>
162 Automatable::control (Parameter parameter, bool create_if_missing)
163 {
164         Controls::iterator i = _controls.find(parameter);
165
166         if (i != _controls.end()) {
167                 return i->second;
168
169         } else if (create_if_missing) {
170                 boost::shared_ptr<AutomationList> al (new AutomationList (
171                                         parameter, FLT_MIN, FLT_MAX, default_parameter_value (parameter)));
172                 boost::shared_ptr<AutomationControl> ac(control_factory(al));
173                 add_control(ac);
174                 return ac;
175
176         } else {
177                 //warning << "AutomationList " << parameter.to_string() << " not found for " << _name << endmsg;
178                 return boost::shared_ptr<AutomationControl>();
179         }
180 }
181
182 boost::shared_ptr<const AutomationControl>
183 Automatable::control (Parameter parameter) const
184 {
185         Controls::const_iterator i = _controls.find(parameter);
186
187         if (i != _controls.end()) {
188                 return i->second;
189         } else {
190                 //warning << "AutomationList " << parameter.to_string() << " not found for " << _name << endmsg;
191                 return boost::shared_ptr<AutomationControl>();
192         }
193 }
194
195
196 string
197 Automatable::describe_parameter (Parameter param)
198 {
199         /* derived classes like PluginInsert should override this */
200
201         if (param == Parameter(GainAutomation))
202                 return _("Fader");
203         else if (param.type() == PanAutomation)
204                 return (string_compose(_("Pan %1"), param.id()));
205         else if (param.type() == MidiCCAutomation)
206                 return string_compose("CC %1", param.id());
207         else
208                 return param.to_string();
209 }
210
211 void
212 Automatable::can_automate (Parameter what)
213 {
214         _can_automate_list.insert (what);
215 }
216
217 void
218 Automatable::mark_automation_visible (Parameter what, bool yn)
219 {
220         if (yn) {
221                 _visible_controls.insert (what);
222         } else {
223                 set<Parameter>::iterator i;
224
225                 if ((i = _visible_controls.find (what)) != _visible_controls.end()) {
226                         _visible_controls.erase (i);
227                 }
228         }
229 }
230
231 bool
232 Automatable::find_next_event (nframes_t now, nframes_t end, ControlEvent& next_event) const
233 {
234         Controls::const_iterator li;    
235         AutomationList::TimeComparator cmp;
236
237         next_event.when = max_frames;
238         
239         for (li = _controls.begin(); li != _controls.end(); ++li) {
240                 
241                 AutomationList::const_iterator i;
242                 boost::shared_ptr<const AutomationList> alist (li->second->list());
243                 ControlEvent cp (now, 0.0f);
244                 
245                 for (i = lower_bound (alist->const_begin(), alist->const_end(), &cp, cmp); i != alist->const_end() && (*i)->when < end; ++i) {
246                         if ((*i)->when > now) {
247                                 break; 
248                         }
249                 }
250                 
251                 if (i != alist->const_end() && (*i)->when < end) {
252                         
253                         if ((*i)->when < next_event.when) {
254                                 next_event.when = (*i)->when;
255                         }
256                 }
257         }
258
259         return next_event.when != max_frames;
260 }
261
262 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
263  * had a single automation parameter, with it's type implicit.  Derived objects should
264  * pass that type and it will be used for the untyped AutomationList found.
265  */
266 int
267 Automatable::set_automation_state (const XMLNode& node, Parameter legacy_param)
268 {       
269         Glib::Mutex::Lock lm (_automation_lock);
270
271         /* Don't clear controls, since some may be special derived Controllable classes */
272
273         _visible_controls.clear ();
274
275         XMLNodeList nlist = node.children();
276         XMLNodeIterator niter;
277         
278         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
279
280                 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, &param) != 1) {
281                   error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
282                   continue;
283                   }*/
284
285                 if ((*niter)->name() == "AutomationList") {
286
287                         const XMLProperty* id_prop = (*niter)->property("automation-id");
288
289                         Parameter param = (id_prop ? Parameter(id_prop->value()) : legacy_param);
290                         
291                         boost::shared_ptr<AutomationList> al (new AutomationList(**niter, param));
292                         
293                         if (!id_prop) {
294                                 warning << "AutomationList node without automation-id property, "
295                                         << "using default: " << legacy_param.to_string() << endmsg;
296                                 al->set_parameter(legacy_param);
297                         }
298
299                         boost::shared_ptr<AutomationControl> existing = control(param);
300                         if (existing)
301                                 existing->set_list(al);
302                         else
303                                 add_control(control_factory(al));
304
305                 } else {
306                         error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
307                 }
308         }
309
310         _last_automation_snapshot = 0;
311
312         return 0;
313 }
314
315 XMLNode&
316 Automatable::get_automation_state ()
317 {
318         Glib::Mutex::Lock lm (_automation_lock);
319         XMLNode* node = new XMLNode (X_("Automation"));
320         
321         if (_controls.empty()) {
322                 return *node;
323         }
324
325         for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li) {
326                 node->add_child_nocopy (li->second->list()->get_state ());
327         }
328
329         return *node;
330 }
331
332 void
333 Automatable::clear_automation ()
334 {
335         Glib::Mutex::Lock lm (_automation_lock);
336
337         for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
338                 li->second->list()->clear();
339 }
340         
341 void
342 Automatable::set_parameter_automation_state (Parameter param, AutoState s)
343 {
344         Glib::Mutex::Lock lm (_automation_lock);
345         
346         boost::shared_ptr<AutomationControl> c = control (param, true);
347
348         if (s != c->list()->automation_state()) {
349                 c->list()->set_automation_state (s);
350                 _session.set_dirty ();
351         }
352 }
353
354 AutoState
355 Automatable::get_parameter_automation_state (Parameter param, bool lock)
356 {
357         AutoState result = Off;
358
359         if (lock)
360                 _automation_lock.lock();
361
362         boost::shared_ptr<AutomationControl> c = control(param);
363
364         if (c)
365                 result = c->list()->automation_state();
366         
367         if (lock)
368                 _automation_lock.unlock();
369
370         return result;
371 }
372
373 void
374 Automatable::set_parameter_automation_style (Parameter param, AutoStyle s)
375 {
376         Glib::Mutex::Lock lm (_automation_lock);
377         
378         boost::shared_ptr<AutomationControl> c = control(param, true);
379
380         if (s != c->list()->automation_style()) {
381                 c->list()->set_automation_style (s);
382                 _session.set_dirty ();
383         }
384 }
385
386 AutoStyle
387 Automatable::get_parameter_automation_style (Parameter param)
388 {
389         Glib::Mutex::Lock lm (_automation_lock);
390
391         boost::shared_ptr<AutomationControl> c = control(param);
392
393         if (c) {
394                 return c->list()->automation_style();
395         } else {
396                 return Absolute; // whatever
397         }
398 }
399
400 void
401 Automatable::protect_automation ()
402 {
403         set<Parameter> automated_params;
404
405         what_has_automation (automated_params);
406
407         for (set<Parameter>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
408
409                 boost::shared_ptr<AutomationControl> c = control(*i);
410
411                 switch (c->list()->automation_state()) {
412                 case Write:
413                         c->list()->set_automation_state (Off);
414                         break;
415                 case Touch:
416                         c->list()->set_automation_state (Play);
417                         break;
418                 default:
419                         break;
420                 }
421         }
422 }
423
424 void
425 Automatable::automation_snapshot (nframes_t now)
426 {
427         if (_last_automation_snapshot > now || (now - _last_automation_snapshot) > _session.automation_interval()) {
428
429                 for (Controls::iterator i = _controls.begin(); i != _controls.end(); ++i) {
430                         if (i->second->list()->automation_write()) {
431                                 i->second->list()->rt_add (now, i->second->user_value());
432                         }
433                 }
434                 
435                 _last_automation_snapshot = now;
436         }
437 }
438
439 void
440 Automatable::transport_stopped (nframes_t now)
441 {
442         for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li) {
443                 
444                 boost::shared_ptr<AutomationControl> c = li->second;
445                 
446                 c->list()->reposition_for_rt_add (now);
447
448                 if (c->list()->automation_state() != Off) {
449                         c->set_value(c->list()->eval(now));
450                 }
451         }
452 }
453
454 /* FIXME: this probably doesn't belong here */
455 boost::shared_ptr<AutomationControl>
456 Automatable::control_factory(boost::shared_ptr<AutomationList> list)
457 {
458         if (list->parameter().type() == MidiCCAutomation) {
459                 // FIXME: this will die horribly if this is not a MidiTrack
460                 return boost::shared_ptr<AutomationControl>(new MidiTrack::MidiControl((MidiTrack*)this, list));
461         } else {
462                 cerr << "WARNING: Default AutomationControl created for " << list->parameter().to_string() << endl;
463                 return boost::shared_ptr<AutomationControl>(new AutomationControl(_session, list));
464         }
465 }
466