Trim declaration.
[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.h"
31 #include "ardour/automation_list.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         /* the envelope and fades are not scalar items and so
49            currently (2010/02) are not stored using Property.
50            However, these descriptors enable us to notify
51            about changes to them via PropertyChange.
52         */
53
54         extern PBD::PropertyDescriptor<bool> envelope;
55         extern PBD::PropertyDescriptor<bool> fade_in;
56         extern PBD::PropertyDescriptor<bool> fade_out;
57 }
58
59 class Playlist;
60 class Session;
61 class Filter;
62 class AudioSource;
63
64
65 class AudioRegion : public Region
66 {
67   public:
68         static void make_property_quarks ();
69
70         ~AudioRegion();
71
72         void copy_settings (boost::shared_ptr<const AudioRegion>);
73
74         bool source_equivalent (boost::shared_ptr<const Region>) const;
75
76         bool speed_mismatch (float) const;
77
78         boost::shared_ptr<AudioSource> audio_source (uint32_t n=0) const;
79
80         void   set_scale_amplitude (gain_t);
81         gain_t scale_amplitude() const { return _scale_amplitude; }
82
83         void normalize (float, float target_in_dB = 0.0f);
84         double maximum_amplitude (Progress* p = 0) const;
85
86         bool envelope_active () const { return _envelope_active; }
87         bool fade_in_active ()  const { return _fade_in_active; }
88         bool fade_out_active () const { return _fade_out_active; }
89
90         boost::shared_ptr<AutomationList> fade_in()  { return _fade_in; }
91         boost::shared_ptr<AutomationList> fade_out() { return _fade_out; }
92         boost::shared_ptr<AutomationList> envelope() { return _envelope; }
93
94         virtual framecnt_t read_peaks (PeakData *buf, framecnt_t npeaks,
95                         framecnt_t offset, framecnt_t cnt,
96                         uint32_t chan_n=0, double samples_per_unit= 1.0) const;
97
98         /* Readable interface */
99
100         enum ReadOps {
101                 ReadOpsNone = 0x0,
102                 ReadOpsOwnAutomation = 0x1,
103                 ReadOpsOwnScaling = 0x2,
104                 ReadOpsFades = 0x4
105         };
106
107         virtual framecnt_t read (Sample*, framepos_t pos, framecnt_t cnt, int channel) 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) const;
114
115         virtual framecnt_t master_read_at (Sample *buf, Sample *mixdown_buf, float *gain_buf,
116                                            framepos_t position, framecnt_t cnt, uint32_t chan_n=0) const;
117
118         virtual framecnt_t read_raw_internal (Sample*, framepos_t, framecnt_t, int channel) const;
119
120         XMLNode& state ();
121         int set_state (const XMLNode&, int version);
122
123         static void set_default_fade (float steepness, framecnt_t len);
124         bool fade_in_is_default () const;
125         bool fade_out_is_default () const;
126
127         void set_fade_in_active (bool yn);
128         void set_fade_in_shape (FadeShape);
129         void set_fade_in_length (framecnt_t);
130         void set_fade_in (FadeShape, framecnt_t);
131         void set_fade_in (boost::shared_ptr<AutomationList>);
132
133         void set_fade_out_active (bool yn);
134         void set_fade_out_shape (FadeShape);
135         void set_fade_out_length (framecnt_t);
136         void set_fade_out (FadeShape, framecnt_t);
137         void set_fade_out (boost::shared_ptr<AutomationList>);
138
139         void set_envelope_active (bool yn);
140         void set_default_envelope ();
141
142         int separate_by_channel (ARDOUR::Session&, std::vector<boost::shared_ptr<Region> >&) const;
143
144         /* automation */
145
146         boost::shared_ptr<Evoral::Control>
147         control(const Evoral::Parameter& id, bool create=false) {
148                 return _automatable.control(id, create);
149         }
150
151         virtual boost::shared_ptr<const Evoral::Control>
152         control(const Evoral::Parameter& id) const {
153                 return _automatable.control(id);
154         }
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         void add_transient (framepos_t where);
164         void remove_transient (framepos_t where);
165         int set_transients (AnalysisFeatureList&);
166         int get_transients (AnalysisFeatureList&, bool force_new = false);
167         int update_transient (framepos_t old_position, framepos_t new_position);
168         int adjust_transients (framepos_t delta);
169
170         AudioIntervalResult find_silence (Sample, framecnt_t, InterThreadInfo&) const;
171
172   private:
173         friend class RegionFactory;
174         friend class Crossfade;
175
176         AudioRegion (boost::shared_ptr<AudioSource>);
177         AudioRegion (const SourceList &);
178         AudioRegion (boost::shared_ptr<const AudioRegion>);
179         AudioRegion (boost::shared_ptr<const AudioRegion>, frameoffset_t offset);
180         AudioRegion (boost::shared_ptr<const AudioRegion>, const SourceList&);
181         AudioRegion (SourceList &);
182
183   private:
184         PBD::Property<bool>     _envelope_active;
185         PBD::Property<bool>     _default_fade_in;
186         PBD::Property<bool>     _default_fade_out;
187         PBD::Property<bool>     _fade_in_active;
188         PBD::Property<bool>     _fade_out_active;
189         /** linear gain to apply to the whole region */
190         PBD::Property<gain_t>   _scale_amplitude;
191
192         void register_properties ();
193         void post_set (const PBD::PropertyChange&);
194
195         void init ();
196         void set_default_fades ();
197         void set_default_fade_in ();
198         void set_default_fade_out ();
199
200         void recompute_gain_at_end ();
201         void recompute_gain_at_start ();
202
203         framecnt_t _read_at (const SourceList&, framecnt_t limit,
204                              Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
205                              framepos_t position, framecnt_t cnt,
206                              uint32_t chan_n = 0,
207                              ReadOps readops = ReadOps (~0)) const;
208
209         void recompute_at_start ();
210         void recompute_at_end ();
211
212         void envelope_changed ();
213         void fade_in_changed ();
214         void fade_out_changed ();
215         void source_offset_changed ();
216         void listen_to_my_curves ();
217         void connect_to_analysis_changed ();
218         void connect_to_header_position_offset_changed ();
219
220         Automatable _automatable;
221
222         boost::shared_ptr<AutomationList> _fade_in;
223         boost::shared_ptr<AutomationList> _fade_out;
224         boost::shared_ptr<AutomationList> _envelope;
225         uint32_t                          _fade_in_suspended;
226         uint32_t                          _fade_out_suspended;
227
228   protected:
229         /* default constructor for derived (compound) types */
230
231         AudioRegion (Session& s, framepos_t, framecnt_t, std::string name);
232
233         int _set_state (const XMLNode&, int version, PBD::PropertyChange& what_changed, bool send_signal);
234 };
235
236 } /* namespace ARDOUR */
237
238 /* access from C objects */
239
240 extern "C" {
241         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);
242         uint32_t region_length_from_c (void *arg);
243         uint32_t sourcefile_length_from_c (void *arg, double);
244 }
245
246 #endif /* __ardour_audio_region_h__ */