Merge with 2.0-ongoing R2988
[ardour.git] / libs / ardour / ardour / region.h
1 /*
2     Copyright (C) 2000-2001 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 */
19
20 #ifndef __ardour_region_h__
21 #define __ardour_region_h__
22
23 #include <vector>
24 #include <boost/shared_ptr.hpp>
25 #include <boost/enable_shared_from_this.hpp>
26
27 #include <pbd/undo.h>
28
29 #include <ardour/ardour.h>
30 #include <ardour/data_type.h>
31 #include <ardour/automatable.h>
32 #include <ardour/readable.h>
33
34 class XMLNode;
35
36 namespace ARDOUR {
37
38 class Playlist;
39 class Filter;
40
41 enum RegionEditState {
42         EditChangesNothing = 0,
43         EditChangesName    = 1,
44         EditChangesID      = 2
45 };
46
47 class Region : public Automatable, public boost::enable_shared_from_this<Region>, public Readable
48 {
49   public:
50         typedef std::vector<boost::shared_ptr<Source> > SourceList;
51
52         enum Flag {
53                 Muted = 0x1,
54                 Opaque = 0x2,
55                 EnvelopeActive = 0x4,
56                 DefaultFadeIn = 0x8,
57                 DefaultFadeOut = 0x10,
58                 Locked = 0x20,
59                 Automatic = 0x40,
60                 WholeFile = 0x80,
61                 FadeIn = 0x100,
62                 FadeOut = 0x200,
63                 Copied = 0x400,
64                 Import = 0x800,
65                 External = 0x1000,
66                 SyncMarked = 0x2000,
67                 LeftOfSplit = 0x4000,
68                 RightOfSplit = 0x8000,
69                 Hidden = 0x10000,
70                 DoNotSaveState = 0x20000,
71                 PositionLocked = 0x40000,
72                 //
73                 range_guarantoor = USHRT_MAX
74         };
75
76         enum PositionLockStyle {
77                 AudioTime,
78                 MusicTime
79         };
80
81         static const Flag DefaultFlags = Flag (Opaque|DefaultFadeIn|DefaultFadeOut|FadeIn|FadeOut);
82
83         static Change FadeChanged;
84         static Change SyncOffsetChanged;
85         static Change MuteChanged;
86         static Change OpacityChanged;
87         static Change LockChanged;
88         static Change LayerChanged;
89         static Change HiddenChanged;
90
91         sigc::signal<void,Change> StateChanged;
92
93         virtual ~Region();
94
95         /** Note: changing the name of a Region does not constitute an edit */
96         bool set_name (const std::string& str);
97
98         const DataType& data_type() const { return _type; }
99
100         nframes_t position () const { return _position; }
101         nframes_t start ()    const { return _start; }
102         nframes_t length()    const { return _length; }
103         layer_t   layer ()    const { return _layer; }
104
105         /* these two are valid ONLY during a StateChanged signal handler */
106
107         nframes_t last_position() const { return _last_position; }
108         nframes_t last_length() const { return _last_length; }
109
110         nframes64_t ancestral_start () const { return _ancestral_start; }
111         nframes64_t ancestral_length () const { return _ancestral_length; }
112         float stretch() const { return _stretch; }
113         float shift() const { return _shift; }
114
115         void set_ancestral_data (nframes64_t start, nframes64_t length, float stretch, float shift);
116
117         nframes_t sync_offset(int& dir) const;
118         nframes_t sync_position() const;
119
120         nframes_t adjust_to_sync (nframes_t);
121         
122         /* first_frame() is an alias; last_frame() just hides some math */
123
124         nframes_t first_frame() const { return _position; }
125         nframes_t last_frame() const { return _position + _length - 1; }
126
127         Flag flags()      const { return _flags; }
128         bool hidden()     const { return _flags & Hidden; }
129         bool muted()      const { return _flags & Muted; }
130         bool opaque ()    const { return _flags & Opaque; }
131         bool locked()     const { return _flags & Locked; }
132         bool position_locked() const { return _flags & PositionLocked; }
133         bool automatic()  const { return _flags & Automatic; }
134         bool whole_file() const { return _flags & WholeFile ; }
135         bool captured()   const { return !(_flags & (Region::Flag (Region::Import|Region::External))); }
136         bool can_move()   const { return !(_flags & (Locked|PositionLocked)); }
137
138         PositionLockStyle positional_lock_style() const { return _positional_lock_style; }
139         void set_position_lock_style (PositionLockStyle ps);
140
141         virtual bool should_save_state () const { return !(_flags & DoNotSaveState); };
142
143         void freeze ();
144         void thaw (const string& why);
145
146         bool covers (nframes_t frame) const {
147                 return first_frame() <= frame && frame <= last_frame();
148         }
149
150         OverlapType coverage (nframes_t start, nframes_t end) const {
151                 return ARDOUR::coverage (first_frame(), last_frame(), start, end);
152         }
153         
154         bool equivalent (boost::shared_ptr<const Region>) const;
155         bool size_equivalent (boost::shared_ptr<const Region>) const;
156         bool overlap_equivalent (boost::shared_ptr<const Region>) const;
157         bool region_list_equivalent (boost::shared_ptr<const Region>) const;
158         bool source_equivalent (boost::shared_ptr<const Region>) const;
159         
160         /* EDITING OPERATIONS */
161
162         void set_length (nframes_t, void *src);
163         void set_start (nframes_t, void *src);
164         void set_position (nframes_t, void *src);
165         void set_position_on_top (nframes_t, void *src);
166         void special_set_position (nframes_t);
167         void update_position_after_tempo_map_change ();
168         void nudge_position (nframes64_t, void *src);
169
170         bool at_natural_position () const;
171         void move_to_natural_position (void *src);
172
173         void trim_start (nframes_t new_position, void *src);
174         void trim_front (nframes_t new_position, void *src);
175         void trim_end (nframes_t new_position, void *src);
176         void trim_to (nframes_t position, nframes_t length, void *src);
177         
178         void set_layer (layer_t l); /* ONLY Playlist can call this */
179         void raise ();
180         void lower ();
181         void raise_to_top ();
182         void lower_to_bottom ();
183
184         void set_sync_position (nframes_t n);
185         void clear_sync_position ();
186         void set_hidden (bool yn);
187         void set_muted (bool yn);
188         void set_opaque (bool yn);
189         void set_locked (bool yn);
190         void set_position_locked (bool yn);
191         
192         int apply (Filter&);
193
194         virtual uint32_t read_data_count() const { return _read_data_count; }
195
196         boost::shared_ptr<ARDOUR::Playlist> playlist() const { return _playlist.lock(); }
197         virtual void set_playlist (boost::weak_ptr<ARDOUR::Playlist>);
198
199         void source_deleted (boost::shared_ptr<Source>);
200         
201         boost::shared_ptr<Source> source (uint32_t n=0) const { return _sources[ (n < _sources.size()) ? n : 0 ]; }
202         uint32_t                  n_channels()          const { return _sources.size(); }
203
204         const SourceList& sources() const { return _sources; }
205         const SourceList& master_sources() const { return _master_sources; }
206
207         std::vector<string> master_source_names();
208         void set_master_sources (SourceList&);
209         
210         /* serialization */
211         
212         XMLNode&         get_state ();
213         virtual XMLNode& state (bool);
214         virtual int      set_state (const XMLNode&);
215         virtual int      set_live_state (const XMLNode&, Change&, bool send);
216
217         virtual boost::shared_ptr<Region> get_parent() const;
218         
219         uint64_t last_layer_op() const { return _last_layer_op; }
220         void set_last_layer_op (uint64_t when);
221
222         virtual bool is_dependent() const { return false; }
223         virtual bool depends_on (boost::shared_ptr<Region> other) const { return false; }
224
225         virtual int get_transients (AnalysisFeatureList&, bool force_new = false) { 
226                 // no transients, but its OK
227                 return 0;
228         }
229
230         void invalidate_transients ();  
231
232   protected:
233         friend class RegionFactory;
234
235         Region (boost::shared_ptr<Source> src, nframes_t start, nframes_t length, 
236                 const string& name, DataType type, layer_t = 0, Flag flags = DefaultFlags);
237         Region (const SourceList& srcs, nframes_t start, nframes_t length, 
238                 const string& name, DataType type, layer_t = 0, Flag flags = DefaultFlags);
239         
240         Region (boost::shared_ptr<const Region>, nframes_t start, nframes_t length, const string& name, layer_t = 0, Flag flags = DefaultFlags);
241         Region (boost::shared_ptr<const Region>);
242         Region (boost::shared_ptr<Source> src, const XMLNode&);
243         Region (const SourceList& srcs, const XMLNode&);
244
245         Region (Session& s, nframes_t start, nframes_t length, const string& name, DataType, layer_t = 0, Flag flags = DefaultFlags);
246
247   protected:
248         XMLNode& get_short_state (); /* used only by Session */
249
250         void send_change (Change);
251
252         void trim_to_internal (nframes_t position, nframes_t length, void *src);
253         void set_position_internal (nframes_t pos, bool allow_bbt_recompute);
254
255         bool copied() const { return _flags & Copied; }
256         void maybe_uncopy ();
257         void first_edit ();
258         
259         bool verify_start (nframes_t);
260         bool verify_start_and_length (nframes_t, nframes_t&);
261         bool verify_start_mutable (nframes_t&_start);
262         bool verify_length (nframes_t);
263         
264         virtual void recompute_at_start () = 0;
265         virtual void recompute_at_end () = 0;
266
267         DataType                _type;
268         Flag                    _flags;
269         nframes_t               _start;
270         nframes_t               _length;
271         nframes_t               _last_length;
272         nframes_t               _position;
273         nframes_t               _last_position;
274         PositionLockStyle       _positional_lock_style;
275         nframes_t               _sync_position;
276         layer_t                 _layer;
277         mutable RegionEditState _first_edit;
278         int                     _frozen;
279         nframes64_t             _ancestral_start;
280         nframes64_t             _ancestral_length;
281         float                   _stretch;
282         float                   _shift;
283         BBT_Time                _bbt_time;
284         AnalysisFeatureList     _transients;
285         bool                    _valid_transients;
286         mutable uint32_t        _read_data_count;  ///< modified in read()
287         Change                  _pending_changed;
288         uint64_t                _last_layer_op;  ///< timestamp
289         Glib::Mutex             _lock;
290         SourceList              _sources;
291         /** Used when timefx are applied, so we can always use the original source */
292         SourceList              _master_sources;
293         
294         boost::weak_ptr<ARDOUR::Playlist> _playlist;
295 };
296
297 } /* namespace ARDOUR */
298
299 #endif /* __ardour_region_h__ */