Merged with trunk R920.
[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/state_manager.h>
44 #include <ardour/data_type.h>
45
46 namespace ARDOUR  {
47
48 class Session;
49 class Region;
50
51 class Playlist : public StateManager, public PBD::StatefulDestructible {
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 (const Playlist&, string name, bool hidden = false);
58         Playlist (const Playlist&, jack_nframes_t start, jack_nframes_t cnt, string name, bool hidden = false);
59
60         virtual void clear (bool with_save = true);
61         virtual void dump () const;
62         virtual UndoAction get_memento() const = 0;
63
64         void ref();
65         void unref();
66         uint32_t refcnt() const { return _refcnt; }
67
68         const string& name() const { return _name; }
69         void set_name (const string& str);
70
71         const DataType& data_type() const { return _type; }
72
73         bool frozen() const { return _frozen; }
74         void set_frozen (bool yn);
75
76         bool hidden() const { return _hidden; }
77         bool empty() const;
78         jack_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>, jack_nframes_t position, float times = 1, bool with_save = true);
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, jack_nframes_t pos);
91         void split_region (boost::shared_ptr<Region>, jack_nframes_t position);
92         void partition (jack_nframes_t start, jack_nframes_t end, bool just_top_level);
93         void duplicate (boost::shared_ptr<Region>, jack_nframes_t position, float times);
94         void nudge_after (jack_nframes_t start, jack_nframes_t distance, bool forwards);
95
96         boost::shared_ptr<Region> find_region (const PBD::ID&) const;
97
98         Playlist* cut  (list<AudioRange>&, bool result_is_hidden = true);
99         Playlist* copy (list<AudioRange>&, bool result_is_hidden = true);
100         int       paste (Playlist&, jack_nframes_t position, float times);
101
102         uint32_t read_data_count() { return _read_data_count; }
103
104         RegionList* regions_at (jack_nframes_t frame);
105         RegionList* regions_touched (jack_nframes_t start, jack_nframes_t end);
106         boost::shared_ptr<Region>  top_region_at (jack_nframes_t frame);
107
108         boost::shared_ptr<Region>     find_next_region (jack_nframes_t frame, RegionPoint point, int dir);
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,boost::shared_ptr<Region> > RegionAdded;
118         sigc::signal<void,boost::shared_ptr<Region> > RegionRemoved;
119         sigc::signal<void,Playlist*,bool> InUse;
120         sigc::signal<void>                Modified;
121         sigc::signal<void>                NameChanged;
122         sigc::signal<void>                LengthChanged;
123         sigc::signal<void>                LayeringChanged;
124         sigc::signal<void>                StatePushed;
125
126         static sigc::signal<void,Playlist*> PlaylistCreated;
127
128         static string bump_name (string old_name, Session&);
129         static string bump_name_once (string old_name);
130
131         void freeze ();
132         void thaw ();
133
134         void raise_region (boost::shared_ptr<Region>);
135         void lower_region (boost::shared_ptr<Region>);
136         void raise_region_to_top (boost::shared_ptr<Region>);
137         void lower_region_to_bottom (boost::shared_ptr<Region>);
138
139         uint32_t read_data_count() const { return _read_data_count; }
140
141         Session& session() { return _session; }
142
143         const PBD::ID& get_orig_diskstream_id () const { return _orig_diskstream_id; }
144         void set_orig_diskstream_id (const PBD::ID& did) { _orig_diskstream_id = did; }  
145
146         /* destructive editing */
147         
148         virtual bool destroy_region (boost::shared_ptr<Region>) = 0;
149
150   protected:
151         friend class Session;
152         virtual ~Playlist ();  /* members of the public use unref() */
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;
175         string          _name;
176         Session&        _session;
177         DataType        _type;
178         mutable gint    block_notifications;
179         mutable gint    ignore_state_changes;
180         mutable Glib::Mutex region_lock;
181         RegionList       pending_removals;
182         RegionList       pending_adds;
183         RegionList       pending_bounds;
184         bool             pending_modified;
185         bool             pending_length;
186         bool             save_on_thaw;
187         string           last_save_reason;
188         bool             in_set_state;
189         bool            _hidden;
190         bool            _splicing;
191         bool            _nudging;
192         uint32_t        _refcnt;
193         EditMode        _edit_mode;
194         bool             in_flush;
195         bool             in_partition;
196         bool            _frozen;
197         uint32_t         subcnt;
198         uint32_t        _read_data_count;
199         PBD::ID         _orig_diskstream_id;
200         uint64_t         layer_op_counter;
201         jack_nframes_t   freeze_length;
202
203         void init (bool hide);
204
205         bool holding_state () const { 
206                 return g_atomic_int_get (&block_notifications) != 0 ||
207                         g_atomic_int_get (&ignore_state_changes) != 0;
208         }
209
210         /* prevent the compiler from ever generating these */
211
212         Playlist (const Playlist&);
213         Playlist (Playlist&);
214
215         void delay_notifications ();
216         void release_notifications ();
217         virtual void flush_notifications ();
218
219         void notify_region_removed (boost::shared_ptr<Region>);
220         void notify_region_added (boost::shared_ptr<Region>);
221         void notify_length_changed ();
222         void notify_layering_changed ();
223         void notify_modified ();
224         void notify_state_changed (Change);
225
226         void mark_session_dirty();
227
228         void region_changed_proxy (Change, boost::shared_ptr<Region>);
229         virtual bool region_changed (Change, boost::shared_ptr<Region>);
230
231         void region_bounds_changed (Change, boost::shared_ptr<Region>);
232         void region_deleted (boost::shared_ptr<Region>);
233
234         void sort_regions ();
235
236         void possibly_splice ();
237         void possibly_splice_unlocked();
238         void core_splice ();
239         void splice_locked ();
240         void splice_unlocked ();
241
242
243         virtual void finalize_split_region (boost::shared_ptr<Region> original, boost::shared_ptr<Region> left, boost::shared_ptr<Region> right) {}
244         
245         virtual void check_dependents (boost::shared_ptr<Region> region, bool norefresh) {}
246         virtual void refresh_dependents (boost::shared_ptr<Region> region) {}
247         virtual void remove_dependents (boost::shared_ptr<Region> region) {}
248
249         virtual XMLNode& state (bool);
250
251         /* override state_manager::save_state so we can check in_set_state() */
252
253         void save_state (std::string why);
254         void maybe_save_state (std::string why);
255
256         void add_region_internal (boost::shared_ptr<Region>, jack_nframes_t position, bool delay_sort = false);
257
258         int remove_region_internal (boost::shared_ptr<Region>, bool delay_sort = false);
259         RegionList *find_regions_at (jack_nframes_t frame);
260         void copy_regions (RegionList&) const;
261         void partition_internal (jack_nframes_t start, jack_nframes_t end, bool cutting, RegionList& thawlist);
262
263         jack_nframes_t _get_maximum_extent() const;
264
265         Playlist* cut_copy (Playlist* (Playlist::*pmf)(jack_nframes_t, jack_nframes_t, bool), 
266                             list<AudioRange>& ranges, bool result_is_hidden);
267         Playlist *cut (jack_nframes_t start, jack_nframes_t cnt, bool result_is_hidden);
268         Playlist *copy (jack_nframes_t start, jack_nframes_t cnt, bool result_is_hidden);
269
270
271         int move_region_to_layer (layer_t, boost::shared_ptr<Region> r, int dir);
272         void relayer ();
273
274         static Playlist* copyPlaylist (const Playlist&, jack_nframes_t start, jack_nframes_t length,
275                                        string name, bool result_is_hidden);
276         
277         void unset_freeze_parent (Playlist*);
278         void unset_freeze_child (Playlist*);
279
280         void timestamp_layer_op (boost::shared_ptr<Region>);
281 };
282
283 } /* namespace ARDOUR */
284
285 #endif  /* __ardour_playlist_h__ */
286
287