Use AudioFile.h instead of ExtAudioFile.h.
[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 <pbd/lockmonitor.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 erase_range (double start, double end);
89         void erase (iterator);
90         void erase (iterator, iterator);
91         void move_range (iterator start, iterator end, double, double);
92         void modify (iterator, double, double);
93
94         AutomationList* cut (double, double);
95         AutomationList* copy (double, double);
96         void clear (double, double);
97
98         AutomationList* cut (iterator, iterator);
99         AutomationList* copy (iterator, iterator);
100         void clear (iterator, iterator);
101
102         bool paste (AutomationList&, double position, float times);
103
104         void set_automation_state (AutoState);
105         AutoState automation_state() const { return _state; }
106         sigc::signal<void> automation_style_changed;
107
108         void set_automation_style (AutoStyle m);
109         AutoStyle automation_style() const { return _style; }
110         sigc::signal<void> automation_state_changed;
111
112         bool automation_playback() {
113                 return (_state & Play) || ((_state & Touch) && !_touching);
114         }
115         bool automation_write () {
116                 return (_state & Write) || ((_state & Touch) && _touching);
117         }
118
119         void start_touch ();
120         void stop_touch ();
121         bool touching() const { return _touching; }
122
123         void set_yrange (double min, double max) {
124                 min_yval = min;
125                 max_yval = max;
126         }
127
128         double get_max_y() const { return max_yval; }
129         double get_min_y() const { return min_yval; }
130
131         void truncate_end (double length);
132         void truncate_start (double length);
133         
134         iterator begin() { return events.begin(); }
135         iterator end() { return events.end(); }
136
137         ControlEvent* back() { return events.back(); }
138         ControlEvent* front() { return events.front(); }
139
140         const_iterator const_begin() const { return events.begin(); }
141         const_iterator const_end() const { return events.end(); }
142
143         std::pair<AutomationList::iterator,AutomationList::iterator> control_points_adjacent (double when);
144
145         template<class T> void apply_to_points (T& obj, void (T::*method)(const AutomationList&)) {
146                 LockMonitor lm (lock, __LINE__, __FILE__);
147                 (obj.*method)(*this);
148         }
149
150         UndoAction get_memento () const;
151         
152         virtual void store_state (XMLNode& node) const;
153         virtual void load_state (const XMLNode&);
154
155         void set_max_xval (double);
156         double get_max_xval() const { return max_xval; }
157
158         double eval (double where) {
159                 LockMonitor lm (lock, __LINE__, __FILE__);
160                 return unlocked_eval (where);
161         }
162
163         double rt_safe_eval (double where, bool& ok) {
164
165                 TentativeLockMonitor lm (lock, __LINE__, __FILE__);
166
167                 if ((ok = lm.locked())) {
168                         return unlocked_eval (where);
169                 } else {
170                         return 0.0;
171                 }
172         }
173
174         struct TimeComparator {
175                 bool operator() (const ControlEvent* a, const ControlEvent* b) { 
176                         return a->when < b->when;
177                 }
178         };
179
180   protected:
181         struct State : public ARDOUR::StateManager::State {
182             AutomationEventList events;
183
184             State (std::string why) : ARDOUR::StateManager::State (why) {}
185         };
186
187         AutomationEventList events;
188         mutable PBD::NonBlockingLock lock;
189         bool   _frozen;
190         bool    changed_when_thawed;
191         bool   _dirty;
192
193         struct LookupCache {
194             double left;  /* leftmost x coordinate used when finding "range" */
195             std::pair<AutomationList::iterator,AutomationList::iterator> range;
196         };
197
198         LookupCache lookup_cache;
199
200         AutoState  _state;
201         AutoStyle  _style;
202         bool  _touching;
203         bool  _new_touch;
204         double max_xval;
205         double min_yval;
206         double max_yval;
207         double default_value;
208         bool   no_state;
209
210         iterator rt_insertion_point;
211         double   rt_pos;
212
213         void maybe_signal_changed ();
214         void mark_dirty ();
215         void _x_scale (double factor);
216
217         /* called by type-specific unlocked_eval() to handle
218            common case of 0, 1 or 2 control points.
219         */
220
221         double shared_eval (double x);
222
223         /* called by shared_eval() to handle any case of
224            3 or more control points.
225         */
226
227         virtual double multipoint_eval (double x); 
228
229         /* called by locked entry point and various private
230            locations where we already hold the lock.
231         */
232
233         virtual double unlocked_eval (double where);
234
235         Change   restore_state (StateManager::State&);
236         StateManager::State* state_factory (std::string why) const;
237
238         virtual ControlEvent* point_factory (double,double) const;
239         virtual ControlEvent* point_factory (const ControlEvent&) const;
240
241
242         AutomationList* cut_copy_clear (double, double, int op);
243 };
244
245 } // namespace
246
247 #endif /* __ardour_automation_event_h__ */