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