b8cde44b77b877ab737cda25e7bd514cd5bb26a6
[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 #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
34 class XMLNode;
35
36 namespace ARDOUR {
37
38 using std::vector;
39
40 class Route;
41 class Playlist;
42 class Session;
43 class Filter;
44 class AudioSource;
45
46 class AudioRegion : public Region
47 {
48   public:
49         static Change FadeInChanged;
50         static Change FadeOutChanged;
51         static Change FadeInActiveChanged;
52         static Change FadeOutActiveChanged;
53         static Change EnvelopeActiveChanged;
54         static Change ScaleAmplitudeChanged;
55         static Change EnvelopeChanged;
56
57         ~AudioRegion();
58
59         bool speed_mismatch (float) const;
60
61         boost::shared_ptr<AudioSource> audio_source (uint32_t n=0) const;
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         bool envelope_active () const { return _flags & Region::EnvelopeActive; }
69         bool fade_in_active ()  const { return _flags & Region::FadeIn; }
70         bool fade_out_active () const { return _flags & Region::FadeOut; }
71
72         boost::shared_ptr<AutomationList> fade_in()  { return _fade_in; }
73         boost::shared_ptr<AutomationList> fade_out() { return _fade_out; }
74         boost::shared_ptr<AutomationList> envelope() { return _envelope; }
75
76         virtual nframes_t read_peaks (PeakData *buf, nframes_t npeaks,
77                                       nframes_t offset, nframes_t cnt,
78                                       uint32_t chan_n=0, double samples_per_unit= 1.0) const;
79         
80         /* Readable interface */
81         
82         virtual nframes64_t read (Sample*, nframes64_t pos, nframes64_t cnt, int channel) const;
83         virtual nframes64_t readable_length() const { return length(); }
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         virtual 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         virtual nframes_t read_raw_internal (Sample*, nframes_t, nframes_t) const;
96
97         XMLNode& state (bool);
98         int      set_state (const XMLNode&);
99
100         static void set_default_fade (float steepness, nframes_t len);
101         bool fade_in_is_default () const;
102         bool fade_out_is_default () const;
103
104         enum FadeShape {
105                 Linear,
106                 Fast,
107                 Slow,
108                 LogA,
109                 LogB
110         };
111
112         void set_fade_in_active (bool yn);
113         void set_fade_in_shape (FadeShape);
114         void set_fade_in_length (nframes_t);
115         void set_fade_in (FadeShape, nframes_t);
116
117         void set_fade_out_active (bool yn);
118         void set_fade_out_shape (FadeShape);
119         void set_fade_out_length (nframes_t);
120         void set_fade_out (FadeShape, nframes_t);
121
122         void set_envelope_active (bool yn);
123         void set_default_envelope ();
124
125         int separate_by_channel (ARDOUR::Session&, vector<boost::shared_ptr<AudioRegion> >&) const;
126         
127         /* automation */
128         
129         boost::shared_ptr<Evoral::Control>
130         control(const Evoral::Parameter& id, bool create=false) {
131                 return _automatable.data().control(id, create);
132         }
133
134         virtual boost::shared_ptr<const Evoral::Control>
135         control(const Evoral::Parameter& id) const {
136                 return _automatable.data().control(id);
137         }
138
139         /* export */
140
141         int exportme (ARDOUR::Session&, ARDOUR::ExportSpecification&);
142
143         /* xfade/fade interactions */
144
145         void suspend_fade_in ();
146         void suspend_fade_out ();
147         void resume_fade_in ();
148         void resume_fade_out ();
149
150         int get_transients (AnalysisFeatureList&, bool force_new = false);
151
152   private:
153         friend class RegionFactory;
154
155         AudioRegion (boost::shared_ptr<AudioSource>, nframes_t start, nframes_t length);
156         AudioRegion (boost::shared_ptr<AudioSource>, nframes_t start, nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags);
157         AudioRegion (const SourceList &, nframes_t start, nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags);
158         AudioRegion (boost::shared_ptr<const AudioRegion>, nframes_t start, nframes_t length, const string& name, layer_t = 0, Region::Flag flags = Region::DefaultFlags);
159         AudioRegion (boost::shared_ptr<AudioSource>, const XMLNode&);
160         AudioRegion (SourceList &, const XMLNode&);
161
162   private:
163         void init ();
164         void set_default_fades ();
165         void set_default_fade_in ();
166         void set_default_fade_out ();
167
168         void recompute_gain_at_end ();
169         void recompute_gain_at_start ();
170
171         nframes_t _read_at (const SourceList&, nframes_t limit,
172                             Sample *buf, Sample *mixdown_buffer, 
173                             float *gain_buffer, nframes_t position, nframes_t cnt, 
174                             uint32_t chan_n = 0,
175                             nframes_t read_frames = 0,
176                             nframes_t skip_frames = 0,
177                             bool raw = false) const;
178
179         void recompute_at_start ();
180         void recompute_at_end ();
181
182         void envelope_changed ();
183         void fade_in_changed ();
184         void fade_out_changed ();
185         void source_offset_changed ();
186         void listen_to_my_curves ();
187         void listen_to_my_sources ();
188
189         AutomatableControls _automatable;
190
191         boost::shared_ptr<AutomationList> _fade_in;
192         FadeShape                         _fade_in_shape;
193         boost::shared_ptr<AutomationList> _fade_out;
194         FadeShape                         _fade_out_shape;
195         boost::shared_ptr<AutomationList> _envelope;
196         gain_t                            _scale_amplitude;
197         uint32_t                          _fade_in_disabled;
198         uint32_t                          _fade_out_disabled;
199
200   protected:
201         /* default constructor for derived (compound) types */
202
203         AudioRegion (Session& s, nframes_t, nframes_t, std::string name); 
204         AudioRegion (boost::shared_ptr<const AudioRegion>);
205
206         int set_live_state (const XMLNode&, Change&, bool send);
207 };
208
209 } /* namespace ARDOUR */
210
211 /* access from C objects */
212
213 extern "C" {
214         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);
215         uint32_t region_length_from_c (void *arg);
216         uint32_t sourcefile_length_from_c (void *arg, double);
217 }
218
219 #endif /* __ardour_audio_region_h__ */