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