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