1 /*
2  * Copyright (C) 2006-2011 David Robillard <d@drobilla.net>
3  * Copyright (C) 2006-2017 Paul Davis <paul@linuxaudiosystems.com>
4  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
6  * Copyright (C) 2016-2017 Nick Mainsbridge <mainsbridge@gmail.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #ifndef __ardour_audio_region_h__
24 #define __ardour_audio_region_h__
25 
26 #include <vector>
27 #include <list>
28 
29 #include "pbd/fastlog.h"
30 #include "pbd/undo.h"
31 
32 #include "ardour/ardour.h"
33 #include "ardour/automatable.h"
34 #include "ardour/automation_list.h"
35 #include "ardour/interthread_info.h"
36 #include "ardour/logcurve.h"
37 #include "ardour/region.h"
38 
39 class XMLNode;
40 class AudioRegionReadTest;
41 class PlaylistReadTest;
42 
43 namespace ARDOUR {
44 
45 namespace Properties {
46 	LIBARDOUR_API extern PBD::PropertyDescriptor<bool> envelope_active;
47 	LIBARDOUR_API extern PBD::PropertyDescriptor<bool> default_fade_in;
48 	LIBARDOUR_API extern PBD::PropertyDescriptor<bool> default_fade_out;
49 	LIBARDOUR_API extern PBD::PropertyDescriptor<bool> fade_in_active;
50 	LIBARDOUR_API extern PBD::PropertyDescriptor<bool> fade_out_active;
51 	LIBARDOUR_API extern PBD::PropertyDescriptor<float> scale_amplitude;
52 	LIBARDOUR_API extern PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > fade_in;
53 	LIBARDOUR_API extern PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > inverse_fade_in;
54 	LIBARDOUR_API extern PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > fade_out;
55 	LIBARDOUR_API extern PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > inverse_fade_out;
56 	LIBARDOUR_API extern PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > envelope;
57 }
58 
59 class Playlist;
60 class Session;
61 class Filter;
62 class AudioSource;
63 
64 
65 class LIBARDOUR_API 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);
scale_amplitude()81 	gain_t scale_amplitude() const { return _scale_amplitude; }
82 
83 	void normalize (float, float target_in_dB = 0.0f);
84 
85 	/** @return the maximum (linear) amplitude of the region, or a -ve
86 	 *  number if the Progress object reports that the process was cancelled.
87 	 */
88 	double maximum_amplitude (Progress* p = 0) const;
89 
90 	/** @return the maximum (rms) signal power of the region, or a -1
91 	 *  if the Progress object reports that the process was cancelled.
92 	 */
93 	double rms (Progress* p = 0) const;
94 
envelope_active()95 	bool envelope_active () const { return _envelope_active; }
fade_in_active()96 	bool fade_in_active ()  const { return _fade_in_active; }
fade_out_active()97 	bool fade_out_active () const { return _fade_out_active; }
98 
fade_in()99 	boost::shared_ptr<AutomationList> fade_in()  { return _fade_in.val (); }
inverse_fade_in()100 	boost::shared_ptr<AutomationList> inverse_fade_in()  { return _inverse_fade_in.val (); }
fade_out()101 	boost::shared_ptr<AutomationList> fade_out() { return _fade_out.val (); }
inverse_fade_out()102 	boost::shared_ptr<AutomationList> inverse_fade_out()  { return _inverse_fade_out.val (); }
envelope()103 	boost::shared_ptr<AutomationList> envelope() { return _envelope.val (); }
104 
105 	Evoral::Range<samplepos_t> body_range () const;
106 
107 	virtual samplecnt_t read_peaks (PeakData *buf, samplecnt_t npeaks,
108 	                                samplecnt_t offset, samplecnt_t cnt,
109 	                                uint32_t chan_n=0, double samples_per_pixel = 1.0) const;
110 
111 	/* Readable interface */
112 
113 	virtual samplecnt_t read (Sample*, samplepos_t pos, samplecnt_t cnt, int channel) const;
readable_length()114 	virtual samplecnt_t readable_length() const { return length(); }
115 
116 	virtual samplecnt_t read_at (Sample *buf, Sample *mixdown_buf, float *gain_buf,
117 	                             samplepos_t position,
118 	                             samplecnt_t cnt,
119 	                             uint32_t   chan_n = 0) const;
120 
121 	virtual samplecnt_t master_read_at (Sample *buf, Sample *mixdown_buf, float *gain_buf,
122 	                                    samplepos_t position, samplecnt_t cnt,
123 	                                    uint32_t chan_n=0) const;
124 
125 	virtual samplecnt_t read_raw_internal (Sample*, samplepos_t, samplecnt_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 (samplepos_t, samplepos_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 (samplecnt_t);
139 	void set_fade_in (FadeShape, samplecnt_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 (samplecnt_t);
145 	void set_fade_out (FadeShape, samplecnt_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 	samplecnt_t verify_xfade_bounds (samplecnt_t, bool start);
152 
153 	void set_envelope_active (bool yn);
154 	void set_default_envelope ();
155 
156 	int separate_by_channel (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>
control(const Evoral::Parameter & id)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 (samplepos_t where);
178 	void remove_transient (samplepos_t where);
179 	void clear_transients ();
180 	void set_onsets (AnalysisFeatureList&);
181 	void get_transients (AnalysisFeatureList&);
182 	void update_transient (samplepos_t old_position, samplepos_t new_position);
183 
184 	AudioIntervalResult find_silence (Sample, samplecnt_t, samplecnt_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::MusicSample 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 	samplecnt_t read_from_sources (SourceList const &, samplecnt_t, Sample *, samplepos_t, samplecnt_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, samplepos_t, samplecnt_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__ */
263