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