permit different sizes for audio playback & capture buffers
[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                 _controls[ac->parameter()] = 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         ControlSet::add_control(ac);
151         _can_automate_list.insert(param);
152         auto_state_changed(param); // sync everything up
153 }
154
155 void
156 Automatable::what_has_visible_data(set<Evoral::Parameter>& s) const
157 {
158         Glib::Mutex::Lock lm (control_lock());
159         set<Evoral::Parameter>::const_iterator li;
160
161         for (li = _visible_controls.begin(); li != _visible_controls.end(); ++li) {
162                 s.insert  (*li);
163         }
164 }
165
166 string
167 Automatable::describe_parameter (Evoral::Parameter param)
168 {
169         /* derived classes like PluginInsert should override this */
170
171         if (param == Evoral::Parameter(GainAutomation)) {
172                 return _("Fader");
173         } else if (param.type() == PanAutomation) {
174                 /* ID's are zero-based, present them as 1-based */
175                 return (string_compose(_("Pan %1"), param.id() + 1));
176         } else if (param.type() == MidiCCAutomation) {
177                 return string_compose("%2 [%3]",
178                                 param.id() + 1, midi_name(param.id()), int(param.channel()) + 1);
179         } else if (param.type() == MidiPgmChangeAutomation) {
180                 return string_compose("Program [%1]", int(param.channel()) + 1);
181         } else if (param.type() == MidiPitchBenderAutomation) {
182                 return string_compose("Bender [%1]", int(param.channel()) + 1);
183         } else if (param.type() == MidiChannelPressureAutomation) {
184                 return string_compose("Pressure [%1]", int(param.channel()) + 1);
185         } else {
186                 return EventTypeMap::instance().to_symbol(param);
187         }
188 }
189
190 void
191 Automatable::can_automate (Evoral::Parameter what)
192 {
193         _can_automate_list.insert (what);
194 }
195
196 void
197 Automatable::mark_automation_visible (Evoral::Parameter what, bool yn)
198 {
199         if (yn) {
200                 _visible_controls.insert (what);
201         } else {
202                 set<Evoral::Parameter>::iterator i;
203
204                 if ((i = _visible_controls.find (what)) != _visible_controls.end()) {
205                         _visible_controls.erase (i);
206                 }
207         }
208 }
209
210 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
211  * had a single automation parameter, with it's type implicit.  Derived objects should
212  * pass that type and it will be used for the untyped AutomationList found.
213  */
214 int
215 Automatable::set_automation_state (const XMLNode& node, Evoral::Parameter legacy_param)
216 {
217         Glib::Mutex::Lock lm (control_lock());
218
219         /* Don't clear controls, since some may be special derived Controllable classes */
220
221         _visible_controls.clear ();
222
223         XMLNodeList nlist = node.children();
224         XMLNodeIterator niter;
225
226         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
227
228                 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, &param) != 1) {
229                   error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
230                   continue;
231                   }*/
232
233                 if ((*niter)->name() == "AutomationList") {
234
235                         const XMLProperty* id_prop = (*niter)->property("automation-id");
236
237                         Evoral::Parameter param = (id_prop
238                                         ? EventTypeMap::instance().new_parameter(id_prop->value())
239                                         : legacy_param);
240
241                         if (param.type() == NullAutomation) {
242                                 warning << "Automation has null type" << endl;
243                                 continue;
244                         }
245
246                         boost::shared_ptr<AutomationList> al (new AutomationList(**niter, param));
247
248                         if (!id_prop) {
249                                 warning << "AutomationList node without automation-id property, "
250                                         << "using default: " << EventTypeMap::instance().to_symbol(legacy_param) << endmsg;
251                         }
252
253                         boost::shared_ptr<Evoral::Control> existing = control(param);
254                         if (existing) {
255                                 existing->set_list(al);
256                         } else {
257                             boost::shared_ptr<Evoral::Control> newcontrol = control_factory(param);
258                                 add_control(newcontrol);
259                                 newcontrol->set_list(al);
260                         }
261
262                 } else {
263                         error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
264                 }
265         }
266
267         _last_automation_snapshot = 0;
268
269         return 0;
270 }
271
272 XMLNode&
273 Automatable::get_automation_state ()
274 {
275         Glib::Mutex::Lock lm (control_lock());
276         XMLNode* node = new XMLNode (X_("Automation"));
277
278         if (controls().empty()) {
279                 return *node;
280         }
281
282         for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
283                 boost::shared_ptr<AutomationList> l
284                                 = boost::dynamic_pointer_cast<AutomationList>(li->second->list());
285                 if (!l->empty()) {
286                         node->add_child_nocopy (l->get_state ());
287                 }
288         }
289
290         return *node;
291 }
292
293 void
294 Automatable::set_parameter_automation_state (Evoral::Parameter param, AutoState s)
295 {
296         Glib::Mutex::Lock lm (control_lock());
297
298         boost::shared_ptr<Evoral::Control> c = control (param, true);
299         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
300
301         if (s != l->automation_state()) {
302                 l->set_automation_state (s);
303                 _a_session.set_dirty ();
304         }
305 }
306
307 AutoState
308 Automatable::get_parameter_automation_state (Evoral::Parameter param, bool lock)
309 {
310         AutoState result = Off;
311
312         if (lock)
313                 control_lock().lock();
314
315         boost::shared_ptr<Evoral::Control> c = control(param);
316         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
317
318         if (c)
319                 result = l->automation_state();
320
321         if (lock)
322                 control_lock().unlock();
323
324         return result;
325 }
326
327 void
328 Automatable::set_parameter_automation_style (Evoral::Parameter param, AutoStyle s)
329 {
330         Glib::Mutex::Lock lm (control_lock());
331
332         boost::shared_ptr<Evoral::Control> c = control(param, true);
333         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
334
335         if (s != l->automation_style()) {
336                 l->set_automation_style (s);
337                 _a_session.set_dirty ();
338         }
339 }
340
341 AutoStyle
342 Automatable::get_parameter_automation_style (Evoral::Parameter param)
343 {
344         Glib::Mutex::Lock lm (control_lock());
345
346         boost::shared_ptr<Evoral::Control> c = control(param);
347         boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
348
349         if (c) {
350                 return l->automation_style();
351         } else {
352                 return Absolute; // whatever
353         }
354 }
355
356 void
357 Automatable::protect_automation ()
358 {
359         typedef set<Evoral::Parameter> ParameterSet;
360         ParameterSet automated_params;
361
362         what_has_data(automated_params);
363
364         for (ParameterSet::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
365
366                 boost::shared_ptr<Evoral::Control> c = control(*i);
367                 boost::shared_ptr<AutomationList> l = boost::dynamic_pointer_cast<AutomationList>(c->list());
368
369                 switch (l->automation_state()) {
370                 case Write:
371                         l->set_automation_state (Off);
372                         break;
373                 case Touch:
374                         l->set_automation_state (Play);
375                         break;
376                 default:
377                         break;
378                 }
379         }
380 }
381
382 void
383 Automatable::automation_snapshot (nframes_t now, bool force)
384 {
385         if (force || _last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
386
387                 for (Controls::iterator i = controls().begin(); i != controls().end(); ++i) {
388                         boost::shared_ptr<AutomationControl> c
389                                         = boost::dynamic_pointer_cast<AutomationControl>(i->second);
390                         if (c->automation_write()) {
391                                 c->list()->rt_add (now, i->second->user_float());
392                         }
393                 }
394
395                 _last_automation_snapshot = now;
396         }
397 }
398
399 void
400 Automatable::transport_stopped (sframes_t now)
401 {
402         for (Controls::iterator li = controls().begin(); li != controls().end(); ++li) {
403
404                 boost::shared_ptr<AutomationControl> c
405                                 = boost::dynamic_pointer_cast<AutomationControl>(li->second);
406                 boost::shared_ptr<AutomationList> l
407                                 = boost::dynamic_pointer_cast<AutomationList>(c->list());
408
409                 c->list()->reposition_for_rt_add (now);
410
411                 if (c->automation_state() != Off) {
412                         c->set_value(c->list()->eval(now));
413                 }
414         }
415 }
416
417 boost::shared_ptr<Evoral::Control>
418 Automatable::control_factory(const Evoral::Parameter& param)
419 {
420         boost::shared_ptr<AutomationList> list(new AutomationList(param));
421         Evoral::Control* control = NULL;
422         if (param.type() >= MidiCCAutomation && param.type() <= MidiChannelPressureAutomation) {
423                 MidiTrack* mt = dynamic_cast<MidiTrack*>(this);
424                 if (mt) {
425                         control = new MidiTrack::MidiControl(mt, param);
426                 } else {
427                         warning << "MidiCCAutomation for non-MidiTrack" << endl;
428                 }
429         } else if (param.type() == PluginAutomation) {
430                 PluginInsert* pi = dynamic_cast<PluginInsert*>(this);
431                 if (pi) {
432                         control = new PluginInsert::PluginControl(pi, param);
433                 } else {
434                         warning << "PluginAutomation for non-Plugin" << endl;
435                 }
436         } else if (param.type() == GainAutomation) {
437                 Amp* amp = dynamic_cast<Amp*>(this);
438                 if (amp) {
439                         control = new Amp::GainControl(X_("gaincontrol"), _a_session, amp, param);
440                 } else {
441                         warning << "GainAutomation for non-Amp" << endl;
442                 }
443         } else if (param.type() == PanAutomation) {
444                 Panner* me = dynamic_cast<Panner*>(this);
445                 if (me) {
446                         control = new Panner::PanControllable(me->session(), X_("panner"), *me, param);
447                 } else {
448                         warning << "PanAutomation for non-Panner" << endl;
449                 }
450         }
451
452         if (!control) {
453                 control = new AutomationControl(_a_session, param);
454         }
455
456         control->set_list(list);
457         return boost::shared_ptr<Evoral::Control>(control);
458 }
459
460 boost::shared_ptr<AutomationControl>
461 Automatable::automation_control (const Evoral::Parameter& id, bool create)
462 {
463         return boost::dynamic_pointer_cast<AutomationControl>(Evoral::ControlSet::control(id, create));
464 }
465
466 boost::shared_ptr<const AutomationControl>
467 Automatable::automation_control (const Evoral::Parameter& id) const
468 {
469         return boost::dynamic_pointer_cast<const AutomationControl>(Evoral::ControlSet::control(id));
470 }
471