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