Ardour  9.2-79-gba93f2fe52
ControlList.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2012 Carl Hetherington <carl@carlh.net>
3  * Copyright (C) 2008-2014 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2008-2015 David Robillard <d@drobilla.net>
5  * Copyright (C) 2012-2017 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2015 Nick Mainsbridge <mainsbridge@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #ifndef EVORAL_CONTROL_LIST_HPP
24 #define EVORAL_CONTROL_LIST_HPP
25 
26 #include <cassert>
27 #include <list>
28 #include <stdint.h>
29 
30 #include <boost/pool/pool.hpp>
31 #include <boost/pool/pool_alloc.hpp>
32 
33 #include "pbd/rwlock.h"
34 #include "pbd/signals.h"
35 
37 #include "temporal/domain_swap.h"
38 #include "temporal/timeline.h"
39 #include "temporal/types.h"
40 #include "temporal/range.h"
41 
42 #include "evoral/visibility.h"
43 
44 #include "evoral/Parameter.h"
46 
47 namespace Evoral {
48 
49 class Curve;
50 class TypeMap;
51 
55 public:
56 
57  ControlEvent (Temporal::timepos_t const & w, double v)
58  : when (w), value (v), coeff (0)
59  {}
60 
61  ControlEvent (const ControlEvent& other)
62  : when (other.when), value (other.value), coeff (0)
63  {
64  if (other.coeff) {
65  create_coeffs();
66  for (size_t i = 0; i < 4; ++i)
67  coeff[i] = other.coeff[i];
68  }
69  }
70 
71  ~ControlEvent() { if (coeff) delete[] coeff; }
72 
73  void create_coeffs() {
74  if (!coeff)
75  coeff = new double[4];
76 
77  coeff[0] = coeff[1] = coeff[2] = coeff[3] = 0.0;
78  }
79 
81  double value;
82  double* coeff;
83 };
84 
85 
89 {
90 public:
91  typedef std::list<ControlEvent*> EventList;
92  typedef EventList::iterator iterator;
93  typedef EventList::reverse_iterator reverse_iterator;
94  typedef EventList::const_iterator const_iterator;
95  typedef EventList::const_reverse_iterator const_reverse_iterator;
96 
100  virtual ~ControlList();
101 
102  virtual std::shared_ptr<ControlList> create(const Parameter& id, const ParameterDescriptor& desc, Temporal::TimeDomainProvider const &);
103 
104  void dump (std::ostream&);
105 
106  ControlList& operator= (const ControlList&);
107  bool operator== (const ControlList&);
108  void copy_events (const ControlList&);
109 
110  virtual void freeze();
111  virtual void thaw ();
112  bool frozen() const { return _frozen; }
113 
114  const Parameter& parameter() const { return _parameter; }
115  void set_parameter(const Parameter& p) { _parameter = p; }
116 
117  const ParameterDescriptor& descriptor() const { return _desc; }
118  void set_descriptor(const ParameterDescriptor& d) { _desc = d; }
119 
120  EventList::size_type size() const { return _events.size(); }
121 
123  Temporal::timepos_t when (bool at_start) const {
124  PBD::RWLock::ReaderLock lm (_lock);
125  if (_events.empty()) {
126  return std::numeric_limits<Temporal::timepos_t>::min();
127  }
128  return at_start ? _events.front()->when : _events.back()->when;
129  }
130 
132  PBD::RWLock::ReaderLock lm (_lock);
133  return _events.empty() ? std::numeric_limits<Temporal::timecnt_t>::min() : Temporal::timecnt_t (_events.back()->when);
134  }
135  bool empty() const { return _events.empty(); }
136 
138  void clear ();
139  void x_scale (Temporal::ratio_t const &);
141  void slide (iterator before, Temporal::timecnt_t const & distance);
142  void shift (Temporal::timepos_t const & before, Temporal::timecnt_t const & distance);
143  void simple_shift (Temporal::timepos_t const & distance);
144 
145  void y_transform (std::function<double(double)> callback);
146  void list_merge (ControlList const& other, std::function<double(double, double)> callback);
147 
158  virtual void add (Temporal::timepos_t const & when, double value, bool with_guards=true, bool with_initial=true);
159 
171  virtual bool editor_add (Temporal::timepos_t const & when, double value, bool with_guard);
172 
173  struct OrderedPoint {
175  double value;
176  OrderedPoint (Temporal::timepos_t const & t, double v) : when (t), value (v) {}
177  };
178  typedef std::vector<OrderedPoint> OrderedPoints;
179 
180  virtual bool editor_add_ordered (OrderedPoints const &, bool with_guard);
181 
182  /* to be used only for loading pre-sorted data from saved state */
183  void fast_simple_add (Temporal::timepos_t const & when, double value);
184 
186  void erase (iterator);
188  void erase (Temporal::timepos_t const &, double);
189  bool move_ranges (std::list<Temporal::RangeMove> const &);
190  void modify (iterator, Temporal::timepos_t const &, double);
191 
210  void thin (double thinning_factor);
211 
212  std::shared_ptr<ControlList> cut (Temporal::timepos_t const &, Temporal::timepos_t const &);
213  std::shared_ptr<ControlList> copy (Temporal::timepos_t const &, Temporal::timepos_t const &);
214 
215  bool has_event_at (Temporal::timepos_t const &) const;
216 
222  void clear (Temporal::timepos_t const & start, Temporal::timepos_t const & end);
223 
224  bool paste (const ControlList&, Temporal::timepos_t const &);
225 
230  void truncate_end (Temporal::timepos_t const & last_coordinate);
231 
236  void truncate_start (Temporal::timecnt_t const & overall_length);
237 
238  iterator begin() { return _events.begin(); }
239  const_iterator begin() const { return _events.begin(); }
240  iterator end() { return _events.end(); }
241  const_iterator end() const { return _events.end(); }
242  reverse_iterator rbegin() { return _events.rbegin(); }
243  const_reverse_iterator rbegin() const { return _events.rbegin(); }
244  reverse_iterator rend() { return _events.rend(); }
245  const_reverse_iterator rend() const { return _events.rend(); }
246  ControlEvent* back() { return _events.back(); }
247  const ControlEvent* back() const { return _events.back(); }
248  ControlEvent* front() { return _events.front(); }
249  const ControlEvent* front() const { return _events.front(); }
250 
251  std::pair<ControlList::iterator,ControlList::iterator> control_points_adjacent (Temporal::timepos_t const & when);
252 
253  template<class T> void apply_to_points (T& obj, void (T::*method)(const ControlList&)) {
254  PBD::RWLock::WriterLock lm (_lock);
255  (obj.*method)(*this);
256  }
257 
264  double eval (Temporal::timepos_t const & where) const {
265  PBD::RWLock::ReaderLock lm (_lock);
266  return unlocked_eval (where);
267  }
268 
276  double rt_safe_eval (Temporal::timepos_t const & where, bool& ok) const {
277 
279 
280  if ((ok = lm.locked())) {
281  return unlocked_eval (where);
282  } else {
283  return 0.0;
284  }
285  }
286 
287  static inline bool time_comparator (const ControlEvent* a, const ControlEvent* b) {
288  return a->when < b->when;
289  }
290 
292  struct LookupCache {
293  LookupCache() : left (std::numeric_limits<Temporal::timepos_t>::max()) {}
294  Temporal::timepos_t left; /* leftmost x coordinate used when finding "range" */
295  std::pair<ControlList::const_iterator,ControlList::const_iterator> range;
296  };
297 
299  struct SearchCache {
300  SearchCache () : left (std::numeric_limits<Temporal::timepos_t>::max()) {}
301  Temporal::timepos_t left; /* leftmost x coordinate used when finding "first" */
303  };
304 
306  const EventList& events() const { return _events; }
307 
308  // FIXME: const violations for Curve
309  PBD::RWLock& lock() const { return _lock; }
310  LookupCache& lookup_cache() const { return _lookup_cache; }
311  SearchCache& search_cache() const { return _search_cache; }
312 
318  double unlocked_eval (Temporal::timepos_t const & x) const;
319 
320  bool rt_safe_earliest_event_discrete_unlocked (Temporal::timepos_t const & start, Temporal::timepos_t & x, double& y, bool inclusive) const;
322 
323  void create_curve();
325 
326  Curve& curve() { assert(_curve); return *_curve; }
327  const Curve& curve() const { assert(_curve); return *_curve; }
328 
329  void mark_dirty () const;
330 
334  Curved, // spline, used for x-fades
336  Exponential // fader, gain
337  };
338 
342  InterpolationStyle interpolation() const { return _interpolation; }
343 
346 
356 
357  virtual bool touching() const { return false; }
358  virtual bool writing() const { return false; }
359  virtual bool touch_enabled() const { return false; }
361  void write_pass_finished (Temporal::timepos_t const &, double thinning_factor=0.0);
362  void set_in_write_pass (bool, bool add_point = false, Temporal::timepos_t = std::numeric_limits<Temporal::timepos_t>::min());
364  bool in_write_pass () const;
365  bool in_new_write_pass () { return new_write_pass; }
366 
369  mutable PBD::Signal<void()> Dirty;
372 
373  bool operator!= (ControlList const &) const;
374 
376 
379 
380  protected:
381 
383  double multipoint_eval (Temporal::timepos_t const & x) const;
384 
386 
387  std::shared_ptr<ControlList> cut_copy_clear (Temporal::timepos_t const &, Temporal::timepos_t const &, int op);
389 
392  bool maybe_insert_straight_line (Temporal::timepos_t const & when, double value);
393 
394  virtual void maybe_signal_changed ();
395 
396  void _x_scale (Temporal::ratio_t const &);
397 
400 
402 
407  int8_t _frozen;
410 
412 
413  private:
419 
422  void add_guard_point (Temporal::timepos_t const & when, Temporal::timecnt_t const & offset);
423 
424  bool is_sorted () const;
426 
427 };
428 
429 } // namespace Evoral
430 
431 #endif // EVORAL_CONTROL_LIST_HPP
double * coeff
double[4] allocated by Curve as needed
Definition: ControlList.h:82
Temporal::timepos_t when
Definition: ControlList.h:80
ControlEvent(const ControlEvent &other)
Definition: ControlList.h:61
ControlEvent(Temporal::timepos_t const &w, double v)
Definition: ControlList.h:57
PBD::RWLock & lock() const
Definition: ControlList.h:309
void start_domain_bounce(Temporal::DomainBounceInfo &)
EventList::size_type size() const
Definition: ControlList.h:120
bool move_ranges(std::list< Temporal::RangeMove > const &)
void y_transform(std::function< double(double)> callback)
double unlocked_eval(Temporal::timepos_t const &x) const
Parameter _parameter
Definition: ControlList.h:403
std::vector< OrderedPoint > OrderedPoints
Definition: ControlList.h:178
void maybe_add_insert_guard(Temporal::timepos_t const &when)
virtual ~ControlList()
bool rt_safe_earliest_event_discrete_unlocked(Temporal::timepos_t const &start, Temporal::timepos_t &x, double &y, bool inclusive) const
void erase(iterator)
std::pair< ControlList::iterator, ControlList::iterator > control_points_adjacent(Temporal::timepos_t const &when)
reverse_iterator rbegin()
Definition: ControlList.h:242
bool erase_range_internal(Temporal::timepos_t const &start, Temporal::timepos_t const &end, EventList &)
std::shared_ptr< ControlList > cut(Temporal::timepos_t const &, Temporal::timepos_t const &)
std::shared_ptr< ControlList > cut_copy_clear(Temporal::timepos_t const &, Temporal::timepos_t const &, int op)
EventList::iterator iterator
Definition: ControlList.h:92
const ParameterDescriptor & descriptor() const
Definition: ControlList.h:117
void x_scale(Temporal::ratio_t const &)
const Parameter & parameter() const
Definition: ControlList.h:114
ControlList(const ControlList &)
bool paste(const ControlList &, Temporal::timepos_t const &)
LookupCache _lookup_cache
Definition: ControlList.h:398
SearchCache _search_cache
Definition: ControlList.h:399
virtual std::shared_ptr< ControlList > create(const Parameter &id, const ParameterDescriptor &desc, Temporal::TimeDomainProvider const &)
void set_descriptor(const ParameterDescriptor &d)
Definition: ControlList.h:118
void dump(std::ostream &)
EventList::reverse_iterator reverse_iterator
Definition: ControlList.h:93
void simple_shift(Temporal::timepos_t const &distance)
InterpolationStyle _interpolation
Definition: ControlList.h:405
SearchCache & search_cache() const
Definition: ControlList.h:311
virtual void add(Temporal::timepos_t const &when, double value, bool with_guards=true, bool with_initial=true)
PBD::Signal< void()> Dirty
Definition: ControlList.h:369
void erase_range(Temporal::timepos_t const &start, Temporal::timepos_t const &end)
double rt_safe_eval(Temporal::timepos_t const &where, bool &ok) const
Definition: ControlList.h:276
ControlList(const Parameter &id, const ParameterDescriptor &desc, Temporal::TimeDomainProvider const &)
PBD::RWLock _lock
Definition: ControlList.h:401
ControlList(const ControlList &, Temporal::timepos_t const &start, Temporal::timepos_t const &end)
void set_parameter(const Parameter &p)
Definition: ControlList.h:115
bool rt_safe_earliest_event_linear_unlocked(Temporal::timepos_t const &start, Temporal::timepos_t &x, double &y, bool inclusive, Temporal::timecnt_t min_x_delta=Temporal::timecnt_t::max()) const
LookupCache & lookup_cache() const
Definition: ControlList.h:310
const Curve & curve() const
Definition: ControlList.h:327
bool is_sorted() const
EventList::const_reverse_iterator const_reverse_iterator
Definition: ControlList.h:95
Temporal::timepos_t when(bool at_start) const
Definition: ControlList.h:123
void erase(Temporal::timepos_t const &, double)
void thin(double thinning_factor)
ParameterDescriptor _desc
Definition: ControlList.h:404
virtual bool touching() const
Definition: ControlList.h:357
const ControlEvent * back() const
Definition: ControlList.h:247
virtual InterpolationStyle default_interpolation() const
static bool time_comparator(const ControlEvent *a, const ControlEvent *b)
Definition: ControlList.h:287
void set_in_write_pass(bool, bool add_point=false, Temporal::timepos_t=std::numeric_limits< Temporal::timepos_t >::min())
void fast_simple_add(Temporal::timepos_t const &when, double value)
const ControlEvent * front() const
Definition: ControlList.h:249
InterpolationStyle interpolation() const
Definition: ControlList.h:342
PBD::Signal< void()> WritePassStarted
Definition: ControlList.h:367
void unlocked_remove_duplicates()
void invalidate_insert_iterator()
Temporal::timecnt_t length() const
Definition: ControlList.h:131
double eval(Temporal::timepos_t const &where) const
Definition: ControlList.h:264
bool maybe_insert_straight_line(Temporal::timepos_t const &when, double value)
std::list< ControlEvent * > EventList
Definition: ControlList.h:91
virtual void maybe_signal_changed()
virtual void thaw()
void add_guard_point(Temporal::timepos_t const &when, Temporal::timecnt_t const &offset)
void start_write_pass(Temporal::timepos_t const &)
void apply_to_points(T &obj, void(T::*method)(const ControlList &))
Definition: ControlList.h:253
const_iterator end() const
Definition: ControlList.h:241
const EventList & events() const
Definition: ControlList.h:306
virtual bool writing() const
Definition: ControlList.h:358
void unlocked_invalidate_insert_iterator()
void _x_scale(Temporal::ratio_t const &)
const_reverse_iterator rend() const
Definition: ControlList.h:245
void truncate_start(Temporal::timecnt_t const &overall_length)
bool in_write_pass() const
const_reverse_iterator rbegin() const
Definition: ControlList.h:243
Temporal::timepos_t insert_position
Definition: ControlList.h:415
ControlEvent * back()
Definition: ControlList.h:246
bool frozen() const
Definition: ControlList.h:112
void erase(iterator, iterator)
PBD::Signal< void(InterpolationStyle)> InterpolationChanged
Definition: ControlList.h:371
void copy_events(const ControlList &)
void modify(iterator, Temporal::timepos_t const &, double)
virtual bool editor_add(Temporal::timepos_t const &when, double value, bool with_guard)
virtual bool editor_add_ordered(OrderedPoints const &, bool with_guard)
void finish_domain_bounce(Temporal::DomainBounceInfo &)
void mark_dirty() const
bool empty() const
Definition: ControlList.h:135
void clear(Temporal::timepos_t const &start, Temporal::timepos_t const &end)
void truncate_end(Temporal::timepos_t const &last_coordinate)
std::shared_ptr< ControlList > copy(Temporal::timepos_t const &, Temporal::timepos_t const &)
virtual bool touch_enabled() const
Definition: ControlList.h:359
bool extend_to(Temporal::timepos_t const &)
void list_merge(ControlList const &other, std::function< double(double, double)> callback)
iterator erase_from_iterator_to(iterator iter, Temporal::timepos_t const &when)
virtual void freeze()
reverse_iterator rend()
Definition: ControlList.h:244
bool has_event_at(Temporal::timepos_t const &) const
EventList::const_iterator const_iterator
Definition: ControlList.h:94
void build_search_cache_if_necessary(Temporal::timepos_t const &start) const
ControlEvent * front()
Definition: ControlList.h:248
void shift(Temporal::timepos_t const &before, Temporal::timecnt_t const &distance)
void write_pass_finished(Temporal::timepos_t const &, double thinning_factor=0.0)
Temporal::timepos_t ensure_time_domain(Temporal::timepos_t const &) const
const_iterator begin() const
Definition: ControlList.h:239
bool set_interpolation(InterpolationStyle is)
void slide(iterator before, Temporal::timecnt_t const &distance)
double multipoint_eval(Temporal::timepos_t const &x) const
iterator most_recent_insert_iterator
Definition: ControlList.h:414
bool locked() const
Definition: rwlock.h:100
@ TryLock
Definition: rwlock.h:34
static timecnt_t const & max()
Definition: timeline.h:351
#define LIBEVORAL_API
PBD::PropertyDescriptor< timepos_t > start
Temporal::timecnt_t timecnt_t
Temporal::timepos_t timepos_t
Definition: editor.h:87
bool operator==(const ProcessorSelection &a, const ProcessorSelection &b)
std::pair< ControlList::const_iterator, ControlList::const_iterator > range
Definition: ControlList.h:295
OrderedPoint(Temporal::timepos_t const &t, double v)
Definition: ControlList.h:176
ControlList::const_iterator first
Definition: ControlList.h:302