summaryrefslogtreecommitdiff
path: root/libs/ardour/ardour/crossfade.h
blob: ebafa0e8cc77ee6a062c0c7553588ecc267113e3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
    Copyright (C) 2000 Paul Davis 

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

    $Id$
*/

#ifndef __ardour_overlap_h__
#define __ardour_overlap_h__

#include <vector>
#include <algorithm>

#include <sigc++/signal.h>

#include <pbd/undo.h>

#include <ardour/ardour.h>
#include <ardour/curve.h>
#include <ardour/audioregion.h>
#include <ardour/state_manager.h>
#include <ardour/crossfade_compare.h>

namespace ARDOUR {

class AudioRegion;
class Playlist;

struct CrossfadeState : public StateManager::State {
    CrossfadeState (std::string reason) : StateManager::State (reason) {}

    UndoAction fade_in_memento;
    UndoAction fade_out_memento;
    jack_nframes_t position;
    jack_nframes_t length;
    AnchorPoint    anchor_point;
    bool           follow_overlap;
    bool           active;
};

class Crossfade : public Stateful, public StateManager
{
  public:

	class NoCrossfadeHere: std::exception {
	  public:
		virtual const char *what() const throw() { return "no crossfade should be constructed here"; }
	};
	
	/* constructor for "fixed" xfades at each end of an internal overlap */

	Crossfade (ARDOUR::AudioRegion& in, ARDOUR::AudioRegion& out,
		   jack_nframes_t position,
		   jack_nframes_t initial_length,
		   AnchorPoint);

	/* constructor for xfade between two regions that are overlapped in any way
	   except the "internal" case.
	*/
	
	Crossfade (ARDOUR::AudioRegion& in, ARDOUR::AudioRegion& out, CrossfadeModel, bool active);


	/* copy constructor to copy a crossfade with new regions. used (for example)
	   when a playlist copy is made */
	Crossfade (const Crossfade &, ARDOUR::AudioRegion *, ARDOUR::AudioRegion *);
	
	/* the usual XML constructor */

	Crossfade (const ARDOUR::Playlist&, XMLNode&);
	virtual ~Crossfade();

	bool operator== (const ARDOUR::Crossfade&);

	XMLNode& get_state (void);
	int set_state (const XMLNode&);

	ARDOUR::AudioRegion& in() const { return *_in; }
	ARDOUR::AudioRegion& out() const { return *_out; }
	
	jack_nframes_t read_at (Sample *buf, Sample *mixdown_buffer, 
				float *gain_buffer, char * workbuf, jack_nframes_t position, jack_nframes_t cnt, 
				uint32_t chan_n,
				jack_nframes_t read_frames = 0,
				jack_nframes_t skip_frames = 0);
	
	bool refresh ();

	uint32_t upper_layer () const {
		return std::max (_in->layer(), _out->layer());
	}

	uint32_t lower_layer () const {
		return std::min (_in->layer(), _out->layer());
	}

	bool involves (ARDOUR::AudioRegion& region) const {
		return _in == &region || _out == &region;
	}

	bool involves (ARDOUR::AudioRegion& a, ARDOUR::AudioRegion& b) const {
		return (_in == &a && _out == &b) || (_in == &b && _out == &a);
	}

	jack_nframes_t length() const { return _length; }
	jack_nframes_t overlap_length() const;
	jack_nframes_t position() const { return _position; }

	sigc::signal<void,Crossfade*> Invalidated;
	sigc::signal<void>            GoingAway;

	bool covers (jack_nframes_t frame) const {
		return _position <= frame && frame < _position + _length;
	}

	OverlapType coverage (jack_nframes_t start, jack_nframes_t end) const;

	UndoAction get_memento() const;	

	static void set_buffer_size (jack_nframes_t);

	bool active () const { return _active; }
	void set_active (bool yn);

	bool following_overlap() const { return _follow_overlap; }
	bool can_follow_overlap() const;
	void set_follow_overlap (bool yn);

	Curve& fade_in() { return _fade_in; } 
	Curve& fade_out() { return _fade_out; }

	jack_nframes_t set_length (jack_nframes_t);
	
	static jack_nframes_t short_xfade_length() { return _short_xfade_length; }
	static void set_short_xfade_length (jack_nframes_t n);

	static Change ActiveChanged;

  private:
	friend struct CrossfadeComparePtr;
	friend class AudioPlaylist;

	static jack_nframes_t _short_xfade_length;

	ARDOUR::AudioRegion* _in;
	ARDOUR::AudioRegion* _out;
	bool                 _active;
	bool                 _in_update;
	OverlapType           overlap_type;
	jack_nframes_t       _length;
	jack_nframes_t       _position;
	AnchorPoint          _anchor_point;
	bool                 _follow_overlap;
	bool                 _fixed;
	Curve _fade_in;
	Curve _fade_out;

	static Sample* crossfade_buffer_out;
	static Sample* crossfade_buffer_in;

	void initialize (bool savestate=true);
	int  compute (ARDOUR::AudioRegion&, ARDOUR::AudioRegion&, CrossfadeModel);
	bool update (bool force);

	StateManager::State* state_factory (std::string why) const;
	Change restore_state (StateManager::State&);

	void member_changed (ARDOUR::Change);

};


} // namespace ARDOUR

#endif /* __ardour_overlap_h__ */