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