c3e93fc7ae51ea7880d6cb2e152b777310edcc60
[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     $Id$
19 */
20
21 #ifndef __ardour_region_h__
22 #define __ardour_region_h__
23
24 #include <vector>
25 #include <boost/shared_ptr.hpp>
26 #include <boost/enable_shared_from_this.hpp>
27
28 #include <pbd/undo.h>
29 #include <pbd/statefuldestructible.h> 
30
31 #include <ardour/ardour.h>
32 #include <ardour/state_manager.h>
33 #include <ardour/data_type.h>
34
35 class XMLNode;
36
37 namespace ARDOUR {
38
39 class Playlist;
40 class Source;
41
42 enum RegionEditState {
43         EditChangesNothing = 0,
44         EditChangesName    = 1,
45         EditChangesID      = 2
46 };
47
48 struct RegionState : public StateManager::State
49 {
50         RegionState (std::string why) : StateManager::State (why) {}
51
52         jack_nframes_t          _start;
53         jack_nframes_t          _length;
54         jack_nframes_t          _position;
55         uint32_t                _flags;
56         jack_nframes_t          _sync_position;
57         layer_t                 _layer;
58         string                  _name;        
59         mutable RegionEditState _first_edit;
60 };
61
62 class Region : public PBD::StatefulDestructible, public StateManager, public boost::enable_shared_from_this<Region>
63 {
64   public:
65         typedef std::vector<boost::shared_ptr<Source> > SourceList;
66
67         enum Flag {
68                 Muted = 0x1,
69                 Opaque = 0x2,
70                 EnvelopeActive = 0x4,
71                 DefaultFadeIn = 0x8,
72                 DefaultFadeOut = 0x10,
73                 Locked = 0x20,
74                 Automatic = 0x40,
75                 WholeFile = 0x80,
76                 FadeIn = 0x100,
77                 FadeOut = 0x200,
78                 Copied = 0x400,
79                 Import = 0x800,
80                 External = 0x1000,
81                 SyncMarked = 0x2000,
82                 LeftOfSplit = 0x4000,
83                 RightOfSplit = 0x8000,
84                 Hidden = 0x10000,
85                 DoNotSaveState = 0x20000,
86                 //
87                 range_guarantoor = USHRT_MAX
88         };
89
90         static const Flag DefaultFlags = Flag (Opaque|DefaultFadeIn|DefaultFadeOut|FadeIn|FadeOut);
91
92         static Change FadeChanged;
93         static Change SyncOffsetChanged;
94         static Change MuteChanged;
95         static Change OpacityChanged;
96         static Change LockChanged;
97         static Change LayerChanged;
98         static Change HiddenChanged;
99
100         virtual ~Region();
101
102         const PBD::ID& id() const { return _id; }
103
104         /* Note: changing the name of a Region does not constitute an edit */
105
106         string name() const { return _name; }
107         void set_name (string str);
108
109         const DataType& data_type() const { return _type; }
110
111         jack_nframes_t position () const { return _position; }
112         jack_nframes_t start ()    const { return _start; }
113         jack_nframes_t length()    const { return _length; }
114         layer_t        layer ()    const { return _layer; }
115         
116         jack_nframes_t sync_offset(int& dir) const;
117         jack_nframes_t sync_position() const;
118
119         jack_nframes_t adjust_to_sync (jack_nframes_t);
120         
121         /* first_frame() is an alias; last_frame() just hides some math */
122
123         jack_nframes_t first_frame() const { return _position; }
124         jack_nframes_t last_frame() const { return _position + _length - 1; }
125
126         Flag flags()      const { return _flags; }
127         bool hidden()     const { return _flags & Hidden; }
128         bool muted()      const { return _flags & Muted; }
129         bool opaque ()    const { return _flags & Opaque; }
130         bool locked()     const { return _flags & Locked; }
131         bool automatic()  const { return _flags & Automatic; }
132         bool whole_file() const { return _flags & WholeFile ; }
133         bool captured()   const { return !(_flags & (Region::Flag (Region::Import|Region::External))); }
134
135         virtual bool should_save_state () const { return !(_flags & DoNotSaveState); };
136
137         void freeze ();
138         void thaw (const string& why);
139
140         bool covers (jack_nframes_t frame) const {
141                 return _position <= frame && frame < _position + _length;
142         }
143
144         OverlapType coverage (jack_nframes_t start, jack_nframes_t end) const {
145                 return ARDOUR::coverage (_position, _position + _length - 1, start, end);
146         }
147         
148         bool equivalent (boost::shared_ptr<const Region>) const;
149         bool size_equivalent (boost::shared_ptr<const Region>) const;
150         bool overlap_equivalent (boost::shared_ptr<const Region>) const;
151         bool region_list_equivalent (boost::shared_ptr<const Region>) const;
152         bool source_equivalent (boost::shared_ptr<const Region>) const;
153         
154         /* EDITING OPERATIONS */
155
156         void set_length (jack_nframes_t, void *src);
157         void set_start (jack_nframes_t, void *src);
158         void set_position (jack_nframes_t, void *src);
159         void set_position_on_top (jack_nframes_t, void *src);
160         void special_set_position (jack_nframes_t);
161         void nudge_position (long, void *src);
162
163         void move_to_natural_position (void *src);
164
165         void trim_start (jack_nframes_t new_position, void *src);
166         void trim_front (jack_nframes_t new_position, void *src);
167         void trim_end (jack_nframes_t new_position, void *src);
168         void trim_to (jack_nframes_t position, jack_nframes_t length, void *src);
169         
170         void set_layer (layer_t l); /* ONLY Playlist can call this */
171         void raise ();
172         void lower ();
173         void raise_to_top ();
174         void lower_to_bottom ();
175
176         void set_sync_position (jack_nframes_t n);
177         void clear_sync_position ();
178         void set_hidden (bool yn);
179         void set_muted (bool yn);
180         void set_opaque (bool yn);
181         void set_locked (bool yn);
182
183         virtual uint32_t read_data_count() const { return _read_data_count; }
184
185         ARDOUR::Playlist* playlist() const { return _playlist; }
186
187         virtual UndoAction get_memento() const = 0;
188
189         void set_playlist (ARDOUR::Playlist*);
190
191         void source_deleted (boost::shared_ptr<Source>);
192
193         boost::shared_ptr<Source> source (uint32_t n=0) const { return _sources[ (n < _sources.size()) ? n : 0 ]; }
194         uint32_t                  n_channels()          const { return _sources.size(); }
195
196         std::vector<string> master_source_names();
197
198
199         /* serialization */
200         
201         XMLNode&         get_state ();
202         virtual XMLNode& state (bool);
203         virtual int      set_state (const XMLNode&);
204
205         boost::shared_ptr<Region> get_parent();
206         
207         uint64_t last_layer_op() const { return _last_layer_op; }
208         void set_last_layer_op (uint64_t when);
209
210   protected:
211         friend class RegionFactory;
212
213         Region (boost::shared_ptr<Source> src, jack_nframes_t start, jack_nframes_t length, 
214                 const string& name, DataType type, layer_t = 0, Flag flags = DefaultFlags);
215         Region (SourceList& srcs, jack_nframes_t start, jack_nframes_t length, 
216                 const string& name, DataType type, layer_t = 0, Flag flags = DefaultFlags);
217         Region (boost::shared_ptr<const Region>, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t = 0, Flag flags = DefaultFlags);
218         Region (boost::shared_ptr<const Region>);
219         Region (boost::shared_ptr<Source> src, const XMLNode&);
220         Region (SourceList& srcs, const XMLNode&);
221
222   protected:
223         XMLNode& get_short_state (); /* used only by Session */
224
225         /* state management */
226
227         void send_change (Change);
228
229         /* derived classes need these during their own state management calls */
230
231         void   store_state (RegionState&) const;
232         Change restore_and_return_flags (RegionState&);
233         
234         void trim_to_internal (jack_nframes_t position, jack_nframes_t length, void *src);
235
236         bool copied() const { return _flags & Copied; }
237         void maybe_uncopy ();
238         void first_edit ();
239         
240         bool verify_start (jack_nframes_t);
241         bool verify_start_and_length (jack_nframes_t, jack_nframes_t);
242         bool verify_start_mutable (jack_nframes_t&_start);
243         bool verify_length (jack_nframes_t);
244         virtual void recompute_at_start () = 0;
245         virtual void recompute_at_end () = 0;
246         
247
248         PBD::ID                 _id;
249         string                  _name;
250         DataType                _type;
251         Flag                    _flags;
252         jack_nframes_t          _start;
253         jack_nframes_t          _length;
254         jack_nframes_t          _position;
255         jack_nframes_t          _sync_position;
256         layer_t                 _layer;
257         mutable RegionEditState _first_edit;
258         int                     _frozen;
259         mutable uint32_t        _read_data_count;  ///< modified in read()
260         Change                  _pending_changed;
261         uint64_t                _last_layer_op;  ///< timestamp
262         Glib::Mutex             _lock;
263         ARDOUR::Playlist*       _playlist;
264         SourceList              _sources;
265         /** Used when timefx are applied, so we can always use the original source */
266         SourceList              _master_sources;
267 };
268
269 } /* namespace ARDOUR */
270
271 #endif /* __ardour_region_h__ */