93a0b0ef2523db5c1fe3226b9327a73058bd1ba6
[ardour.git] / libs / evoral / src / ControlSet.cpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 Dave 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 <limits>
20 #include <evoral/ControlSet.hpp>
21 #include <evoral/ControlList.hpp>
22 #include <evoral/Control.hpp>
23 #include <evoral/Event.hpp>
24
25 using namespace std;
26
27 namespace Evoral {
28
29 ControlSet::ControlSet()
30 {
31 }
32
33 void
34 ControlSet::add_control(boost::shared_ptr<Control> ac)
35 {
36         _controls[ac->parameter()] = ac;
37 }
38
39 void
40 ControlSet::what_has_data (set<Parameter>& s) const
41 {
42         Glib::Mutex::Lock lm (_control_lock);
43         Controls::const_iterator li;
44         
45         // FIXME: correct semantics?
46         for (li = _controls.begin(); li != _controls.end(); ++li) {
47                 s.insert  ((*li).first);
48         }
49 }
50
51 /** If \a create_if_missing is true, a control list will be created and returned
52  * if one does not already exists.  Otherwise NULL will be returned if a control list
53  * for \a parameter does not exist.
54  */
55 boost::shared_ptr<Control>
56 ControlSet::control (Parameter parameter, bool create_if_missing)
57 {
58         Controls::iterator i = _controls.find(parameter);
59
60         if (i != _controls.end()) {
61                 return i->second;
62
63         } else if (create_if_missing) {
64                 boost::shared_ptr<ControlList> al (control_list_factory(parameter));
65                 boost::shared_ptr<Control> ac(control_factory(al));
66                 add_control(ac);
67                 return ac;
68
69         } else {
70                 //warning << "ControlList " << parameter.to_string() << " not found for " << _name << endmsg;
71                 return boost::shared_ptr<Control>();
72         }
73 }
74
75 boost::shared_ptr<const Control>
76 ControlSet::control (Parameter parameter) const
77 {
78         Controls::const_iterator i = _controls.find(parameter);
79
80         if (i != _controls.end()) {
81                 return i->second;
82         } else {
83                 //warning << "ControlList " << parameter.to_string() << " not found for " << _name << endmsg;
84                 return boost::shared_ptr<Control>();
85         }
86 }
87
88 bool
89 ControlSet::find_next_event (nframes_t now, nframes_t end, ControlEvent& next_event) const
90 {
91         Controls::const_iterator li;    
92
93         next_event.when = std::numeric_limits<nframes_t>::max();
94         
95         for (li = _controls.begin(); li != _controls.end(); ++li) {
96                 ControlList::const_iterator i;
97                 boost::shared_ptr<const ControlList> alist (li->second->list());
98                 ControlEvent cp (now, 0.0f);
99                 
100                 for (i = lower_bound (alist->begin(), alist->end(), &cp, ControlList::time_comparator);
101                                 i != alist->end() && (*i)->when < end; ++i) {
102                         if ((*i)->when > now) {
103                                 break; 
104                         }
105                 }
106                 
107                 if (i != alist->end() && (*i)->when < end) {
108                         if ((*i)->when < next_event.when) {
109                                 next_event.when = (*i)->when;
110                         }
111                 }
112         }
113
114         return next_event.when != std::numeric_limits<nframes_t>::max();
115 }
116
117 void
118 ControlSet::clear ()
119 {
120         Glib::Mutex::Lock lm (_control_lock);
121
122         for (Controls::iterator li = _controls.begin(); li != _controls.end(); ++li)
123                 li->second->list()->clear();
124 }
125         
126 boost::shared_ptr<Control>
127 ControlSet::control_factory(boost::shared_ptr<ControlList> list) const
128 {
129         return boost::shared_ptr<Control>(new Control(list));
130 }
131
132 boost::shared_ptr<ControlList>
133 ControlSet::control_list_factory(const Parameter& param) const
134 {
135         return boost::shared_ptr<ControlList>(new ControlList(param));
136 }
137
138
139 } // namespace Evoral