Re-work layering in possibly debatable ways. Sketchy docs in doc/layering.
[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 #include "pbd/sequence_property.h"
40
41 #include "evoral/types.hpp"
42
43 #include "ardour/ardour.h"
44 #include "ardour/session_object.h"
45 #include "ardour/data_type.h"
46
47 class PlaylistOverlapCacheTest;
48 class PlaylistLayeringTest;
49
50 namespace ARDOUR  {
51
52 class Session;
53 class Region;
54 class Playlist;
55 class Crossfade;
56
57 namespace Properties {
58         /* fake the type, since regions are handled by SequenceProperty which doesn't
59            care about such things.
60         */
61         extern PBD::PropertyDescriptor<bool> regions;
62 }
63
64 class RegionListProperty : public PBD::SequenceProperty<std::list<boost::shared_ptr<Region> > >
65 {
66   public:
67         RegionListProperty (Playlist&);
68
69         RegionListProperty* clone () const;
70         void get_content_as_xml (boost::shared_ptr<Region>, XMLNode &) const;
71         boost::shared_ptr<Region> get_content_from_xml (XMLNode const &) const;
72
73   private:
74         RegionListProperty* create () const;
75
76         /* copy construction only by ourselves */
77         RegionListProperty (RegionListProperty const & p);
78
79         friend class Playlist;
80         /* we live and die with our playlist, no lifetime management needed */
81         Playlist& _playlist;
82 };
83
84 class Playlist : public SessionObject , public boost::enable_shared_from_this<Playlist>
85 {
86 public:
87         typedef std::list<boost::shared_ptr<Region> > RegionList;
88         static void make_property_quarks ();
89
90         Playlist (Session&, const XMLNode&, DataType type, bool hidden = false);
91         Playlist (Session&, std::string name, DataType type, bool hidden = false);
92         Playlist (boost::shared_ptr<const Playlist>, std::string name, bool hidden = false);
93         Playlist (boost::shared_ptr<const Playlist>, framepos_t start, framecnt_t cnt, std::string name, bool hidden = false);
94
95         virtual ~Playlist ();
96
97         void update (const RegionListProperty::ChangeRecord&);
98         void clear_owned_changes ();
99         void rdiff (std::vector<Command*>&) const;
100
101         boost::shared_ptr<Region> region_by_id (const PBD::ID&) const;
102
103         uint32_t max_source_level () const;
104
105         void set_region_ownership ();
106
107         virtual void clear (bool with_signals=true);
108         virtual void dump () const;
109
110         void use();
111         void release();
112         bool used () const { return _refcnt != 0; }
113
114         bool set_name (const std::string& str);
115         int sort_id() { return _sort_id; }
116
117         const DataType& data_type() const { return _type; }
118
119         bool frozen() const { return _frozen; }
120         void set_frozen (bool yn);
121
122         bool hidden() const { return _hidden; }
123         bool empty() const;
124         uint32_t n_regions() const;
125         std::pair<framepos_t, framepos_t> get_extent () const;
126         layer_t top_layer() const;
127
128         EditMode get_edit_mode() const { return _edit_mode; }
129         void set_edit_mode (EditMode);
130
131         /* Editing operations */
132
133         void add_region (boost::shared_ptr<Region>, framepos_t position, float times = 1, bool auto_partition = false);
134         void remove_region (boost::shared_ptr<Region>);
135         void remove_region_by_source (boost::shared_ptr<Source>);
136         void get_equivalent_regions (boost::shared_ptr<Region>, std::vector<boost::shared_ptr<Region> >&);
137         void get_region_list_equivalent_regions (boost::shared_ptr<Region>, std::vector<boost::shared_ptr<Region> >&);
138         void replace_region (boost::shared_ptr<Region> old, boost::shared_ptr<Region> newr, framepos_t pos);
139         void split_region (boost::shared_ptr<Region>, framepos_t position);
140         void split (framepos_t at);
141         void shift (framepos_t at, frameoffset_t distance, bool move_intersected, bool ignore_music_glue);
142         void partition (framepos_t start, framepos_t end, bool cut = false);
143         void duplicate (boost::shared_ptr<Region>, framepos_t position, float times);
144         void nudge_after (framepos_t start, framecnt_t distance, bool forwards);
145         boost::shared_ptr<Region> combine (const RegionList&);
146         void uncombine (boost::shared_ptr<Region>);
147
148         void shuffle (boost::shared_ptr<Region>, int dir);
149         void update_after_tempo_map_change ();
150
151         boost::shared_ptr<Playlist> cut  (std::list<AudioRange>&, bool result_is_hidden = true);
152         boost::shared_ptr<Playlist> copy (std::list<AudioRange>&, bool result_is_hidden = true);
153         int                         paste (boost::shared_ptr<Playlist>, framepos_t position, float times);
154
155         const RegionListProperty& region_list () const { return regions; }
156
157         RegionList*                regions_at (framepos_t frame);
158         uint32_t                   count_regions_at (framepos_t) const;
159         uint32_t                   count_joined_regions () const;
160         RegionList*                regions_touched (framepos_t start, framepos_t end);
161         RegionList*                regions_to_read (framepos_t start, framepos_t end);
162         uint32_t                   region_use_count (boost::shared_ptr<Region>) const;
163         boost::shared_ptr<Region>  find_region (const PBD::ID&) const;
164         boost::shared_ptr<Region>  top_region_at (framepos_t frame);
165         boost::shared_ptr<Region>  top_unmuted_region_at (framepos_t frame);
166         boost::shared_ptr<Region>  find_next_region (framepos_t frame, RegionPoint point, int dir);
167         framepos_t                 find_next_region_boundary (framepos_t frame, int dir);
168         bool                       region_is_shuffle_constrained (boost::shared_ptr<Region>);
169         bool                       has_region_at (framepos_t const) const;
170
171         bool uses_source (boost::shared_ptr<const Source> src) const;
172
173         framepos_t find_next_transient (framepos_t position, int dir);
174
175         void foreach_region (boost::function<void (boost::shared_ptr<Region>)>);
176
177         XMLNode& get_state ();
178         int set_state (const XMLNode&, int version);
179         XMLNode& get_template ();
180
181         PBD::Signal1<void,bool> InUse;
182         PBD::Signal0<void>      ContentsChanged;
183         PBD::Signal1<void,boost::weak_ptr<Region> > RegionAdded;
184         PBD::Signal1<void,boost::weak_ptr<Region> > RegionRemoved;
185         PBD::Signal0<void>      NameChanged;
186         PBD::Signal0<void>      LayeringChanged;
187
188         /** Emitted when regions have moved (not when regions have only been trimmed) */
189         PBD::Signal2<void,std::list< Evoral::RangeMove<framepos_t> > const &, bool> RangesMoved;
190
191         /** Emitted when regions are extended; the ranges passed are the new extra time ranges
192             that these regions now occupy.
193         */
194         PBD::Signal1<void,std::list< Evoral::Range<framepos_t> > const &> RegionsExtended;
195
196         static std::string bump_name (std::string old_name, Session&);
197
198         void freeze ();
199         void thaw (bool from_undo = false);
200
201         void raise_region (boost::shared_ptr<Region>);
202         void lower_region (boost::shared_ptr<Region>);
203         void raise_region_to_top (boost::shared_ptr<Region>);
204         void lower_region_to_bottom (boost::shared_ptr<Region>);
205
206         const PBD::ID& get_orig_track_id () const { return _orig_track_id; }
207         void set_orig_track_id (const PBD::ID& did);
208
209         /* destructive editing */
210
211         virtual bool destroy_region (boost::shared_ptr<Region>) = 0;
212
213         void sync_all_regions_with_regions ();
214
215         /* special case function used by UI selection objects, which have playlists that actually own the regions
216            within them.
217         */
218
219         void drop_regions ();
220
221         virtual boost::shared_ptr<Crossfade> find_crossfade (const PBD::ID &) const {
222                 return boost::shared_ptr<Crossfade> ();
223         }
224
225         framepos_t find_next_top_layer_position (framepos_t) const;
226         uint32_t combine_ops() const { return _combine_ops; }
227
228         void relayer (boost::shared_ptr<Region>, double);
229         void suspend_relayer ();
230         void resume_relayer ();
231         
232   protected:
233         friend class Session;
234
235   protected:
236         struct RegionLock {
237                 RegionLock (Playlist *pl, bool do_block_notify = true) : playlist (pl), block_notify (do_block_notify) {
238                         playlist->region_lock.lock();
239                         if (block_notify) {
240                                 playlist->delay_notifications();
241                         }
242                 }
243                 ~RegionLock() {
244                         playlist->region_lock.unlock();
245                         if (block_notify) {
246                                 playlist->release_notifications ();
247                         }
248                 }
249                 Playlist *playlist;
250                 bool block_notify;
251         };
252
253         friend class RegionLock;
254
255         RegionListProperty   regions;  /* the current list of regions in the playlist */
256         std::set<boost::shared_ptr<Region> > all_regions; /* all regions ever added to this playlist */
257         PBD::ScopedConnectionList region_state_changed_connections;
258         DataType        _type;
259         int             _sort_id;
260         mutable gint    block_notifications;
261         mutable gint    ignore_state_changes;
262         mutable Glib::RecMutex region_lock;
263         std::set<boost::shared_ptr<Region> > pending_adds;
264         std::set<boost::shared_ptr<Region> > pending_removes;
265         RegionList       pending_bounds;
266         bool             pending_contents_change;
267         bool             pending_layering;
268
269         /** Movements of time ranges caused by region moves; note that
270          *  region trims are not included in this list; it is used to
271          *  do automation-follows-regions.
272          */
273         std::list< Evoral::RangeMove<framepos_t> > pending_range_moves;
274         /** Extra sections added to regions during trims */
275         std::list< Evoral::Range<framepos_t> >     pending_region_extensions;
276         bool             save_on_thaw;
277         std::string      last_save_reason;
278         uint32_t         in_set_state;
279         bool             in_update;
280         bool             first_set_state;
281         bool            _hidden;
282         bool            _splicing;
283         bool            _shuffling;
284         bool            _nudging;
285         uint32_t        _refcnt;
286         EditMode        _edit_mode;
287         bool             in_flush;
288         bool             in_partition;
289         bool            _frozen;
290         uint32_t         subcnt;
291         PBD::ID         _orig_track_id;
292         framecnt_t       freeze_length;
293         bool             auto_partition;
294         uint32_t        _combine_ops;
295
296         void init (bool hide);
297
298         bool holding_state () const {
299                 return g_atomic_int_get (&block_notifications) != 0 ||
300                         g_atomic_int_get (&ignore_state_changes) != 0;
301         }
302
303         void delay_notifications ();
304         void release_notifications (bool from_undo = false);
305         virtual void flush_notifications (bool from_undo = false);
306         void clear_pending ();
307
308         void _set_sort_id ();
309
310         void notify_region_removed (boost::shared_ptr<Region>);
311         void notify_region_added (boost::shared_ptr<Region>);
312         void notify_layering_changed ();
313         void notify_contents_changed ();
314         void notify_state_changed (const PBD::PropertyChange&);
315         void notify_region_moved (boost::shared_ptr<Region>);
316         void notify_region_start_trimmed (boost::shared_ptr<Region>);
317         void notify_region_end_trimmed (boost::shared_ptr<Region>);
318
319         void mark_session_dirty();
320
321         void region_changed_proxy (const PBD::PropertyChange&, boost::weak_ptr<Region>);
322         virtual bool region_changed (const PBD::PropertyChange&, boost::shared_ptr<Region>);
323
324         void region_bounds_changed (const PBD::PropertyChange&, boost::shared_ptr<Region>);
325         void region_deleted (boost::shared_ptr<Region>);
326
327         void sort_regions ();
328
329         void possibly_splice (framepos_t at, framecnt_t distance, boost::shared_ptr<Region> exclude = boost::shared_ptr<Region>());
330         void possibly_splice_unlocked(framepos_t at, framecnt_t distance, boost::shared_ptr<Region> exclude = boost::shared_ptr<Region>());
331
332         void core_splice (framepos_t at, framecnt_t distance, boost::shared_ptr<Region> exclude);
333         void splice_locked (framepos_t at, framecnt_t distance, boost::shared_ptr<Region> exclude);
334         void splice_unlocked (framepos_t at, framecnt_t distance, boost::shared_ptr<Region> exclude);
335
336         virtual void finalize_split_region (boost::shared_ptr<Region> /*original*/, boost::shared_ptr<Region> /*left*/, boost::shared_ptr<Region> /*right*/) {}
337
338         virtual void check_dependents (boost::shared_ptr<Region> /*region*/, bool /*norefresh*/) {}
339         virtual void refresh_dependents (boost::shared_ptr<Region> /*region*/) {}
340         virtual void remove_dependents (boost::shared_ptr<Region> /*region*/) {}
341
342         virtual XMLNode& state (bool);
343
344         bool add_region_internal (boost::shared_ptr<Region>, framepos_t position);
345
346         int remove_region_internal (boost::shared_ptr<Region>);
347         RegionList *find_regions_at (framepos_t);
348         void copy_regions (RegionList&) const;
349         void partition_internal (framepos_t start, framepos_t end, bool cutting, RegionList& thawlist);
350
351         std::pair<framepos_t, framepos_t> _get_extent() const;
352
353         boost::shared_ptr<Playlist> cut_copy (boost::shared_ptr<Playlist> (Playlist::*pmf)(framepos_t, framecnt_t, bool),
354                                               std::list<AudioRange>& ranges, bool result_is_hidden);
355         boost::shared_ptr<Playlist> cut (framepos_t start, framecnt_t cnt, bool result_is_hidden);
356         boost::shared_ptr<Playlist> copy (framepos_t start, framecnt_t cnt, bool result_is_hidden);
357
358         void relayer (boost::shared_ptr<Region>);
359         void relayer (RegionList const &);
360
361         void begin_undo ();
362         void end_undo ();
363         void unset_freeze_parent (Playlist*);
364         void unset_freeze_child (Playlist*);
365
366         void _split_region (boost::shared_ptr<Region>, framepos_t position);
367
368         typedef std::pair<boost::shared_ptr<Region>, boost::shared_ptr<Region> > TwoRegions;
369         virtual void copy_dependents (const std::vector<TwoRegions>&, Playlist*) const { }
370
371         struct RegionInfo {
372             boost::shared_ptr<Region> region;
373             framepos_t position;
374             framecnt_t length;
375             framepos_t start;
376         };
377
378         /* this is called before we create a new compound region */
379         virtual void pre_combine (std::vector<boost::shared_ptr<Region> >&) {}
380         /* this is called before we create a new compound region */
381         virtual void post_combine (std::vector<boost::shared_ptr<Region> >&, boost::shared_ptr<Region>) {}
382         /* this is called before we remove a compound region and replace it
383            with its constituent regions
384         */
385         virtual void pre_uncombine (std::vector<boost::shared_ptr<Region> >&, boost::shared_ptr<Region>) {}
386
387 private:
388         friend class ::PlaylistOverlapCacheTest;
389         friend class ::PlaylistLayeringTest;
390         
391         /** A class which is used to store temporary (fractional)
392          *  layer assignments for some regions.
393          */
394         class TemporaryLayers
395         {
396         public:
397                 void set (boost::shared_ptr<Region>, double);
398                 double get (boost::shared_ptr<Region>) const;
399
400         private:                
401                 typedef std::map<boost::shared_ptr<Region>, double> Map;
402                 Map _map;
403         };
404
405         /** Class to sort by temporary layer, for use with std::list<>::sort() */
406         class SortByTemporaryLayer
407         {
408         public:
409                 SortByTemporaryLayer (TemporaryLayers const & t)
410                         : _temporary_layers (t) {}
411                 
412                 bool operator() (boost::shared_ptr<Region> a, boost::shared_ptr<Region> b) const {
413                         return _temporary_layers.get (a) < _temporary_layers.get (b);
414                 }
415
416         private:
417                 Playlist::TemporaryLayers const & _temporary_layers;
418         };
419
420         /** A cache of what overlaps what, for a given playlist in a given state.
421          *  Divides a playlist up into time periods and notes which regions cover those
422          *  periods, so that get() is reasonably quick.
423          */
424         class OverlapCache
425         {
426         public:
427                 OverlapCache (Playlist *);
428
429                 RegionList get (Evoral::Range<framepos_t>) const;
430
431         private:
432                 std::pair<int, int> cache_indices (Evoral::Range<framepos_t>) const;
433                 
434                 double _division_size;
435                 std::vector<RegionList> _cache;
436                 Evoral::Range<framepos_t> _range;
437
438                 static int const _divisions;
439         };
440         
441         TemporaryLayers compute_temporary_layers (RegionList const &);
442         void commit_temporary_layers (TemporaryLayers const &);
443
444         RegionList recursive_regions_touched (boost::shared_ptr<Region>, OverlapCache const &, boost::shared_ptr<Region>) const;
445         void recursive_regions_touched_sub (boost::shared_ptr<Region>, OverlapCache const &, boost::shared_ptr<Region>, RegionList &) const;
446
447         void timestamp_layer_op (LayerOp, boost::shared_ptr<Region>);
448         uint64_t layer_op_counter;
449
450         bool _relayer_suspended;
451 };
452
453 } /* namespace ARDOUR */
454
455 #endif  /* __ardour_playlist_h__ */
456
457