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