restore compilability amidst automation state mgmt changes
[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, bool no_state = false);
64         ~AutomationList();
65
66         AutomationList (const AutomationList&);
67         AutomationList (const AutomationList&, double start, double end);
68         AutomationList& operator= (const AutomationList&);
69         bool operator== (const AutomationList&);
70
71         void freeze();
72         void thaw ();
73
74         AutomationEventList::size_type size() const { return events.size(); }
75         bool empty() const { return events.empty(); }
76
77         void reset_default (double val) {
78                 default_value = val;
79         }
80
81         void clear ();
82         void x_scale (double factor);
83         bool extend_to (double);
84
85         void reposition_for_rt_add (double when);
86         void rt_add (double when, double value);
87         void add (double when, double value);
88
89         void reset_range (double start, double end);
90         void erase_range (double start, double end);
91         void erase (iterator);
92         void erase (iterator, iterator);
93         void move_range (iterator start, iterator end, double, double);
94         void modify (iterator, double, double);
95
96         AutomationList* cut (double, double);
97         AutomationList* copy (double, double);
98         void clear (double, double);
99
100         AutomationList* cut (iterator, iterator);
101         AutomationList* copy (iterator, iterator);
102         void clear (iterator, iterator);
103
104         bool paste (AutomationList&, double position, float times);
105
106         void set_automation_state (AutoState);
107         AutoState automation_state() const { return _state; }
108         sigc::signal<void> automation_style_changed;
109
110         void set_automation_style (AutoStyle m);
111         AutoStyle automation_style() const { return _style; }
112         sigc::signal<void> automation_state_changed;
113
114         bool automation_playback() {
115                 return (_state & Play) || ((_state & Touch) && !_touching);
116         }
117         bool automation_write () {
118                 return (_state & Write) || ((_state & Touch) && _touching);
119         }
120
121         void start_touch ();
122         void stop_touch ();
123         bool touching() const { return _touching; }
124
125         void set_yrange (double min, double max) {
126                 min_yval = min;
127                 max_yval = max;
128         }
129
130         double get_max_y() const { return max_yval; }
131         double get_min_y() const { return min_yval; }
132
133         void truncate_end (double length);
134         void truncate_start (double length);
135         
136         iterator begin() { return events.begin(); }
137         iterator end() { return events.end(); }
138
139         ControlEvent* back() { return events.back(); }
140         ControlEvent* front() { return events.front(); }
141
142         const_iterator const_begin() const { return events.begin(); }
143         const_iterator const_end() const { return events.end(); }
144
145         std::pair<AutomationList::iterator,AutomationList::iterator> control_points_adjacent (double when);
146
147         template<class T> void apply_to_points (T& obj, void (T::*method)(const AutomationList&)) {
148                 Glib::Mutex::Lock lm (lock);
149                 (obj.*method)(*this);
150         }
151
152         sigc::signal<void,Change> StateChanged;
153
154         XMLNode &get_state(void); 
155         int set_state (const XMLNode &s);
156
157         void set_max_xval (double);
158         double get_max_xval() const { return max_xval; }
159
160         double eval (double where) {
161                 Glib::Mutex::Lock lm (lock);
162                 return unlocked_eval (where);
163         }
164
165         double rt_safe_eval (double where, bool& ok) {
166
167                 Glib::Mutex::Lock lm (lock, Glib::TRY_LOCK);
168
169                 if ((ok = lm.locked())) {
170                         return unlocked_eval (where);
171                 } else {
172                         return 0.0;
173                 }
174         }
175
176         struct TimeComparator {
177                 bool operator() (const ControlEvent* a, const ControlEvent* b) { 
178                         return a->when < b->when;
179                 }
180         };
181
182         static sigc::signal<void, AutomationList*> AutomationListCreated;
183
184   protected:
185
186         AutomationEventList events;
187         mutable Glib::Mutex lock;
188         bool   _frozen;
189         bool    changed_when_thawed;
190         bool   _dirty;
191
192         struct LookupCache {
193             double left;  /* leftmost x coordinate used when finding "range" */
194             std::pair<AutomationList::iterator,AutomationList::iterator> range;
195         };
196
197         LookupCache lookup_cache;
198
199         AutoState  _state;
200         AutoStyle  _style;
201         bool  _touching;
202         bool  _new_touch;
203         double max_xval;
204         double min_yval;
205         double max_yval;
206         double default_value;
207         bool   no_state;
208
209         iterator rt_insertion_point;
210         double   rt_pos;
211
212         void maybe_signal_changed ();
213         void mark_dirty ();
214         void _x_scale (double factor);
215
216         /* called by type-specific unlocked_eval() to handle
217            common case of 0, 1 or 2 control points.
218         */
219
220         double shared_eval (double x);
221
222         /* called by shared_eval() to handle any case of
223            3 or more control points.
224         */
225
226         virtual double multipoint_eval (double x); 
227
228         /* called by locked entry point and various private
229            locations where we already hold the lock.
230         */
231
232         virtual double unlocked_eval (double where);
233
234         virtual ControlEvent* point_factory (double,double) const;
235         virtual ControlEvent* point_factory (const ControlEvent&) const;
236
237         AutomationList* cut_copy_clear (double, double, int op);
238 };
239
240 } // namespace
241
242 #endif /* __ardour_automation_event_h__ */