067e64a870f5b4c1e974125416084639c9845b4b
[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 <ardour/session.h>
28 #include <ardour/automatable.h>
29 #include <ardour/midi_track.h>
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace ARDOUR;
35 using namespace PBD;
36
37 nframes_t Automatable::_automation_interval = 0;
38
39 Automatable::Automatable(Session& _session, const string& name)
40         : SessionObject(_session, name)
41         , _last_automation_snapshot(0)
42 {}
43
44 int
45 Automatable::old_set_automation_state (const XMLNode& node)
46 {
47         const XMLProperty *prop;
48                         
49         if ((prop = node.property ("path")) != 0) {
50                 load_automation (prop->value());
51         } else {
52                 warning << string_compose(_("%1: Automation node has no path property"), _name) << endmsg;
53         }
54         
55         if ((prop = node.property ("visible")) != 0) {
56                 uint32_t what;
57                 stringstream sstr;
58                 
59                 _visible_controls.clear ();
60                 
61                 sstr << prop->value();
62                 while (1) {
63                         sstr >> what;
64                         if (sstr.fail()) {
65                                 break;
66                         }
67                         mark_automation_visible (Parameter(PluginAutomation, what), true);
68                 }
69         }
70         
71         _last_automation_snapshot = 0;
72
73         return 0;
74 }
75
76 int
77 Automatable::load_automation (const string& path)
78 {
79         string fullpath;
80
81         if (path[0] == '/') { // legacy
82                 fullpath = path;
83         } else {
84                 fullpath = _session.automation_dir();
85                 fullpath += path;
86         }
87         ifstream in (fullpath.c_str());
88
89         if (!in) {
90                 warning << string_compose(_("%1: cannot open %2 to load automation data (%3)"), _name, fullpath, strerror (errno)) << endmsg;
91                 return 1;
92         }
93
94         Glib::Mutex::Lock lm (_automation_lock);
95         set<Parameter> tosave;
96         _controls.clear ();
97         
98         _last_automation_snapshot = 0;
99
100         while (in) {
101                 double when;
102                 double value;
103                 uint32_t port;
104
105                 in >> port;  if (!in) break;
106                 in >> when;  if (!in) goto bad;
107                 in >> value; if (!in) goto bad;
108                 
109                 /* FIXME: this is legacy and only used for plugin inserts?  I think? */
110                 boost::shared_ptr<AutomationControl> c = control (Parameter(PluginAutomation, port), true);
111                 c->list()->add (when, value);
112                 tosave.insert (Parameter(PluginAutomation, port));
113         }
114         
115         return 0;
116
117   bad:
118         error << string_compose(_("%1: cannot load automation data from %2"), _name, fullpath) << endmsg;
119         _controls.clear ();
120         return -1;
121 }
122
123 void
124 Automatable::add_control(boost::shared_ptr<AutomationControl> ac)
125 {
126         Parameter param = ac->parameter();
127
128         _controls[param] = ac;
129         
130         _can_automate_list.insert(param);
131
132         // Sync everything (derived classes) up to initial values
133         auto_state_changed(param);
134 }
135
136 void
137 Automatable::what_has_automation (set<Parameter>& s) const
138 {
139         Glib::Mutex::Lock lm (_automation_lock);
140         Controls::const_iterator li;
141         
142         // FIXME: correct semantics?
143         for (li = _controls.begin(); li != _controls.end(); ++li) {
144                 s.insert  ((*li).first);
145         }
146 }
147
148 void
149 Automatable::what_has_visible_automation (set<Parameter>& s) const
150 {
151         Glib::Mutex::Lock lm (_automation_lock);
152         set<Parameter>::const_iterator li;
153         
154         for (li = _visible_controls.begin(); li != _visible_controls.end(); ++li) {
155                 s.insert  (*li);
156         }
157 }
158
159 /** Returns NULL if we don't have an AutomationList for \a parameter.
160  */
161 boost::shared_ptr<AutomationControl>
162 Automatable::control (Parameter parameter, bool create_if_missing)
163 {
164         Controls::iterator i = _controls.find(parameter);
165
166         if (i != _controls.end()) {
167                 return i->second;
168
169         } else if (create_if_missing) {
170                 boost::shared_ptr<AutomationList> al (new AutomationList (
171                                         parameter, FLT_MIN, FLT_MAX, default_parameter_value (parameter)));
172                 boost::shared_ptr<AutomationControl> ac(control_factory(al));
173                 add_control(ac);
174                 return ac;
175
176         } else {
177                 //warning << "AutomationList " << parameter.to_string() << " not found for " << _name << endmsg;
178                 return boost::shared_ptr<AutomationControl>();
179         }
180 }
181
182 boost::shared_ptr<const AutomationControl>
183 Automatable::control (Parameter parameter) const
184 {
185         Controls::const_iterator i = _controls.find(parameter);
186
187         if (i != _controls.end()) {
188                 return i->second;
189         } else {
190                 //warning << "AutomationList " << parameter.to_string() << " not found for " << _name << endmsg;
191                 return boost::shared_ptr<AutomationControl>();
192         }
193 }
194
195
196 string
197 Automatable::describe_parameter (Parameter param)
198 {
199         /* derived classes like PluginInsert should override this */
200
201         if (param == Parameter(GainAutomation)) {
202                 return _("Fader");
203         } else if (param.type() == PanAutomation) {
204                 return (string_compose(_("Pan %1"), param.id()));
205         } else if (param.type() == MidiCCAutomation) {
206                 string name = get_name_for_cc_number(param.id());
207                 if(name.length() != 0) {
208                         return string_compose("%1 [%2]", name, int(param.channel()) + 1);                       
209                 } else {
210                         return string_compose("CC %1 [%2]", param.id(), int(param.channel()) + 1);
211                 }
212         } else if (param.type() == MidiPgmChangeAutomation) {
213                 return string_compose("Program [%1]", int(param.channel()) + 1);
214         } else if (param.type() == MidiPitchBenderAutomation) {
215                 return string_compose("Bender [%1]", int(param.channel()) + 1);
216         } else if (param.type() == MidiChannelAftertouchAutomation) {
217                 return string_compose("Aftertouch [%1]", int(param.channel()) + 1);
218         } else {
219                 return param.to_string();
220         }
221 }
222
223 string
224 Automatable::get_name_for_cc_number (uint32_t cc_number)
225 {
226         string name;
227         
228         switch (cc_number) {
229                 case 0:
230                         name = "Upper Bank";
231                         break;
232                         
233                 case 32:
234                         name = "Lower Bank";
235                         break;
236                 
237                 case 1:
238                         name = "Modulation MSB";
239                         break;
240                         
241                 case 2:
242                         name = "Breath Controller";
243                         break;  
244                         
245                 case 4:
246                         name = "Foot Controller";
247                         break;
248                         
249                 case 5:
250                         name = "Portamento Time";
251                         break;
252                         
253                 case 6:
254                         name = "RPN Controller";
255                         break;
256                         
257                 case 7:
258                         name = "Main Volume";
259                         break;
260                         
261                 case 8:
262                         name = "Balance";
263                         break;
264                         
265                 case 10:
266                         name = "Panorama";
267                         break;
268                         
269                 case 11:
270                         name = "Expression";
271                         break;
272                         
273                 case 12:
274                         name = "Effect 1";
275                         break;
276                         
277                 case 13:
278                         name = "Effect 2";
279                         break;
280                         
281                 case 16:
282                 case 17:
283                 case 18:
284                 case 19:
285                         name = string_compose("General Purpose %1", cc_number - 15);
286                         break;
287                         
288                 case 64:
289                         name = "Sustain Pedal";
290                         break;
291                         
292                 case 65:
293                         name = "Portamento";
294                         break;
295                         
296                 case 66:
297                         name = "Sostenuto";
298                         break;
299                         
300                 case 67:
301                         name = "Soft Pedal";
302                         break;
303                         
304                 case 68:
305                         name = "Legato Footswitch";
306                         break;
307                         
308                 case 69:
309                         name = "Hold 2";
310                         break;
311                         
312                 case 70:
313                 case 71:
314                 case 72:
315                 case 73:
316                 case 74:
317                         name = string_compose("Sound Controller %1", cc_number - 69);
318                         break;
319                         
320                 case 80:
321                 case 81:
322                 case 82:
323                 case 83:
324                         name = string_compose("General Purpose %1", cc_number - 75);
325                         break;
326                         
327                 case 84:
328                         name = "Portamento Control";
329                         break;
330                         
331                 case 91:
332                 case 92:
333                 case 93:
334                 case 94:
335                 case 95:
336                         name = string_compose("Effects %1 Depth", cc_number - 90);
337                         break;                  
338                         
339                 case 96:
340                         name = "Data Increment RPN/NRPN";
341                         break;
342                         
343                 case 97:
344                         name = "Data Decrement RPN/NRPN";
345                         break;
346                         
347                 case 98:
348                         name = "NRPN LSB";
349                         break;
350                         
351                 case 99:
352                         name = "NRPN MSB";
353                         break;
354                                                 
355                 case 100:
356                         name = "RPN LSB";
357                         break;
358                         
359                 case 101:
360                         name = "RPN MSB";
361                         break;
362                         
363                 case 120:
364                         name = "all sounds off";
365                         break;
366                         
367                 case 121:
368                         name = "Controller Reset";
369                         break;
370                         
371                 case 122:
372                         name = "Local Control on/off";
373                         break;
374                         
375                 case 123:
376                         name = "all notes off";
377                         break;
378                         
379                 case 124:
380                         name = "omni off";
381                         break;
382                         
383                 case 125:
384                         name = "omni on";
385                         break;
386                         
387                 case 126:
388                         name = "mono on / poly off";
389                         break;
390                         
391                 case 127:
392                         name = "poly on / mono off";
393                         break;  
394                         
395                 default:
396                         break;
397         }
398         
399         return name;
400 }
401
402 void
403 Automatable::can_automate (Parameter what)
404 {
405         _can_automate_list.insert (what);
406 }
407
408 void
409 Automatable::mark_automation_visible (Parameter what, bool yn)
410 {
411         if (yn) {
412                 _visible_controls.insert (what);
413         } else {
414                 set<Parameter>::iterator i;
415
416                 if ((i = _visible_controls.find (what)) != _visible_controls.end()) {
417                         _visible_controls.erase (i);
418                 }
419         }
420 }
421
422 bool
423 Automatable::find_next_event (nframes_t now, nframes_t end, ControlEvent& next_event) const
424 {
425         Controls::const_iterator li;    
426
427         next_event.when = max_frames;
428         
429         for (li = _controls.begin(); li != _controls.end(); ++li) {
430                 
431                 AutomationList::const_iterator i;
432                 boost::shared_ptr<const AutomationList> alist (li->second->list());
433                 ControlEvent cp (now, 0.0f);
434                 
435                 for (i = lower_bound (alist->const_begin(), alist->const_end(), &cp, AutomationList::time_comparator);
436                                 i != alist->const_end() && (*i)->when < end; ++i) {
437                         if ((*i)->when > now) {
438                                 break; 
439                         }
440                 }
441                 
442                 if (i != alist->const_end() && (*i)->when < end) {
443                         
444                         if ((*i)->when < next_event.when) {
445                                 next_event.when = (*i)->when;
446                         }
447                 }
448         }
449
450         return next_event.when != max_frames;
451 }
452
453 /** \a legacy_param is used for loading legacy sessions where an object (IO, Panner)
454  * had a single automation parameter, with it's type implicit.  Derived objects should
455  * pass that type and it will be used for the untyped AutomationList found.
456  */
457 int
458 Automatable::set_automation_state (const XMLNode& node, Parameter legacy_param)
459 {       
460         Glib::Mutex::Lock lm (_automation_lock);
461
462         /* Don't clear controls, since some may be special derived Controllable classes */
463
464         _visible_controls.clear ();
465
466         XMLNodeList nlist = node.children();
467         XMLNodeIterator niter;
468         
469         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
470
471                 /*if (sscanf ((*niter)->name().c_str(), "parameter-%" PRIu32, &param) != 1) {
472                   error << string_compose (_("%2: badly formatted node name in XML automation state, ignored"), _name) << endmsg;
473                   continue;
474                   }*/
475
476                 if ((*niter)->name() == "AutomationList") {
477
478                         const XMLProperty* id_prop = (*niter)->property("automation-id");
479
480                         Parameter param = (id_prop ? Parameter(id_prop->value()) : legacy_param);
481                         
482                         boost::shared_ptr<AutomationList> al (new AutomationList(**niter, param));
483                         
484                         if (!id_prop) {
485                                 warning << "AutomationList node without automation-id property, "
486                                         << "using default: " << legacy_param.to_string() << endmsg;
487                                 al->set_parameter(legacy_param);
488                         }
489
490                         boost::shared_ptr<AutomationControl> existing = control(param);
491                         if (existing)
492                                 existing->set_list(al);
493                         else
494                                 add_control(control_factory(al));
495
496                 } else {
497                         error << "Expected AutomationList node, got '" << (*niter)->name() << endmsg;
498                 }
499         }
500
501         _last_automation_snapshot = 0;
502
503         return 0;
504 }
505
506 XMLNode&
507 Automatable::get_automation_state ()
508 {
509         Glib::Mutex::Lock lm (_automation_lock);
510         XMLNode* node = new XMLNode (X_("Automation"));
511         
512         if (_controls.empty()) {
513                 return *node;
514         }
515
516         for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li) {
517                 node->add_child_nocopy (li->second->list()->get_state ());
518         }
519
520         return *node;
521 }
522
523 void
524 Automatable::clear_automation ()
525 {
526         Glib::Mutex::Lock lm (_automation_lock);
527
528         for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
529                 li->second->list()->clear();
530 }
531         
532 void
533 Automatable::set_parameter_automation_state (Parameter param, AutoState s)
534 {
535         Glib::Mutex::Lock lm (_automation_lock);
536         
537         boost::shared_ptr<AutomationControl> c = control (param, true);
538
539         if (s != c->list()->automation_state()) {
540                 c->list()->set_automation_state (s);
541                 _session.set_dirty ();
542         }
543 }
544
545 AutoState
546 Automatable::get_parameter_automation_state (Parameter param, bool lock)
547 {
548         AutoState result = Off;
549
550         if (lock)
551                 _automation_lock.lock();
552
553         boost::shared_ptr<AutomationControl> c = control(param);
554
555         if (c)
556                 result = c->list()->automation_state();
557         
558         if (lock)
559                 _automation_lock.unlock();
560
561         return result;
562 }
563
564 void
565 Automatable::set_parameter_automation_style (Parameter param, AutoStyle s)
566 {
567         Glib::Mutex::Lock lm (_automation_lock);
568         
569         boost::shared_ptr<AutomationControl> c = control(param, true);
570
571         if (s != c->list()->automation_style()) {
572                 c->list()->set_automation_style (s);
573                 _session.set_dirty ();
574         }
575 }
576
577 AutoStyle
578 Automatable::get_parameter_automation_style (Parameter param)
579 {
580         Glib::Mutex::Lock lm (_automation_lock);
581
582         boost::shared_ptr<AutomationControl> c = control(param);
583
584         if (c) {
585                 return c->list()->automation_style();
586         } else {
587                 return Absolute; // whatever
588         }
589 }
590
591 void
592 Automatable::protect_automation ()
593 {
594         set<Parameter> automated_params;
595
596         what_has_automation (automated_params);
597
598         for (set<Parameter>::iterator i = automated_params.begin(); i != automated_params.end(); ++i) {
599
600                 boost::shared_ptr<AutomationControl> c = control(*i);
601
602                 switch (c->list()->automation_state()) {
603                 case Write:
604                         c->list()->set_automation_state (Off);
605                         break;
606                 case Touch:
607                         c->list()->set_automation_state (Play);
608                         break;
609                 default:
610                         break;
611                 }
612         }
613 }
614
615 void
616 Automatable::automation_snapshot (nframes_t now, bool force)
617 {
618         if (force || _last_automation_snapshot > now || (now - _last_automation_snapshot) > _automation_interval) {
619
620                 for (Controls::iterator i = _controls.begin(); i != _controls.end(); ++i) {
621                         if (i->second->list()->automation_write()) {
622                                 i->second->list()->rt_add (now, i->second->user_value());
623                         }
624                 }
625                 
626                 _last_automation_snapshot = now;
627         }
628 }
629
630 void
631 Automatable::transport_stopped (nframes_t now)
632 {
633         for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li) {
634                 
635                 boost::shared_ptr<AutomationControl> c = li->second;
636                 
637                 c->list()->reposition_for_rt_add (now);
638
639                 if (c->list()->automation_state() != Off) {
640                         c->set_value(c->list()->eval(now));
641                 }
642         }
643 }
644
645 /* FIXME: this probably doesn't belong here */
646 boost::shared_ptr<AutomationControl>
647 Automatable::control_factory(boost::shared_ptr<AutomationList> list)
648 {
649         if (
650                         list->parameter().type() == MidiCCAutomation ||
651                         list->parameter().type() == MidiPgmChangeAutomation ||
652                         list->parameter().type() == MidiChannelAftertouchAutomation 
653            ) {
654                 // FIXME: this will die horribly if this is not a MidiTrack
655                 return boost::shared_ptr<AutomationControl>(new MidiTrack::MidiControl((MidiTrack*)this, list));
656         } else {
657                 return boost::shared_ptr<AutomationControl>(new AutomationControl(_session, list));
658         }
659 }
660