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