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