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