3784b2572054331806376a1e0b7fe05ebdf49e19
[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 <stdint.h>
25
26 #include <boost/pool/pool.hpp>
27 #include <boost/pool/pool_alloc.hpp>
28
29 #include <glibmm/threads.h>
30
31 #include "pbd/signals.h"
32
33 #include "evoral/visibility.h"
34 #include "evoral/types.hpp"
35 #include "evoral/Range.hpp"
36 #include "evoral/Parameter.hpp"
37 #include "evoral/ParameterDescriptor.hpp"
38
39 namespace Evoral {
40
41 class Curve;
42 class TypeMap;
43
44 /** A single event (time-stamped value) for a control
45  */
46 class LIBEVORAL_API ControlEvent {
47 public:
48         ControlEvent (double w, double v)
49                 : when (w), value (v), coeff (0)
50         {}
51
52         ControlEvent (const ControlEvent& other)
53                 : when (other.when), value (other.value), coeff (0)
54         {
55                 if (other.coeff) {
56                         create_coeffs();
57                         for (size_t i = 0; i < 4; ++i)
58                                 coeff[i] = other.coeff[i];
59                 }
60         }
61
62         ~ControlEvent() { if (coeff) delete[] coeff; }
63
64         void create_coeffs() {
65                 if (!coeff)
66                         coeff = new double[4];
67
68                 coeff[0] = coeff[1] = coeff[2] = coeff[3] = 0.0;
69         }
70
71         double  when;
72         double  value;
73         double* coeff; ///< double[4] allocated by Curve as needed
74 };
75
76 /** A list (sequence) of time-stamped values for a control
77  */
78 class LIBEVORAL_API ControlList
79 {
80 public:
81         typedef std::list<ControlEvent*> EventList;
82         typedef EventList::iterator iterator;
83         typedef EventList::reverse_iterator reverse_iterator;
84         typedef EventList::const_iterator const_iterator;
85         typedef EventList::const_reverse_iterator const_reverse_iterator;
86
87         ControlList (const Parameter& id, const ParameterDescriptor& desc);
88         ControlList (const ControlList&);
89         ControlList (const ControlList&, double start, double end);
90         virtual ~ControlList();
91
92         virtual boost::shared_ptr<ControlList> create(const Parameter& id, const ParameterDescriptor& desc);
93
94         void dump (std::ostream&);
95
96         ControlList& operator= (const ControlList&);
97         bool operator== (const ControlList&);
98         void copy_events (const ControlList&);
99
100         virtual void freeze();
101         virtual void thaw ();
102         bool frozen() const { return _frozen; }
103
104         const Parameter& parameter() const                 { return _parameter; }
105         void             set_parameter(const Parameter& p) { _parameter = p; }
106
107         const ParameterDescriptor& descriptor() const                           { return _desc; }
108         void                       set_descriptor(const ParameterDescriptor& d) { _desc = d; }
109
110         EventList::size_type size() const { return _events.size(); }
111         double length() const {
112                 Glib::Threads::RWLock::ReaderLock lm (_lock);
113                 return _events.empty() ? 0.0 : _events.back()->when;
114         }
115         bool empty() const { return _events.empty(); }
116
117         void reset_default (double val) {
118                 _default_value = val;
119         }
120
121         void clear ();
122         void x_scale (double factor);
123         bool extend_to (double);
124         void slide (iterator before, double distance);
125         void shift (double before, double distance);
126
127         virtual void add (double when, double value, bool with_guards=true, bool with_initial=true);
128         virtual void editor_add (double when, double value, bool with_guard);
129
130         void fast_simple_add (double when, double value);
131
132         void erase_range (double start, double end);
133         void erase (iterator);
134         void erase (iterator, iterator);
135         void erase (double, double);
136         bool move_ranges (std::list< RangeMove<double> > const &);
137         void modify (iterator, double, double);
138
139         /** Thin the number of events in this list.
140          *
141          * The thinning factor has no units but corresponds to the area of a
142          * triangle computed between three points in the list.  If the area is
143          * large, it indicates significant non-linearity between the points.
144          *
145          * During automation recording we thin the recorded points using this
146          * value.  If a point is sufficiently co-linear with its neighbours (as
147          * defined by the area of the triangle formed by three of them), we will
148          * not include it in the list.  The larger the value, the more points are
149          * excluded, so this effectively measures the amount of thinning to be
150          * done.
151          */
152         void thin (double thinning_factor);
153
154         boost::shared_ptr<ControlList> cut (double, double);
155         boost::shared_ptr<ControlList> copy (double, double);
156         void clear (double, double);
157
158         bool paste (const ControlList&, double position, float times);
159
160         void set_yrange (double min, double max) {
161                 _min_yval = min;
162                 _max_yval = max;
163         }
164
165         double get_max_y() const { return _max_yval; }
166         double get_min_y() const { return _min_yval; }
167
168         void truncate_end (double length);
169         void truncate_start (double length);
170
171         iterator            begin()       { return _events.begin(); }
172         const_iterator      begin() const { return _events.begin(); }
173         iterator            end()         { return _events.end(); }
174         const_iterator      end()   const { return _events.end(); }
175         reverse_iterator            rbegin()       { return _events.rbegin(); }
176         const_reverse_iterator      rbegin() const { return _events.rbegin(); }
177         reverse_iterator            rend()         { return _events.rend(); }
178         const_reverse_iterator      rend()   const { return _events.rend(); }
179         ControlEvent*       back()        { return _events.back(); }
180         const ControlEvent* back()  const { return _events.back(); }
181         ControlEvent*       front()       { return _events.front(); }
182         const ControlEvent* front() const { return _events.front(); }
183
184         std::pair<ControlList::iterator,ControlList::iterator> control_points_adjacent (double when);
185
186         template<class T> void apply_to_points (T& obj, void (T::*method)(const ControlList&)) {
187                 Glib::Threads::RWLock::WriterLock lm (_lock);
188                 (obj.*method)(*this);
189         }
190
191         double eval (double where) {
192                 Glib::Threads::RWLock::ReaderLock lm (_lock);
193                 return unlocked_eval (where);
194         }
195
196         double rt_safe_eval (double where, bool& ok) {
197
198                 Glib::Threads::RWLock::ReaderLock lm (_lock, Glib::Threads::TRY_LOCK);
199
200                 if ((ok = lm.locked())) {
201                         return unlocked_eval (where);
202                 } else {
203                         return 0.0;
204                 }
205         }
206
207         static inline bool time_comparator (const ControlEvent* a, const ControlEvent* b) {
208                 return a->when < b->when;
209         }
210
211         /** Lookup cache for eval functions, range contains equivalent values */
212         struct LookupCache {
213                 LookupCache() : left(-1) {}
214                 double left;  /* leftmost x coordinate used when finding "range" */
215                 std::pair<ControlList::const_iterator,ControlList::const_iterator> range;
216         };
217
218         /** Lookup cache for point finding, range contains points after left */
219         struct SearchCache {
220                 SearchCache () : left(-1) {}
221                 double left;  /* leftmost x coordinate used when finding "first" */
222                 ControlList::const_iterator first;
223         };
224
225         const EventList& events() const { return _events; }
226         double default_value() const { return _default_value; }
227
228         // FIXME: const violations for Curve
229         Glib::Threads::RWLock& lock()       const { return _lock; }
230         LookupCache& lookup_cache() const { return _lookup_cache; }
231         SearchCache& search_cache() const { return _search_cache; }
232
233         /** Called by locked entry point and various private
234          * locations where we already hold the lock.
235          *
236          * FIXME: Should this be private?  Curve needs it..
237          */
238         double unlocked_eval (double x) const;
239
240         bool rt_safe_earliest_event (double start, double& x, double& y, bool start_inclusive=false) const;
241         bool rt_safe_earliest_event_unlocked (double start, double& x, double& y, bool start_inclusive=false) const;
242         bool rt_safe_earliest_event_linear_unlocked (double start, double& x, double& y, bool inclusive) const;
243         bool rt_safe_earliest_event_discrete_unlocked (double start, double& x, double& y, bool inclusive) const;
244
245         void create_curve();
246         void destroy_curve();
247
248         Curve&       curve()       { assert(_curve); return *_curve; }
249         const Curve& curve() const { assert(_curve); return *_curve; }
250
251         void mark_dirty () const;
252
253         enum InterpolationStyle {
254                 Discrete,
255                 Linear,
256                 Curved
257         };
258
259         InterpolationStyle interpolation() const { return _interpolation; }
260         void set_interpolation (InterpolationStyle);
261
262         virtual bool touching() const { return false; }
263         virtual bool writing() const { return false; }
264         virtual bool touch_enabled() const { return false; }
265         void start_write_pass (double time);
266         void write_pass_finished (double when, double thinning_factor=0.0);
267         void set_in_write_pass (bool, bool add_point = false, double when = 0.0);
268         bool in_write_pass () const;
269
270         /** Emitted when mark_dirty() is called on this object */
271         mutable PBD::Signal0<void> Dirty;
272         /** Emitted when our interpolation style changes */
273         PBD::Signal1<void, InterpolationStyle> InterpolationChanged;
274
275         bool operator!= (ControlList const &) const;
276
277         void invalidate_insert_iterator ();
278
279 protected:
280
281         /** Called by unlocked_eval() to handle cases of 3 or more control points. */
282         double multipoint_eval (double x) const;
283
284         void build_search_cache_if_necessary (double start) const;
285
286         boost::shared_ptr<ControlList> cut_copy_clear (double, double, int op);
287         bool erase_range_internal (double start, double end, EventList &);
288
289         void     maybe_add_insert_guard (double when);
290         iterator erase_from_iterator_to (iterator iter, double when);
291         bool     maybe_insert_straight_line (double when, double value);
292
293         virtual void maybe_signal_changed ();
294
295         void _x_scale (double factor);
296
297         mutable LookupCache   _lookup_cache;
298         mutable SearchCache   _search_cache;
299
300         mutable Glib::Threads::RWLock _lock;
301
302         Parameter             _parameter;
303         ParameterDescriptor   _desc;
304         InterpolationStyle    _interpolation;
305         EventList             _events;
306         int8_t                _frozen;
307         bool                  _changed_when_thawed;
308         double                _min_yval;
309         double                _max_yval;
310         double                _default_value;
311         bool                  _sort_pending;
312
313         Curve* _curve;
314
315   private:
316     iterator   most_recent_insert_iterator;
317     double     insert_position;
318     bool       new_write_pass;
319     bool       did_write_during_pass;
320     bool       _in_write_pass;
321     void unlocked_invalidate_insert_iterator ();
322     void add_guard_point (double when);
323 };
324
325 } // namespace Evoral
326
327 #endif // EVORAL_CONTROL_LIST_HPP
328