Compile cleanly with clang.
[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/thread.h>
27 #include "pbd/signals.h"
28 #include "evoral/types.hpp"
29 #include "evoral/Range.hpp"
30 #include "evoral/Parameter.hpp"
31
32 namespace Evoral {
33
34 class Curve;
35
36 /** A single event (time-stamped value) for a control
37  */
38 class ControlEvent {
39 public:
40         ControlEvent (double w, double v)
41                 : when (w), value (v), coeff (0)
42         {}
43
44         ControlEvent (const ControlEvent& other)
45                 : when (other.when), value (other.value), coeff (0)
46         {
47                 if (other.coeff) {
48                         create_coeffs();
49                         for (size_t i = 0; i < 4; ++i)
50                                 coeff[i] = other.coeff[i];
51                 }
52         }
53
54         ~ControlEvent() { if (coeff) delete[] coeff; }
55
56         void create_coeffs() {
57                 if (!coeff)
58                         coeff = new double[4];
59
60                 coeff[0] = coeff[1] = coeff[2] = coeff[3] = 0.0;
61         }
62
63         double  when;
64         double  value;
65         double* coeff; ///< double[4] allocated by Curve as needed
66 };
67
68
69 /** Pool allocator for control lists that does not use a lock
70  * and allocates 8k blocks of new pointers at a time
71  */
72 typedef boost::fast_pool_allocator<
73         ControlEvent*,
74         boost::default_user_allocator_new_delete,
75         boost::details::pool::null_mutex,
76         8192>
77 ControlEventAllocator;
78
79
80 /** A list (sequence) of time-stamped values for a control
81  */
82 class ControlList
83 {
84 public:
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;
89
90         ControlList (const Parameter& id);
91         ControlList (const ControlList&);
92         ControlList (const ControlList&, double start, double end);
93         virtual ~ControlList();
94
95         virtual boost::shared_ptr<ControlList> create(Parameter id);
96
97         ControlList& operator= (const ControlList&);
98         bool operator== (const ControlList&);
99         void copy_events (const ControlList&);
100
101         virtual void freeze();
102         virtual void thaw ();
103         bool frozen() const { return _frozen; }
104
105         const Parameter& parameter() const                 { return _parameter; }
106         void             set_parameter(const Parameter& p) { _parameter = p; }
107
108         EventList::size_type size() const { return _events.size(); }
109         bool empty() const { return _events.empty(); }
110
111         void reset_default (double val) {
112                 _default_value = val;
113         }
114
115         void clear ();
116         void x_scale (double factor);
117         bool extend_to (double);
118         void slide (iterator before, double distance);
119         void shift (double before, double distance);
120
121         virtual bool clamp_value (double& /*when*/, double& /*value*/) const { return true; }
122
123         void rt_add (double when, double value);
124         void add (double when, double value);
125         void fast_simple_add (double when, double value);
126         void merge_nascent (double when);
127
128         void erase_range (double start, double end);
129         void erase (iterator);
130         void erase (iterator, iterator);
131         void erase (double, double);
132         bool move_ranges (std::list< RangeMove<double> > const &);
133         void modify (iterator, double, double);
134
135         void thin ();
136
137         boost::shared_ptr<ControlList> cut (double, double);
138         boost::shared_ptr<ControlList> copy (double, double);
139         void clear (double, double);
140
141         bool paste (ControlList&, double position, float times);
142
143         void set_yrange (double min, double max) {
144                 _min_yval = min;
145                 _max_yval = max;
146         }
147
148         double get_max_y() const { return _max_yval; }
149         double get_min_y() const { return _min_yval; }
150
151         void truncate_end (double length);
152         void truncate_start (double length);
153
154         iterator            begin()       { return _events.begin(); }
155         const_iterator      begin() const { return _events.begin(); }
156         iterator            end()         { return _events.end(); }
157         const_iterator      end()   const { return _events.end(); }
158         ControlEvent*       back()        { return _events.back(); }
159         const ControlEvent* back()  const { return _events.back(); }
160         ControlEvent*       front()       { return _events.front(); }
161         const ControlEvent* front() const { return _events.front(); }
162
163         std::pair<ControlList::iterator,ControlList::iterator> control_points_adjacent (double when);
164
165         template<class T> void apply_to_points (T& obj, void (T::*method)(const ControlList&)) {
166                 Glib::Mutex::Lock lm (_lock);
167                 (obj.*method)(*this);
168         }
169
170         double eval (double where) {
171                 Glib::Mutex::Lock lm (_lock);
172                 return unlocked_eval (where);
173         }
174
175         double rt_safe_eval (double where, bool& ok) {
176
177                 Glib::Mutex::Lock lm (_lock, Glib::TRY_LOCK);
178
179                 if ((ok = lm.locked())) {
180                         return unlocked_eval (where);
181                 } else {
182                         return 0.0;
183                 }
184         }
185
186         static inline bool time_comparator (const ControlEvent* a, const ControlEvent* b) {
187                 return a->when < b->when;
188         }
189
190         /** Lookup cache for eval functions, range contains equivalent values */
191         struct LookupCache {
192                 LookupCache() : left(-1) {}
193                 double left;  /* leftmost x coordinate used when finding "range" */
194                 std::pair<ControlList::const_iterator,ControlList::const_iterator> range;
195         };
196
197         /** Lookup cache for point finding, range contains points after left */
198         struct SearchCache {
199                 SearchCache () : left(-1) {}
200                 double left;  /* leftmost x coordinate used when finding "first" */
201                 ControlList::const_iterator first;
202         };
203
204         const EventList& events() const { return _events; }
205         double default_value() const { return _parameter.normal(); }
206
207         // FIXME: const violations for Curve
208         Glib::Mutex& lock()         const { return _lock; }
209         LookupCache& lookup_cache() const { return _lookup_cache; }
210         SearchCache& search_cache() const { return _search_cache; }
211
212         /** Called by locked entry point and various private
213          * locations where we already hold the lock.
214          *
215          * FIXME: Should this be private?  Curve needs it..
216          */
217         double unlocked_eval (double x) const;
218
219         bool rt_safe_earliest_event (double start, double& x, double& y, bool start_inclusive=false) const;
220         bool rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool start_inclusive=false) const;
221         bool rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const;
222         bool rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const;
223
224         void create_curve();
225         void destroy_curve();
226
227         Curve&       curve()       { assert(_curve); return *_curve; }
228         const Curve& curve() const { assert(_curve); return *_curve; }
229
230         void mark_dirty () const;
231
232         enum InterpolationStyle {
233                 Discrete,
234                 Linear,
235                 Curved
236         };
237
238         InterpolationStyle interpolation() const { return _interpolation; }
239         void set_interpolation (InterpolationStyle);
240
241         virtual bool touching() const { return false; }
242         virtual bool writing() const { return false; }
243         virtual bool touch_enabled() const { return false; }
244         void write_pass_finished (double when);
245
246         /** Emitted when mark_dirty() is called on this object */
247         mutable PBD::Signal0<void> Dirty;
248         /** Emitted when our interpolation style changes */
249         PBD::Signal1<void, InterpolationStyle> InterpolationChanged;
250
251         static void set_thinning_factor (double d);
252         static double thinning_factor() { return _thinning_factor; }
253
254 protected:
255
256         /** Called by unlocked_eval() to handle cases of 3 or more control points. */
257         double multipoint_eval (double x) const;
258
259         void build_search_cache_if_necessary (double start) const;
260
261         boost::shared_ptr<ControlList> cut_copy_clear (double, double, int op);
262         bool erase_range_internal (double start, double end, EventList &);
263
264         virtual void maybe_signal_changed ();
265
266         void _x_scale (double factor);
267
268         mutable LookupCache   _lookup_cache;
269         mutable SearchCache   _search_cache;
270
271         Parameter             _parameter;
272         InterpolationStyle    _interpolation;
273         EventList             _events;
274         mutable Glib::Mutex   _lock;
275         int8_t                _frozen;
276         bool                  _changed_when_thawed;
277         double                _min_yval;
278         double                _max_yval;
279         double                _default_value;
280         bool                  _sort_pending;
281
282         Curve* _curve;
283
284         struct NascentInfo {
285                 EventList events;
286                 double start_time;
287                 double end_time;
288
289                 NascentInfo (double start = -1.0)
290                         : start_time (start)
291                         , end_time (-1.0)
292                 {}
293         };
294
295         std::list<NascentInfo*> nascent;
296         static double _thinning_factor;
297 };
298
299 } // namespace Evoral
300
301 #endif // EVORAL_CONTROL_LIST_HPP
302