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