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