ardour
ControlList.hpp
Go to the documentation of this file.
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"
38 
39 namespace Evoral {
40 
41 class Curve;
42 class TypeMap;
43 
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;
74 };
75 
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_default=true);
128  virtual void editor_add (double when, double value);
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 
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 
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 
219  struct SearchCache {
220  SearchCache () : left(-1) {}
221  double left; /* leftmost x coordinate used when finding "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 
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 
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 
271  mutable PBD::Signal0<void> Dirty;
273  PBD::Signal1<void, InterpolationStyle> InterpolationChanged;
274 
275  bool operator!= (ControlList const &) const;
276 
277  void invalidate_insert_iterator ();
278 
279 protected:
280 
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 
299 
300  mutable Glib::Threads::RWLock _lock;
301 
305  EventList _events;
306  int8_t _frozen;
308  double _min_yval;
309  double _max_yval;
312 
314 
315  private:
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 
ControlEvent * front()
const ControlEvent * front() const
const ParameterDescriptor & descriptor() const
void set_yrange(double min, double max)
bool operator!=(shared_ptr< T > const &a, shared_ptr< U > const &b)
Definition: shared_ptr.hpp:360
double rt_safe_eval(double where, bool &ok)
Glib::Threads::RWLock _lock
std::list< ControlEvent * > EventList
Definition: ControlList.hpp:81
ControlList::const_iterator first
double default_value() const
double get_min_y() const
#define LIBEVORAL_API
Definition: visibility.h:45
LIBARDOUR_API GQuark paste
Definition: operations.cc:25
EventList::const_reverse_iterator const_reverse_iterator
Definition: ControlList.hpp:85
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > start
Definition: region.cc:63
PBD::Signal0< void > Dirty
LookupCache & lookup_cache() const
const_reverse_iterator rend() const
ParameterDescriptor _desc
SearchCache & search_cache() const
virtual bool writing() const
virtual bool touch_enabled() const
LIBARDOUR_API PBD::PropertyDescriptor< float > shift
Definition: region.cc:71
double eval(double where)
reverse_iterator rend()
double length() const
void apply_to_points(T &obj, void(T::*method)(const ControlList &))
EventList::size_type size() const
double get_max_y() const
PBD::Signal1< void, InterpolationStyle > InterpolationChanged
const Parameter & parameter() const
const_iterator begin() const
ControlEvent(const ControlEvent &other)
Definition: ControlList.hpp:52
bool frozen() const
void set_parameter(const Parameter &p)
iterator most_recent_insert_iterator
InterpolationStyle interpolation() const
SearchCache _search_cache
std::pair< ControlList::const_iterator, ControlList::const_iterator > range
void reset_default(double val)
LIBEVORAL_API uint64_t ControlList
Definition: debug.cpp:5
LIBARDOUR_API PBD::PropertyDescriptor< framepos_t > position
Definition: region.cc:65
bool operator==(Range< T > a, Range< T > b)
Definition: Range.hpp:144
Glib::Threads::RWLock & lock() const
EventList::const_iterator const_iterator
Definition: ControlList.hpp:84
EventList::reverse_iterator reverse_iterator
Definition: ControlList.hpp:83
ControlEvent(double w, double v)
Definition: ControlList.hpp:48
ControlEvent * back()
const Curve & curve() const
LookupCache _lookup_cache
const_reverse_iterator rbegin() const
const ControlEvent * back() const
const_iterator end() const
virtual bool touching() const
void set_descriptor(const ParameterDescriptor &d)
static bool time_comparator(const ControlEvent *a, const ControlEvent *b)
EventList::iterator iterator
Definition: ControlList.hpp:82
reverse_iterator rbegin()
double * coeff
double[4] allocated by Curve as needed
Definition: ControlList.hpp:73
bool empty() const
LIBARDOUR_API PBD::PropertyDescriptor< framecnt_t > length
Definition: region.cc:64
const EventList & events() const
InterpolationStyle _interpolation