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);
128 virtual void editor_add (double when, double value, bool with_guard);
130 void fast_simple_add (double when, double value);
132 void erase_range (double start, double end);
133 void erase (iterator);
134 void erase (iterator, iterator);
135 void erase (double, double);
136 bool move_ranges (std::list< RangeMove<double> > const &);
137 void modify (iterator, double, double);
139 /** Thin the number of events in this list.
141 * The thinning factor has no units but corresponds to the area of a
142 * triangle computed between three points in the list. If the area is
143 * large, it indicates significant non-linearity between the points.
145 * During automation recording we thin the recorded points using this
146 * value. If a point is sufficiently co-linear with its neighbours (as
147 * defined by the area of the triangle formed by three of them), we will
148 * not include it in the list. The larger the value, the more points are
149 * excluded, so this effectively measures the amount of thinning to be
152 void thin (double thinning_factor);
154 boost::shared_ptr<ControlList> cut (double, double);
155 boost::shared_ptr<ControlList> copy (double, double);
156 void clear (double, double);
158 bool paste (const ControlList&, double position, float times);
160 void set_yrange (double min, double max) {
165 double get_max_y() const { return _max_yval; }
166 double get_min_y() const { return _min_yval; }
168 void truncate_end (double length);
169 void truncate_start (double length);
171 iterator begin() { return _events.begin(); }
172 const_iterator begin() const { return _events.begin(); }
173 iterator end() { return _events.end(); }
174 const_iterator end() const { return _events.end(); }
175 reverse_iterator rbegin() { return _events.rbegin(); }
176 const_reverse_iterator rbegin() const { return _events.rbegin(); }
177 reverse_iterator rend() { return _events.rend(); }
178 const_reverse_iterator rend() const { return _events.rend(); }
179 ControlEvent* back() { return _events.back(); }
180 const ControlEvent* back() const { return _events.back(); }
181 ControlEvent* front() { return _events.front(); }
182 const ControlEvent* front() const { return _events.front(); }
184 std::pair<ControlList::iterator,ControlList::iterator> control_points_adjacent (double when);
186 template<class T> void apply_to_points (T& obj, void (T::*method)(const ControlList&)) {
187 Glib::Threads::RWLock::WriterLock lm (_lock);
188 (obj.*method)(*this);
191 double eval (double where) {
192 Glib::Threads::RWLock::ReaderLock lm (_lock);
193 return unlocked_eval (where);
196 double rt_safe_eval (double where, bool& ok) {
198 Glib::Threads::RWLock::ReaderLock lm (_lock, Glib::Threads::TRY_LOCK);
200 if ((ok = lm.locked())) {
201 return unlocked_eval (where);
207 static inline bool time_comparator (const ControlEvent* a, const ControlEvent* b) {
208 return a->when < b->when;
211 /** Lookup cache for eval functions, range contains equivalent values */
213 LookupCache() : left(-1) {}
214 double left; /* leftmost x coordinate used when finding "range" */
215 std::pair<ControlList::const_iterator,ControlList::const_iterator> range;
218 /** Lookup cache for point finding, range contains points after left */
220 SearchCache () : left(-1) {}
221 double left; /* leftmost x coordinate used when finding "first" */
222 ControlList::const_iterator first;
225 const EventList& events() const { return _events; }
226 double default_value() const { return _default_value; }
228 // FIXME: const violations for Curve
229 Glib::Threads::RWLock& lock() const { return _lock; }
230 LookupCache& lookup_cache() const { return _lookup_cache; }
231 SearchCache& search_cache() const { return _search_cache; }
233 /** Called by locked entry point and various private
234 * locations where we already hold the lock.
236 * FIXME: Should this be private? Curve needs it..
238 double unlocked_eval (double x) const;
240 bool rt_safe_earliest_event (double start, double& x, double& y, bool start_inclusive=false) const;
241 bool rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool start_inclusive=false) const;
242 bool rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const;
243 bool rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const;
246 void destroy_curve();
248 Curve& curve() { assert(_curve); return *_curve; }
249 const Curve& curve() const { assert(_curve); return *_curve; }
251 void mark_dirty () const;
253 enum InterpolationStyle {
259 InterpolationStyle interpolation() const { return _interpolation; }
260 void set_interpolation (InterpolationStyle);
262 virtual bool touching() const { return false; }
263 virtual bool writing() const { return false; }
264 virtual bool touch_enabled() const { return false; }
265 void start_write_pass (double time);
266 void write_pass_finished (double when, double thinning_factor=0.0);
267 void set_in_write_pass (bool, bool add_point = false, double when = 0.0);
268 bool in_write_pass () const;
270 /** Emitted when mark_dirty() is called on this object */
271 mutable PBD::Signal0<void> Dirty;
272 /** Emitted when our interpolation style changes */
273 PBD::Signal1<void, InterpolationStyle> InterpolationChanged;
275 bool operator!= (ControlList const &) const;
277 void invalidate_insert_iterator ();
281 /** Called by unlocked_eval() to handle cases of 3 or more control points. */
282 double multipoint_eval (double x) const;
284 void build_search_cache_if_necessary (double start) const;
286 boost::shared_ptr<ControlList> cut_copy_clear (double, double, int op);
287 bool erase_range_internal (double start, double end, EventList &);
289 void maybe_add_insert_guard (double when);
290 iterator erase_from_iterator_to (iterator iter, double when);
291 bool maybe_insert_straight_line (double when, double value);
293 virtual void maybe_signal_changed ();
295 void _x_scale (double factor);
297 mutable LookupCache _lookup_cache;
298 mutable SearchCache _search_cache;
300 mutable Glib::Threads::RWLock _lock;
302 Parameter _parameter;
303 ParameterDescriptor _desc;
304 InterpolationStyle _interpolation;
307 bool _changed_when_thawed;
310 double _default_value;
316 iterator most_recent_insert_iterator;
317 double insert_position;
319 bool did_write_during_pass;
321 void unlocked_invalidate_insert_iterator ();
322 void add_guard_point (double when);
325 } // namespace Evoral
327 #endif // EVORAL_CONTROL_LIST_HPP