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