summaryrefslogtreecommitdiff
path: root/libs/ardour/ardour/automation_event.h
blob: af1a3cb70483dcf8e7736850779bbba5cc462ca5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
    Copyright (C) 2002 Paul Davis 

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#ifndef __ardour_automation_event_h__
#define __ardour_automation_event_h__

#include <stdint.h>
#include <list>
#include <cmath>

#include <sigc++/signal.h>
#include <glibmm/thread.h>

#include <pbd/undo.h>
#include <pbd/xml++.h>
#include <pbd/statefuldestructible.h> 

#include <ardour/ardour.h>
#include <ardour/param_id.h>

namespace ARDOUR {

class Curve;

struct ControlEvent {

    ControlEvent (double w, double v)
	    : when (w), value (v) { 
	    coeff[0] = coeff[1] = coeff[2] = coeff[3] = 0.0;
	}

    ControlEvent (const ControlEvent& other) 
	    : when (other.when), value (other.value) {
	    coeff[0] = coeff[1] = coeff[2] = coeff[3] = 0.0;
	}
    
	double when;
    double value;
	double coeff[4]; ///< Used by Curve
};


class AutomationList : public PBD::StatefulDestructible
{
  public:
	typedef std::list<ControlEvent*> EventList;
	typedef EventList::iterator iterator;
	typedef EventList::const_iterator const_iterator;

	AutomationList (ParamID id, double min_val, double max_val, double default_val);
	AutomationList (const XMLNode&, ParamID id);
	~AutomationList();

	AutomationList (const AutomationList&);
	AutomationList (const AutomationList&, double start, double end);
	AutomationList& operator= (const AutomationList&);
	bool operator== (const AutomationList&);

	ParamID param_id() const         { return _param_id; }
	void    set_param_id(ParamID id) { _param_id = id; }

	void freeze();
	void thaw ();

	EventList::size_type size() const { return _events.size(); }
	bool empty() const { return _events.empty(); }

	void reset_default (double val) {
		_default_value = val;
	}

	void clear ();
	void x_scale (double factor);
	bool extend_to (double);
	void slide (iterator before, double distance);
	
	void reposition_for_rt_add (double when);
	void rt_add (double when, double value);
	void add (double when, double value);
	/* this should be private but old-school automation loading needs it in IO/Redirect */
	void fast_simple_add (double when, double value);

	void reset_range (double start, double end);
	void erase_range (double start, double end);
	void erase (iterator);
	void erase (iterator, iterator);
	void move_range (iterator start, iterator end, double, double);
	void modify (iterator, double, double);

	AutomationList* cut (double, double);
	AutomationList* copy (double, double);
	void clear (double, double);

	AutomationList* cut (iterator, iterator);
	AutomationList* copy (iterator, iterator);
	void clear (iterator, iterator);

	bool paste (AutomationList&, double position, float times);

	void set_automation_state (AutoState);
	AutoState automation_state() const { return _state; }
	sigc::signal<void> automation_style_changed;

	void set_automation_style (AutoStyle m);
	AutoStyle automation_style() const { return _style; }
	sigc::signal<void> automation_state_changed;

	bool automation_playback() const {
		return (_state & Play) || ((_state & Touch) && !_touching);
	}
	bool automation_write () const {
		return (_state & Write) || ((_state & Touch) && _touching);
	}

	void start_touch ();
	void stop_touch ();
	bool touching() const { return _touching; }

	void set_yrange (double min, double max) {
		_min_yval = min;
		_max_yval = max;
	}

	double get_max_y() const { return _max_yval; }
	double get_min_y() const { return _min_yval; }

	void truncate_end (double length);
	void truncate_start (double length);
	
	iterator begin() { return _events.begin(); }
	iterator end() { return _events.end(); }

	ControlEvent* back() { return _events.back(); }
	ControlEvent* front() { return _events.front(); }

	const_iterator const_begin() const { return _events.begin(); }
	const_iterator const_end() const { return _events.end(); }

	std::pair<AutomationList::iterator,AutomationList::iterator> control_points_adjacent (double when);

	template<class T> void apply_to_points (T& obj, void (T::*method)(const AutomationList&)) {
		Glib::Mutex::Lock lm (_lock);
		(obj.*method)(*this);
	}

	sigc::signal<void> StateChanged;

	XMLNode& get_state(void); 
	int set_state (const XMLNode &s);
	XMLNode& state (bool full);
	XMLNode& serialize_events ();

	void set_max_xval (double);
	double get_max_xval() const { return _max_xval; }

	double eval (double where) {
		Glib::Mutex::Lock lm (_lock);
		return unlocked_eval (where);
	}

	double rt_safe_eval (double where, bool& ok) {

		Glib::Mutex::Lock lm (_lock, Glib::TRY_LOCK);

		if ((ok = lm.locked())) {
			return unlocked_eval (where);
		} else {
			return 0.0;
		}
	}

	struct TimeComparator {
		bool operator() (const ControlEvent* a, const ControlEvent* b) { 
			return a->when < b->when;
		}
	};
	
	struct LookupCache {
	    double left;  /* leftmost x coordinate used when finding "range" */
	    std::pair<AutomationList::const_iterator,AutomationList::const_iterator> range;
	};

	static sigc::signal<void, AutomationList*> AutomationListCreated;

	const EventList& events() const { return _events; }
	double default_value() const { return _default_value; }

	// teeny const violations for Curve
	mutable sigc::signal<void> Dirty;
	Glib::Mutex& lock() const { return _lock; }
	LookupCache& lookup_cache() const { return _lookup_cache; }
	
	/** Called by locked entry point and various private
	 * locations where we already hold the lock.
	 * 
	 * FIXME: Should this be private?  Curve needs it..
	 */
	double unlocked_eval (double x) const;

	Curve&       curve()       { return *_curve; }
	const Curve& curve() const { return *_curve; }

  protected:

	/** Called by unlocked_eval() to handle cases of 3 or more control points.
	 */
	virtual double multipoint_eval (double x) const; 

	AutomationList* cut_copy_clear (double, double, int op);

	int deserialize_events (const XMLNode&);
	
	void maybe_signal_changed ();
	void mark_dirty ();
	void _x_scale (double factor);

	mutable LookupCache _lookup_cache;
	
	ParamID             _param_id;
	EventList           _events;
	mutable Glib::Mutex _lock;
	int8_t              _frozen;
	bool                _changed_when_thawed;
	AutoState           _state;
	AutoStyle           _style;
	bool                _touching;
	bool                _new_touch;
	double              _max_xval;
	double              _min_yval;
	double              _max_yval;
	double              _default_value;
	bool                _sort_pending;
	iterator            _rt_insertion_point;
	double              _rt_pos;

	Curve* _curve;
};

} // namespace

#endif /* __ardour_automation_event_h__ */