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