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