Ardour  9.0-pre0-350-gf17a656217
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 
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 
220  void clear (Temporal::timepos_t const & start, Temporal::timepos_t const & end);
221 
222  bool paste (const ControlList&, Temporal::timepos_t const &);
223 
228  void truncate_end (Temporal::timepos_t const & last_coordinate);
229 
234  void truncate_start (Temporal::timecnt_t const & overall_length);
235 
236  iterator begin() { return _events.begin(); }
237  const_iterator begin() const { return _events.begin(); }
238  iterator end() { return _events.end(); }
239  const_iterator end() const { return _events.end(); }
240  reverse_iterator rbegin() { return _events.rbegin(); }
241  const_reverse_iterator rbegin() const { return _events.rbegin(); }
242  reverse_iterator rend() { return _events.rend(); }
243  const_reverse_iterator rend() const { return _events.rend(); }
244  ControlEvent* back() { return _events.back(); }
245  const ControlEvent* back() const { return _events.back(); }
246  ControlEvent* front() { return _events.front(); }
247  const ControlEvent* front() const { return _events.front(); }
248 
249  std::pair<ControlList::iterator,ControlList::iterator> control_points_adjacent (Temporal::timepos_t const & when);
250 
251  template<class T> void apply_to_points (T& obj, void (T::*method)(const ControlList&)) {
252  Glib::Threads::RWLock::WriterLock lm (_lock);
253  (obj.*method)(*this);
254  }
255 
262  double eval (Temporal::timepos_t const & where) const {
263  Glib::Threads::RWLock::ReaderLock lm (_lock);
264  return unlocked_eval (where);
265  }
266 
274  double rt_safe_eval (Temporal::timepos_t const & where, bool& ok) const {
275 
276  Glib::Threads::RWLock::ReaderLock lm (_lock, Glib::Threads::TRY_LOCK);
277 
278  if ((ok = lm.locked())) {
279  return unlocked_eval (where);
280  } else {
281  return 0.0;
282  }
283  }
284 
285  static inline bool time_comparator (const ControlEvent* a, const ControlEvent* b) {
286  return a->when < b->when;
287  }
288 
290  struct LookupCache {
291  LookupCache() : left (std::numeric_limits<Temporal::timepos_t>::max()) {}
292  Temporal::timepos_t left; /* leftmost x coordinate used when finding "range" */
293  std::pair<ControlList::const_iterator,ControlList::const_iterator> range;
294  };
295 
297  struct SearchCache {
298  SearchCache () : left (std::numeric_limits<Temporal::timepos_t>::max()) {}
299  Temporal::timepos_t left; /* leftmost x coordinate used when finding "first" */
301  };
302 
304  const EventList& events() const { return _events; }
305 
306  // FIXME: const violations for Curve
307  Glib::Threads::RWLock& lock() const { return _lock; }
308  LookupCache& lookup_cache() const { return _lookup_cache; }
309  SearchCache& search_cache() const { return _search_cache; }
310 
316  double unlocked_eval (Temporal::timepos_t const & x) const;
317 
318  bool rt_safe_earliest_event_discrete_unlocked (Temporal::timepos_t const & start, Temporal::timepos_t & x, double& y, bool inclusive) const;
320 
321  void create_curve();
323 
324  Curve& curve() { assert(_curve); return *_curve; }
325  const Curve& curve() const { assert(_curve); return *_curve; }
326 
327  void mark_dirty () const;
328 
332  Curved, // spline, used for x-fades
334  Exponential // fader, gain
335  };
336 
340  InterpolationStyle interpolation() const { return _interpolation; }
341 
344 
354 
355  virtual bool touching() const { return false; }
356  virtual bool writing() const { return false; }
357  virtual bool touch_enabled() const { return false; }
359  void write_pass_finished (Temporal::timepos_t const &, double thinning_factor=0.0);
360  void set_in_write_pass (bool, bool add_point = false, Temporal::timepos_t = std::numeric_limits<Temporal::timepos_t>::min());
362  bool in_write_pass () const;
363  bool in_new_write_pass () { return new_write_pass; }
364 
367  mutable PBD::Signal<void()> Dirty;
370 
371  bool operator!= (ControlList const &) const;
372 
374 
377 
378  protected:
379 
381  double multipoint_eval (Temporal::timepos_t const & x) const;
382 
384 
385  std::shared_ptr<ControlList> cut_copy_clear (Temporal::timepos_t const &, Temporal::timepos_t const &, int op);
387 
390  bool maybe_insert_straight_line (Temporal::timepos_t const & when, double value);
391 
392  virtual void maybe_signal_changed ();
393 
394  void _x_scale (Temporal::ratio_t const &);
395 
398 
399  mutable Glib::Threads::RWLock _lock;
400 
405  int8_t _frozen;
408 
410 
411  private:
417 
420  void add_guard_point (Temporal::timepos_t const & when, Temporal::timecnt_t const & offset);
421 
422  bool is_sorted () const;
424 
425 };
426 
427 } // namespace Evoral
428 
429 #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:401
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:240
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:396
SearchCache _search_cache
Definition: ControlList.h:397
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
InterpolationStyle _interpolation
Definition: ControlList.h:403
SearchCache & search_cache() const
Definition: ControlList.h:309
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:367
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:274
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:308
const Curve & curve() const
Definition: ControlList.h:325
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:307
ParameterDescriptor _desc
Definition: ControlList.h:402
virtual bool touching() const
Definition: ControlList.h:355
const ControlEvent * back() const
Definition: ControlList.h:245
virtual InterpolationStyle default_interpolation() const
static bool time_comparator(const ControlEvent *a, const ControlEvent *b)
Definition: ControlList.h:285
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:247
InterpolationStyle interpolation() const
Definition: ControlList.h:340
PBD::Signal< void()> WritePassStarted
Definition: ControlList.h:365
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:262
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:251
const_iterator end() const
Definition: ControlList.h:239
const EventList & events() const
Definition: ControlList.h:304
virtual bool writing() const
Definition: ControlList.h:356
void unlocked_invalidate_insert_iterator()
void _x_scale(Temporal::ratio_t const &)
const_reverse_iterator rend() const
Definition: ControlList.h:243
void truncate_start(Temporal::timecnt_t const &overall_length)
bool in_write_pass() const
const_reverse_iterator rbegin() const
Definition: ControlList.h:241
Temporal::timepos_t insert_position
Definition: ControlList.h:413
Glib::Threads::RWLock _lock
Definition: ControlList.h:399
ControlEvent * back()
Definition: ControlList.h:244
bool frozen() const
Definition: ControlList.h:113
void erase(iterator, iterator)
PBD::Signal< void(InterpolationStyle)> InterpolationChanged
Definition: ControlList.h:369
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:357
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:242
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:246
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:237
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:412
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:85
bool operator==(const ProcessorSelection &a, const ProcessorSelection &b)
std::pair< ControlList::const_iterator, ControlList::const_iterator > range
Definition: ControlList.h:293
OrderedPoint(Temporal::timepos_t const &t, double v)
Definition: ControlList.h:176
ControlList::const_iterator first
Definition: ControlList.h:300