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