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