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