Merged with trunk R1283.
[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     $Id$
19 */
20
21 #ifndef __ardour_overlap_h__
22 #define __ardour_overlap_h__
23
24 #include <vector>
25 #include <algorithm>
26 #include <boost/shared_ptr.hpp>
27
28 #include <sigc++/signal.h>
29
30 #include <pbd/undo.h>
31 #include <pbd/statefuldestructible.h> 
32
33 #include <ardour/ardour.h>
34 #include <ardour/curve.h>
35 #include <ardour/audioregion.h>
36 #include <ardour/crossfade_compare.h>
37
38 namespace ARDOUR {
39
40 class AudioRegion;
41 class Playlist;
42
43 class Crossfade : public PBD::StatefulDestructible
44 {
45   public:
46
47         class NoCrossfadeHere: std::exception {
48           public:
49                 virtual const char *what() const throw() { return "no crossfade should be constructed here"; }
50         };
51         
52         /* constructor for "fixed" xfades at each end of an internal overlap */
53
54         Crossfade (boost::shared_ptr<ARDOUR::AudioRegion> in, boost::shared_ptr<ARDOUR::AudioRegion> out,
55                    nframes_t position,
56                    nframes_t initial_length,
57                    AnchorPoint);
58
59         /* constructor for xfade between two regions that are overlapped in any way
60            except the "internal" case.
61         */
62         
63         Crossfade (boost::shared_ptr<ARDOUR::AudioRegion> in, boost::shared_ptr<ARDOUR::AudioRegion> out, CrossfadeModel, bool active);
64
65
66         /* copy constructor to copy a crossfade with new regions. used (for example)
67            when a playlist copy is made 
68         */
69         Crossfade (const Crossfade &, boost::shared_ptr<ARDOUR::AudioRegion>, boost::shared_ptr<ARDOUR::AudioRegion>);
70         
71         /* the usual XML constructor */
72
73         Crossfade (const Playlist&, XMLNode&);
74         virtual ~Crossfade();
75
76         bool operator== (const ARDOUR::Crossfade&);
77
78         XMLNode& get_state (void);
79         int set_state (const XMLNode&);
80
81         boost::shared_ptr<ARDOUR::AudioRegion> in() const { return _in; }
82         boost::shared_ptr<ARDOUR::AudioRegion> out() const { return _out; }
83         
84         nframes_t read_at (Sample *buf, Sample *mixdown_buffer, 
85                                 float *gain_buffer, nframes_t position, nframes_t cnt, 
86                                 uint32_t chan_n,
87                                 nframes_t read_frames = 0,
88                                 nframes_t skip_frames = 0);
89         
90         bool refresh ();
91
92         uint32_t upper_layer () const {
93                 return std::max (_in->layer(), _out->layer());
94         }
95
96         uint32_t lower_layer () const {
97                 return std::min (_in->layer(), _out->layer());
98         }
99
100         bool involves (boost::shared_ptr<ARDOUR::AudioRegion> region) const {
101                 return _in == region || _out == region;
102         }
103
104         bool involves (boost::shared_ptr<ARDOUR::AudioRegion> a, boost::shared_ptr<ARDOUR::AudioRegion> b) const {
105                 return (_in == a && _out == b) || (_in == b && _out == a);
106         }
107
108         nframes_t length() const { return _length; }
109         nframes_t overlap_length() const;
110         nframes_t position() const { return _position; }
111
112         void invalidate();
113
114         sigc::signal<void,Crossfade*> Invalidated;
115         sigc::signal<void,Change>     StateChanged;
116
117         bool covers (nframes_t frame) const {
118                 return _position <= frame && frame < _position + _length;
119         }
120
121         OverlapType coverage (nframes_t start, nframes_t end) const;
122
123         static void set_buffer_size (nframes_t);
124
125         bool active () const { return _active; }
126         void set_active (bool yn);
127
128         bool following_overlap() const { return _follow_overlap; }
129         bool can_follow_overlap() const;
130         void set_follow_overlap (bool yn);
131
132         Curve& fade_in() { return _fade_in; } 
133         Curve& fade_out() { return _fade_out; }
134
135         nframes_t set_length (nframes_t);
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         nframes_t       _length;
155         nframes_t       _position;
156         AnchorPoint          _anchor_point;
157         bool                 _follow_overlap;
158         bool                 _fixed;
159         Curve _fade_in;
160         Curve _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 (bool force);
168
169         void member_changed (ARDOUR::Change);
170 };
171
172
173 } // namespace ARDOUR
174
175 #endif /* __ardour_overlap_h__ */