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