c6cb65efc525d225d6a7e22e7213af6c5fc55ff3
[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 <sigc++/signal.h>
32 #include <pbd/atomic.h>
33 #include <pbd/undo.h>
34
35 #include <ardour/ardour.h>
36 #include <ardour/crossfade_compare.h>
37 #include <ardour/location.h>
38 #include <ardour/stateful.h>
39 #include <ardour/source.h>
40 #include <ardour/state_manager.h>
41
42 namespace ARDOUR  {
43
44 class Session;
45 class Region;
46
47 class Playlist : public Stateful, public StateManager {
48   public:
49         typedef list<Region*>    RegionList;
50
51         Playlist (Session&, const XMLNode&, bool hidden = false);
52         Playlist (Session&, string name, bool hidden = false);
53         Playlist (const Playlist&, string name, bool hidden = false);
54         Playlist (const Playlist&, jack_nframes_t start, jack_nframes_t cnt, string name, bool hidden = false);
55
56         virtual jack_nframes_t read (Sample *dst, Sample *mixdown, float *gain_buffer, char * workbuf, jack_nframes_t start, jack_nframes_t cnt, uint32_t chan_n=0) = 0;
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         /* Editing operations */
80
81         void add_region (const Region&, jack_nframes_t position, float times = 1, bool with_save = true);
82         void remove_region (Region *);
83         void replace_region (Region& old, Region& newr, jack_nframes_t pos);
84         void split_region (Region&, jack_nframes_t position);
85         void partition (jack_nframes_t start, jack_nframes_t end, bool just_top_level);
86         void duplicate (Region&, jack_nframes_t position, float times);
87         void nudge_after (jack_nframes_t start, jack_nframes_t distance, bool forwards);
88
89         Region* find_region (id_t) const;
90
91         Playlist* cut  (list<AudioRange>&, bool result_is_hidden = true);
92         Playlist* copy (list<AudioRange>&, bool result_is_hidden = true);
93         int       paste (Playlist&, jack_nframes_t position, float times);
94
95         uint32_t read_data_count() { return _read_data_count; }
96
97         RegionList* regions_at (jack_nframes_t frame);
98         RegionList* regions_touched (jack_nframes_t start, jack_nframes_t end);
99         Region*     top_region_at (jack_nframes_t frame);
100
101         Region*     find_next_region (jack_nframes_t frame, RegionPoint point, int dir);
102
103         template<class T> void foreach_region (T *t, void (T::*func)(Region *, void *), void *arg);
104         template<class T> void foreach_region (T *t, void (T::*func)(Region *));
105
106         XMLNode& get_state ();
107         int set_state (const XMLNode&);
108         XMLNode& get_template ();
109
110         sigc::signal<void,Region *> RegionAdded;
111         sigc::signal<void,Region *> RegionRemoved;
112
113         sigc::signal<void,Playlist*,bool> InUse;
114         sigc::signal<void>            Modified;
115         sigc::signal<void>            NameChanged;
116         sigc::signal<void>            LengthChanged;
117         sigc::signal<void>            LayeringChanged;
118         sigc::signal<void,Playlist *> GoingAway;
119         sigc::signal<void>            StatePushed;
120
121         static sigc::signal<void,Playlist*> PlaylistCreated;
122
123         static string bump_name (string old_name, Session&);
124         static string bump_name_once (string old_name);
125
126         void freeze ();
127         void thaw ();
128
129         void raise_region (Region&);
130         void lower_region (Region&);
131         void raise_region_to_top (Region&);
132         void lower_region_to_bottom (Region&);
133
134         uint32_t read_data_count() const { return _read_data_count; }
135
136         Session& session() { return _session; }
137
138         id_t get_orig_diskstream_id () const { return _orig_diskstream_id; }
139         void set_orig_diskstream_id (id_t did) { _orig_diskstream_id = did; }  
140
141         /* destructive editing */
142         
143         virtual bool destroy_region (Region *) = 0;
144
145   protected:
146         friend class Session;
147         virtual ~Playlist ();  /* members of the public use unref() */
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;
170         string          _name;
171         Session&        _session;
172         atomic_t         block_notifications;
173         atomic_t         ignore_state_changes;
174         mutable PBD::NonBlockingLock region_lock;
175         RegionList       pending_removals;
176         RegionList       pending_adds;
177         RegionList       pending_bounds;
178         bool             pending_modified;
179         bool             pending_length;
180         bool             save_on_thaw;
181         string           last_save_reason;
182         bool             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         id_t            _orig_diskstream_id;
194         uint64_t         layer_op_counter;
195         jack_nframes_t   freeze_length;
196
197         void init (bool hide);
198
199         bool holding_state () const { 
200                 return atomic_read (&block_notifications) != 0 ||
201                         atomic_read (&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 (Region *);
214         void notify_region_added (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, Region*);
223         virtual bool region_changed (Change, Region*);
224
225         void region_bounds_changed (Change, Region *);
226         void region_deleted (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 (Region *original, Region *left, Region *right) {}
238         
239         virtual void check_dependents (Region& region, bool norefresh) {}
240         virtual void refresh_dependents (Region& region) {}
241         virtual void remove_dependents (Region& region) {}
242
243         virtual XMLNode& state (bool);
244
245         /* override state_manager::save_state so we can check in_set_state() */
246
247         void save_state (std::string why);
248         void maybe_save_state (std::string why);
249
250         void add_region_internal (Region *, jack_nframes_t position, bool delay_sort = false);
251
252         int remove_region_internal (Region *, bool delay_sort = false);
253         RegionList *find_regions_at (jack_nframes_t frame);
254         void copy_regions (RegionList&) const;
255         void partition_internal (jack_nframes_t start, jack_nframes_t end, bool cutting, RegionList& thawlist);
256
257         jack_nframes_t _get_maximum_extent() const;
258
259         Playlist* cut_copy (Playlist* (Playlist::*pmf)(jack_nframes_t, jack_nframes_t, bool), 
260                             list<AudioRange>& ranges, bool result_is_hidden);
261         Playlist *cut (jack_nframes_t start, jack_nframes_t cnt, bool result_is_hidden);
262         Playlist *copy (jack_nframes_t start, jack_nframes_t cnt, bool result_is_hidden);
263
264
265         int move_region_to_layer (layer_t, Region& r, int dir);
266         void relayer ();
267
268         static Playlist* copyPlaylist (const Playlist&, jack_nframes_t start, jack_nframes_t length,
269                                        string name, bool result_is_hidden);
270         
271         void unset_freeze_parent (Playlist*);
272         void unset_freeze_child (Playlist*);
273
274         void timestamp_layer_op (Region&);
275 };
276
277 } /* namespace ARDOUR */
278
279 #endif  /* __ardour_playlist_h__ */
280
281