Fix my name :)
[ardour.git] / libs / evoral / src / ControlSet.cpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 David Robillard <http://drobilla.net>
3  * Copyright (C) 2000-2008 Paul Davis
4  *
5  * Evoral is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 2 of the License, or (at your option) any later
8  * version.
9  *
10  * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <iostream>
20 #include <limits>
21 #include "evoral/ControlSet.hpp"
22 #include "evoral/ControlList.hpp"
23 #include "evoral/Control.hpp"
24 #include "evoral/Event.hpp"
25
26 using namespace std;
27
28 namespace Evoral {
29
30
31 ControlSet::ControlSet()
32 {
33 }
34
35 ControlSet::ControlSet (const ControlSet& other)
36         : noncopyable ()
37 {
38         /* derived class must copy controls */
39 }
40
41 void
42 ControlSet::add_control(boost::shared_ptr<Control> ac)
43 {
44         _controls[ac->parameter()] = ac;
45
46         ac->ListMarkedDirty.connect_same_thread (_control_connections, boost::bind (&ControlSet::control_list_marked_dirty, this));
47
48         ac->list()->InterpolationChanged.connect_same_thread (
49                 _list_connections, boost::bind (&ControlSet::control_list_interpolation_changed, this, ac->parameter(), _1)
50                 );
51 }
52
53 void
54 ControlSet::what_has_data (set<Parameter>& s) const
55 {
56         Glib::Mutex::Lock lm (_control_lock);
57         for (Controls::const_iterator li = _controls.begin(); li != _controls.end(); ++li) {
58                 s.insert(li->first);
59         }
60 }
61
62 /** If a control for the given parameter does not exist and \a create_if_missing is true,
63  * a control will be created, added to this set, and returned.
64  * If \a create_if_missing is false this function may return null.
65  */
66 boost::shared_ptr<Control>
67 ControlSet::control (const Parameter& parameter, bool create_if_missing)
68 {
69         Controls::iterator i = _controls.find(parameter);
70
71         if (i != _controls.end()) {
72                 return i->second;
73
74         } else if (create_if_missing) {
75                 boost::shared_ptr<Control> ac(control_factory(parameter));
76                 add_control(ac);
77                 return ac;
78
79         } else {
80                 return boost::shared_ptr<Control>();
81         }
82 }
83
84 bool
85 ControlSet::find_next_event (double now, double end, ControlEvent& next_event) const
86 {
87         Controls::const_iterator li;
88
89         next_event.when = std::numeric_limits<double>::max();
90
91         for (li = _controls.begin(); li != _controls.end(); ++li) {
92                 ControlList::const_iterator i;
93                 boost::shared_ptr<const ControlList> alist (li->second->list());
94                 ControlEvent cp (now, 0.0f);
95
96                 for (i = lower_bound (alist->begin(), alist->end(), &cp, ControlList::time_comparator);
97                                 i != alist->end() && (*i)->when < end; ++i) {
98                         if ((*i)->when > now) {
99                                 break;
100                         }
101                 }
102
103                 if (i != alist->end() && (*i)->when < end) {
104                         if ((*i)->when < next_event.when) {
105                                 next_event.when = (*i)->when;
106                         }
107                 }
108         }
109
110         return next_event.when != std::numeric_limits<double>::max();
111 }
112
113 void
114 ControlSet::clear_controls ()
115 {
116         Glib::Mutex::Lock lm (_control_lock);
117
118         _control_connections.drop_connections ();
119         _list_connections.drop_connections ();
120
121         for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
122                 li->second->list()->clear();
123 }
124
125 } // namespace Evoral