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