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