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