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