78daa531dd0e438992e84bb4b750b352bebb713a
[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
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         void set_max_xval (double);
157         double get_max_xval() const { return max_xval; }
158
159         double eval (double where) {
160                 Glib::Mutex::Lock lm (lock);
161                 return unlocked_eval (where);
162         }
163
164         double rt_safe_eval (double where, bool& ok) {
165
166                 Glib::Mutex::Lock lm (lock, Glib::TRY_LOCK);
167
168                 if ((ok = lm.locked())) {
169                         return unlocked_eval (where);
170                 } else {
171                         return 0.0;
172                 }
173         }
174
175         struct TimeComparator {
176                 bool operator() (const ControlEvent* a, const ControlEvent* b) { 
177                         return a->when < b->when;
178                 }
179         };
180
181   protected:
182         struct State : public ARDOUR::StateManager::State {
183             AutomationEventList events;
184
185             State (std::string why) : ARDOUR::StateManager::State (why) {}
186         };
187
188         AutomationEventList events;
189         mutable Glib::Mutex lock;
190         bool   _frozen;
191         bool    changed_when_thawed;
192         bool   _dirty;
193
194         struct LookupCache {
195             double left;  /* leftmost x coordinate used when finding "range" */
196             std::pair<AutomationList::iterator,AutomationList::iterator> range;
197         };
198
199         LookupCache lookup_cache;
200
201         AutoState  _state;
202         AutoStyle  _style;
203         bool  _touching;
204         bool  _new_touch;
205         double max_xval;
206         double min_yval;
207         double max_yval;
208         double default_value;
209         bool   no_state;
210
211         iterator rt_insertion_point;
212         double   rt_pos;
213
214         void maybe_signal_changed ();
215         void mark_dirty ();
216         void _x_scale (double factor);
217
218         /* called by type-specific unlocked_eval() to handle
219            common case of 0, 1 or 2 control points.
220         */
221
222         double shared_eval (double x);
223
224         /* called by shared_eval() to handle any case of
225            3 or more control points.
226         */
227
228         virtual double multipoint_eval (double x); 
229
230         /* called by locked entry point and various private
231            locations where we already hold the lock.
232         */
233
234         virtual double unlocked_eval (double where);
235
236         Change   restore_state (StateManager::State&);
237         StateManager::State* state_factory (std::string why) const;
238
239         virtual ControlEvent* point_factory (double,double) const;
240         virtual ControlEvent* point_factory (const ControlEvent&) const;
241
242
243         AutomationList* cut_copy_clear (double, double, int op);
244 };
245
246 } // namespace
247
248 #endif /* __ardour_automation_event_h__ */