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