bump to 2.0.5
[ardour.git] / libs / ardour / ardour / automation_event.h
1 /*
2     Copyright (C) 2002 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
20 #ifndef __ardour_automation_event_h__
21 #define __ardour_automation_event_h__
22
23 #include <stdint.h>
24 #include <list>
25 #include <cmath>
26
27 #include <boost/pool/pool.hpp>
28 #include <boost/pool/pool_alloc.hpp>
29
30 #include <sigc++/signal.h>
31 #include <glibmm/thread.h>
32
33 #include <pbd/undo.h>
34 #include <pbd/xml++.h>
35 #include <pbd/statefuldestructible.h> 
36
37 #include <ardour/ardour.h>
38
39 namespace ARDOUR {
40         
41 class ControlEvent {
42   public:
43     double when;
44     double value;
45     
46     ControlEvent (double w, double v)
47             : when (w), value (v) { }
48     ControlEvent (const ControlEvent& other) 
49             : when (other.when), value (other.value) {}
50
51     virtual ~ControlEvent() {}
52     
53 //    bool operator==(const ControlEvent& other) {
54 //          return value == other.value && when == other.when;
55 //    }
56 };
57
58 /* automation lists use a pool allocator that does not use a lock and 
59    allocates 8k of new pointers at a time
60 */
61
62 typedef boost::fast_pool_allocator<ControlEvent*,
63         boost::default_user_allocator_new_delete,
64         boost::details::pool::null_mutex,
65         8192> ControlEventAllocator;
66
67 class AutomationList : public PBD::StatefulDestructible
68 {
69   public:
70         typedef std::list<ControlEvent*> AutomationEventList;
71         typedef AutomationEventList::iterator iterator;
72         typedef AutomationEventList::const_iterator const_iterator;
73
74         AutomationList (double default_value);
75         AutomationList (const XMLNode&);
76         ~AutomationList();
77
78         AutomationList (const AutomationList&);
79         AutomationList (const AutomationList&, double start, double end);
80         AutomationList& operator= (const AutomationList&);
81         bool operator== (const AutomationList&);
82
83         void freeze();
84         void thaw ();
85
86         AutomationEventList::size_type size() const { return events.size(); }
87         bool empty() const { return events.empty(); }
88
89         void reset_default (double val) {
90                 default_value = val;
91         }
92
93         void clear ();
94         void x_scale (double factor);
95         bool extend_to (double);
96         void slide (iterator before, double distance);
97         
98         void reposition_for_rt_add (double when);
99         void rt_add (double when, double value);
100         void add (double when, double value);
101         /* this should be private but old-school automation loading needs it in IO/Redirect */
102         void fast_simple_add (double when, double value);
103
104         void reset_range (double start, double end);
105         void erase_range (double start, double end);
106         void erase (iterator);
107         void erase (iterator, iterator);
108         void move_range (iterator start, iterator end, double, double);
109         void modify (iterator, double, double);
110
111         AutomationList* cut (double, double);
112         AutomationList* copy (double, double);
113         void clear (double, double);
114
115         AutomationList* cut (iterator, iterator);
116         AutomationList* copy (iterator, iterator);
117         void clear (iterator, iterator);
118
119         bool paste (AutomationList&, double position, float times);
120
121         void set_automation_state (AutoState);
122         AutoState automation_state() const { return _state; }
123         sigc::signal<void> automation_style_changed;
124
125         void set_automation_style (AutoStyle m);
126         AutoStyle automation_style() const { return _style; }
127         sigc::signal<void> automation_state_changed;
128
129         bool automation_playback() {
130                 return (_state & Play) || ((_state & Touch) && !_touching);
131         }
132         bool automation_write () {
133                 return (_state & Write) || ((_state & Touch) && _touching);
134         }
135
136         void start_touch ();
137         void stop_touch ();
138         bool touching() const { return _touching; }
139
140         void set_yrange (double min, double max) {
141                 min_yval = min;
142                 max_yval = max;
143         }
144
145         double get_max_y() const { return max_yval; }
146         double get_min_y() const { return min_yval; }
147
148         void truncate_end (double length);
149         void truncate_start (double length);
150         
151         iterator begin() { return events.begin(); }
152         iterator end() { return events.end(); }
153
154         ControlEvent* back() { return events.back(); }
155         ControlEvent* front() { return events.front(); }
156
157         const_iterator const_begin() const { return events.begin(); }
158         const_iterator const_end() const { return events.end(); }
159
160         std::pair<AutomationList::iterator,AutomationList::iterator> control_points_adjacent (double when);
161
162         template<class T> void apply_to_points (T& obj, void (T::*method)(const AutomationList&)) {
163                 Glib::Mutex::Lock lm (lock);
164                 (obj.*method)(*this);
165         }
166
167         sigc::signal<void> StateChanged;
168
169         XMLNode& get_state(void); 
170         int set_state (const XMLNode &s);
171         XMLNode& state (bool full);
172         XMLNode& serialize_events ();
173
174         void set_max_xval (double);
175         double get_max_xval() const { return max_xval; }
176
177         double eval (double where) {
178                 Glib::Mutex::Lock lm (lock);
179                 return unlocked_eval (where);
180         }
181
182         double rt_safe_eval (double where, bool& ok) {
183
184                 Glib::Mutex::Lock lm (lock, Glib::TRY_LOCK);
185
186                 if ((ok = lm.locked())) {
187                         return unlocked_eval (where);
188                 } else {
189                         return 0.0;
190                 }
191         }
192
193         struct TimeComparator {
194                 bool operator() (const ControlEvent* a, const ControlEvent* b) { 
195                         return a->when < b->when;
196                 }
197         };
198
199         static sigc::signal<void, AutomationList*> AutomationListCreated;
200
201   protected:
202
203         AutomationEventList events;
204         mutable Glib::Mutex lock;
205         int8_t  _frozen;
206         bool    changed_when_thawed;
207         bool   _dirty;
208
209         struct LookupCache {
210             double left;  /* leftmost x coordinate used when finding "range" */
211             std::pair<AutomationList::iterator,AutomationList::iterator> range;
212         };
213
214         LookupCache lookup_cache;
215
216         AutoState  _state;
217         AutoStyle  _style;
218         bool  _touching;
219         bool  _new_touch;
220         double max_xval;
221         double min_yval;
222         double max_yval;
223         double default_value;
224         bool   sort_pending;
225
226         iterator rt_insertion_point;
227         double   rt_pos;
228
229         void maybe_signal_changed ();
230         void mark_dirty ();
231         void _x_scale (double factor);
232
233         /* called by type-specific unlocked_eval() to handle
234            common case of 0, 1 or 2 control points.
235         */
236
237         double shared_eval (double x);
238
239         /* called by shared_eval() to handle any case of
240            3 or more control points.
241         */
242
243         virtual double multipoint_eval (double x); 
244
245         /* called by locked entry point and various private
246            locations where we already hold the lock.
247         */
248
249         virtual double unlocked_eval (double where);
250
251         virtual ControlEvent* point_factory (double,double) const;
252         virtual ControlEvent* point_factory (const ControlEvent&) const;
253
254         AutomationList* cut_copy_clear (double, double, int op);
255
256         int deserialize_events (const XMLNode&);
257 };
258
259 } // namespace
260
261 #endif /* __ardour_automation_event_h__ */