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
24 #include <boost/pool/pool.hpp>
25 #include <boost/pool/pool_alloc.hpp>
26 #include <glibmm/threads.h>
27 #include "pbd/signals.h"
29 #include "evoral/visibility.h"
30 #include "evoral/types.hpp"
31 #include "evoral/Range.hpp"
32 #include "evoral/Parameter.hpp"
38 /** A single event (time-stamped value) for a control
40 class LIBEVORAL_API ControlEvent {
42 ControlEvent (double w, double v)
43 : when (w), value (v), coeff (0)
46 ControlEvent (const ControlEvent& other)
47 : when (other.when), value (other.value), coeff (0)
51 for (size_t i = 0; i < 4; ++i)
52 coeff[i] = other.coeff[i];
56 ~ControlEvent() { if (coeff) delete[] coeff; }
58 void create_coeffs() {
60 coeff = new double[4];
62 coeff[0] = coeff[1] = coeff[2] = coeff[3] = 0.0;
67 double* coeff; ///< double[4] allocated by Curve as needed
70 /** A list (sequence) of time-stamped values for a control
72 class LIBEVORAL_API ControlList
75 typedef std::list<ControlEvent*> EventList;
76 typedef EventList::iterator iterator;
77 typedef EventList::reverse_iterator reverse_iterator;
78 typedef EventList::const_iterator const_iterator;
79 typedef EventList::const_reverse_iterator const_reverse_iterator;
81 ControlList (const Parameter& id);
82 ControlList (const ControlList&);
83 ControlList (const ControlList&, double start, double end);
84 virtual ~ControlList();
86 virtual boost::shared_ptr<ControlList> create(Parameter id);
88 void dump (std::ostream&);
90 ControlList& operator= (const ControlList&);
91 bool operator== (const ControlList&);
92 void copy_events (const ControlList&);
94 virtual void freeze();
96 bool frozen() const { return _frozen; }
98 const Parameter& parameter() const { return _parameter; }
99 void set_parameter(const Parameter& p) { _parameter = p; }
101 EventList::size_type size() const { return _events.size(); }
102 double length() const {
103 Glib::Threads::Mutex::Lock lm (_lock);
104 return _events.empty() ? 0.0 : _events.back()->when;
106 bool empty() const { return _events.empty(); }
108 void reset_default (double val) {
109 _default_value = val;
113 void x_scale (double factor);
114 bool extend_to (double);
115 void slide (iterator before, double distance);
116 void shift (double before, double distance);
118 virtual bool clamp_value (double& /*when*/, double& /*value*/) const { return true; }
120 virtual void add (double when, double value);
121 void fast_simple_add (double when, double value);
123 void erase_range (double start, double end);
124 void erase (iterator);
125 void erase (iterator, iterator);
126 void erase (double, double);
127 bool move_ranges (std::list< RangeMove<double> > const &);
128 void modify (iterator, double, double);
132 boost::shared_ptr<ControlList> cut (double, double);
133 boost::shared_ptr<ControlList> copy (double, double);
134 void clear (double, double);
136 bool paste (ControlList&, double position, float times);
138 void set_yrange (double min, double max) {
143 double get_max_y() const { return _max_yval; }
144 double get_min_y() const { return _min_yval; }
146 void truncate_end (double length);
147 void truncate_start (double length);
149 iterator begin() { return _events.begin(); }
150 const_iterator begin() const { return _events.begin(); }
151 iterator end() { return _events.end(); }
152 const_iterator end() const { return _events.end(); }
153 reverse_iterator rbegin() { return _events.rbegin(); }
154 const_reverse_iterator rbegin() const { return _events.rbegin(); }
155 reverse_iterator rend() { return _events.rend(); }
156 const_reverse_iterator rend() const { return _events.rend(); }
157 ControlEvent* back() { return _events.back(); }
158 const ControlEvent* back() const { return _events.back(); }
159 ControlEvent* front() { return _events.front(); }
160 const ControlEvent* front() const { return _events.front(); }
162 std::pair<ControlList::iterator,ControlList::iterator> control_points_adjacent (double when);
164 template<class T> void apply_to_points (T& obj, void (T::*method)(const ControlList&)) {
165 Glib::Threads::Mutex::Lock lm (_lock);
166 (obj.*method)(*this);
169 double eval (double where) {
170 Glib::Threads::Mutex::Lock lm (_lock);
171 return unlocked_eval (where);
174 double rt_safe_eval (double where, bool& ok) {
176 Glib::Threads::Mutex::Lock lm (_lock, Glib::Threads::TRY_LOCK);
178 if ((ok = lm.locked())) {
179 return unlocked_eval (where);
185 static inline bool time_comparator (const ControlEvent* a, const ControlEvent* b) {
186 return a->when < b->when;
189 /** Lookup cache for eval functions, range contains equivalent values */
191 LookupCache() : left(-1) {}
192 double left; /* leftmost x coordinate used when finding "range" */
193 std::pair<ControlList::const_iterator,ControlList::const_iterator> range;
196 /** Lookup cache for point finding, range contains points after left */
198 SearchCache () : left(-1) {}
199 double left; /* leftmost x coordinate used when finding "first" */
200 ControlList::const_iterator first;
203 const EventList& events() const { return _events; }
204 double default_value() const { return _parameter.normal(); }
206 // FIXME: const violations for Curve
207 Glib::Threads::Mutex& lock() const { return _lock; }
208 LookupCache& lookup_cache() const { return _lookup_cache; }
209 SearchCache& search_cache() const { return _search_cache; }
211 /** Called by locked entry point and various private
212 * locations where we already hold the lock.
214 * FIXME: Should this be private? Curve needs it..
216 double unlocked_eval (double x) const;
218 bool rt_safe_earliest_event (double start, double& x, double& y, bool start_inclusive=false) const;
219 bool rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool start_inclusive=false) const;
220 bool rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const;
221 bool rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const;
224 void destroy_curve();
226 Curve& curve() { assert(_curve); return *_curve; }
227 const Curve& curve() const { assert(_curve); return *_curve; }
229 void mark_dirty () const;
231 enum InterpolationStyle {
237 InterpolationStyle interpolation() const { return _interpolation; }
238 void set_interpolation (InterpolationStyle);
240 virtual bool touching() const { return false; }
241 virtual bool writing() const { return false; }
242 virtual bool touch_enabled() const { return false; }
243 void start_write_pass (double time);
244 void write_pass_finished (double when);
245 void set_in_write_pass (bool, bool add_point = false, double when = 0.0);
246 bool in_write_pass () const;
248 /** Emitted when mark_dirty() is called on this object */
249 mutable PBD::Signal0<void> Dirty;
250 /** Emitted when our interpolation style changes */
251 PBD::Signal1<void, InterpolationStyle> InterpolationChanged;
253 static void set_thinning_factor (double d);
254 static double thinning_factor() { return _thinning_factor; }
256 bool operator!= (ControlList const &) const;
258 void invalidate_insert_iterator ();
262 /** Called by unlocked_eval() to handle cases of 3 or more control points. */
263 double multipoint_eval (double x) const;
265 void build_search_cache_if_necessary (double start) const;
267 boost::shared_ptr<ControlList> cut_copy_clear (double, double, int op);
268 bool erase_range_internal (double start, double end, EventList &);
270 virtual void maybe_signal_changed ();
272 void _x_scale (double factor);
274 mutable LookupCache _lookup_cache;
275 mutable SearchCache _search_cache;
277 Parameter _parameter;
278 InterpolationStyle _interpolation;
280 mutable Glib::Threads::Mutex _lock;
282 bool _changed_when_thawed;
285 double _default_value;
290 static double _thinning_factor;
293 iterator most_recent_insert_iterator;
294 double insert_position;
296 bool did_write_during_pass;
298 void unlocked_invalidate_insert_iterator ();
299 void add_guard_point (double when);
302 } // namespace Evoral
304 #endif // EVORAL_CONTROL_LIST_HPP