Display recorded controller data (fix show all/existing automation).
[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)
41         : _a_session(session)
42         , _last_automation_snapshot(0)
43 {
44 }
45
46 int
47 Automatable::old_set_automation_state (const XMLNode& node)
48 {
49         const XMLProperty *prop;
50                         
51         if ((prop = node.property ("path")) != 0) {
52                 load_automation (prop->value());
53         } else {
54                 warning << _("Automation node has no path property") << endmsg;
55         }
56         
57         if ((prop = node.property ("visible")) != 0) {
58                 uint32_t what;
59                 stringstream sstr;
60                 
61                 _visible_controls.clear ();
62                 
63                 sstr << prop->value();
64                 while (1) {
65                         sstr >> what;
66                         if (sstr.fail()) {
67                                 break;
68                         }
69                         mark_automation_visible (Parameter(PluginAutomation, what), true);
70                 }
71         }
72         
73         _last_automation_snapshot = 0;
74
75         return 0;
76 }
77
78 int
79 Automatable::load_automation (const string& path)
80 {
81         string fullpath;
82
83         if (path[0] == '/') { // legacy
84                 fullpath = path;
85         } else {
86                 fullpath = _a_session.automation_dir();
87                 fullpath += path;
88         }
89         ifstream in (fullpath.c_str());
90
91         if (!in) {
92                 warning << string_compose(_("cannot open %2 to load automation data (%3)")
93                                 , fullpath, strerror (errno)) << endmsg;
94                 return 1;
95         }
96
97         Glib::Mutex::Lock lm (control_lock());
98         set<Parameter> tosave;
99         controls().clear ();
100         
101         _last_automation_snapshot = 0;
102
103         while (in) {
104                 double when;
105                 double value;
106                 uint32_t port;
107
108                 in >> port;  if (!in) break;
109                 in >> when;  if (!in) goto bad;
110                 in >> value; if (!in) goto bad;
111                 
112                 /* FIXME: this is legacy and only used for plugin inserts?  I think? */
113                 boost::shared_ptr<Evoral::Control> c = control (Parameter(PluginAutomation, port), true);
114                 c->list()->add (when, value);
115                 tosave.insert (Parameter(PluginAutomation, port));
116         }
117         
118         return 0;
119
120   bad:
121         error << string_compose(_("cannot load automation data from %2"), fullpath) << endmsg;
122         controls().clear ();
123         return -1;
124 }
125
126 void
127 Automatable::add_control(boost::shared_ptr<Evoral::Control> ac)
128 {
129         Parameter param = ac->parameter();
130         
131         ControlSet::add_control(ac);
132         _can_automate_list.insert(param);
133         auto_state_changed(param); // sync everything up
134 }
135
136 void
137 Automatable::what_has_visible_data(set<Parameter>& s) const
138 {
139         Glib::Mutex::Lock lm (control_lock());
140         set<Parameter>::const_iterator li;
141         
142         for (li = _visible_controls.begin(); li != _visible_controls.end(); ++li) {
143                 s.insert  (*li);
144         }
145 }
146
147 string
148 Automatable::describe_parameter (Parameter param)
149 {
150         /* derived classes like PluginInsert should override this */
151
152         if (param == Parameter(GainAutomation)) {
153                 return _("Fader");
154         } else if (param.type() == PanAutomation) {
155                 /* ID's are zero-based, present them as 1-based */
156                 return (string_compose(_("Pan %1"), param.id() + 1));
157         } else if (param.type() == MidiCCAutomation) {
158                 return string_compose("CC %1 (%2) [%3]",
159                                 param.id() + 1, midi_name(param.id()), int(param.channel()) + 1);                       
160         } else if (param.type() == MidiPgmChangeAutomation) {
161                 return string_compose("Program [%1]", int(param.channel()) + 1);
162         } else if (param.type() == MidiPitchBenderAutomation) {
163                 return string_compose("Bender [%1]", int(param.channel()) + 1);
164         } else if (param.type() == MidiChannelAftertouchAutomation) {
165                 return string_compose("Aftertouch [%1]", int(param.channel()) + 1);
166         } else {
167                 return param.symbol();
168         }
169 }
170
171 void
172 Automatable::can_automate (Parameter what)
173 {
174         _can_automate_list.insert (what);
175 }
176
177 void
178 Automatable::mark_automation_visible (Parameter what, bool yn)
179 {
180         if (yn) {
181                 _visible_controls.insert (what);
182         } else {
183                 set<Parameter>::iterator i;
184
185                 if ((i = _visible_controls.find (what)) != _visible_controls.end()) {
186                         _visible_controls.erase (i);
187                 }
188         }
189 }
190
191 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
192  * had a single automation parameter, with it's type implicit.  Derived objects should
193  * pass that type and it will be used for the untyped AutomationList found.
194  */
195 int
196 Automatable::set_automation_state (const XMLNode& node, Parameter legacy_param)
197 {       
198         Glib::Mutex::Lock lm (control_lock());
199
200         /* Don't clear controls, since some may be special derived Controllable classes */
201
202         _visible_controls.clear ();
203
204         XMLNodeList nlist = node.children();
205         XMLNodeIterator niter;
206         
207         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
208
209                 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, &param) != 1) {
210                   error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
211                   continue;
212                   }*/
213
214                 if ((*niter)->name() == "AutomationList") {
215
216                         const XMLProperty* id_prop = (*niter)->property("automation-id");
217
218                         Parameter param = (id_prop ? Parameter(id_prop->value()) : legacy_param);
219                         if (param.type() == NullAutomation) {
220                                 warning << "Automation has null type" << endl;
221                                 continue;
222                         }
223                         
224                         boost::shared_ptr<AutomationList> al (new AutomationList(**niter, param));
225                         
226                         if (!id_prop) {
227                                 warning << "AutomationList node without automation-id property, "
228                                         << "using default: " << legacy_param.symbol() << endmsg;
229                         }
230
231                         boost::shared_ptr<Evoral::Control> existing = control(param);
232                         if (existing)
233                                 existing->set_list(al);
234                         else
235                                 add_control(control_factory(param));
236
237                 } else {
238                         error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
239                 }
240         }
241
242         _last_automation_snapshot = 0;
243
244         return 0;
245 }
246
247 XMLNode&
248 Automatable::get_automation_state ()
249 {
250         Glib::Mutex::Lock lm (control_lock());
251         XMLNode* node = new XMLNode (X_("Automation"));
252         
253         if (controls().empty()) {
254                 return *node;
255         }
256
257         for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
258                 boost::shared_ptr<AutomationList> l
259                                 = boost::dynamic_pointer_cast<AutomationList>(li->second->list());
260                 node->add_child_nocopy (l->get_state ());
261         }
262
263         return *node;
264 }
265
266 void
267 Automatable::set_parameter_automation_state (Parameter param, AutoState s)
268 {
269         Glib::Mutex::Lock lm (control_lock());
270         
271         boost::shared_ptr<Evoral::Control> c = control (param, true);
272         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
273
274         if (s != l->automation_state()) {
275                 l->set_automation_state (s);
276                 _a_session.set_dirty ();
277         }
278 }
279
280 AutoState
281 Automatable::get_parameter_automation_state (Parameter param, bool lock)
282 {
283         AutoState result = Off;
284
285         if (lock)
286                 control_lock().lock();
287
288         boost::shared_ptr<Evoral::Control> c = control(param);
289         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
290
291         if (c)
292                 result = l->automation_state();
293         
294         if (lock)
295                 control_lock().unlock();
296
297         return result;
298 }
299
300 void
301 Automatable::set_parameter_automation_style (Parameter param, AutoStyle s)
302 {
303         Glib::Mutex::Lock lm (control_lock());
304         
305         boost::shared_ptr<Evoral::Control> c = control(param, true);
306         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
307
308         if (s != l->automation_style()) {
309                 l->set_automation_style (s);
310                 _a_session.set_dirty ();
311         }
312 }
313
314 AutoStyle
315 Automatable::get_parameter_automation_style (Parameter param)
316 {
317         Glib::Mutex::Lock lm (control_lock());
318
319         boost::shared_ptr<Evoral::Control> c = control(param);
320         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
321
322         if (c) {
323                 return l->automation_style();
324         } else {
325                 return Absolute; // whatever
326         }
327 }
328
329 void
330 Automatable::protect_automation ()
331 {
332         typedef set<Evoral::Parameter> ParameterSet;
333         ParameterSet automated_params;
334
335         what_has_data(automated_params);
336
337         for (ParameterSet::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
338
339                 boost::shared_ptr<Evoral::Control> c = control(*i);
340                 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
341
342                 switch (l->automation_state()) {
343                 case Write:
344                         l->set_automation_state (Off);
345                         break;
346                 case Touch:
347                         l->set_automation_state (Play);
348                         break;
349                 default:
350                         break;
351                 }
352         }
353 }
354
355 void
356 Automatable::automation_snapshot (nframes_t now, bool force)
357 {
358         if (force || _last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
359
360                 for (Controls::iterator i = controls().begin(); i != controls().end(); ++i) {
361                         boost::shared_ptr<AutomationControl> c
362                                         = boost::dynamic_pointer_cast<AutomationControl>(i->second);
363                         if (c->automation_write()) {
364                                 c->list()->rt_add (now, i->second->user_value());
365                         }
366                 }
367                 
368                 _last_automation_snapshot = now;
369         }
370 }
371
372 void
373 Automatable::transport_stopped (nframes_t now)
374 {
375         for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
376                 
377                 boost::shared_ptr<AutomationControl> c
378                                 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
379                 boost::shared_ptr<AutomationList> l
380                                 = boost::dynamic_pointer_cast<AutomationList>(c->list());
381                 
382                 c->list()->reposition_for_rt_add (now);
383
384                 if (c->automation_state() != Off) {
385                         c->set_value(c->list()->eval(now));
386                 }
387         }
388 }
389
390 boost::shared_ptr<Evoral::Control>
391 Automatable::control_factory(const Evoral::Parameter& param)
392 {
393         boost::shared_ptr<AutomationList> list(new AutomationList(param));
394         Evoral::Control* control = NULL;
395         if (param.type() >= MidiCCAutomation && param.type() <= MidiChannelAftertouchAutomation) {
396                 control = new MidiTrack::MidiControl((MidiTrack*)this, param);
397         } else {
398                 control = new AutomationControl(_a_session, param);
399         }
400         control->set_list(list);
401         return boost::shared_ptr<Evoral::Control>(control);
402 }
403