Merge branch 'master' into cairocanvas
[ardour.git] / libs / evoral / evoral / ControlList.hpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 David Robillard <http://drobilla.net>
3  * Copyright (C) 2000-2008 Paul Davis
4  *
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
8  * version.
9  *
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.
13  *
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
17  */
18
19 #ifndef EVORAL_CONTROL_LIST_HPP
20 #define EVORAL_CONTROL_LIST_HPP
21
22 #include <cassert>
23 #include <list>
24 #include <boost/pool/pool.hpp>
25 #include <boost/pool/pool_alloc.hpp>
26 #include <glibmm/threads.h>
27 #include "pbd/signals.h"
28
29 #include "evoral/visibility.h"
30 #include "evoral/types.hpp"
31 #include "evoral/Range.hpp"
32 #include "evoral/Parameter.hpp"
33
34 namespace Evoral {
35
36 class Curve;
37
38 /** A single event (time-stamped value) for a control
39  */
40 class LIBEVORAL_API ControlEvent {
41 public:
42         ControlEvent (double w, double v)
43                 : when (w), value (v), coeff (0)
44         {}
45
46         ControlEvent (const ControlEvent& other)
47                 : when (other.when), value (other.value), coeff (0)
48         {
49                 if (other.coeff) {
50                         create_coeffs();
51                         for (size_t i = 0; i < 4; ++i)
52                                 coeff[i] = other.coeff[i];
53                 }
54         }
55
56         ~ControlEvent() { if (coeff) delete[] coeff; }
57
58         void create_coeffs() {
59                 if (!coeff)
60                         coeff = new double[4];
61
62                 coeff[0] = coeff[1] = coeff[2] = coeff[3] = 0.0;
63         }
64
65         double  when;
66         double  value;
67         double* coeff; ///< double[4] allocated by Curve as needed
68 };
69
70 /** A list (sequence) of time-stamped values for a control
71  */
72 class LIBEVORAL_API ControlList
73 {
74 public:
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;
80
81         ControlList (const Parameter& id);
82         ControlList (const ControlList&);
83         ControlList (const ControlList&, double start, double end);
84         virtual ~ControlList();
85
86         virtual boost::shared_ptr<ControlList> create(Parameter id);
87
88         void dump (std::ostream&);
89
90         ControlList& operator= (const ControlList&);
91         bool operator== (const ControlList&);
92         void copy_events (const ControlList&);
93
94         virtual void freeze();
95         virtual void thaw ();
96         bool frozen() const { return _frozen; }
97
98         const Parameter& parameter() const                 { return _parameter; }
99         void             set_parameter(const Parameter& p) { _parameter = p; }
100
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;
105         }
106         bool empty() const { return _events.empty(); }
107
108         void reset_default (double val) {
109                 _default_value = val;
110         }
111
112         void clear ();
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);
117
118         virtual bool clamp_value (double& /*when*/, double& /*value*/) const { return true; }
119
120         virtual void add (double when, double value);
121         void fast_simple_add (double when, double value);
122
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);
129
130         void thin ();
131
132         boost::shared_ptr<ControlList> cut (double, double);
133         boost::shared_ptr<ControlList> copy (double, double);
134         void clear (double, double);
135
136         bool paste (ControlList&, double position, float times);
137
138         void set_yrange (double min, double max) {
139                 _min_yval = min;
140                 _max_yval = max;
141         }
142
143         double get_max_y() const { return _max_yval; }
144         double get_min_y() const { return _min_yval; }
145
146         void truncate_end (double length);
147         void truncate_start (double length);
148
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(); }
161
162         std::pair<ControlList::iterator,ControlList::iterator> control_points_adjacent (double when);
163
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);
167         }
168
169         double eval (double where) {
170                 Glib::Threads::Mutex::Lock lm (_lock);
171                 return unlocked_eval (where);
172         }
173
174         double rt_safe_eval (double where, bool& ok) {
175
176                 Glib::Threads::Mutex::Lock lm (_lock, Glib::Threads::TRY_LOCK);
177
178                 if ((ok = lm.locked())) {
179                         return unlocked_eval (where);
180                 } else {
181                         return 0.0;
182                 }
183         }
184
185         static inline bool time_comparator (const ControlEvent* a, const ControlEvent* b) {
186                 return a->when < b->when;
187         }
188
189         /** Lookup cache for eval functions, range contains equivalent values */
190         struct LookupCache {
191                 LookupCache() : left(-1) {}
192                 double left;  /* leftmost x coordinate used when finding "range" */
193                 std::pair<ControlList::const_iterator,ControlList::const_iterator> range;
194         };
195
196         /** Lookup cache for point finding, range contains points after left */
197         struct SearchCache {
198                 SearchCache () : left(-1) {}
199                 double left;  /* leftmost x coordinate used when finding "first" */
200                 ControlList::const_iterator first;
201         };
202
203         const EventList& events() const { return _events; }
204         double default_value() const { return _parameter.normal(); }
205
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; }
210
211         /** Called by locked entry point and various private
212          * locations where we already hold the lock.
213          *
214          * FIXME: Should this be private?  Curve needs it..
215          */
216         double unlocked_eval (double x) const;
217
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;
222
223         void create_curve();
224         void destroy_curve();
225
226         Curve&       curve()       { assert(_curve); return *_curve; }
227         const Curve& curve() const { assert(_curve); return *_curve; }
228
229         void mark_dirty () const;
230
231         enum InterpolationStyle {
232                 Discrete,
233                 Linear,
234                 Curved
235         };
236
237         InterpolationStyle interpolation() const { return _interpolation; }
238         void set_interpolation (InterpolationStyle);
239
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;
247
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;
252
253         static void set_thinning_factor (double d);
254         static double thinning_factor() { return _thinning_factor; }
255
256         bool operator!= (ControlList const &) const;
257
258         void invalidate_insert_iterator ();
259
260 protected:
261
262         /** Called by unlocked_eval() to handle cases of 3 or more control points. */
263         double multipoint_eval (double x) const;
264
265         void build_search_cache_if_necessary (double start) const;
266
267         boost::shared_ptr<ControlList> cut_copy_clear (double, double, int op);
268         bool erase_range_internal (double start, double end, EventList &);
269
270         virtual void maybe_signal_changed ();
271
272         void _x_scale (double factor);
273
274         mutable LookupCache   _lookup_cache;
275         mutable SearchCache   _search_cache;
276
277         Parameter             _parameter;
278         InterpolationStyle    _interpolation;
279         EventList             _events;
280         mutable Glib::Threads::Mutex   _lock;
281         int8_t                _frozen;
282         bool                  _changed_when_thawed;
283         double                _min_yval;
284         double                _max_yval;
285         double                _default_value;
286         bool                  _sort_pending;
287
288         Curve* _curve;
289
290         static double _thinning_factor;
291
292   private:
293     iterator   most_recent_insert_iterator;
294     double     insert_position;
295     bool       new_write_pass;
296     bool       did_write_during_pass;
297     bool       _in_write_pass;
298     void unlocked_invalidate_insert_iterator ();
299     void add_guard_point (double when);
300 };
301
302 } // namespace Evoral
303
304 #endif // EVORAL_CONTROL_LIST_HPP
305