d2920fb6105e9c5059ec15171be69c2665944655
[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
30 #include <sys/stat.h>
31
32 #include <glib.h>
33
34 #include <sigc++/signal.h>
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, public boost::enable_shared_from_this<Playlist> {
54   public:
55         typedef list<boost::shared_ptr<Region> >    RegionList;
56
57         Playlist (Session&, const XMLNode&, DataType type, bool hidden = false);
58         Playlist (Session&, string name, DataType type, bool hidden = false);
59         Playlist (boost::shared_ptr<const Playlist>, string name, bool hidden = false);
60         Playlist (boost::shared_ptr<const Playlist>, nframes_t start, nframes_t cnt, string name, bool hidden = false);
61
62         virtual ~Playlist ();  
63
64         void set_region_ownership ();
65
66         virtual void clear (bool with_signals=true);
67         virtual void dump () const;
68
69         void use();
70         void release();
71         bool used () const { return _refcnt != 0; }
72
73         bool set_name (const string& str);
74
75         const DataType& data_type() const { return _type; }
76
77         bool frozen() const { return _frozen; }
78         void set_frozen (bool yn);
79
80         bool hidden() const { return _hidden; }
81         bool empty() const;
82         uint32_t n_regions() const;
83         nframes_t get_maximum_extent () const;
84         layer_t top_layer() const;
85
86         EditMode get_edit_mode() const { return _edit_mode; }
87         void set_edit_mode (EditMode);
88
89         /* Editing operations */
90
91         void add_region (boost::shared_ptr<Region>, nframes_t position, float times = 1);
92         void remove_region (boost::shared_ptr<Region>);
93         void get_equivalent_regions (boost::shared_ptr<Region>, std::vector<boost::shared_ptr<Region> >&);
94         void get_region_list_equivalent_regions (boost::shared_ptr<Region>, std::vector<boost::shared_ptr<Region> >&);
95         void replace_region (boost::shared_ptr<Region> old, boost::shared_ptr<Region> newr, nframes_t pos);
96         void split_region (boost::shared_ptr<Region>, nframes_t position);
97         void split (nframes64_t at);
98         void shift (nframes64_t at, nframes64_t distance, bool move_intersected, bool ignore_music_glue);
99         void partition (nframes_t start, nframes_t end, bool just_top_level);
100         void duplicate (boost::shared_ptr<Region>, nframes_t position, float times);
101         void nudge_after (nframes_t start, nframes_t distance, bool forwards);
102         void shuffle (boost::shared_ptr<Region>, int dir);
103         void update_after_tempo_map_change ();
104
105         boost::shared_ptr<Playlist> cut  (list<AudioRange>&, bool result_is_hidden = true);
106         boost::shared_ptr<Playlist> copy (list<AudioRange>&, bool result_is_hidden = true);
107         int                         paste (boost::shared_ptr<Playlist>, nframes_t position, float times);
108
109         RegionList*                regions_at (nframes_t frame);
110         RegionList*                regions_touched (nframes_t start, nframes_t end);
111         RegionList*                regions_to_read (nframes_t start, nframes_t end);
112         boost::shared_ptr<Region>  find_region (const PBD::ID&) const;
113         boost::shared_ptr<Region>  top_region_at (nframes_t frame);
114         boost::shared_ptr<Region>  find_next_region (nframes_t frame, RegionPoint point, int dir);
115         nframes64_t                find_next_region_boundary (nframes64_t frame, int dir);
116         bool                       region_is_shuffle_constrained (boost::shared_ptr<Region>);
117
118         nframes64_t find_next_transient (nframes64_t position, int dir);
119
120         template<class T> void foreach_region (T *t, void (T::*func)(boost::shared_ptr<Region>, void *), void *arg);
121         template<class T> void foreach_region (T *t, void (T::*func)(boost::shared_ptr<Region>));
122
123         XMLNode& get_state ();
124         int set_state (const XMLNode&);
125         XMLNode& get_template ();
126
127         sigc::signal<void,bool> InUse;
128         sigc::signal<void>      Modified;
129         sigc::signal<void>      NameChanged;
130         sigc::signal<void>      LengthChanged;
131         sigc::signal<void, list< Evoral::RangeMove<nframes_t> > const &> RangesMoved;
132
133         static string bump_name (string old_name, Session&);
134
135         void freeze ();
136         void thaw ();
137
138         void raise_region (boost::shared_ptr<Region>);
139         void lower_region (boost::shared_ptr<Region>);
140         void raise_region_to_top (boost::shared_ptr<Region>);
141         void lower_region_to_bottom (boost::shared_ptr<Region>);
142
143         uint32_t read_data_count() const { return _read_data_count; }
144
145         const PBD::ID& get_orig_diskstream_id () const { return _orig_diskstream_id; }
146         void set_orig_diskstream_id (const PBD::ID& did) { _orig_diskstream_id = did; }  
147
148         /* destructive editing */
149         
150         virtual bool destroy_region (boost::shared_ptr<Region>) = 0;
151
152         /* special case function used by UI selection objects, which have playlists that actually own the regions
153            within them.
154         */
155
156         void drop_regions ();
157
158   protected:
159         friend class Session;
160
161   protected:
162         struct RegionLock {
163             RegionLock (Playlist *pl, bool do_block_notify = true) : playlist (pl), block_notify (do_block_notify) {
164                     playlist->region_lock.lock();
165                     if (block_notify) {
166                             playlist->delay_notifications();
167                     }
168             }
169             ~RegionLock() { 
170                     playlist->region_lock.unlock();
171                     if (block_notify) {
172                             playlist->release_notifications ();
173                     }
174             }
175             Playlist *playlist;
176             bool block_notify;
177         };
178
179         friend class RegionLock;
180
181         RegionList       regions;  /* the current list of regions in the playlist */
182         std::set<boost::shared_ptr<Region> > all_regions; /* all regions ever added to this playlist */
183         std::list<sigc::connection> region_state_changed_connections;
184         DataType        _type;
185         mutable gint    block_notifications;
186         mutable gint    ignore_state_changes;
187         mutable Glib::Mutex region_lock;
188         std::set<boost::shared_ptr<Region> > pending_adds;
189         std::set<boost::shared_ptr<Region> > pending_removes;
190         RegionList       pending_bounds;
191         bool             pending_modified;
192         bool             pending_length;
193         list< Evoral::RangeMove<nframes_t> > pending_range_moves;
194         bool             save_on_thaw;
195         string           last_save_reason;
196         uint32_t         in_set_state;
197         bool             first_set_state;
198         bool            _hidden;
199         bool            _splicing;
200         bool            _shuffling;
201         bool            _nudging;
202         uint32_t        _refcnt;
203         EditMode        _edit_mode;
204         bool             in_flush;
205         bool             in_partition;
206         bool            _frozen;
207         uint32_t         subcnt;
208         uint32_t        _read_data_count;
209         PBD::ID         _orig_diskstream_id;
210         uint64_t         layer_op_counter;
211         nframes_t   freeze_length;
212
213         void init (bool hide);
214
215         bool holding_state () const { 
216                 return g_atomic_int_get (&block_notifications) != 0 ||
217                         g_atomic_int_get (&ignore_state_changes) != 0;
218         }
219
220         /* prevent the compiler from ever generating these */
221
222         Playlist (const Playlist&);
223         Playlist (Playlist&);
224
225         void delay_notifications ();
226         void release_notifications ();
227         virtual void flush_notifications ();
228
229         void notify_region_removed (boost::shared_ptr<Region>);
230         void notify_region_added (boost::shared_ptr<Region>);
231         void notify_length_changed ();
232         void notify_layering_changed ();
233         void notify_modified ();
234         void notify_state_changed (Change);
235         void notify_region_moved (boost::shared_ptr<Region>);
236
237         void mark_session_dirty();
238
239         void region_changed_proxy (Change, boost::weak_ptr<Region>);
240         virtual bool region_changed (Change, boost::shared_ptr<Region>);
241
242         void region_bounds_changed (Change, boost::shared_ptr<Region>);
243         void region_deleted (boost::shared_ptr<Region>);
244
245         void sort_regions ();
246
247         void possibly_splice (nframes_t at, nframes64_t distance, boost::shared_ptr<Region> exclude = boost::shared_ptr<Region>());
248         void possibly_splice_unlocked(nframes_t at, nframes64_t distance, boost::shared_ptr<Region> exclude = boost::shared_ptr<Region>());
249
250         void core_splice (nframes_t at, nframes64_t distance, boost::shared_ptr<Region> exclude);
251         void splice_locked (nframes_t at, nframes64_t distance, boost::shared_ptr<Region> exclude);
252         void splice_unlocked (nframes_t at, nframes64_t distance, boost::shared_ptr<Region> exclude);
253
254         virtual void finalize_split_region (boost::shared_ptr<Region> original, boost::shared_ptr<Region> left, boost::shared_ptr<Region> right) {}
255         
256         virtual void check_dependents (boost::shared_ptr<Region> region, bool norefresh) {}
257         virtual void refresh_dependents (boost::shared_ptr<Region> region) {}
258         virtual void remove_dependents (boost::shared_ptr<Region> region) {}
259
260         virtual XMLNode& state (bool);
261
262         boost::shared_ptr<Region> region_by_id (PBD::ID);
263
264         bool add_region_internal (boost::shared_ptr<Region>, nframes_t position);
265         
266         int remove_region_internal (boost::shared_ptr<Region>);
267         RegionList *find_regions_at (nframes_t frame);
268         void copy_regions (RegionList&) const;
269         void partition_internal (nframes_t start, nframes_t end, bool cutting, RegionList& thawlist);
270
271         nframes_t _get_maximum_extent() const;
272
273         boost::shared_ptr<Playlist> cut_copy (boost::shared_ptr<Playlist> (Playlist::*pmf)(nframes_t, nframes_t, bool), 
274                                               list<AudioRange>& ranges, bool result_is_hidden);
275         boost::shared_ptr<Playlist> cut (nframes_t start, nframes_t cnt, bool result_is_hidden);
276         boost::shared_ptr<Playlist> copy (nframes_t start, nframes_t cnt, bool result_is_hidden);
277
278         int move_region_to_layer (layer_t, boost::shared_ptr<Region> r, int dir);
279         void relayer ();
280         
281         void unset_freeze_parent (Playlist*);
282         void unset_freeze_child (Playlist*);
283
284         void timestamp_layer_op (boost::shared_ptr<Region>);
285
286         void _split_region (boost::shared_ptr<Region>, nframes_t position);
287 };
288
289 } /* namespace ARDOUR */
290
291 #endif  /* __ardour_playlist_h__ */
292
293