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"
42 /** A single event (time-stamped value) for a control
44 class LIBEVORAL_API ControlEvent {
46 ControlEvent (double w, double v)
47 : when (w), value (v), coeff (0)
50 ControlEvent (const ControlEvent& other)
51 : when (other.when), value (other.value), coeff (0)
55 for (size_t i = 0; i < 4; ++i)
56 coeff[i] = other.coeff[i];
60 ~ControlEvent() { if (coeff) delete[] coeff; }
62 void create_coeffs() {
64 coeff = new double[4];
66 coeff[0] = coeff[1] = coeff[2] = coeff[3] = 0.0;
71 double* coeff; ///< double[4] allocated by Curve as needed
74 /** A list (sequence) of time-stamped values for a control
76 class LIBEVORAL_API ControlList
79 typedef std::list<ControlEvent*> EventList;
80 typedef EventList::iterator iterator;
81 typedef EventList::reverse_iterator reverse_iterator;
82 typedef EventList::const_iterator const_iterator;
83 typedef EventList::const_reverse_iterator const_reverse_iterator;
85 ControlList (const Parameter& id);
86 ControlList (const ControlList&);
87 ControlList (const ControlList&, double start, double end);
88 virtual ~ControlList();
90 virtual boost::shared_ptr<ControlList> create(Parameter id);
92 void dump (std::ostream&);
94 ControlList& operator= (const ControlList&);
95 bool operator== (const ControlList&);
96 void copy_events (const ControlList&);
98 virtual void freeze();
100 bool frozen() const { return _frozen; }
102 const Parameter& parameter() const { return _parameter; }
103 void set_parameter(const Parameter& p) { _parameter = p; }
105 EventList::size_type size() const { return _events.size(); }
106 double length() const {
107 Glib::Threads::Mutex::Lock lm (_lock);
108 return _events.empty() ? 0.0 : _events.back()->when;
110 bool empty() const { return _events.empty(); }
112 void reset_default (double val) {
113 _default_value = val;
117 void x_scale (double factor);
118 bool extend_to (double);
119 void slide (iterator before, double distance);
120 void shift (double before, double distance);
122 virtual bool clamp_value (double& /*when*/, double& /*value*/) const { return true; }
124 virtual void add (double when, double value, bool with_guards=true);
125 virtual void editor_add (double when, double value);
127 void fast_simple_add (double when, double value);
129 void erase_range (double start, double end);
130 void erase (iterator);
131 void erase (iterator, iterator);
132 void erase (double, double);
133 bool move_ranges (std::list< RangeMove<double> > const &);
134 void modify (iterator, double, double);
138 boost::shared_ptr<ControlList> cut (double, double);
139 boost::shared_ptr<ControlList> copy (double, double);
140 void clear (double, double);
142 bool paste (ControlList&, double position, float times);
144 void set_yrange (double min, double max) {
149 double get_max_y() const { return _max_yval; }
150 double get_min_y() const { return _min_yval; }
152 void truncate_end (double length);
153 void truncate_start (double length);
155 iterator begin() { return _events.begin(); }
156 const_iterator begin() const { return _events.begin(); }
157 iterator end() { return _events.end(); }
158 const_iterator end() const { return _events.end(); }
159 reverse_iterator rbegin() { return _events.rbegin(); }
160 const_reverse_iterator rbegin() const { return _events.rbegin(); }
161 reverse_iterator rend() { return _events.rend(); }
162 const_reverse_iterator rend() const { return _events.rend(); }
163 ControlEvent* back() { return _events.back(); }
164 const ControlEvent* back() const { return _events.back(); }
165 ControlEvent* front() { return _events.front(); }
166 const ControlEvent* front() const { return _events.front(); }
168 std::pair<ControlList::iterator,ControlList::iterator> control_points_adjacent (double when);
170 template<class T> void apply_to_points (T& obj, void (T::*method)(const ControlList&)) {
171 Glib::Threads::Mutex::Lock lm (_lock);
172 (obj.*method)(*this);
175 double eval (double where) {
176 Glib::Threads::Mutex::Lock lm (_lock);
177 return unlocked_eval (where);
180 double rt_safe_eval (double where, bool& ok) {
182 Glib::Threads::Mutex::Lock lm (_lock, Glib::Threads::TRY_LOCK);
184 if ((ok = lm.locked())) {
185 return unlocked_eval (where);
191 static inline bool time_comparator (const ControlEvent* a, const ControlEvent* b) {
192 return a->when < b->when;
195 /** Lookup cache for eval functions, range contains equivalent values */
197 LookupCache() : left(-1) {}
198 double left; /* leftmost x coordinate used when finding "range" */
199 std::pair<ControlList::const_iterator,ControlList::const_iterator> range;
202 /** Lookup cache for point finding, range contains points after left */
204 SearchCache () : left(-1) {}
205 double left; /* leftmost x coordinate used when finding "first" */
206 ControlList::const_iterator first;
209 const EventList& events() const { return _events; }
210 double default_value() const { return _parameter.normal(); }
212 // FIXME: const violations for Curve
213 Glib::Threads::Mutex& lock() const { return _lock; }
214 LookupCache& lookup_cache() const { return _lookup_cache; }
215 SearchCache& search_cache() const { return _search_cache; }
217 /** Called by locked entry point and various private
218 * locations where we already hold the lock.
220 * FIXME: Should this be private? Curve needs it..
222 double unlocked_eval (double x) const;
224 bool rt_safe_earliest_event (double start, double& x, double& y, bool start_inclusive=false) const;
225 bool rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool start_inclusive=false) const;
226 bool rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const;
227 bool rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const;
230 void destroy_curve();
232 Curve& curve() { assert(_curve); return *_curve; }
233 const Curve& curve() const { assert(_curve); return *_curve; }
235 void mark_dirty () const;
237 enum InterpolationStyle {
243 InterpolationStyle interpolation() const { return _interpolation; }
244 void set_interpolation (InterpolationStyle);
246 virtual bool touching() const { return false; }
247 virtual bool writing() const { return false; }
248 virtual bool touch_enabled() const { return false; }
249 void start_write_pass (double time);
250 void write_pass_finished (double when);
251 void set_in_write_pass (bool, bool add_point = false, double when = 0.0);
252 bool in_write_pass () const;
254 /** Emitted when mark_dirty() is called on this object */
255 mutable PBD::Signal0<void> Dirty;
256 /** Emitted when our interpolation style changes */
257 PBD::Signal1<void, InterpolationStyle> InterpolationChanged;
259 static void set_thinning_factor (double d);
260 static double thinning_factor() { return _thinning_factor; }
262 bool operator!= (ControlList const &) const;
264 void invalidate_insert_iterator ();
268 /** Called by unlocked_eval() to handle cases of 3 or more control points. */
269 double multipoint_eval (double x) const;
271 void build_search_cache_if_necessary (double start) const;
273 boost::shared_ptr<ControlList> cut_copy_clear (double, double, int op);
274 bool erase_range_internal (double start, double end, EventList &);
276 virtual void maybe_signal_changed ();
278 void _x_scale (double factor);
280 mutable LookupCache _lookup_cache;
281 mutable SearchCache _search_cache;
283 Parameter _parameter;
284 InterpolationStyle _interpolation;
286 mutable Glib::Threads::Mutex _lock;
288 bool _changed_when_thawed;
291 double _default_value;
296 static double _thinning_factor;
299 iterator most_recent_insert_iterator;
300 double insert_position;
302 bool did_write_during_pass;
304 void unlocked_invalidate_insert_iterator ();
305 void add_guard_point (double when);
308 } // namespace Evoral
310 #endif // EVORAL_CONTROL_LIST_HPP