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