1 /* This file is part of Evoral.
2 * Copyright (C) 2008 Dave 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/Parameter.hpp"
35 /** A single event (time-stamped value) for a control
38 ControlEvent (double w, double v)
39 : when (w), value (v), coeff (0)
42 ControlEvent (const ControlEvent& other)
43 : when (other.when), value (other.value), coeff (0)
47 for (size_t i = 0; i < 4; ++i)
48 coeff[i] = other.coeff[i];
52 ~ControlEvent() { if (coeff) delete[] coeff; }
54 void create_coeffs() {
56 coeff = new double[4];
58 coeff[0] = coeff[1] = coeff[2] = coeff[3] = 0.0;
63 double* coeff; ///< double[4] allocated by Curve as needed
67 /** Pool allocator for control lists that does not use a lock
68 * and allocates 8k blocks of new pointers at a time
70 typedef boost::fast_pool_allocator<
72 boost::default_user_allocator_new_delete,
73 boost::details::pool::null_mutex,
75 ControlEventAllocator;
78 /** A list (sequence) of time-stamped values for a control
83 typedef std::list<ControlEvent*,ControlEventAllocator> EventList;
84 typedef EventList::iterator iterator;
85 typedef EventList::reverse_iterator reverse_iterator;
86 typedef EventList::const_iterator const_iterator;
88 ControlList (const Parameter& id);
89 ControlList (const ControlList&);
90 ControlList (const ControlList&, double start, double end);
91 virtual ~ControlList();
93 virtual boost::shared_ptr<ControlList> create(Parameter id);
95 ControlList& operator= (const ControlList&);
96 bool operator== (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 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);
117 void rt_add (double when, double value);
118 void add (double when, double value);
119 void fast_simple_add (double when, double value);
120 void merge_nascent (double when);
122 void reset_range (double start, double end);
123 void erase_range (double start, double end);
124 void erase (iterator);
125 void erase (iterator, iterator);
126 bool move_ranges (std::list< RangeMove<double> > const &);
127 void modify (iterator, double, double);
129 boost::shared_ptr<ControlList> cut (double, double);
130 boost::shared_ptr<ControlList> copy (double, double);
131 void clear (double, double);
133 bool paste (ControlList&, double position, float times);
135 void set_yrange (double min, double max) {
140 double get_max_y() const { return _max_yval; }
141 double get_min_y() const { return _min_yval; }
143 void truncate_end (double length);
144 void truncate_start (double length);
146 iterator begin() { return _events.begin(); }
147 const_iterator begin() const { return _events.begin(); }
148 iterator end() { return _events.end(); }
149 const_iterator end() const { return _events.end(); }
150 ControlEvent* back() { return _events.back(); }
151 const ControlEvent* back() const { return _events.back(); }
152 ControlEvent* front() { return _events.front(); }
153 const ControlEvent* front() const { return _events.front(); }
155 std::pair<ControlList::iterator,ControlList::iterator> control_points_adjacent (double when);
157 template<class T> void apply_to_points (T& obj, void (T::*method)(const ControlList&)) {
158 Glib::Mutex::Lock lm (_lock);
159 (obj.*method)(*this);
162 void set_max_xval (double);
163 double get_max_xval() const { return _max_xval; }
165 double eval (double where) {
166 Glib::Mutex::Lock lm (_lock);
167 return unlocked_eval (where);
170 double rt_safe_eval (double where, bool& ok) {
172 Glib::Mutex::Lock lm (_lock, Glib::TRY_LOCK);
174 if ((ok = lm.locked())) {
175 return unlocked_eval (where);
181 static inline bool time_comparator (const ControlEvent* a, const ControlEvent* b) {
182 return a->when < b->when;
185 /** Lookup cache for eval functions, range contains equivalent values */
187 LookupCache() : left(-1) {}
188 double left; /* leftmost x coordinate used when finding "range" */
189 std::pair<ControlList::const_iterator,ControlList::const_iterator> range;
192 /** Lookup cache for point finding, range contains points after left */
194 SearchCache () : left(-1) {}
195 double left; /* leftmost x coordinate used when finding "first" */
196 ControlList::const_iterator first;
199 const EventList& events() const { return _events; }
200 double default_value() const { return _parameter.normal(); }
202 // FIXME: const violations for Curve
203 Glib::Mutex& lock() const { return _lock; }
204 LookupCache& lookup_cache() const { return _lookup_cache; }
205 SearchCache& search_cache() const { return _search_cache; }
207 /** Called by locked entry point and various private
208 * locations where we already hold the lock.
210 * FIXME: Should this be private? Curve needs it..
212 double unlocked_eval (double x) const;
214 bool rt_safe_earliest_event (double start, double& x, double& y, bool start_inclusive=false) const;
215 bool rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool start_inclusive=false) const;
216 bool rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const;
219 void destroy_curve();
221 Curve& curve() { assert(_curve); return *_curve; }
222 const Curve& curve() const { assert(_curve); return *_curve; }
224 void mark_dirty () const;
226 enum InterpolationStyle {
232 InterpolationStyle interpolation() const { return _interpolation; }
233 void set_interpolation (InterpolationStyle);
235 virtual bool touching() const { return false; }
236 virtual bool writing() const { return false; }
237 virtual bool touch_enabled() const { return false; }
238 void write_pass_finished (double when);
240 /** Emitted when mark_dirty() is called on this object */
241 mutable PBD::Signal0<void> Dirty;
242 /** Emitted when our interpolation style changes */
243 PBD::Signal1<void, InterpolationStyle> InterpolationChanged;
247 /** Called by unlocked_eval() to handle cases of 3 or more control points. */
248 double multipoint_eval (double x) const;
250 void build_search_cache_if_necessary (double start) const;
252 bool rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const;
254 boost::shared_ptr<ControlList> cut_copy_clear (double, double, int op);
255 bool erase_range_internal (double start, double end, EventList &);
257 virtual void maybe_signal_changed ();
259 void _x_scale (double factor);
261 mutable LookupCache _lookup_cache;
262 mutable SearchCache _search_cache;
264 Parameter _parameter;
265 InterpolationStyle _interpolation;
267 mutable Glib::Mutex _lock;
269 bool _changed_when_thawed;
273 double _default_value;
284 NascentInfo (bool touching, double start = -1.0)
285 : is_touch (touching)
291 std::list<NascentInfo*> nascent;
294 } // namespace Evoral
296 #endif // EVORAL_CONTROL_LIST_HPP