Fix 'live' CC sending of bar controllers after loading session (previously only worke...
[ardour.git] / libs / ardour / automation_control.cc
1 /*
2     Copyright (C) 2007 Paul Davis 
3     Author: Dave Robillard
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include <iostream>
22 #include <ardour/automation_control.h>
23 #include <ardour/session.h>
24 #include <ardour/automatable.h>
25 #include <ardour/midi_track.h>
26
27 using namespace std;
28 using namespace ARDOUR;
29 using namespace PBD;
30
31
32 AutomationControl::AutomationControl(Session& session, boost::shared_ptr<AutomationList> list, string name)
33         : Controllable((name == "unnamed controllable") ? list->parameter().to_string() : name)
34         , _session(session)
35         , _list(list)
36         , _user_value(list->default_value())
37 {
38         cerr << "Created AutomationControl " << name << "(" << list->parameter().to_string() << ")" << endl;
39 }
40
41
42 /** Get the currently effective value (ie the one that corresponds to current output)
43  */
44 float
45 AutomationControl::get_value() const
46 {
47         if (_list->automation_playback())
48                 return _list->eval(_session.transport_frame());
49         else
50                 return _user_value;
51 }
52
53
54 void
55 AutomationControl::set_value(float value)
56 {
57         _user_value = value;
58         
59         if (_session.transport_stopped() && _list->automation_write())
60                 _list->add(_session.transport_frame(), value);
61
62         Changed(); /* EMIT SIGNAL */
63 }
64
65
66 /** Get the latest user-set value, which may not equal get_value() when automation
67  * is playing back, etc.
68  *
69  * Automation write/touch works by periodically sampling this value and adding it
70  * to the AutomationList.
71  */
72 float
73 AutomationControl::user_value() const
74 {
75         return _user_value;
76 }
77         
78
79 void
80 AutomationControl::set_list(boost::shared_ptr<ARDOUR::AutomationList> list)
81 {
82         _list = list;
83         _user_value = list->default_value();
84         Changed();  /* EMIT SIGNAL */
85 }
86
87         
88 Parameter
89 AutomationControl::parameter() const
90 {
91         return _list->parameter();
92 }