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