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