Merge big changes (mostly Controllable) from trunk
[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 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;
58         virtual void clear (bool with_delete = false, bool with_save = true);
59         virtual void dump () const;
60         virtual UndoAction get_memento() const = 0;
61
62         void ref();
63         void unref();
64         uint32_t refcnt() const { return _refcnt; }
65
66         const string& name() const { return _name; }
67         void set_name (const string& str);
68
69         bool frozen() const { return _frozen; }
70         void set_frozen (bool yn);
71
72         bool hidden() const { return _hidden; }
73         bool empty() const;
74         jack_nframes_t get_maximum_extent () const;
75         layer_t top_layer() const;
76
77         EditMode get_edit_mode() const { return _edit_mode; }
78         void set_edit_mode (EditMode);
79
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 replace_region (Region& old, Region& newr, jack_nframes_t pos);
85         void split_region (Region&, jack_nframes_t position);
86         void partition (jack_nframes_t start, jack_nframes_t end, bool just_top_level);
87         void duplicate (Region&, jack_nframes_t position, float times);
88         void nudge_after (jack_nframes_t start, jack_nframes_t distance, bool forwards);
89
90         Region* find_region (const PBD::ID&) const;
91
92         Playlist* cut  (list<AudioRange>&, bool result_is_hidden = true);
93         Playlist* copy (list<AudioRange>&, bool result_is_hidden = true);
94         int       paste (Playlist&, jack_nframes_t position, float times);
95
96         uint32_t read_data_count() { return _read_data_count; }
97
98         RegionList* regions_at (jack_nframes_t frame);
99         RegionList* regions_touched (jack_nframes_t start, jack_nframes_t end);
100         Region*     top_region_at (jack_nframes_t frame);
101
102         Region*     find_next_region (jack_nframes_t frame, RegionPoint point, int dir);
103
104         template<class T> void foreach_region (T *t, void (T::*func)(Region *, void *), void *arg);
105         template<class T> void foreach_region (T *t, void (T::*func)(Region *));
106
107         XMLNode& get_state ();
108         int set_state (const XMLNode&);
109         XMLNode& get_template ();
110
111         sigc::signal<void,Region *> RegionAdded;
112         sigc::signal<void,Region *> RegionRemoved;
113
114         sigc::signal<void,Playlist*,bool> InUse;
115         sigc::signal<void>            Modified;
116         sigc::signal<void>            NameChanged;
117         sigc::signal<void>            LengthChanged;
118         sigc::signal<void>            LayeringChanged;
119         sigc::signal<void,Playlist *> GoingAway;
120         sigc::signal<void>            StatePushed;
121
122         static sigc::signal<void,Playlist*> PlaylistCreated;
123
124         static string bump_name (string old_name, Session&);
125         static string bump_name_once (string old_name);
126
127         void freeze ();
128         void thaw ();
129
130         void raise_region (Region&);
131         void lower_region (Region&);
132         void raise_region_to_top (Region&);
133         void lower_region_to_bottom (Region&);
134
135         uint32_t read_data_count() const { return _read_data_count; }
136
137         Session& session() { return _session; }
138
139         const PBD::ID& get_orig_diskstream_id () const { return _orig_diskstream_id; }
140         void set_orig_diskstream_id (const PBD::ID& did) { _orig_diskstream_id = did; }  
141
142         /* destructive editing */
143         
144         virtual bool destroy_region (Region *) = 0;
145
146   protected:
147         friend class Session;
148         virtual ~Playlist ();  /* members of the public use unref() */
149
150   protected:
151         struct RegionLock {
152             RegionLock (Playlist *pl, bool do_block_notify = true) : playlist (pl), block_notify (do_block_notify) {
153                     playlist->region_lock.lock();
154                     if (block_notify) {
155                             playlist->delay_notifications();
156                     }
157             }
158             ~RegionLock() { 
159                     playlist->region_lock.unlock();
160                     if (block_notify) {
161                             playlist->release_notifications ();
162                     }
163             }
164             Playlist *playlist;
165             bool block_notify;
166         };
167
168         friend class RegionLock;
169
170         RegionList       regions;
171         string          _name;
172         Session&        _session;
173         mutable gint    block_notifications;
174         mutable gint    ignore_state_changes;
175         mutable Glib::Mutex region_lock;
176         RegionList       pending_removals;
177         RegionList       pending_adds;
178         RegionList       pending_bounds;
179         bool             pending_modified;
180         bool             pending_length;
181         bool             save_on_thaw;
182         string           last_save_reason;
183         bool             in_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         jack_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 (Region *);
215         void notify_region_added (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, Region*);
224         virtual bool region_changed (Change, Region*);
225
226         void region_bounds_changed (Change, Region *);
227         void region_deleted (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 (Region *original, Region *left, Region *right) {}
239         
240         virtual void check_dependents (Region& region, bool norefresh) {}
241         virtual void refresh_dependents (Region& region) {}
242         virtual void remove_dependents (Region& region) {}
243
244         virtual XMLNode& state (bool);
245
246         /* override state_manager::save_state so we can check in_set_state() */
247
248         void save_state (std::string why);
249         void maybe_save_state (std::string why);
250
251         void add_region_internal (Region *, jack_nframes_t position, bool delay_sort = false);
252
253         int remove_region_internal (Region *, bool delay_sort = false);
254         RegionList *find_regions_at (jack_nframes_t frame);
255         void copy_regions (RegionList&) const;
256         void partition_internal (jack_nframes_t start, jack_nframes_t end, bool cutting, RegionList& thawlist);
257
258         jack_nframes_t _get_maximum_extent() const;
259
260         Playlist* cut_copy (Playlist* (Playlist::*pmf)(jack_nframes_t, jack_nframes_t, bool), 
261                             list<AudioRange>& ranges, bool result_is_hidden);
262         Playlist *cut (jack_nframes_t start, jack_nframes_t cnt, bool result_is_hidden);
263         Playlist *copy (jack_nframes_t start, jack_nframes_t cnt, bool result_is_hidden);
264
265
266         int move_region_to_layer (layer_t, Region& r, int dir);
267         void relayer ();
268
269         static Playlist* copyPlaylist (const Playlist&, jack_nframes_t start, jack_nframes_t length,
270                                        string name, bool result_is_hidden);
271         
272         void unset_freeze_parent (Playlist*);
273         void unset_freeze_child (Playlist*);
274
275         void timestamp_layer_op (Region&);
276 };
277
278 } /* namespace ARDOUR */
279
280 #endif  /* __ardour_playlist_h__ */
281
282