2 Copyright (C) 2002 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #ifndef __ardour_automation_event_h__
21 #define __ardour_automation_event_h__
27 #include <sigc++/signal.h>
28 #include <glibmm/thread.h>
31 #include <pbd/xml++.h>
32 #include <pbd/statefuldestructible.h>
34 #include <ardour/ardour.h>
35 #include <ardour/parameter.h>
43 ControlEvent (double w, double v)
44 : when (w), value (v), coeff (0) {
47 ControlEvent (const ControlEvent& other)
48 : when (other.when), value (other.value), coeff (0) {
51 for (size_t i=0; i < 4; ++i)
52 coeff[i] = other.coeff[i];
56 ~ControlEvent() { if (coeff) delete[] coeff; }
58 void create_coeffs() {
60 coeff = new double[4];
62 coeff[0] = coeff[1] = coeff[2] = coeff[3] = 0.0;
67 double* coeff; ///< double[4] allocated by Curve as needed
71 class AutomationList : public PBD::StatefulDestructible
74 typedef std::list<ControlEvent*> EventList;
75 typedef EventList::iterator iterator;
76 typedef EventList::const_iterator const_iterator;
78 AutomationList (Parameter id, double min_val, double max_val, double default_val);
79 AutomationList (const XMLNode&, Parameter id);
82 AutomationList (const AutomationList&);
83 AutomationList (const AutomationList&, double start, double end);
84 AutomationList& operator= (const AutomationList&);
85 bool operator== (const AutomationList&);
87 Parameter parameter() const { return _parameter; }
88 void set_parameter(Parameter p) { _parameter = p; }
93 EventList::size_type size() const { return _events.size(); }
94 bool empty() const { return _events.empty(); }
96 void reset_default (double val) {
101 void x_scale (double factor);
102 bool extend_to (double);
103 void slide (iterator before, double distance);
105 void reposition_for_rt_add (double when);
106 void rt_add (double when, double value);
107 void add (double when, double value);
108 /* this should be private but old-school automation loading needs it in IO/IOProcessor */
109 void fast_simple_add (double when, double value);
111 void reset_range (double start, double end);
112 void erase_range (double start, double end);
113 void erase (iterator);
114 void erase (iterator, iterator);
115 void move_range (iterator start, iterator end, double, double);
116 void modify (iterator, double, double);
118 AutomationList* cut (double, double);
119 AutomationList* copy (double, double);
120 void clear (double, double);
122 AutomationList* cut (iterator, iterator);
123 AutomationList* copy (iterator, iterator);
124 void clear (iterator, iterator);
126 bool paste (AutomationList&, double position, float times);
128 void set_automation_state (AutoState);
129 AutoState automation_state() const { return _state; }
130 sigc::signal<void> automation_style_changed;
132 void set_automation_style (AutoStyle m);
133 AutoStyle automation_style() const { return _style; }
134 sigc::signal<void> automation_state_changed;
136 bool automation_playback() const {
137 return (_state & Play) || ((_state & Touch) && !_touching);
139 bool automation_write () const {
140 return (_state & Write) || ((_state & Touch) && _touching);
145 bool touching() const { return _touching; }
147 void set_yrange (double min, double max) {
152 double get_max_y() const { return _max_yval; }
153 double get_min_y() const { return _min_yval; }
155 void truncate_end (double length);
156 void truncate_start (double length);
158 iterator begin() { return _events.begin(); }
159 iterator end() { return _events.end(); }
161 ControlEvent* back() { return _events.back(); }
162 ControlEvent* front() { return _events.front(); }
164 const_iterator const_begin() const { return _events.begin(); }
165 const_iterator const_end() const { return _events.end(); }
167 std::pair<AutomationList::iterator,AutomationList::iterator> control_points_adjacent (double when);
169 template<class T> void apply_to_points (T& obj, void (T::*method)(const AutomationList&)) {
170 Glib::Mutex::Lock lm (_lock);
171 (obj.*method)(*this);
174 sigc::signal<void> StateChanged;
176 XMLNode& get_state(void);
177 int set_state (const XMLNode &s);
178 XMLNode& state (bool full);
179 XMLNode& serialize_events ();
181 void set_max_xval (double);
182 double get_max_xval() const { return _max_xval; }
184 double eval (double where) {
185 Glib::Mutex::Lock lm (_lock);
186 return unlocked_eval (where);
189 double rt_safe_eval (double where, bool& ok) {
191 Glib::Mutex::Lock lm (_lock, Glib::TRY_LOCK);
193 if ((ok = lm.locked())) {
194 return unlocked_eval (where);
200 static inline bool time_comparator (const ControlEvent* a, const ControlEvent* b) {
201 return a->when < b->when;
204 /** Lookup cache for eval functions, range contains equivalent values */
206 LookupCache() : left(-1) {}
207 double left; /* leftmost x coordinate used when finding "range" */
208 std::pair<AutomationList::const_iterator,AutomationList::const_iterator> range;
211 /** Lookup cache for point finding, range contains points between left and right */
213 SearchCache() : left(-1), right(-1) {}
214 double left; /* leftmost x coordinate used when finding "range" */
215 double right; /* rightmost x coordinate used when finding "range" */
216 std::pair<AutomationList::const_iterator,AutomationList::const_iterator> range;
219 static sigc::signal<void, AutomationList*> AutomationListCreated;
221 const EventList& events() const { return _events; }
222 double default_value() const { return _default_value; }
224 // teeny const violations for Curve
225 mutable sigc::signal<void> Dirty;
226 Glib::Mutex& lock() const { return _lock; }
227 LookupCache& lookup_cache() const { return _lookup_cache; }
228 SearchCache& search_cache() const { return _search_cache; }
230 /** Called by locked entry point and various private
231 * locations where we already hold the lock.
233 * FIXME: Should this be private? Curve needs it..
235 double unlocked_eval (double x) const;
237 bool rt_safe_earliest_event (double start, double end, double& x, double& y) const;
239 Curve& curve() { return *_curve; }
240 const Curve& curve() const { return *_curve; }
242 enum InterpolationStyle {
248 InterpolationStyle interpolation() const { return _interpolation; }
249 void set_interpolation(InterpolationStyle style) { _interpolation = style; }
253 /** Called by unlocked_eval() to handle cases of 3 or more control points.
255 double multipoint_eval (double x) const;
257 void build_search_cache_if_necessary(double start, double end) const;
259 bool rt_safe_earliest_event_discrete (double start, double end, double& x, double& y) const;
260 bool rt_safe_earliest_event_linear (double start, double end, double& x, double& y) const;
262 AutomationList* cut_copy_clear (double, double, int op);
264 int deserialize_events (const XMLNode&);
266 void maybe_signal_changed ();
268 void _x_scale (double factor);
270 mutable LookupCache _lookup_cache;
271 mutable SearchCache _search_cache;
273 Parameter _parameter;
274 InterpolationStyle _interpolation;
276 mutable Glib::Mutex _lock;
278 bool _changed_when_thawed;
286 double _default_value;
288 iterator _rt_insertion_point;
296 #endif /* __ardour_automation_event_h__ */