Clarify behaviour of ::read (overwrites its buffer) wrt ::read_at (mixes into its...
[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 AudioRegionTest;
38 class PlaylistReadTest;
39
40 namespace ARDOUR {
41
42 namespace Properties {
43         extern PBD::PropertyDescriptor<bool> envelope_active;
44         extern PBD::PropertyDescriptor<bool> default_fade_in;
45         extern PBD::PropertyDescriptor<bool> default_fade_out;
46         extern PBD::PropertyDescriptor<bool> fade_in_active;
47         extern PBD::PropertyDescriptor<bool> fade_out_active;
48         extern PBD::PropertyDescriptor<float> scale_amplitude;
49
50         /* the envelope and fades are not scalar items and so
51            currently (2010/02) are not stored using Property.
52            However, these descriptors enable us to notify
53            about changes to them via PropertyChange.
54         */
55
56         extern PBD::PropertyDescriptor<bool> envelope;
57         extern PBD::PropertyDescriptor<bool> fade_in;
58         extern PBD::PropertyDescriptor<bool> fade_out;
59 }
60
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 (Progress* p = 0) 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         bool fade_in_is_xfade() const { return _fade_in_is_xfade; }
93         void set_fade_in_is_xfade (bool yn);
94         bool fade_out_is_xfade() const { return _fade_out_is_xfade; }
95         void set_fade_out_is_xfade (bool yn);
96
97         boost::shared_ptr<AutomationList> fade_in()  { return _fade_in; }
98         boost::shared_ptr<AutomationList> fade_out() { return _fade_out; }
99         boost::shared_ptr<AutomationList> envelope() { return _envelope; }
100
101         Evoral::Range<framepos_t> body_range () const;
102
103         virtual framecnt_t read_peaks (PeakData *buf, framecnt_t npeaks,
104                         framecnt_t offset, framecnt_t cnt,
105                         uint32_t chan_n=0, double samples_per_unit= 1.0) const;
106
107         /* Readable interface */
108
109         virtual framecnt_t read (Sample*, framepos_t pos, framecnt_t cnt, int channel) const;
110         virtual framecnt_t readable_length() const { return length(); }
111
112         virtual framecnt_t read_at (Sample *buf, Sample *mixdown_buf, float *gain_buf,
113                                     framepos_t position,
114                                     framecnt_t cnt,
115                                     uint32_t   chan_n = 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 ();
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         void set_fade_in_active (bool yn);
130         void set_fade_in_shape (FadeShape);
131         void set_fade_in_length (framecnt_t);
132         void set_fade_in (FadeShape, framecnt_t);
133         void set_fade_in (boost::shared_ptr<AutomationList>);
134
135         void set_fade_out_active (bool yn);
136         void set_fade_out_shape (FadeShape);
137         void set_fade_out_length (framecnt_t);
138         void set_fade_out (FadeShape, framecnt_t);
139         void set_fade_out (boost::shared_ptr<AutomationList>);
140
141         void set_default_fade_in ();
142         void set_default_fade_out ();
143         
144         void set_envelope_active (bool yn);
145         void set_default_envelope ();
146
147         int separate_by_channel (ARDOUR::Session&, std::vector<boost::shared_ptr<Region> >&) const;
148
149         /* automation */
150
151         boost::shared_ptr<Evoral::Control>
152         control(const Evoral::Parameter& id, bool create=false) {
153                 return _automatable.control(id, create);
154         }
155
156         virtual boost::shared_ptr<const Evoral::Control>
157         control(const Evoral::Parameter& id) const {
158                 return _automatable.control(id);
159         }
160
161         /* xfade/fade interactions */
162
163         void suspend_fade_in ();
164         void suspend_fade_out ();
165         void resume_fade_in ();
166         void resume_fade_out ();
167
168         void add_transient (framepos_t where);
169         void remove_transient (framepos_t where);
170         int set_transients (AnalysisFeatureList&);
171         int get_transients (AnalysisFeatureList&, bool force_new = false);
172         int update_transient (framepos_t old_position, framepos_t new_position);
173         int adjust_transients (frameoffset_t delta);
174
175         AudioIntervalResult find_silence (Sample, framecnt_t, InterThreadInfo&) const;
176
177   private:
178         friend class RegionFactory;
179         friend class Crossfade;
180
181         AudioRegion (boost::shared_ptr<AudioSource>);
182         AudioRegion (const SourceList &);
183         AudioRegion (boost::shared_ptr<const AudioRegion>);
184         AudioRegion (boost::shared_ptr<const AudioRegion>, frameoffset_t offset);
185         AudioRegion (boost::shared_ptr<const AudioRegion>, const SourceList&);
186         AudioRegion (SourceList &);
187
188   private:
189         friend class ::AudioRegionTest;
190         friend class ::PlaylistReadTest;
191         
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         /** linear gain to apply to the whole region */
198         PBD::Property<gain_t>   _scale_amplitude;
199
200         void register_properties ();
201         void post_set (const PBD::PropertyChange&);
202
203         void init ();
204         void set_default_fades ();
205
206         void recompute_gain_at_end ();
207         void recompute_gain_at_start ();
208
209         framecnt_t read_from_sources (SourceList const &, framecnt_t, Sample *, framepos_t, framecnt_t, uint32_t) const;
210
211         void recompute_at_start ();
212         void recompute_at_end ();
213
214         void envelope_changed ();
215         void fade_in_changed ();
216         void fade_out_changed ();
217         void source_offset_changed ();
218         void listen_to_my_curves ();
219         void connect_to_analysis_changed ();
220         void connect_to_header_position_offset_changed ();
221
222         Automatable _automatable;
223
224         boost::shared_ptr<AutomationList> _fade_in;
225         boost::shared_ptr<AutomationList> _inverse_fade_in;
226         boost::shared_ptr<AutomationList> _fade_out;
227         boost::shared_ptr<AutomationList> _inverse_fade_out;
228         boost::shared_ptr<AutomationList> _envelope;
229         uint32_t                          _fade_in_suspended;
230         uint32_t                          _fade_out_suspended;
231         /* This is not a Property because its not subject to user control,
232            or undo/redo. XXX this may prove to be a mistake.
233         */
234         bool                              _fade_in_is_xfade;
235         bool                              _fade_out_is_xfade;
236
237   protected:
238         /* default constructor for derived (compound) types */
239
240         AudioRegion (Session& s, framepos_t, framecnt_t, std::string name);
241
242         int _set_state (const XMLNode&, int version, PBD::PropertyChange& what_changed, bool send_signal);
243 };
244
245 } /* namespace ARDOUR */
246
247 /* access from C objects */
248
249 extern "C" {
250         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);
251         uint32_t region_length_from_c (void *arg);
252         uint32_t sourcefile_length_from_c (void *arg, double);
253 }
254
255 #endif /* __ardour_audio_region_h__ */