Move ARDOUR::Change into PBD so that Stateful can be aware of
[ardour.git] / libs / ardour / ardour / playlist.h
1 /*
2     Copyright (C) 2000 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_playlist_h__
21 #define __ardour_playlist_h__
22
23 #include <string>
24 #include <set>
25 #include <map>
26 #include <list>
27 #include <boost/shared_ptr.hpp>
28 #include <boost/enable_shared_from_this.hpp>
29 #include <boost/utility.hpp>
30
31 #include <sys/stat.h>
32
33 #include <glib.h>
34
35
36 #include "pbd/undo.h"
37 #include "pbd/stateful.h"
38 #include "pbd/statefuldestructible.h"
39
40 #include "evoral/types.hpp"
41
42 #include "ardour/ardour.h"
43 #include "ardour/session_object.h"
44 #include "ardour/crossfade_compare.h"
45 #include "ardour/location.h"
46 #include "ardour/data_type.h"
47
48 namespace ARDOUR  {
49
50 class Session;
51 class Region;
52
53 class Playlist : public SessionObject
54                , public boost::enable_shared_from_this<Playlist> {
55   public:
56         typedef std::list<boost::shared_ptr<Region> >    RegionList;
57
58         Playlist (Session&, const XMLNode&, DataType type, bool hidden = false);
59         Playlist (Session&, std::string name, DataType type, bool hidden = false);
60         Playlist (boost::shared_ptr<const Playlist>, std::string name, bool hidden = false);
61         Playlist (boost::shared_ptr<const Playlist>, nframes_t start, nframes_t cnt, std::string name, bool hidden = false);
62
63         virtual ~Playlist ();
64
65         void set_region_ownership ();
66
67         virtual void clear (bool with_signals=true);
68         virtual void dump () const;
69
70         void use();
71         void release();
72         bool used () const { return _refcnt != 0; }
73
74         bool set_name (const std::string& str);
75
76         const DataType& data_type() const { return _type; }
77
78         bool frozen() const { return _frozen; }
79         void set_frozen (bool yn);
80
81         bool hidden() const { return _hidden; }
82         bool empty() const;
83         uint32_t n_regions() const;
84         nframes_t get_maximum_extent () const;
85         layer_t top_layer() const;
86
87         EditMode get_edit_mode() const { return _edit_mode; }
88         void set_edit_mode (EditMode);
89
90         /* Editing operations */
91
92         void add_region (boost::shared_ptr<Region>, nframes_t position, float times = 1, bool auto_partition = false);
93         void remove_region (boost::shared_ptr<Region>);
94         void get_equivalent_regions (boost::shared_ptr<Region>, std::vector<boost::shared_ptr<Region> >&);
95         void get_region_list_equivalent_regions (boost::shared_ptr<Region>, std::vector<boost::shared_ptr<Region> >&);
96         void replace_region (boost::shared_ptr<Region> old, boost::shared_ptr<Region> newr, nframes_t pos);
97         void split_region (boost::shared_ptr<Region>, nframes_t position);
98         void split (nframes64_t at);
99         void shift (nframes64_t at, nframes64_t distance, bool move_intersected, bool ignore_music_glue);
100         void partition (nframes_t start, nframes_t end, bool cut = false);
101         void duplicate (boost::shared_ptr<Region>, nframes_t position, float times);
102         void nudge_after (nframes_t start, nframes_t distance, bool forwards);
103         void shuffle (boost::shared_ptr<Region>, int dir);
104         void update_after_tempo_map_change ();
105
106         boost::shared_ptr<Playlist> cut  (std::list<AudioRange>&, bool result_is_hidden = true);
107         boost::shared_ptr<Playlist> copy (std::list<AudioRange>&, bool result_is_hidden = true);
108         int                         paste (boost::shared_ptr<Playlist>, nframes_t position, float times);
109
110         const RegionList& region_list () const { return regions; }
111
112         RegionList*                regions_at (nframes_t frame);
113         RegionList*                regions_touched (nframes_t start, nframes_t end);
114         RegionList*                regions_to_read (nframes_t start, nframes_t end);
115         boost::shared_ptr<Region>  find_region (const PBD::ID&) const;
116         boost::shared_ptr<Region>  top_region_at (nframes_t frame);
117         boost::shared_ptr<Region>  top_unmuted_region_at (nframes_t frame);
118         boost::shared_ptr<Region>  find_next_region (nframes_t frame, RegionPoint point, int dir);
119         nframes64_t                find_next_region_boundary (nframes64_t frame, int dir);
120         bool                       region_is_shuffle_constrained (boost::shared_ptr<Region>);
121         bool                       has_region_at (nframes64_t const) const;
122
123
124         nframes64_t find_next_transient (nframes64_t position, int dir);
125
126         void foreach_region (boost::function<void (boost::shared_ptr<Region>)>);
127
128         XMLNode& get_state ();
129         int set_state (const XMLNode&, int version);
130         XMLNode& get_template ();
131
132         PBD::Signal1<void,bool> InUse;
133         PBD::Signal0<void>      ContentsChanged;
134         PBD::Signal1<void,boost::weak_ptr<Region> > RegionAdded;
135         PBD::Signal1<void,boost::weak_ptr<Region> > RegionRemoved;
136         PBD::Signal0<void>      NameChanged;
137         PBD::Signal0<void>      LengthChanged;
138         PBD::Signal0<void>      LayeringChanged;
139         PBD::Signal1<void,std::list< Evoral::RangeMove<nframes_t> > const &> RangesMoved;
140
141         static std::string bump_name (std::string old_name, Session&);
142
143         void freeze ();
144         void thaw ();
145
146         void raise_region (boost::shared_ptr<Region>);
147         void lower_region (boost::shared_ptr<Region>);
148         void raise_region_to_top (boost::shared_ptr<Region>);
149         void lower_region_to_bottom (boost::shared_ptr<Region>);
150
151         uint32_t read_data_count() const { return _read_data_count; }
152
153         const PBD::ID& get_orig_diskstream_id () const { return _orig_diskstream_id; }
154         void set_orig_diskstream_id (const PBD::ID& did) { _orig_diskstream_id = did; }
155
156         /* destructive editing */
157
158         virtual bool destroy_region (boost::shared_ptr<Region>) = 0;
159
160         /* special case function used by UI selection objects, which have playlists that actually own the regions
161            within them.
162         */
163
164         void drop_regions ();
165
166         bool explicit_relayering () const {
167                 return _explicit_relayering;
168         }
169
170         void set_explicit_relayering (bool e);
171
172   protected:
173         friend class Session;
174
175   protected:
176         struct RegionLock {
177                 RegionLock (Playlist *pl, bool do_block_notify = true) : playlist (pl), block_notify (do_block_notify) {
178                         playlist->region_lock.lock();
179                         if (block_notify) {
180                                 playlist->delay_notifications();
181                         }
182                 }
183                 ~RegionLock() {
184                         playlist->region_lock.unlock();
185                         if (block_notify) {
186                                 playlist->release_notifications ();
187                         }
188                 }
189                 Playlist *playlist;
190                 bool block_notify;
191         };
192
193         friend class RegionLock;
194
195         RegionList       regions;  /* the current list of regions in the playlist */
196         std::set<boost::shared_ptr<Region> > all_regions; /* all regions ever added to this playlist */
197         PBD::ScopedConnectionList region_state_changed_connections;
198         DataType        _type;
199         mutable gint    block_notifications;
200         mutable gint    ignore_state_changes;
201         mutable Glib::RecMutex region_lock;
202         std::set<boost::shared_ptr<Region> > pending_adds;
203         std::set<boost::shared_ptr<Region> > pending_removes;
204         RegionList       pending_bounds;
205         bool             pending_contents_change;
206         bool             pending_layering;
207         bool             pending_length;
208         std::list< Evoral::RangeMove<nframes_t> > pending_range_moves;
209         bool             save_on_thaw;
210         std::string      last_save_reason;
211         uint32_t         in_set_state;
212         bool             first_set_state;
213         bool            _hidden;
214         bool            _splicing;
215         bool            _shuffling;
216         bool            _nudging;
217         uint32_t        _refcnt;
218         EditMode        _edit_mode;
219         bool             in_flush;
220         bool             in_partition;
221         bool            _frozen;
222         uint32_t         subcnt;
223         uint32_t        _read_data_count;
224         PBD::ID         _orig_diskstream_id;
225         uint64_t         layer_op_counter;
226         nframes_t        freeze_length;
227         bool             auto_partition;
228
229         /** true if relayering should be done using region's current layers and their `pending explicit relayer'
230          *  flags; otherwise false if relayering should be done using the layer-model (most recently moved etc.)
231          *  Explicit relayering is used by tracks in stacked regionview mode.
232          */
233         bool            _explicit_relayering;
234
235         void init (bool hide);
236
237         bool holding_state () const {
238                 return g_atomic_int_get (&block_notifications) != 0 ||
239                         g_atomic_int_get (&ignore_state_changes) != 0;
240         }
241
242         void delay_notifications ();
243         void release_notifications ();
244         virtual void flush_notifications ();
245         void clear_pending ();
246
247         void notify_region_removed (boost::shared_ptr<Region>);
248         void notify_region_added (boost::shared_ptr<Region>);
249         void notify_length_changed ();
250         void notify_layering_changed ();
251         void notify_contents_changed ();
252         void notify_state_changed (PBD::Change);
253         void notify_region_moved (boost::shared_ptr<Region>);
254
255         void mark_session_dirty();
256
257         void region_changed_proxy (PBD::Change, boost::weak_ptr<Region>);
258         virtual bool region_changed (PBD::Change, boost::shared_ptr<Region>);
259
260         void region_bounds_changed (PBD::Change, boost::shared_ptr<Region>);
261         void region_deleted (boost::shared_ptr<Region>);
262
263         void sort_regions ();
264
265         void possibly_splice (nframes_t at, nframes64_t distance, boost::shared_ptr<Region> exclude = boost::shared_ptr<Region>());
266         void possibly_splice_unlocked(nframes_t at, nframes64_t distance, boost::shared_ptr<Region> exclude = boost::shared_ptr<Region>());
267
268         void core_splice (nframes_t at, nframes64_t distance, boost::shared_ptr<Region> exclude);
269         void splice_locked (nframes_t at, nframes64_t distance, boost::shared_ptr<Region> exclude);
270         void splice_unlocked (nframes_t at, nframes64_t distance, boost::shared_ptr<Region> exclude);
271
272         virtual void finalize_split_region (boost::shared_ptr<Region> /*original*/, boost::shared_ptr<Region> /*left*/, boost::shared_ptr<Region> /*right*/) {}
273
274         virtual void check_dependents (boost::shared_ptr<Region> /*region*/, bool /*norefresh*/) {}
275         virtual void refresh_dependents (boost::shared_ptr<Region> /*region*/) {}
276         virtual void remove_dependents (boost::shared_ptr<Region> /*region*/) {}
277
278         virtual XMLNode& state (bool);
279
280         boost::shared_ptr<Region> region_by_id (PBD::ID);
281
282         bool add_region_internal (boost::shared_ptr<Region>, nframes_t position);
283
284         int remove_region_internal (boost::shared_ptr<Region>);
285         RegionList *find_regions_at (nframes_t frame);
286         void copy_regions (RegionList&) const;
287         void partition_internal (nframes_t start, nframes_t end, bool cutting, RegionList& thawlist);
288
289         nframes_t _get_maximum_extent() const;
290
291         boost::shared_ptr<Playlist> cut_copy (boost::shared_ptr<Playlist> (Playlist::*pmf)(nframes_t, nframes_t, bool),
292                         std::list<AudioRange>& ranges, bool result_is_hidden);
293         boost::shared_ptr<Playlist> cut (nframes_t start, nframes_t cnt, bool result_is_hidden);
294         boost::shared_ptr<Playlist> copy (nframes_t start, nframes_t cnt, bool result_is_hidden);
295
296         int move_region_to_layer (layer_t, boost::shared_ptr<Region> r, int dir);
297         void relayer ();
298
299         void unset_freeze_parent (Playlist*);
300         void unset_freeze_child (Playlist*);
301
302         void timestamp_layer_op (boost::shared_ptr<Region>);
303
304         void _split_region (boost::shared_ptr<Region>, nframes_t position);
305 };
306
307 } /* namespace ARDOUR */
308
309 #endif  /* __ardour_playlist_h__ */
310
311