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