redesign VCA control over gain (and theoretically other scalar controls)
[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/interthread_info.h"
33 #include "ardour/logcurve.h"
34 #include "ardour/region.h"
35
36 class XMLNode;
37 class AudioRegionReadTest;
38 class PlaylistReadTest;
39
40 namespace ARDOUR {
41
42 namespace Properties {
43         LIBARDOUR_API extern PBD::PropertyDescriptor<bool> envelope_active;
44         LIBARDOUR_API extern PBD::PropertyDescriptor<bool> default_fade_in;
45         LIBARDOUR_API extern PBD::PropertyDescriptor<bool> default_fade_out;
46         LIBARDOUR_API extern PBD::PropertyDescriptor<bool> fade_in_active;
47         LIBARDOUR_API extern PBD::PropertyDescriptor<bool> fade_out_active;
48         LIBARDOUR_API extern PBD::PropertyDescriptor<float> scale_amplitude;
49         LIBARDOUR_API extern PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > fade_in;
50         LIBARDOUR_API extern PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > inverse_fade_in;
51         LIBARDOUR_API extern PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > fade_out;
52         LIBARDOUR_API extern PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > inverse_fade_out;
53         LIBARDOUR_API extern PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > envelope;
54 }
55
56 class Playlist;
57 class Session;
58 class Filter;
59 class AudioSource;
60
61
62 class LIBARDOUR_API AudioRegion : public Region
63 {
64   public:
65         static void make_property_quarks ();
66
67         ~AudioRegion();
68
69         void copy_settings (boost::shared_ptr<const AudioRegion>);
70
71         bool source_equivalent (boost::shared_ptr<const Region>) const;
72
73         bool speed_mismatch (float) const;
74
75         boost::shared_ptr<AudioSource> audio_source (uint32_t n=0) const;
76
77     // if several audio files associated with a region,
78     // information about file with MAX channel count will be provided
79     uint32_t get_related_audio_file_channel_count () const;
80
81         void   set_scale_amplitude (gain_t);
82         gain_t scale_amplitude() const { return _scale_amplitude; }
83
84         void normalize (float, float target_in_dB = 0.0f);
85
86         /** @return the maximum (linear) amplitude of the region, or a -ve
87          *  number if the Progress object reports that the process was cancelled.
88          */
89         double maximum_amplitude (Progress* p = 0) const;
90
91         /** @return the maximum (rms) signal power of the region, or a -1
92          *  if the Progress object reports that the process was cancelled.
93          */
94         double rms (Progress* p = 0) const;
95
96         bool envelope_active () const { return _envelope_active; }
97         bool fade_in_active ()  const { return _fade_in_active; }
98         bool fade_out_active () const { return _fade_out_active; }
99
100         boost::shared_ptr<AutomationList> fade_in()  { return _fade_in.val (); }
101         boost::shared_ptr<AutomationList> inverse_fade_in()  { return _inverse_fade_in.val (); }
102         boost::shared_ptr<AutomationList> fade_out() { return _fade_out.val (); }
103         boost::shared_ptr<AutomationList> inverse_fade_out()  { return _inverse_fade_out.val (); }
104         boost::shared_ptr<AutomationList> envelope() { return _envelope.val (); }
105
106         Evoral::Range<framepos_t> body_range () const;
107
108         virtual framecnt_t read_peaks (PeakData *buf, framecnt_t npeaks,
109                         framecnt_t offset, framecnt_t cnt,
110                         uint32_t chan_n=0, double frames_per_pixel = 1.0) const;
111
112         /* Readable interface */
113
114         virtual framecnt_t read (Sample*, framepos_t pos, framecnt_t cnt, int channel) const;
115         virtual framecnt_t readable_length() const { return length(); }
116
117         virtual framecnt_t read_at (Sample *buf, Sample *mixdown_buf, float *gain_buf,
118                                     framepos_t position,
119                                     framecnt_t cnt,
120                                     uint32_t   chan_n = 0) const;
121
122         virtual framecnt_t master_read_at (Sample *buf, Sample *mixdown_buf, float *gain_buf,
123                                            framepos_t position, framecnt_t cnt, uint32_t chan_n=0) const;
124
125         virtual framecnt_t read_raw_internal (Sample*, framepos_t, framecnt_t, int channel) const;
126
127         XMLNode& state ();
128         XMLNode& get_basic_state ();
129         int set_state (const XMLNode&, int version);
130
131         void fade_range (framepos_t, framepos_t);
132
133         bool fade_in_is_default () const;
134         bool fade_out_is_default () const;
135
136         void set_fade_in_active (bool yn);
137         void set_fade_in_shape (FadeShape);
138         void set_fade_in_length (framecnt_t);
139         void set_fade_in (FadeShape, framecnt_t);
140         void set_fade_in (boost::shared_ptr<AutomationList>);
141
142         void set_fade_out_active (bool yn);
143         void set_fade_out_shape (FadeShape);
144         void set_fade_out_length (framecnt_t);
145         void set_fade_out (FadeShape, framecnt_t);
146         void set_fade_out (boost::shared_ptr<AutomationList>);
147
148         void set_default_fade_in ();
149         void set_default_fade_out ();
150
151         framecnt_t verify_xfade_bounds (framecnt_t, bool start);
152
153         void set_envelope_active (bool yn);
154         void set_default_envelope ();
155
156         int separate_by_channel (ARDOUR::Session&, std::vector<boost::shared_ptr<Region> >&) const;
157
158         /* automation */
159
160         boost::shared_ptr<Evoral::Control>
161         control(const Evoral::Parameter& id, bool create=false) {
162                 return _automatable.control(id, create);
163         }
164
165         virtual boost::shared_ptr<const Evoral::Control>
166         control(const Evoral::Parameter& id) const {
167                 return _automatable.control(id);
168         }
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         void add_transient (framepos_t where);
178         void remove_transient (framepos_t where);
179         void clear_transients ();
180         void set_onsets (AnalysisFeatureList&);
181         void get_transients (AnalysisFeatureList&);
182         void update_transient (framepos_t old_position, framepos_t new_position);
183
184         AudioIntervalResult find_silence (Sample, framecnt_t, framecnt_t, InterThreadInfo&) const;
185
186   private:
187         friend class RegionFactory;
188
189         AudioRegion (boost::shared_ptr<AudioSource>);
190         AudioRegion (const SourceList &);
191         AudioRegion (boost::shared_ptr<const AudioRegion>);
192         AudioRegion (boost::shared_ptr<const AudioRegion>, ARDOUR::MusicFrame offset);
193         AudioRegion (boost::shared_ptr<const AudioRegion>, const SourceList&);
194         AudioRegion (SourceList &);
195
196   private:
197         friend class ::AudioRegionReadTest;
198         friend class ::PlaylistReadTest;
199
200         void build_transients ();
201
202         PBD::Property<bool>     _envelope_active;
203         PBD::Property<bool>     _default_fade_in;
204         PBD::Property<bool>     _default_fade_out;
205         PBD::Property<bool>     _fade_in_active;
206         PBD::Property<bool>     _fade_out_active;
207         /** linear gain to apply to the whole region */
208         PBD::Property<gain_t>   _scale_amplitude;
209
210         void register_properties ();
211         void post_set (const PBD::PropertyChange&);
212
213         void init ();
214         void set_default_fades ();
215
216         void recompute_gain_at_end ();
217         void recompute_gain_at_start ();
218
219         framecnt_t read_from_sources (SourceList const &, framecnt_t, Sample *, framepos_t, framecnt_t, uint32_t) const;
220
221         void recompute_at_start ();
222         void recompute_at_end ();
223
224         void envelope_changed ();
225         void fade_in_changed ();
226         void fade_out_changed ();
227         void source_offset_changed ();
228         void listen_to_my_curves ();
229         void connect_to_analysis_changed ();
230         void connect_to_header_position_offset_changed ();
231
232
233         AutomationListProperty _fade_in;
234         AutomationListProperty _inverse_fade_in;
235         AutomationListProperty _fade_out;
236         AutomationListProperty _inverse_fade_out;
237         AutomationListProperty _envelope;
238         Automatable            _automatable;
239         uint32_t               _fade_in_suspended;
240         uint32_t               _fade_out_suspended;
241
242         boost::shared_ptr<ARDOUR::Region> get_single_other_xfade_region (bool start) const;
243
244   protected:
245         /* default constructor for derived (compound) types */
246
247         AudioRegion (Session& s, framepos_t, framecnt_t, std::string name);
248
249         int _set_state (const XMLNode&, int version, PBD::PropertyChange& what_changed, bool send_signal);
250 };
251
252 } /* namespace ARDOUR */
253
254 /* access from C objects */
255
256 extern "C" {
257         LIBARDOUR_API 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);
258         LIBARDOUR_API uint32_t region_length_from_c (void *arg);
259         LIBARDOUR_API uint32_t sourcefile_length_from_c (void *arg, double);
260 }
261
262 #endif /* __ardour_audio_region_h__ */