new ancestral data handling for regions; new Stretch AudioFilter replaces session...
[ardour.git] / libs / ardour / ardour / audioregion.h
1 /*
2     Copyright (C) 2000-2001 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_audio_region_h__
21 #define __ardour_audio_region_h__
22
23 #include <vector>
24
25 #include <pbd/fastlog.h>
26 #include <pbd/undo.h>
27
28 #include <ardour/ardour.h>
29 #include <ardour/region.h>
30 #include <ardour/gain.h>
31 #include <ardour/logcurve.h>
32 #include <ardour/export.h>
33
34 class XMLNode;
35
36 namespace ARDOUR {
37
38 class Route;
39 class Playlist;
40 class Session;
41 class AudioFilter;
42 class AudioSource;
43
44 class AudioRegion : public Region
45 {
46   public:
47         static Change FadeInChanged;
48         static Change FadeOutChanged;
49         static Change FadeInActiveChanged;
50         static Change FadeOutActiveChanged;
51         static Change EnvelopeActiveChanged;
52         static Change ScaleAmplitudeChanged;
53         static Change EnvelopeChanged;
54
55         ~AudioRegion();
56
57         bool source_equivalent (boost::shared_ptr<const Region>) const;
58
59         bool speed_mismatch (float) const;
60
61         boost::shared_ptr<AudioSource> source (uint32_t n=0) const { if (n < sources.size()) return sources[n]; else return sources[0]; } 
62
63         void set_scale_amplitude (gain_t);
64         gain_t scale_amplitude() const { return _scale_amplitude; }
65         
66         void normalize_to (float target_in_dB = 0.0f);
67
68         uint32_t n_channels() const { return sources.size(); }
69         vector<string> master_source_names();
70         void set_master_sources (SourceList&);
71
72         bool envelope_active () const { return _flags & Region::EnvelopeActive; }
73         bool fade_in_active ()  const { return _flags & Region::FadeIn; }
74         bool fade_out_active () const { return _flags & Region::FadeOut; }
75         bool captured() const { return !(_flags & (Region::Flag (Region::Import|Region::External))); }
76
77         Curve& fade_in()  { return _fade_in; }
78         Curve& fade_out() { return _fade_out; }
79         Curve& envelope() { return _envelope; }
80
81         nframes_t read_peaks (PeakData *buf, nframes_t npeaks,
82                         nframes_t offset, nframes_t cnt,
83                         uint32_t chan_n=0, double samples_per_unit= 1.0) const;
84
85         virtual nframes_t read_at (Sample *buf, Sample *mixdown_buf,
86                         float *gain_buf, nframes_t position, nframes_t cnt, 
87                         uint32_t       chan_n      = 0,
88                         nframes_t read_frames = 0,
89                         nframes_t skip_frames = 0) const;
90
91         nframes_t master_read_at (Sample *buf, Sample *mixdown_buf, 
92                         float *gain_buf,
93                         nframes_t position, nframes_t cnt, uint32_t chan_n=0) const;
94
95         XMLNode& state (bool);
96         int      set_state (const XMLNode&);
97
98         static void set_default_fade (float steepness, nframes_t len);
99         bool fade_in_is_default () const;
100         bool fade_out_is_default () const;
101
102         enum FadeShape {
103                 Linear,
104                 Fast,
105                 Slow,
106                 LogA,
107                 LogB
108         };
109
110         void set_fade_in_active (bool yn);
111         void set_fade_in_shape (FadeShape);
112         void set_fade_in_length (nframes_t);
113         void set_fade_in (FadeShape, nframes_t);
114
115         void set_fade_out_active (bool yn);
116         void set_fade_out_shape (FadeShape);
117         void set_fade_out_length (nframes_t);
118         void set_fade_out (FadeShape, nframes_t);
119
120         void set_envelope_active (bool yn);
121         void set_default_envelope ();
122
123         int separate_by_channel (ARDOUR::Session&, vector<boost::shared_ptr<AudioRegion> >&) const;
124
125         /* filter */
126
127         int apply (AudioFilter&);
128
129         /* export */
130
131         int exportme (ARDOUR::Session&, ARDOUR::AudioExportSpecification&);
132
133         boost::shared_ptr<Region> get_parent() const;
134
135         /* xfade/fade interactions */
136
137         void suspend_fade_in ();
138         void suspend_fade_out ();
139         void resume_fade_in ();
140         void resume_fade_out ();
141
142         void set_playlist (boost::weak_ptr<Playlist>);
143
144   private:
145         friend class RegionFactory;
146
147         AudioRegion (boost::shared_ptr<AudioSource>, nframes_t start, nframes_t length);
148         AudioRegion (boost::shared_ptr<AudioSource>, nframes_t start, nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags);
149         AudioRegion (SourceList &, nframes_t start, nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags);
150         AudioRegion (boost::shared_ptr<const AudioRegion>, nframes_t start, nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags);
151         AudioRegion (boost::shared_ptr<const AudioRegion>);
152         AudioRegion (boost::shared_ptr<AudioSource>, const XMLNode&);
153         AudioRegion (SourceList &, const XMLNode&);
154
155   private:
156         void set_default_fades ();
157         void set_default_fade_in ();
158         void set_default_fade_out ();
159
160         void recompute_gain_at_end ();
161         void recompute_gain_at_start ();
162
163         nframes_t _read_at (const SourceList&, Sample *buf, Sample *mixdown_buffer, 
164                                  float *gain_buffer, nframes_t position, nframes_t cnt, 
165                                  uint32_t chan_n = 0,
166                                  nframes_t read_frames = 0,
167                                  nframes_t skip_frames = 0) const;
168
169         bool verify_start (nframes_t position);
170         bool verify_length (nframes_t position);
171         bool verify_start_mutable (nframes_t& start);
172         bool verify_start_and_length (nframes_t start, nframes_t length);
173         void recompute_at_start ();
174         void recompute_at_end ();
175
176         void envelope_changed ();
177         void fade_in_changed ();
178         void fade_out_changed ();
179         void source_offset_changed ();
180         void listen_to_my_curves ();
181
182         void source_deleted ();
183         
184         SourceList        sources;
185         
186         /** Used when timefx are applied, so we can always use the original source. */
187         SourceList        master_sources; 
188
189         mutable Curve     _fade_in;
190         FadeShape         _fade_in_shape;
191         mutable Curve     _fade_out;
192         FadeShape         _fade_out_shape;
193         mutable Curve     _envelope;
194         gain_t            _scale_amplitude;
195         uint32_t          _fade_in_disabled;
196         uint32_t          _fade_out_disabled;
197
198   protected:
199         int set_live_state (const XMLNode&, Change&, bool send);
200 };
201
202 } /* namespace ARDOUR */
203
204 /* access from C objects */
205
206 extern "C" {
207         int    region_read_peaks_from_c   (void *arg, uint32_t npeaks, uint32_t start, uint32_t length, intptr_t data, uint32_t n_chan, double samples_per_unit);
208         uint32_t region_length_from_c (void *arg);
209         uint32_t sourcefile_length_from_c (void *arg, double);
210 }
211
212 #endif /* __ardour_audio_region_h__ */