Fix broken whitespace. I'd apologize for the compile times if it was my fault :D
[ardour.git] / libs / ardour / ardour / crossfade.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 */
19
20 #ifndef __ardour_overlap_h__
21 #define __ardour_overlap_h__
22
23 #include <vector>
24 #include <algorithm>
25 #include <boost/shared_ptr.hpp>
26
27
28 #include "pbd/undo.h"
29 #include "pbd/statefuldestructible.h"
30
31 #include "ardour/ardour.h"
32 #include "ardour/audioregion.h"
33 #include "evoral/Curve.hpp"
34
35 namespace ARDOUR {
36         namespace Properties {
37                 /* "active" is defined elsewhere but we use it with crossfade also */
38                 extern PBD::PropertyDescriptor<bool> active;
39                 extern PBD::PropertyDescriptor<bool> follow_overlap;
40         }
41
42 enum AnchorPoint {
43         StartOfIn,
44         EndOfIn,
45         EndOfOut
46 };
47
48 class Playlist;
49
50 class Crossfade : public ARDOUR::AudioRegion
51 {
52   public:
53
54         class NoCrossfadeHere: std::exception {
55         public:
56                 virtual const char *what() const throw() { return "no crossfade should be constructed here"; }
57         };
58
59         /* constructor for "fixed" xfades at each end of an internal overlap */
60
61         Crossfade (boost::shared_ptr<ARDOUR::AudioRegion> in, boost::shared_ptr<ARDOUR::AudioRegion> out,
62                    framepos_t position,
63                    framecnt_t initial_length,
64                    AnchorPoint);
65
66         /* constructor for xfade between two regions that are overlapped in any way
67            except the "internal" case.
68         */
69
70         Crossfade (boost::shared_ptr<ARDOUR::AudioRegion> in, boost::shared_ptr<ARDOUR::AudioRegion> out, CrossfadeModel, bool active);
71
72
73         /* copy constructor to copy a crossfade with new regions. used (for example)
74            when a playlist copy is made
75         */
76         Crossfade (boost::shared_ptr<Crossfade>, boost::shared_ptr<ARDOUR::AudioRegion>, boost::shared_ptr<ARDOUR::AudioRegion>);
77
78         /* the usual XML constructor */
79
80         Crossfade (const Playlist&, XMLNode const &);
81         virtual ~Crossfade();
82
83         static void make_property_quarks ();
84
85         XMLNode& get_state (void);
86         int set_state (const XMLNode&, int version);
87
88         boost::shared_ptr<ARDOUR::AudioRegion> in() const { return _in; }
89         boost::shared_ptr<ARDOUR::AudioRegion> out() const { return _out; }
90
91         framecnt_t read_at (Sample *buf, Sample *mixdown_buffer,
92                             float *gain_buffer, framepos_t position, framecnt_t cnt,
93                             uint32_t chan_n,
94                             framecnt_t read_frames = 0,
95                             framecnt_t skip_frames = 0) const;
96
97         bool refresh ();
98
99         uint32_t upper_layer () const {
100                 return std::max (_in->layer(), _out->layer());
101         }
102
103         uint32_t lower_layer () const {
104                 return std::min (_in->layer(), _out->layer());
105         }
106
107         bool involves (boost::shared_ptr<ARDOUR::AudioRegion> region) const {
108                 return _in == region || _out == region;
109         }
110
111         bool involves (boost::shared_ptr<ARDOUR::AudioRegion> a, boost::shared_ptr<ARDOUR::AudioRegion> b) const {
112                 return (_in == a && _out == b) || (_in == b && _out == a);
113         }
114
115         framecnt_t overlap_length() const;
116
117         PBD::Signal1<void,boost::shared_ptr<Region> > Invalidated;
118
119         bool covers (framepos_t frame) const {
120                 return _position <= frame && frame < _position + _length;
121         }
122
123         OverlapType coverage (framepos_t start, framepos_t end) const;
124
125         static void set_buffer_size (framecnt_t);
126
127         bool active () const { return _active; }
128         void set_active (bool yn);
129
130         bool following_overlap() const { return _follow_overlap; }
131         bool can_follow_overlap() const;
132         void set_follow_overlap (bool yn);
133
134         AutomationList& fade_in() { return _fade_in; }
135         AutomationList& fade_out() { return _fade_out; }
136
137         framecnt_t set_xfade_length (framecnt_t);
138
139         bool is_dependent() const { return true; }
140         bool depends_on (boost::shared_ptr<Region> other) const {
141                 return other == _in || other == _out;
142         }
143
144         static framecnt_t short_xfade_length() { return _short_xfade_length; }
145         static void set_short_xfade_length (framecnt_t n);
146
147         /** emitted when the actual fade curves change, as opposed to one of the Stateful properties */
148         PBD::Signal0<void> FadesChanged;
149
150   private:
151         friend struct CrossfadeComparePtr;
152         friend class AudioPlaylist;
153
154         static framecnt_t _short_xfade_length;
155
156         boost::shared_ptr<ARDOUR::AudioRegion> _in;
157         boost::shared_ptr<ARDOUR::AudioRegion> _out;
158         PBD::Property<bool>  _active;
159         PBD::Property<bool>  _follow_overlap;
160         bool                 _in_update;
161         OverlapType           overlap_type;
162         AnchorPoint          _anchor_point;
163         bool                 _fixed;
164         int32_t               layer_relation;
165
166
167         mutable AutomationList _fade_in;
168         mutable AutomationList _fade_out;
169
170         static Sample* crossfade_buffer_out;
171         static Sample* crossfade_buffer_in;
172
173         void initialize ();
174         int  compute (boost::shared_ptr<ARDOUR::AudioRegion>, boost::shared_ptr<ARDOUR::AudioRegion>, CrossfadeModel);
175         bool update ();
176
177         bool operator== (const ARDOUR::Crossfade&);
178
179   protected:
180         framecnt_t read_raw_internal (Sample*, framepos_t, framecnt_t, int) const;
181 };
182
183
184 } // namespace ARDOUR
185
186 #endif /* __ardour_overlap_h__ */