Ardour  9.0-pre0-1159-gcb8dd39f31
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 <glibmm/threads.h>
34 
35 #include "pbd/signals.h"
36 
38 #include "temporal/domain_swap.h"
39 #include "temporal/timeline.h"
40 #include "temporal/types.h"
41 #include "temporal/range.h"
42 
43 #include "evoral/visibility.h"
44 
45 #include "evoral/Parameter.h"
47 
48 namespace Evoral {
49 
50 class Curve;
51 class TypeMap;
52 
56 public:
57 
58  ControlEvent (Temporal::timepos_t const & w, double v)
59  : when (w), value (v), coeff (0)
60  {}
61 
62  ControlEvent (const ControlEvent& other)
63  : when (other.when), value (other.value), coeff (0)
64  {
65  if (other.coeff) {
66  create_coeffs();
67  for (size_t i = 0; i < 4; ++i)
68  coeff[i] = other.coeff[i];
69  }
70  }
71 
72  ~ControlEvent() { if (coeff) delete[] coeff; }
73 
74  void create_coeffs() {
75  if (!coeff)
76  coeff = new double[4];
77 
78  coeff[0] = coeff[1] = coeff[2] = coeff[3] = 0.0;
79  }
80 
82  double value;
83  double* coeff;
84 };
85 
86 
90 {
91 public:
92  typedef std::list<ControlEvent*> EventList;
93  typedef EventList::iterator iterator;
94  typedef EventList::reverse_iterator reverse_iterator;
95  typedef EventList::const_iterator const_iterator;
96  typedef EventList::const_reverse_iterator const_reverse_iterator;
97 
101  virtual ~ControlList();
102 
103  virtual std::shared_ptr<ControlList> create(const Parameter& id, const ParameterDescriptor& desc, Temporal::TimeDomainProvider const &);
104 
105  void dump (std::ostream&);
106 
107  ControlList& operator= (const ControlList&);
108  bool operator== (const ControlList&);
109  void copy_events (const ControlList&);
110 
111  virtual void freeze();
112  virtual void thaw ();
113  bool frozen() const { return _frozen; }
114 
115  const Parameter& parameter() const { return _parameter; }
116  void set_parameter(const Parameter& p) { _parameter = p; }
117 
118  const ParameterDescriptor& descriptor() const { return _desc; }
119  void set_descriptor(const ParameterDescriptor& d) { _desc = d; }
120 
121  EventList::size_type size() const { return _events.size(); }
122 
124  Temporal::timepos_t when (bool at_start) const {
125  Glib::Threads::RWLock::ReaderLock lm (_lock);
126  if (_events.empty()) {
127  return std::numeric_limits<Temporal::timepos_t>::min();
128  }
129  return at_start ? _events.front()->when : _events.back()->when;
130  }
131 
133  Glib::Threads::RWLock::ReaderLock lm (_lock);
134  return _events.empty() ? std::numeric_limits<Temporal::timecnt_t>::min() : Temporal::timecnt_t (_events.back()->when);
135  }
136  bool empty() const { return _events.empty(); }
137 
139  void clear ();
140  void x_scale (Temporal::ratio_t const &);
142  void slide (iterator before, Temporal::timecnt_t const & distance);
143  void shift (Temporal::timepos_t const & before, Temporal::timecnt_t const & distance);
144  void simple_shift (Temporal::timepos_t const & distance);
145 
146  void y_transform (std::function<double(double)> callback);
147  void list_merge (ControlList const& other, std::function<double(double, double)> callback);
148 
159  virtual void add (Temporal::timepos_t const & when, double value, bool with_guards=true, bool with_initial=true);
160 
172  virtual bool editor_add (Temporal::timepos_t const & when, double value, bool with_guard);
173 
174  struct OrderedPoint {
176  double value;
177  OrderedPoint (Temporal::timepos_t const & t, double v) : when (t), value (v) {}
178  };
179  typedef std::vector<OrderedPoint> OrderedPoints;
180 
181  virtual bool editor_add_ordered (OrderedPoints const &, bool with_guard);
182 
183  /* to be used only for loading pre-sorted data from saved state */
184  void fast_simple_add (Temporal::timepos_t const & when, double value);
185 
187  void erase (iterator);
189  void erase (Temporal::timepos_t const &, double);
190  bool move_ranges (std::list<Temporal::RangeMove> const &);
191  void modify (iterator, Temporal::timepos_t const &, double);
192 
211  void thin (double thinning_factor);
212 
213  std::shared_ptr<ControlList> cut (Temporal::timepos_t const &, Temporal::timepos_t const &);
214  std::shared_ptr<ControlList> copy (Temporal::timepos_t const &, Temporal::timepos_t const &);
215 
221  void clear (Temporal::timepos_t const & start, Temporal::timepos_t const & end);
222 
223  bool paste (const ControlList&, Temporal::timepos_t const &);
224 
229  void truncate_end (Temporal::timepos_t const & last_coordinate);
230 
235  void truncate_start (Temporal::timecnt_t const & overall_length);
236 
237  iterator begin() { return _events.begin(); }
238  const_iterator begin() const { return _events.begin(); }
239  iterator end() { return _events.end(); }
240  const_iterator end() const { return _events.end(); }
241  reverse_iterator rbegin() { return _events.rbegin(); }
242  const_reverse_iterator rbegin() const { return _events.rbegin(); }
243  reverse_iterator rend() { return _events.rend(); }
244  const_reverse_iterator rend() const { return _events.rend(); }
245  ControlEvent* back() { return _events.back(); }
246  const ControlEvent* back() const { return _events.back(); }
247  ControlEvent* front() { return _events.front(); }
248  const ControlEvent* front() const { return _events.front(); }
249 
250  std::pair<ControlList::iterator,ControlList::iterator> control_points_adjacent (Temporal::timepos_t const & when);
251 
252  template<class T> void apply_to_points (T& obj, void (T::*method)(const ControlList&)) {
253  Glib::Threads::RWLock::WriterLock lm (_lock);
254  (obj.*method)(*this);
255  }
256 
263  double eval (Temporal::timepos_t const & where) const {
264  Glib::Threads::RWLock::ReaderLock lm (_lock);
265  return unlocked_eval (where);
266  }
267 
275  double rt_safe_eval (Temporal::timepos_t const & where, bool& ok) const {
276 
277  Glib::Threads::RWLock::ReaderLock lm (_lock, Glib::Threads::TRY_LOCK);
278 
279  if ((ok = lm.locked())) {
280  return unlocked_eval (where);
281  } else {
282  return 0.0;
283  }
284  }
285 
286  static inline bool time_comparator (const ControlEvent* a, const ControlEvent* b) {
287  return a->when < b->when;
288  }
289 
291  struct LookupCache {
292  LookupCache() : left (std::numeric_limits<Temporal::timepos_t>::max()) {}
293  Temporal::timepos_t left; /* leftmost x coordinate used when finding "range" */
294  std::pair<ControlList::const_iterator,ControlList::const_iterator> range;
295  };
296 
298  struct SearchCache {
299  SearchCache () : left (std::numeric_limits<Temporal::timepos_t>::max()) {}
300  Temporal::timepos_t left; /* leftmost x coordinate used when finding "first" */
302  };
303 
305  const EventList& events() const { return _events; }
306 
307  // FIXME: const violations for Curve
308  Glib::Threads::RWLock& lock() const { return _lock; }
309  LookupCache& lookup_cache() const { return _lookup_cache; }
310  SearchCache& search_cache() const { return _search_cache; }
311 
317  double unlocked_eval (Temporal::timepos_t const & x) const;
318 
319  bool rt_safe_earliest_event_discrete_unlocked (Temporal::timepos_t const & start, Temporal::timepos_t & x, double& y, bool inclusive) const;
321 
322  void create_curve();
324 
325  Curve& curve() { assert(_curve); return *_curve; }
326  const Curve& curve() const { assert(_curve); return *_curve; }
327 
328  void mark_dirty () const;
329 
333  Curved, // spline, used for x-fades
335  Exponential // fader, gain
336  };
337 
341  InterpolationStyle interpolation() const { return _interpolation; }
342 
345 
355 
356  virtual bool touching() const { return false; }
357  virtual bool writing() const { return false; }
358  virtual bool touch_enabled() const { return false; }
360  void write_pass_finished (Temporal::timepos_t const &, double thinning_factor=0.0);
361  void set_in_write_pass (bool, bool add_point = false, Temporal::timepos_t = std::numeric_limits<Temporal::timepos_t>::min());
363  bool in_write_pass () const;
364  bool in_new_write_pass () { return new_write_pass; }
365 
368  mutable PBD::Signal<void()> Dirty;
371 
372  bool operator!= (ControlList const &) const;
373 
375 
378 
379  protected:
380 
382  double multipoint_eval (Temporal::timepos_t const & x) const;
383 
385 
386  std::shared_ptr<ControlList> cut_copy_clear (Temporal::timepos_t const &, Temporal::timepos_t const &, int op);
388 
391  bool maybe_insert_straight_line (Temporal::timepos_t const & when, double value);
392 
393  virtual void maybe_signal_changed ();
394 
395  void _x_scale (Temporal::ratio_t const &);
396 
399 
400  mutable Glib::Threads::RWLock _lock;
401 
406  int8_t _frozen;
409 
411 
412  private:
418 
421  void add_guard_point (Temporal::timepos_t const & when, Temporal::timecnt_t const & offset);
422 
423  bool is_sorted () const;
425 
426 };
427 
428 } // namespace Evoral
429 
430 #endif // EVORAL_CONTROL_LIST_HPP
double * coeff
double[4] allocated by Curve as needed
Definition: ControlList.h:83
Temporal::timepos_t when
Definition: ControlList.h:81
ControlEvent(const ControlEvent &other)
Definition: ControlList.h:62
ControlEvent(Temporal::timepos_t const &w, double v)
Definition: ControlList.h:58
void start_domain_bounce(Temporal::DomainBounceInfo &)
EventList::size_type size() const
Definition: ControlList.h:121
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:402
std::vector< OrderedPoint > OrderedPoints
Definition: ControlList.h:179
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:241
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:93
const ParameterDescriptor & descriptor() const
Definition: ControlList.h:118
void x_scale(Temporal::ratio_t const &)
const Parameter & parameter() const
Definition: ControlList.h:115
ControlList(const ControlList &)
bool paste(const ControlList &, Temporal::timepos_t const &)
LookupCache _lookup_cache
Definition: ControlList.h:397
SearchCache _search_cache
Definition: ControlList.h:398
virtual std::shared_ptr< ControlList > create(const Parameter &id, const ParameterDescriptor &desc, Temporal::TimeDomainProvider const &)
void set_descriptor(const ParameterDescriptor &d)
Definition: ControlList.h:119
void dump(std::ostream &)
EventList::reverse_iterator reverse_iterator
Definition: ControlList.h:94
void simple_shift(Temporal::timepos_t const &distance)
InterpolationStyle _interpolation
Definition: ControlList.h:404
SearchCache & search_cache() const
Definition: ControlList.h:310
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:368
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:275
ControlList(const Parameter &id, const ParameterDescriptor &desc, Temporal::TimeDomainProvider const &)
ControlList(const ControlList &, Temporal::timepos_t const &start, Temporal::timepos_t const &end)
void set_parameter(const Parameter &p)
Definition: ControlList.h:116
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:309
const Curve & curve() const
Definition: ControlList.h:326
bool is_sorted() const
EventList::const_reverse_iterator const_reverse_iterator
Definition: ControlList.h:96
Temporal::timepos_t when(bool at_start) const
Definition: ControlList.h:124
void erase(Temporal::timepos_t const &, double)
void thin(double thinning_factor)
Glib::Threads::RWLock & lock() const
Definition: ControlList.h:308
ParameterDescriptor _desc
Definition: ControlList.h:403
virtual bool touching() const
Definition: ControlList.h:356
const ControlEvent * back() const
Definition: ControlList.h:246
virtual InterpolationStyle default_interpolation() const
static bool time_comparator(const ControlEvent *a, const ControlEvent *b)
Definition: ControlList.h:286
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:248
InterpolationStyle interpolation() const
Definition: ControlList.h:341
PBD::Signal< void()> WritePassStarted
Definition: ControlList.h:366
void unlocked_remove_duplicates()
void invalidate_insert_iterator()
Temporal::timecnt_t length() const
Definition: ControlList.h:132
double eval(Temporal::timepos_t const &where) const
Definition: ControlList.h:263
bool maybe_insert_straight_line(Temporal::timepos_t const &when, double value)
std::list< ControlEvent * > EventList
Definition: ControlList.h:92
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:252
const_iterator end() const
Definition: ControlList.h:240
const EventList & events() const
Definition: ControlList.h:305
virtual bool writing() const
Definition: ControlList.h:357
void unlocked_invalidate_insert_iterator()
void _x_scale(Temporal::ratio_t const &)
const_reverse_iterator rend() const
Definition: ControlList.h:244
void truncate_start(Temporal::timecnt_t const &overall_length)
bool in_write_pass() const
const_reverse_iterator rbegin() const
Definition: ControlList.h:242
Temporal::timepos_t insert_position
Definition: ControlList.h:414
Glib::Threads::RWLock _lock
Definition: ControlList.h:400
ControlEvent * back()
Definition: ControlList.h:245
bool frozen() const
Definition: ControlList.h:113
void erase(iterator, iterator)
PBD::Signal< void(InterpolationStyle)> InterpolationChanged
Definition: ControlList.h:370
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:136
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:358
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:243
EventList::const_iterator const_iterator
Definition: ControlList.h:95
void build_search_cache_if_necessary(Temporal::timepos_t const &start) const
ControlEvent * front()
Definition: ControlList.h:247
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:238
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:413
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:294
OrderedPoint(Temporal::timepos_t const &t, double v)
Definition: ControlList.h:177
ControlList::const_iterator first
Definition: ControlList.h:301