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