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/thread.h>
27 #include "pbd/signals.h"
28 #include "evoral/types.hpp"
29 #include "evoral/Range.hpp"
30 #include "evoral/Parameter.hpp"
36 /** A single event (time-stamped value) for a control
40 ControlEvent (double w, double v)
41 : when (w), value (v), coeff (0)
44 ControlEvent (const ControlEvent& other)
45 : when (other.when), value (other.value), coeff (0)
49 for (size_t i = 0; i < 4; ++i)
50 coeff[i] = other.coeff[i];
54 ~ControlEvent() { if (coeff) delete[] coeff; }
56 void create_coeffs() {
58 coeff = new double[4];
60 coeff[0] = coeff[1] = coeff[2] = coeff[3] = 0.0;
65 double* coeff; ///< double[4] allocated by Curve as needed
69 /** Pool allocator for control lists that does not use a lock
70 * and allocates 8k blocks of new pointers at a time
72 typedef boost::fast_pool_allocator<
74 boost::default_user_allocator_new_delete,
75 boost::details::pool::null_mutex,
77 ControlEventAllocator;
80 /** A list (sequence) of time-stamped values for a control
85 typedef std::list<ControlEvent*,ControlEventAllocator> EventList;
86 typedef EventList::iterator iterator;
87 typedef EventList::reverse_iterator reverse_iterator;
88 typedef EventList::const_iterator const_iterator;
90 ControlList (const Parameter& id);
91 ControlList (const ControlList&);
92 ControlList (const ControlList&, double start, double end);
93 virtual ~ControlList();
95 virtual boost::shared_ptr<ControlList> create(Parameter id);
97 ControlList& operator= (const ControlList&);
98 bool operator== (const ControlList&);
99 void copy_events (const ControlList&);
101 virtual void freeze();
102 virtual void thaw ();
103 bool frozen() const { return _frozen; }
105 const Parameter& parameter() const { return _parameter; }
106 void set_parameter(const Parameter& p) { _parameter = p; }
108 EventList::size_type size() const { return _events.size(); }
109 double length() const {
110 Glib::Mutex::Lock lm (_lock);
111 return _events.empty() ? 0.0 : _events.back()->when;
113 bool empty() const { return _events.empty(); }
115 void reset_default (double val) {
116 _default_value = val;
120 void x_scale (double factor);
121 bool extend_to (double);
122 void slide (iterator before, double distance);
123 void shift (double before, double distance);
125 virtual bool clamp_value (double& /*when*/, double& /*value*/) const { return true; }
127 void rt_add (double when, double value);
128 void add (double when, double value);
129 void fast_simple_add (double when, double value);
130 void merge_nascent (double when);
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);
141 boost::shared_ptr<ControlList> cut (double, double);
142 boost::shared_ptr<ControlList> copy (double, double);
143 void clear (double, double);
145 bool paste (ControlList&, double position, float times);
147 void set_yrange (double min, double max) {
152 double get_max_y() const { return _max_yval; }
153 double get_min_y() const { return _min_yval; }
155 void truncate_end (double length);
156 void truncate_start (double length);
158 iterator begin() { return _events.begin(); }
159 const_iterator begin() const { return _events.begin(); }
160 iterator end() { return _events.end(); }
161 const_iterator end() const { return _events.end(); }
162 ControlEvent* back() { return _events.back(); }
163 const ControlEvent* back() const { return _events.back(); }
164 ControlEvent* front() { return _events.front(); }
165 const ControlEvent* front() const { return _events.front(); }
167 std::pair<ControlList::iterator,ControlList::iterator> control_points_adjacent (double when);
169 template<class T> void apply_to_points (T& obj, void (T::*method)(const ControlList&)) {
170 Glib::Mutex::Lock lm (_lock);
171 (obj.*method)(*this);
174 double eval (double where) {
175 Glib::Mutex::Lock lm (_lock);
176 return unlocked_eval (where);
179 double rt_safe_eval (double where, bool& ok) {
181 Glib::Mutex::Lock lm (_lock, Glib::TRY_LOCK);
183 if ((ok = lm.locked())) {
184 return unlocked_eval (where);
190 static inline bool time_comparator (const ControlEvent* a, const ControlEvent* b) {
191 return a->when < b->when;
194 /** Lookup cache for eval functions, range contains equivalent values */
196 LookupCache() : left(-1) {}
197 double left; /* leftmost x coordinate used when finding "range" */
198 std::pair<ControlList::const_iterator,ControlList::const_iterator> range;
201 /** Lookup cache for point finding, range contains points after left */
203 SearchCache () : left(-1) {}
204 double left; /* leftmost x coordinate used when finding "first" */
205 ControlList::const_iterator first;
208 const EventList& events() const { return _events; }
209 double default_value() const { return _parameter.normal(); }
211 // FIXME: const violations for Curve
212 Glib::Mutex& lock() const { return _lock; }
213 LookupCache& lookup_cache() const { return _lookup_cache; }
214 SearchCache& search_cache() const { return _search_cache; }
216 /** Called by locked entry point and various private
217 * locations where we already hold the lock.
219 * FIXME: Should this be private? Curve needs it..
221 double unlocked_eval (double x) const;
223 bool rt_safe_earliest_event (double start, double& x, double& y, bool start_inclusive=false) const;
224 bool rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool start_inclusive=false) const;
225 bool rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const;
226 bool rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const;
229 void destroy_curve();
231 Curve& curve() { assert(_curve); return *_curve; }
232 const Curve& curve() const { assert(_curve); return *_curve; }
234 void mark_dirty () const;
236 enum InterpolationStyle {
242 InterpolationStyle interpolation() const { return _interpolation; }
243 void set_interpolation (InterpolationStyle);
245 virtual bool touching() const { return false; }
246 virtual bool writing() const { return false; }
247 virtual bool touch_enabled() const { return false; }
248 void write_pass_finished (double when);
250 /** Emitted when mark_dirty() is called on this object */
251 mutable PBD::Signal0<void> Dirty;
252 /** Emitted when our interpolation style changes */
253 PBD::Signal1<void, InterpolationStyle> InterpolationChanged;
255 static void set_thinning_factor (double d);
256 static double thinning_factor() { return _thinning_factor; }
260 /** Called by unlocked_eval() to handle cases of 3 or more control points. */
261 double multipoint_eval (double x) const;
263 void build_search_cache_if_necessary (double start) const;
265 boost::shared_ptr<ControlList> cut_copy_clear (double, double, int op);
266 bool erase_range_internal (double start, double end, EventList &);
268 virtual void maybe_signal_changed ();
270 void _x_scale (double factor);
272 mutable LookupCache _lookup_cache;
273 mutable SearchCache _search_cache;
275 Parameter _parameter;
276 InterpolationStyle _interpolation;
278 mutable Glib::Mutex _lock;
280 bool _changed_when_thawed;
283 double _default_value;
293 NascentInfo (double start = -1.0)
299 std::list<NascentInfo*> nascent;
300 static double _thinning_factor;
303 } // namespace Evoral
305 #endif // EVORAL_CONTROL_LIST_HPP