1 /* This file is part of Evoral.
2 * Copyright (C) 2008 David Robillard <http://drobilla.net>
3 * Copyright (C) 2000-2008 Paul Davis
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
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.
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
19 #ifndef EVORAL_CONTROL_LIST_HPP
20 #define EVORAL_CONTROL_LIST_HPP
26 #include <boost/pool/pool.hpp>
27 #include <boost/pool/pool_alloc.hpp>
29 #include <glibmm/threads.h>
31 #include "pbd/signals.h"
33 #include "evoral/visibility.h"
34 #include "evoral/types.hpp"
35 #include "evoral/Range.hpp"
36 #include "evoral/Parameter.hpp"
37 #include "evoral/ParameterDescriptor.hpp"
44 /** A single event (time-stamped value) for a control
46 class LIBEVORAL_API ControlEvent {
48 ControlEvent (double w, double v)
49 : when (w), value (v), coeff (0)
52 ControlEvent (const ControlEvent& other)
53 : when (other.when), value (other.value), coeff (0)
57 for (size_t i = 0; i < 4; ++i)
58 coeff[i] = other.coeff[i];
62 ~ControlEvent() { if (coeff) delete[] coeff; }
64 void create_coeffs() {
66 coeff = new double[4];
68 coeff[0] = coeff[1] = coeff[2] = coeff[3] = 0.0;
73 double* coeff; ///< double[4] allocated by Curve as needed
76 /** A list (sequence) of time-stamped values for a control
78 class LIBEVORAL_API ControlList
81 typedef std::list<ControlEvent*> EventList;
82 typedef EventList::iterator iterator;
83 typedef EventList::reverse_iterator reverse_iterator;
84 typedef EventList::const_iterator const_iterator;
85 typedef EventList::const_reverse_iterator const_reverse_iterator;
87 ControlList (const Parameter& id, const ParameterDescriptor& desc);
88 ControlList (const ControlList&);
89 ControlList (const ControlList&, double start, double end);
90 virtual ~ControlList();
92 virtual boost::shared_ptr<ControlList> create(const Parameter& id, const ParameterDescriptor& desc);
94 void dump (std::ostream&);
96 ControlList& operator= (const ControlList&);
97 bool operator== (const ControlList&);
98 void copy_events (const ControlList&);
100 virtual void freeze();
101 virtual void thaw ();
102 bool frozen() const { return _frozen; }
104 const Parameter& parameter() const { return _parameter; }
105 void set_parameter(const Parameter& p) { _parameter = p; }
107 const ParameterDescriptor& descriptor() const { return _desc; }
108 void set_descriptor(const ParameterDescriptor& d) { _desc = d; }
110 EventList::size_type size() const { return _events.size(); }
111 double length() const {
112 Glib::Threads::RWLock::ReaderLock lm (_lock);
113 return _events.empty() ? 0.0 : _events.back()->when;
115 bool empty() const { return _events.empty(); }
117 void reset_default (double val) {
118 _default_value = val;
122 void x_scale (double factor);
123 bool extend_to (double);
124 void slide (iterator before, double distance);
125 void shift (double before, double distance);
127 virtual void add (double when, double value, bool with_guards=true, bool with_initial=true);
129 virtual bool editor_add (double when, double value, bool with_guard);
131 void fast_simple_add (double when, double value);
133 void erase_range (double start, double end);
134 void erase (iterator);
135 void erase (iterator, iterator);
136 void erase (double, double);
137 bool move_ranges (std::list< RangeMove<double> > const &);
138 void modify (iterator, double, double);
140 /** Thin the number of events in this list.
142 * The thinning factor has no units but corresponds to the area of a
143 * triangle computed between three points in the list. If the area is
144 * large, it indicates significant non-linearity between the points.
146 * During automation recording we thin the recorded points using this
147 * value. If a point is sufficiently co-linear with its neighbours (as
148 * defined by the area of the triangle formed by three of them), we will
149 * not include it in the list. The larger the value, the more points are
150 * excluded, so this effectively measures the amount of thinning to be
153 void thin (double thinning_factor);
155 boost::shared_ptr<ControlList> cut (double, double);
156 boost::shared_ptr<ControlList> copy (double, double);
157 void clear (double, double);
159 bool paste (const ControlList&, double position, float times);
161 void set_yrange (double min, double max) {
166 double get_max_y() const { return _max_yval; }
167 double get_min_y() const { return _min_yval; }
169 void truncate_end (double length);
170 void truncate_start (double length);
172 iterator begin() { return _events.begin(); }
173 const_iterator begin() const { return _events.begin(); }
174 iterator end() { return _events.end(); }
175 const_iterator end() const { return _events.end(); }
176 reverse_iterator rbegin() { return _events.rbegin(); }
177 const_reverse_iterator rbegin() const { return _events.rbegin(); }
178 reverse_iterator rend() { return _events.rend(); }
179 const_reverse_iterator rend() const { return _events.rend(); }
180 ControlEvent* back() { return _events.back(); }
181 const ControlEvent* back() const { return _events.back(); }
182 ControlEvent* front() { return _events.front(); }
183 const ControlEvent* front() const { return _events.front(); }
185 std::pair<ControlList::iterator,ControlList::iterator> control_points_adjacent (double when);
187 template<class T> void apply_to_points (T& obj, void (T::*method)(const ControlList&)) {
188 Glib::Threads::RWLock::WriterLock lm (_lock);
189 (obj.*method)(*this);
192 double eval (double where) {
193 Glib::Threads::RWLock::ReaderLock lm (_lock);
194 return unlocked_eval (where);
197 double rt_safe_eval (double where, bool& ok) {
199 Glib::Threads::RWLock::ReaderLock lm (_lock, Glib::Threads::TRY_LOCK);
201 if ((ok = lm.locked())) {
202 return unlocked_eval (where);
208 static inline bool time_comparator (const ControlEvent* a, const ControlEvent* b) {
209 return a->when < b->when;
212 /** Lookup cache for eval functions, range contains equivalent values */
214 LookupCache() : left(-1) {}
215 double left; /* leftmost x coordinate used when finding "range" */
216 std::pair<ControlList::const_iterator,ControlList::const_iterator> range;
219 /** Lookup cache for point finding, range contains points after left */
221 SearchCache () : left(-1) {}
222 double left; /* leftmost x coordinate used when finding "first" */
223 ControlList::const_iterator first;
226 const EventList& events() const { return _events; }
227 double default_value() const { return _default_value; }
229 // FIXME: const violations for Curve
230 Glib::Threads::RWLock& lock() const { return _lock; }
231 LookupCache& lookup_cache() const { return _lookup_cache; }
232 SearchCache& search_cache() const { return _search_cache; }
234 /** Called by locked entry point and various private
235 * locations where we already hold the lock.
237 * FIXME: Should this be private? Curve needs it..
239 double unlocked_eval (double x) const;
241 bool rt_safe_earliest_event (double start, double& x, double& y, bool start_inclusive=false) const;
242 bool rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool start_inclusive=false) const;
243 bool rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const;
244 bool rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const;
247 void destroy_curve();
249 Curve& curve() { assert(_curve); return *_curve; }
250 const Curve& curve() const { assert(_curve); return *_curve; }
252 void mark_dirty () const;
254 enum InterpolationStyle {
260 InterpolationStyle interpolation() const { return _interpolation; }
261 void set_interpolation (InterpolationStyle);
263 virtual bool touching() const { return false; }
264 virtual bool writing() const { return false; }
265 virtual bool touch_enabled() const { return false; }
266 void start_write_pass (double time);
267 void write_pass_finished (double when, double thinning_factor=0.0);
268 void set_in_write_pass (bool, bool add_point = false, double when = 0.0);
269 bool in_write_pass () const;
270 bool in_new_write_pass () { return new_write_pass; }
272 /** Emitted when mark_dirty() is called on this object */
273 mutable PBD::Signal0<void> Dirty;
274 /** Emitted when our interpolation style changes */
275 PBD::Signal1<void, InterpolationStyle> InterpolationChanged;
277 bool operator!= (ControlList const &) const;
279 void invalidate_insert_iterator ();
283 /** Called by unlocked_eval() to handle cases of 3 or more control points. */
284 double multipoint_eval (double x) const;
286 void build_search_cache_if_necessary (double start) const;
288 boost::shared_ptr<ControlList> cut_copy_clear (double, double, int op);
289 bool erase_range_internal (double start, double end, EventList &);
291 void maybe_add_insert_guard (double when);
292 iterator erase_from_iterator_to (iterator iter, double when);
293 bool maybe_insert_straight_line (double when, double value);
295 virtual void maybe_signal_changed ();
297 void _x_scale (double factor);
299 mutable LookupCache _lookup_cache;
300 mutable SearchCache _search_cache;
302 mutable Glib::Threads::RWLock _lock;
304 Parameter _parameter;
305 ParameterDescriptor _desc;
306 InterpolationStyle _interpolation;
309 bool _changed_when_thawed;
312 double _default_value;
318 iterator most_recent_insert_iterator;
319 double insert_position;
321 bool did_write_during_pass;
323 void unlocked_invalidate_insert_iterator ();
324 void add_guard_point (double when);
327 } // namespace Evoral
329 #endif // EVORAL_CONTROL_LIST_HPP