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