more cleanups, redesigns and subtle bug fixes for automation editing
[ardour2.git] / libs / ardour / ardour / automation_event.h
blob7918845efaf190f826aeed7a613c2c165edbf851
1 /*
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__
23 #include <stdint.h>
24 #include <list>
25 #include <cmath>
27 #include <boost/pool/pool.hpp>
28 #include <boost/pool/pool_alloc.hpp>
30 #include <sigc++/signal.h>
31 #include <glibmm/thread.h>
33 #include <pbd/undo.h>
34 #include <pbd/xml++.h>
35 #include <pbd/statefuldestructible.h>
37 #include <ardour/ardour.h>
39 namespace ARDOUR {
41 class ControlEvent {
42 public:
43 double when;
44 double value;
46 ControlEvent (double w, double v)
47 : when (w), value (v) { }
48 ControlEvent (const ControlEvent& other)
49 : when (other.when), value (other.value) {}
51 virtual ~ControlEvent() {}
53 // bool operator==(const ControlEvent& other) {
54 // return value == other.value && when == other.when;
55 // }
58 /* automation lists use a pool allocator that does not use a lock and
59 allocates 8k of new pointers at a time
62 typedef boost::fast_pool_allocator<ControlEvent*,
63 boost::default_user_allocator_new_delete,
64 boost::details::pool::null_mutex,
65 8192> ControlEventAllocator;
67 class AutomationList : public PBD::StatefulDestructible
69 public:
70 typedef std::list<ControlEvent*,ControlEventAllocator> AutomationEventList;
71 typedef AutomationEventList::iterator iterator;
72 typedef AutomationEventList::reverse_iterator reverse_iterator;
73 typedef AutomationEventList::const_iterator const_iterator;
75 AutomationList (double default_value);
76 AutomationList (const XMLNode&);
77 ~AutomationList();
79 AutomationList (const AutomationList&);
80 AutomationList (const AutomationList&, double start, double end);
81 AutomationList& operator= (const AutomationList&);
82 bool operator== (const AutomationList&);
84 void freeze();
85 void thaw ();
87 AutomationEventList::size_type size() const { return events.size(); }
88 bool empty() const { return events.empty(); }
90 void reset_default (double val) {
91 default_value = val;
94 void clear ();
95 void x_scale (double factor);
96 bool extend_to (double);
97 void slide (iterator before, double distance);
99 void write_pass_finished (double when);
100 void rt_add (double when, double value);
101 void add (double when, double value);
102 /* this should be private but old-school automation loading needs it in IO/Redirect */
103 void fast_simple_add (double when, double value);
104 void merge_nascent (double when);
106 void reset_range (double start, double end);
107 void erase_range (double start, double end);
108 void erase (iterator);
109 void erase (iterator, iterator);
110 void move_range (iterator start, iterator end, double, double);
111 void modify (iterator, double, double);
112 void shift (nframes64_t, nframes64_t);
114 AutomationList* cut (double, double);
115 AutomationList* copy (double, double);
116 void clear (double, double);
118 AutomationList* cut (iterator, iterator);
119 AutomationList* copy (iterator, iterator);
120 void clear (iterator, iterator);
122 bool paste (AutomationList&, double position, float times);
124 void set_automation_state (AutoState);
125 AutoState automation_state() const { return _state; }
126 sigc::signal<void> automation_style_changed;
128 void set_automation_style (AutoStyle m);
129 AutoStyle automation_style() const { return _style; }
130 sigc::signal<void> automation_state_changed;
132 bool automation_playback() {
133 return (_state & Auto_Play) || ((_state & Auto_Touch) && !touching());
135 bool automation_write () {
136 return (_state & Auto_Write) || ((_state & Auto_Touch) && touching());
139 void start_touch (double when);
140 void stop_touch (bool mark, double when);
141 bool touching() const { return g_atomic_int_get (&_touching); }
143 void set_yrange (double min, double max) {
144 min_yval = min;
145 max_yval = max;
148 double get_max_y() const { return max_yval; }
149 double get_min_y() const { return min_yval; }
151 void truncate_end (double length);
152 void truncate_start (double length);
154 iterator begin() { return events.begin(); }
155 iterator end() { return events.end(); }
157 ControlEvent* back() { return events.back(); }
158 ControlEvent* front() { return events.front(); }
160 const_iterator const_begin() const { return events.begin(); }
161 const_iterator const_end() const { return events.end(); }
163 std::pair<AutomationList::iterator,AutomationList::iterator> control_points_adjacent (double when);
165 template<class T> void apply_to_points (T& obj, void (T::*method)(const AutomationList&)) {
166 Glib::Mutex::Lock lm (lock);
167 (obj.*method)(*this);
170 sigc::signal<void> StateChanged;
172 XMLNode& get_state(void);
173 int set_state (const XMLNode &s);
174 XMLNode& state (bool full);
175 XMLNode& serialize_events ();
177 void set_max_xval (double);
178 double get_max_xval() const { return max_xval; }
180 double eval (double where) {
181 Glib::Mutex::Lock lm (lock);
182 return unlocked_eval (where);
185 double rt_safe_eval (double where, bool& ok) {
187 Glib::Mutex::Lock lm (lock, Glib::TRY_LOCK);
189 if ((ok = lm.locked())) {
190 return unlocked_eval (where);
191 } else {
192 return 0.0;
196 struct TimeComparator {
197 bool operator() (const ControlEvent* a, const ControlEvent* b) {
198 return a->when < b->when;
202 static sigc::signal<void, AutomationList*> AutomationListCreated;
204 protected:
206 struct NascentInfo {
207 AutomationEventList events;
208 bool is_touch;
209 double start_time;
210 double end_time;
212 NascentInfo (bool touching, double start = -1.0)
213 : is_touch (touching)
214 , start_time (start)
215 , end_time (-1.0)
219 AutomationEventList events;
220 std::list<NascentInfo*> nascent;
221 mutable Glib::Mutex lock;
222 int8_t _frozen;
223 bool changed_when_thawed;
224 bool _dirty;
226 struct LookupCache {
227 double left; /* leftmost x coordinate used when finding "range" */
228 std::pair<AutomationList::iterator,AutomationList::iterator> range;
231 LookupCache lookup_cache;
233 AutoState _state;
234 AutoStyle _style;
235 gint _touching;
236 bool _new_touch;
237 double max_xval;
238 double min_yval;
239 double max_yval;
240 double default_value;
241 bool sort_pending;
243 void maybe_signal_changed ();
244 void mark_dirty ();
245 void _x_scale (double factor);
247 /* called by type-specific unlocked_eval() to handle
248 common case of 0, 1 or 2 control points.
251 double shared_eval (double x);
253 /* called by shared_eval() to handle any case of
254 3 or more control points.
257 virtual double multipoint_eval (double x);
259 /* called by locked entry point and various private
260 locations where we already hold the lock.
263 virtual double unlocked_eval (double where);
265 virtual ControlEvent* point_factory (double,double) const;
266 virtual ControlEvent* point_factory (const ControlEvent&) const;
268 AutomationList* cut_copy_clear (double, double, int op);
270 int deserialize_events (const XMLNode&);
273 } // namespace
275 #endif /* __ardour_automation_event_h__ */