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