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