e3c5db561dd7e936106e9e58a525a8396a9d2bbc
[ardour.git] / libs / ardour / ardour / audioregion.h
1 /*
2     Copyright (C) 2000-2006 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_audio_region_h__
22 #define __ardour_audio_region_h__
23
24 #include <vector>
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 speed_mismatch (float) const;
59
60         boost::shared_ptr<AudioSource> audio_source (uint32_t n=0) const;
61
62         void   set_scale_amplitude (gain_t);
63         gain_t scale_amplitude() const { return _scale_amplitude; }
64         
65         void normalize_to (float target_in_dB = 0.0f);
66
67         bool envelope_active () const { return _flags & Region::EnvelopeActive; }
68         bool fade_in_active ()  const { return _flags & Region::FadeIn; }
69         bool fade_out_active () const { return _flags & Region::FadeOut; }
70
71         Curve& fade_in()  { return _fade_in; }
72         Curve& fade_out() { return _fade_out; }
73         Curve& envelope() { return _envelope; }
74
75         nframes_t read_peaks (PeakData *buf, nframes_t npeaks,
76                         nframes_t offset, nframes_t cnt,
77                         uint32_t chan_n=0, double samples_per_unit= 1.0) const;
78
79         virtual nframes_t read_at (Sample *buf, Sample *mixdown_buf,
80                         float *gain_buf, nframes_t position, nframes_t cnt, 
81                         uint32_t       chan_n      = 0,
82                         nframes_t read_frames = 0,
83                         nframes_t skip_frames = 0) const;
84
85         nframes_t master_read_at (Sample *buf, Sample *mixdown_buf, 
86                         float *gain_buf,
87                         nframes_t position, nframes_t cnt, uint32_t chan_n=0) const;
88
89         XMLNode& state (bool);
90         int      set_state (const XMLNode&);
91
92         static void set_default_fade (float steepness, nframes_t len);
93
94         enum FadeShape {
95                 Linear,
96                 Fast,
97                 Slow,
98                 LogA,
99                 LogB
100         };
101
102         void set_fade_in_active (bool yn);
103         void set_fade_in_shape (FadeShape);
104         void set_fade_in_length (nframes_t);
105         void set_fade_in (FadeShape, nframes_t);
106
107         void set_fade_out_active (bool yn);
108         void set_fade_out_shape (FadeShape);
109         void set_fade_out_length (nframes_t);
110         void set_fade_out (FadeShape, nframes_t);
111
112         void set_envelope_active (bool yn);
113         void set_default_envelope ();
114
115         int separate_by_channel (ARDOUR::Session&, vector<boost::shared_ptr<AudioRegion> >&) const;
116
117         /* filter */
118
119         int apply (AudioFilter&);
120
121         /* export */
122
123         int exportme (ARDOUR::Session&, ARDOUR::AudioExportSpecification&);
124
125         /* xfade/fade interactions */
126
127         void suspend_fade_in ();
128         void suspend_fade_out ();
129         void resume_fade_in ();
130         void resume_fade_out ();
131
132   private:
133         friend class RegionFactory;
134
135         AudioRegion (boost::shared_ptr<AudioSource>, nframes_t start, nframes_t length);
136         AudioRegion (boost::shared_ptr<AudioSource>, nframes_t start, nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags);
137         AudioRegion (SourceList &, nframes_t start, nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags);
138         AudioRegion (boost::shared_ptr<const AudioRegion>, nframes_t start, nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags);
139         AudioRegion (boost::shared_ptr<const AudioRegion>);
140         AudioRegion (boost::shared_ptr<AudioSource>, const XMLNode&);
141         AudioRegion (SourceList &, const XMLNode&);
142
143   private:
144         void set_default_fades ();
145         void set_default_fade_in ();
146         void set_default_fade_out ();
147
148         void recompute_gain_at_end ();
149         void recompute_gain_at_start ();
150
151         nframes_t _read_at (const SourceList&, Sample *buf, Sample *mixdown_buffer, 
152                                  float *gain_buffer, nframes_t position, nframes_t cnt, 
153                                  uint32_t chan_n = 0,
154                                  nframes_t read_frames = 0,
155                                  nframes_t skip_frames = 0) const;
156
157         void recompute_at_start ();
158         void recompute_at_end ();
159
160         void envelope_changed ();
161         void fade_in_changed ();
162         void fade_out_changed ();
163         void source_offset_changed ();
164         void listen_to_my_curves ();
165
166         mutable Curve     _fade_in;
167         FadeShape         _fade_in_shape;
168         mutable Curve     _fade_out;
169         FadeShape         _fade_out_shape;
170         mutable Curve     _envelope;
171         gain_t            _scale_amplitude;
172         uint32_t          _fade_in_disabled;
173         uint32_t          _fade_out_disabled;
174
175   protected:
176         int set_live_state (const XMLNode&, Change&, bool send);
177         
178         virtual bool verify_start (jack_nframes_t);
179         virtual bool verify_start_and_length (jack_nframes_t, jack_nframes_t);
180         virtual bool verify_start_mutable (jack_nframes_t&_start);
181         virtual bool verify_length (jack_nframes_t);
182         /*virtual void recompute_at_start () = 0;
183         virtual void recompute_at_end () = 0;*/
184 };
185
186 } /* namespace ARDOUR */
187
188 /* access from C objects */
189
190 extern "C" {
191         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);
192         uint32_t region_length_from_c (void *arg);
193         uint32_t sourcefile_length_from_c (void *arg, double);
194 }
195
196 #endif /* __ardour_audio_region_h__ */