2 Copyright (C) 2000-2006 Paul Davis
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.
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.
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.
27 #include <boost/scoped_array.hpp>
28 #include <boost/shared_ptr.hpp>
30 #include <glibmm/threads.h>
32 #include "pbd/basename.h"
33 #include "pbd/xml++.h"
34 #include "pbd/stacktrace.h"
35 #include "pbd/enumwriter.h"
36 #include "pbd/convert.h"
38 #include "evoral/Curve.hpp"
40 #include "ardour/audioregion.h"
41 #include "ardour/session.h"
42 #include "ardour/dB.h"
43 #include "ardour/debug.h"
44 #include "ardour/event_type_map.h"
45 #include "ardour/playlist.h"
46 #include "ardour/audiofilesource.h"
47 #include "ardour/region_factory.h"
48 #include "ardour/runtime_functions.h"
49 #include "ardour/transient_detector.h"
50 #include "ardour/parameter_descriptor.h"
51 #include "ardour/progress.h"
53 #include "ardour/sndfilesource.h"
55 #include "ardour/coreaudiosource.h"
56 #endif // HAVE_COREAUDIO
62 using namespace ARDOUR;
66 namespace Properties {
67 PBD::PropertyDescriptor<bool> envelope_active;
68 PBD::PropertyDescriptor<bool> default_fade_in;
69 PBD::PropertyDescriptor<bool> default_fade_out;
70 PBD::PropertyDescriptor<bool> fade_in_active;
71 PBD::PropertyDescriptor<bool> fade_out_active;
72 PBD::PropertyDescriptor<float> scale_amplitude;
73 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > fade_in;
74 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > inverse_fade_in;
75 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > fade_out;
76 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > inverse_fade_out;
77 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > envelope;
81 /* Curve manipulations */
84 reverse_curve (boost::shared_ptr<Evoral::ControlList> dst, boost::shared_ptr<const Evoral::ControlList> src)
86 size_t len = src->back()->when;
87 for (Evoral::ControlList::const_reverse_iterator it = src->rbegin(); it!=src->rend(); it++) {
88 dst->fast_simple_add (len - (*it)->when, (*it)->value);
93 generate_inverse_power_curve (boost::shared_ptr<Evoral::ControlList> dst, boost::shared_ptr<const Evoral::ControlList> src)
95 // calc inverse curve using sum of squares
96 for (Evoral::ControlList::const_iterator it = src->begin(); it!=src->end(); ++it ) {
97 float value = (*it)->value;
98 value = 1 - powf(value,2);
100 dst->fast_simple_add ( (*it)->when, value );
105 generate_db_fade (boost::shared_ptr<Evoral::ControlList> dst, double len, int num_steps, float dB_drop)
108 dst->fast_simple_add (0, 1);
110 //generate a fade-out curve by successively applying a gain drop
111 float fade_speed = dB_to_coefficient(dB_drop / (float) num_steps);
112 for (int i = 1; i < (num_steps-1); i++) {
113 float coeff = GAIN_COEFF_UNITY;
114 for (int j = 0; j < i; j++) {
117 dst->fast_simple_add (len*(double)i/(double)num_steps, coeff);
120 dst->fast_simple_add (len, GAIN_COEFF_SMALL);
124 merge_curves (boost::shared_ptr<Evoral::ControlList> dst,
125 boost::shared_ptr<const Evoral::ControlList> curve1,
126 boost::shared_ptr<const Evoral::ControlList> curve2)
128 Evoral::ControlList::EventList::size_type size = curve1->size();
130 //curve lengths must match for now
131 if (size != curve2->size()) {
135 Evoral::ControlList::const_iterator c1 = curve1->begin();
137 for (Evoral::ControlList::const_iterator c2 = curve2->begin(); c2!=curve2->end(); c2++ ) {
138 float v1 = accurate_coefficient_to_dB((*c1)->value);
139 float v2 = accurate_coefficient_to_dB((*c2)->value);
141 double interp = v1 * ( 1.0-( (double)count / (double)size) );
142 interp += v2 * ( (double)count / (double)size );
144 interp = dB_to_coefficient(interp);
145 dst->fast_simple_add ( (*c1)->when, interp );
152 AudioRegion::make_property_quarks ()
154 Properties::envelope_active.property_id = g_quark_from_static_string (X_("envelope-active"));
155 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for envelope-active = %1\n", Properties::envelope_active.property_id));
156 Properties::default_fade_in.property_id = g_quark_from_static_string (X_("default-fade-in"));
157 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for default-fade-in = %1\n", Properties::default_fade_in.property_id));
158 Properties::default_fade_out.property_id = g_quark_from_static_string (X_("default-fade-out"));
159 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for default-fade-out = %1\n", Properties::default_fade_out.property_id));
160 Properties::fade_in_active.property_id = g_quark_from_static_string (X_("fade-in-active"));
161 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fade-in-active = %1\n", Properties::fade_in_active.property_id));
162 Properties::fade_out_active.property_id = g_quark_from_static_string (X_("fade-out-active"));
163 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fade-out-active = %1\n", Properties::fade_out_active.property_id));
164 Properties::scale_amplitude.property_id = g_quark_from_static_string (X_("scale-amplitude"));
165 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for scale-amplitude = %1\n", Properties::scale_amplitude.property_id));
166 Properties::fade_in.property_id = g_quark_from_static_string (X_("FadeIn"));
167 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for FadeIn = %1\n", Properties::fade_in.property_id));
168 Properties::inverse_fade_in.property_id = g_quark_from_static_string (X_("InverseFadeIn"));
169 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for InverseFadeIn = %1\n", Properties::inverse_fade_in.property_id));
170 Properties::fade_out.property_id = g_quark_from_static_string (X_("FadeOut"));
171 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for FadeOut = %1\n", Properties::fade_out.property_id));
172 Properties::inverse_fade_out.property_id = g_quark_from_static_string (X_("InverseFadeOut"));
173 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for InverseFadeOut = %1\n", Properties::inverse_fade_out.property_id));
174 Properties::envelope.property_id = g_quark_from_static_string (X_("Envelope"));
175 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for Envelope = %1\n", Properties::envelope.property_id));
179 AudioRegion::register_properties ()
181 /* no need to register parent class properties */
183 add_property (_envelope_active);
184 add_property (_default_fade_in);
185 add_property (_default_fade_out);
186 add_property (_fade_in_active);
187 add_property (_fade_out_active);
188 add_property (_scale_amplitude);
189 add_property (_fade_in);
190 add_property (_inverse_fade_in);
191 add_property (_fade_out);
192 add_property (_inverse_fade_out);
193 add_property (_envelope);
196 #define AUDIOREGION_STATE_DEFAULT \
197 _envelope_active (Properties::envelope_active, false) \
198 , _default_fade_in (Properties::default_fade_in, true) \
199 , _default_fade_out (Properties::default_fade_out, true) \
200 , _fade_in_active (Properties::fade_in_active, true) \
201 , _fade_out_active (Properties::fade_out_active, true) \
202 , _scale_amplitude (Properties::scale_amplitude, 1.0) \
203 , _fade_in (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation)))) \
204 , _inverse_fade_in (Properties::inverse_fade_in, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation)))) \
205 , _fade_out (Properties::fade_out, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeOutAutomation)))) \
206 , _inverse_fade_out (Properties::inverse_fade_out, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeOutAutomation))))
208 #define AUDIOREGION_COPY_STATE(other) \
209 _envelope_active (Properties::envelope_active, other->_envelope_active) \
210 , _default_fade_in (Properties::default_fade_in, other->_default_fade_in) \
211 , _default_fade_out (Properties::default_fade_out, other->_default_fade_out) \
212 , _fade_in_active (Properties::fade_in_active, other->_fade_in_active) \
213 , _fade_out_active (Properties::fade_out_active, other->_fade_out_active) \
214 , _scale_amplitude (Properties::scale_amplitude, other->_scale_amplitude) \
215 , _fade_in (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_fade_in.val()))) \
216 , _inverse_fade_in (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_inverse_fade_in.val()))) \
217 , _fade_out (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_fade_out.val()))) \
218 , _inverse_fade_out (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_inverse_fade_out.val())))
219 /* a Session will reset these to its chosen defaults by calling AudioRegion::set_default_fade() */
224 register_properties ();
226 suspend_property_changes();
227 set_default_fades ();
228 set_default_envelope ();
229 resume_property_changes();
231 listen_to_my_curves ();
232 connect_to_analysis_changed ();
233 connect_to_header_position_offset_changed ();
236 /** Constructor for use by derived types only */
237 AudioRegion::AudioRegion (Session& s, framepos_t start, framecnt_t len, std::string name)
238 : Region (s, start, len, name, DataType::AUDIO)
239 , AUDIOREGION_STATE_DEFAULT
240 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter(EnvelopeAutomation))))
242 , _fade_in_suspended (0)
243 , _fade_out_suspended (0)
246 assert (_sources.size() == _master_sources.size());
249 /** Basic AudioRegion constructor */
250 AudioRegion::AudioRegion (const SourceList& srcs)
252 , AUDIOREGION_STATE_DEFAULT
253 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter(EnvelopeAutomation))))
254 , _automatable(srcs[0]->session())
255 , _fade_in_suspended (0)
256 , _fade_out_suspended (0)
259 assert (_sources.size() == _master_sources.size());
262 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
264 , AUDIOREGION_COPY_STATE (other)
265 /* As far as I can see, the _envelope's times are relative to region position, and have nothing
266 to do with sources (and hence _start). So when we copy the envelope, we just use the supplied offset.
268 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (*other->_envelope.val(), 0, other->_length)))
269 , _automatable (other->session())
270 , _fade_in_suspended (0)
271 , _fade_out_suspended (0)
273 /* don't use init here, because we got fade in/out from the other region
275 register_properties ();
276 listen_to_my_curves ();
277 connect_to_analysis_changed ();
278 connect_to_header_position_offset_changed ();
280 assert(_type == DataType::AUDIO);
281 assert (_sources.size() == _master_sources.size());
284 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, framecnt_t offset)
285 : Region (other, offset)
286 , AUDIOREGION_COPY_STATE (other)
287 /* As far as I can see, the _envelope's times are relative to region position, and have nothing
288 to do with sources (and hence _start). So when we copy the envelope, we just use the supplied offset.
290 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (*other->_envelope.val(), offset, other->_length)))
291 , _automatable (other->session())
292 , _fade_in_suspended (0)
293 , _fade_out_suspended (0)
295 /* don't use init here, because we got fade in/out from the other region
297 register_properties ();
298 listen_to_my_curves ();
299 connect_to_analysis_changed ();
300 connect_to_header_position_offset_changed ();
302 assert(_type == DataType::AUDIO);
303 assert (_sources.size() == _master_sources.size());
306 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, const SourceList& srcs)
307 : Region (boost::static_pointer_cast<const Region>(other), srcs)
308 , AUDIOREGION_COPY_STATE (other)
309 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (*other->_envelope.val())))
310 , _automatable (other->session())
311 , _fade_in_suspended (0)
312 , _fade_out_suspended (0)
314 /* make-a-sort-of-copy-with-different-sources constructor (used by audio filter) */
316 register_properties ();
318 listen_to_my_curves ();
319 connect_to_analysis_changed ();
320 connect_to_header_position_offset_changed ();
322 assert (_sources.size() == _master_sources.size());
325 AudioRegion::AudioRegion (SourceList& srcs)
327 , AUDIOREGION_STATE_DEFAULT
328 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList(Evoral::Parameter(EnvelopeAutomation))))
329 , _automatable(srcs[0]->session())
330 , _fade_in_suspended (0)
331 , _fade_out_suspended (0)
335 assert(_type == DataType::AUDIO);
336 assert (_sources.size() == _master_sources.size());
339 AudioRegion::~AudioRegion ()
344 AudioRegion::post_set (const PropertyChange& /*ignored*/)
347 _sync_position = _start;
350 /* return to default fades if the existing ones are too long */
352 if (_left_of_split) {
353 if (_fade_in->back()->when >= _length) {
354 set_default_fade_in ();
356 set_default_fade_out ();
357 _left_of_split = false;
360 if (_right_of_split) {
361 if (_fade_out->back()->when >= _length) {
362 set_default_fade_out ();
365 set_default_fade_in ();
366 _right_of_split = false;
369 /* If _length changed, adjust our gain envelope accordingly */
370 _envelope->truncate_end (_length);
374 AudioRegion::connect_to_analysis_changed ()
376 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
377 (*i)->AnalysisChanged.connect_same_thread (*this, boost::bind (&AudioRegion::invalidate_transients, this));
382 AudioRegion::connect_to_header_position_offset_changed ()
384 set<boost::shared_ptr<Source> > unique_srcs;
386 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
388 /* connect only once to HeaderPositionOffsetChanged, even if sources are replicated
391 if (unique_srcs.find (*i) == unique_srcs.end ()) {
392 unique_srcs.insert (*i);
393 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
395 afs->HeaderPositionOffsetChanged.connect_same_thread (*this, boost::bind (&AudioRegion::source_offset_changed, this));
402 AudioRegion::listen_to_my_curves ()
404 _envelope->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::envelope_changed, this));
405 _fade_in->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::fade_in_changed, this));
406 _fade_out->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::fade_out_changed, this));
410 AudioRegion::set_envelope_active (bool yn)
412 if (envelope_active() != yn) {
413 _envelope_active = yn;
414 send_change (PropertyChange (Properties::envelope_active));
418 /** @param buf Buffer to put peak data in.
419 * @param npeaks Number of peaks to read (ie the number of PeakDatas in buf)
420 * @param offset Start position, as an offset from the start of this region's source.
421 * @param cnt Number of samples to read.
422 * @param chan_n Channel.
423 * @param frames_per_pixel Number of samples to use to generate one peak value.
427 AudioRegion::read_peaks (PeakData *buf, framecnt_t npeaks, framecnt_t offset, framecnt_t cnt, uint32_t chan_n, double frames_per_pixel) const
429 if (chan_n >= _sources.size()) {
433 if (audio_source(chan_n)->read_peaks (buf, npeaks, offset, cnt, frames_per_pixel)) {
437 if (_scale_amplitude != 1.0f) {
438 for (framecnt_t n = 0; n < npeaks; ++n) {
439 buf[n].max *= _scale_amplitude;
440 buf[n].min *= _scale_amplitude;
447 /** @param buf Buffer to write data to (existing data will be overwritten).
448 * @param pos Position to read from as an offset from the region position.
449 * @param cnt Number of frames to read.
450 * @param channel Channel to read from.
453 AudioRegion::read (Sample* buf, framepos_t pos, framecnt_t cnt, int channel) const
455 /* raw read, no fades, no gain, nada */
456 return read_from_sources (_sources, _length, buf, _position + pos, cnt, channel);
460 AudioRegion::master_read_at (Sample *buf, Sample* /*mixdown_buffer*/, float* /*gain_buffer*/,
461 framepos_t position, framecnt_t cnt, uint32_t chan_n) const
463 /* do not read gain/scaling/fades and do not count this disk i/o in statistics */
466 return read_from_sources (
467 _master_sources, _master_sources.front()->length (_master_sources.front()->timeline_position()),
468 buf, position, cnt, chan_n
472 /** @param buf Buffer to mix data into.
473 * @param mixdown_buffer Scratch buffer for audio data.
474 * @param gain_buffer Scratch buffer for gain data.
475 * @param position Position within the session to read from.
476 * @param cnt Number of frames to read.
477 * @param chan_n Channel number to read.
480 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
483 uint32_t chan_n) const
485 /* We are reading data from this region into buf (possibly via mixdown_buffer).
486 The caller has verified that we cover the desired section.
489 /* See doc/region_read.svg for a drawing which might help to explain
495 if (n_channels() == 0) {
499 /* WORK OUT WHERE TO GET DATA FROM */
503 assert (position >= _position);
504 frameoffset_t const internal_offset = position - _position;
506 if (internal_offset >= _length) {
507 return 0; /* read nothing */
510 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
511 return 0; /* read nothing */
515 /* COMPUTE DETAILS OF ANY FADES INVOLVED IN THIS READ */
517 /* Amount (length) of fade in that we are dealing with in this read */
518 framecnt_t fade_in_limit = 0;
520 /* Offset from buf / mixdown_buffer of the start
521 of any fade out that we are dealing with
523 frameoffset_t fade_out_offset = 0;
525 /* Amount (length) of fade out that we are dealing with in this read */
526 framecnt_t fade_out_limit = 0;
528 framecnt_t fade_interval_start = 0;
532 if (_fade_in_active && _session.config.get_use_region_fades()) {
534 framecnt_t fade_in_length = (framecnt_t) _fade_in->back()->when;
536 /* see if this read is within the fade in */
538 if (internal_offset < fade_in_length) {
539 fade_in_limit = min (to_read, fade_in_length - internal_offset);
545 if (_fade_out_active && _session.config.get_use_region_fades()) {
547 /* see if some part of this read is within the fade out */
549 /* ................. >| REGION
555 * _length - fade_out_length
559 * ^internal_offset + to_read
561 * we need the intersection of [internal_offset,internal_offset+to_read] with
562 * [_length - fade_out_length, _length]
566 fade_interval_start = max (internal_offset, _length - framecnt_t (_fade_out->back()->when));
567 framecnt_t fade_interval_end = min(internal_offset + to_read, _length.val());
569 if (fade_interval_end > fade_interval_start) {
570 /* (part of the) the fade out is in this buffer */
571 fade_out_limit = fade_interval_end - fade_interval_start;
572 fade_out_offset = fade_interval_start - internal_offset;
576 /* READ DATA FROM THE SOURCE INTO mixdown_buffer.
577 We can never read directly into buf, since it may contain data
578 from a region `below' this one in the stack, and our fades (if they exist)
579 may need to mix with the existing data.
582 if (read_from_sources (_sources, _length, mixdown_buffer, position, to_read, chan_n) != to_read) {
586 /* APPLY REGULAR GAIN CURVES AND SCALING TO mixdown_buffer */
588 if (envelope_active()) {
589 _envelope->curve().get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
591 if (_scale_amplitude != 1.0f) {
592 for (framecnt_t n = 0; n < to_read; ++n) {
593 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
596 for (framecnt_t n = 0; n < to_read; ++n) {
597 mixdown_buffer[n] *= gain_buffer[n];
600 } else if (_scale_amplitude != 1.0f) {
601 apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
604 /* APPLY FADES TO THE DATA IN mixdown_buffer AND MIX THE RESULTS INTO
605 * buf. The key things to realize here: (1) the fade being applied is
606 * (as of April 26th 2012) just the inverse of the fade in curve (2)
607 * "buf" contains data from lower regions already. So this operation
608 * fades out the existing material.
611 if (fade_in_limit != 0) {
614 if (_inverse_fade_in) {
616 /* explicit inverse fade in curve (e.g. for constant
617 * power), so we have to fetch it.
620 _inverse_fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
622 /* Fade the data from lower layers out */
623 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
624 buf[n] *= gain_buffer[n];
627 /* refill gain buffer with the fade in */
629 _fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
633 /* no explicit inverse fade in, so just use (1 - fade
634 * in) for the fade out of lower layers
637 _fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
639 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
640 buf[n] *= 1 - gain_buffer[n];
644 _fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
647 /* Mix our newly-read data in, with the fade */
648 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
649 buf[n] += mixdown_buffer[n] * gain_buffer[n];
653 if (fade_out_limit != 0) {
655 framecnt_t const curve_offset = fade_interval_start - (_length - _fade_out->back()->when);
658 if (_inverse_fade_out) {
660 _inverse_fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
662 /* Fade the data from lower levels in */
663 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
664 buf[m] *= gain_buffer[n];
667 /* fetch the actual fade out */
669 _fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
673 /* no explicit inverse fade out (which is
674 * actually a fade in), so just use (1 - fade
675 * out) for the fade in of lower layers
678 _fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
680 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
681 buf[m] *= 1 - gain_buffer[n];
685 _fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
688 /* Mix our newly-read data with whatever was already there,
689 with the fade out applied to our data.
691 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
692 buf[m] += mixdown_buffer[m] * gain_buffer[n];
696 /* MIX OR COPY THE REGION BODY FROM mixdown_buffer INTO buf */
698 framecnt_t const N = to_read - fade_in_limit - fade_out_limit;
701 DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("Region %1 memcpy into buf @ %2 + %3, from mixdown buffer @ %4 + %5, len = %6 cnt was %7\n",
702 name(), buf, fade_in_limit, mixdown_buffer, fade_in_limit, N, cnt));
703 memcpy (buf + fade_in_limit, mixdown_buffer + fade_in_limit, N * sizeof (Sample));
705 mix_buffers_no_gain (buf + fade_in_limit, mixdown_buffer + fade_in_limit, N);
712 /** Read data directly from one of our sources, accounting for the situation when the track has a different channel
713 * count to the region.
715 * @param srcs Source list to get our source from.
716 * @param limit Furthest that we should read, as an offset from the region position.
717 * @param buf Buffer to write data into (existing contents of the buffer will be overwritten)
718 * @param position Position to read from, in session frames.
719 * @param cnt Number of frames to read.
720 * @param chan_n Channel to read from.
721 * @return Number of frames read.
725 AudioRegion::read_from_sources (SourceList const & srcs, framecnt_t limit, Sample* buf, framepos_t position, framecnt_t cnt, uint32_t chan_n) const
727 frameoffset_t const internal_offset = position - _position;
728 if (internal_offset >= limit) {
732 framecnt_t const to_read = min (cnt, limit - internal_offset);
737 if (chan_n < n_channels()) {
739 boost::shared_ptr<AudioSource> src = boost::dynamic_pointer_cast<AudioSource> (srcs[chan_n]);
740 if (src->read (buf, _start + internal_offset, to_read) != to_read) {
741 return 0; /* "read nothing" */
746 /* track is N-channel, this region has fewer channels; silence the ones
750 if (Config->get_replicate_missing_region_channels()) {
752 /* copy an existing channel's data in for this non-existant one */
754 uint32_t channel = chan_n % n_channels();
755 boost::shared_ptr<AudioSource> src = boost::dynamic_pointer_cast<AudioSource> (srcs[channel]);
757 if (src->read (buf, _start + internal_offset, to_read) != to_read) {
758 return 0; /* "read nothing" */
764 memset (buf, 0, sizeof (Sample) * to_read);
772 AudioRegion::get_basic_state ()
774 XMLNode& node (Region::state ());
776 LocaleGuard lg (X_("C"));
778 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
779 node.add_property ("channels", buf);
785 AudioRegion::state ()
787 XMLNode& node (get_basic_state());
789 LocaleGuard lg (X_("C"));
791 child = node.add_child ("Envelope");
793 bool default_env = false;
795 // If there are only two points, the points are in the start of the region and the end of the region
796 // so, if they are both at 1.0f, that means the default region.
798 if (_envelope->size() == 2 &&
799 _envelope->front()->value == GAIN_COEFF_UNITY &&
800 _envelope->back()->value==GAIN_COEFF_UNITY) {
801 if (_envelope->front()->when == 0 && _envelope->back()->when == _length) {
807 child->add_property ("default", "yes");
809 child->add_child_nocopy (_envelope->get_state ());
812 child = node.add_child (X_("FadeIn"));
814 if (_default_fade_in) {
815 child->add_property ("default", "yes");
817 child->add_child_nocopy (_fade_in->get_state ());
820 if (_inverse_fade_in) {
821 child = node.add_child (X_("InverseFadeIn"));
822 child->add_child_nocopy (_inverse_fade_in->get_state ());
825 child = node.add_child (X_("FadeOut"));
827 if (_default_fade_out) {
828 child->add_property ("default", "yes");
830 child->add_child_nocopy (_fade_out->get_state ());
833 if (_inverse_fade_out) {
834 child = node.add_child (X_("InverseFadeOut"));
835 child->add_child_nocopy (_inverse_fade_out->get_state ());
842 AudioRegion::_set_state (const XMLNode& node, int version, PropertyChange& what_changed, bool send)
844 const XMLNodeList& nlist = node.children();
845 const XMLProperty *prop;
846 LocaleGuard lg (X_("C"));
847 boost::shared_ptr<Playlist> the_playlist (_playlist.lock());
849 suspend_property_changes ();
852 the_playlist->freeze ();
856 /* this will set all our State members and stuff controlled by the Region.
857 It should NOT send any changed signals - that is our responsibility.
860 Region::_set_state (node, version, what_changed, false);
862 if ((prop = node.property ("scale-gain")) != 0) {
863 float a = atof (prop->value().c_str());
864 if (a != _scale_amplitude) {
865 _scale_amplitude = a;
866 what_changed.add (Properties::scale_amplitude);
870 /* Now find envelope description and other related child items */
872 _envelope->freeze ();
874 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
880 if (child->name() == "Envelope") {
884 if ((prop = child->property ("default")) != 0 || _envelope->set_state (*child, version)) {
885 set_default_envelope ();
888 _envelope->truncate_end (_length);
891 } else if (child->name() == "FadeIn") {
895 if (((prop = child->property ("default")) != 0 && string_is_affirmative (prop->value())) || (prop = child->property ("steepness")) != 0) {
896 set_default_fade_in ();
898 XMLNode* grandchild = child->child ("AutomationList");
900 _fade_in->set_state (*grandchild, version);
904 if ((prop = child->property ("active")) != 0) {
905 if (string_is_affirmative (prop->value())) {
906 set_fade_in_active (true);
908 set_fade_in_active (false);
912 } else if (child->name() == "FadeOut") {
916 if (((prop = child->property ("default")) != 0 && (string_is_affirmative (prop->value()))) || (prop = child->property ("steepness")) != 0) {
917 set_default_fade_out ();
919 XMLNode* grandchild = child->child ("AutomationList");
921 _fade_out->set_state (*grandchild, version);
925 if ((prop = child->property ("active")) != 0) {
926 if (string_is_affirmative (prop->value())) {
927 set_fade_out_active (true);
929 set_fade_out_active (false);
933 } else if ( (child->name() == "InverseFadeIn") || (child->name() == "InvFadeIn") ) {
934 XMLNode* grandchild = child->child ("AutomationList");
936 _inverse_fade_in->set_state (*grandchild, version);
938 } else if ( (child->name() == "InverseFadeOut") || (child->name() == "InvFadeOut") ) {
939 XMLNode* grandchild = child->child ("AutomationList");
941 _inverse_fade_out->set_state (*grandchild, version);
947 resume_property_changes ();
950 send_change (what_changed);
954 the_playlist->thaw ();
961 AudioRegion::set_state (const XMLNode& node, int version)
963 PropertyChange what_changed;
964 return _set_state (node, version, what_changed, true);
968 AudioRegion::fade_range (framepos_t start, framepos_t end)
972 switch (coverage (start, end)) {
973 case Evoral::OverlapStart:
977 set_fade_in (FadeConstantPower, e - s);
979 case Evoral::OverlapEnd:
982 e = _position + _length;
983 set_fade_out (FadeConstantPower, e - s);
985 case Evoral::OverlapInternal:
986 /* needs addressing, perhaps. Difficult to do if we can't
987 * control one edge of the fade relative to the relevant edge
988 * of the region, which we cannot - fades are currently assumed
989 * to start/end at the start/end of the region
998 AudioRegion::set_fade_in_shape (FadeShape shape)
1000 set_fade_in (shape, (framecnt_t) _fade_in->back()->when);
1004 AudioRegion::set_fade_out_shape (FadeShape shape)
1006 set_fade_out (shape, (framecnt_t) _fade_out->back()->when);
1010 AudioRegion::set_fade_in (boost::shared_ptr<AutomationList> f)
1012 _fade_in->freeze ();
1013 *(_fade_in.val()) = *f;
1015 _default_fade_in = false;
1017 send_change (PropertyChange (Properties::fade_in));
1021 AudioRegion::set_fade_in (FadeShape shape, framecnt_t len)
1023 const ARDOUR::ParameterDescriptor desc(FadeInAutomation);
1024 boost::shared_ptr<Evoral::ControlList> c1 (new Evoral::ControlList (FadeInAutomation, desc));
1025 boost::shared_ptr<Evoral::ControlList> c2 (new Evoral::ControlList (FadeInAutomation, desc));
1026 boost::shared_ptr<Evoral::ControlList> c3 (new Evoral::ControlList (FadeInAutomation, desc));
1028 _fade_in->freeze ();
1030 _inverse_fade_in->clear ();
1032 const int num_steps = 32;
1036 _fade_in->fast_simple_add (0.0, GAIN_COEFF_SMALL);
1037 _fade_in->fast_simple_add (len, GAIN_COEFF_UNITY);
1038 reverse_curve (_inverse_fade_in.val(), _fade_in.val());
1042 generate_db_fade (_fade_in.val(), len, num_steps, -60);
1043 reverse_curve (c1, _fade_in.val());
1044 _fade_in->copy_events (*c1);
1045 generate_inverse_power_curve (_inverse_fade_in.val(), _fade_in.val());
1049 generate_db_fade (c1, len, num_steps, -1); // start off with a slow fade
1050 generate_db_fade (c2, len, num_steps, -80); // end with a fast fade
1051 merge_curves (_fade_in.val(), c1, c2);
1052 reverse_curve (c3, _fade_in.val());
1053 _fade_in->copy_events (*c3);
1054 generate_inverse_power_curve (_inverse_fade_in.val(), _fade_in.val());
1057 case FadeConstantPower:
1058 _fade_in->fast_simple_add (0.0, GAIN_COEFF_SMALL);
1059 for (int i = 1; i < num_steps; ++i) {
1060 const float dist = i / (num_steps + 1.f);
1061 _fade_in->fast_simple_add (len * dist, sin (dist * M_PI / 2.0));
1063 _fade_in->fast_simple_add (len, GAIN_COEFF_UNITY);
1064 reverse_curve (_inverse_fade_in.val(), _fade_in.val());
1068 //start with a nearly linear cuve
1069 _fade_in->fast_simple_add (0, 1);
1070 _fade_in->fast_simple_add (0.5 * len, 0.6);
1071 //now generate a fade-out curve by successively applying a gain drop
1072 const double breakpoint = 0.7; //linear for first 70%
1073 for (int i = 2; i < 9; ++i) {
1074 const float coeff = (1.f - breakpoint) * powf (0.5, i);
1075 _fade_in->fast_simple_add (len * (breakpoint + ((GAIN_COEFF_UNITY - breakpoint) * (double)i / 9.0)), coeff);
1077 _fade_in->fast_simple_add (len, GAIN_COEFF_SMALL);
1078 reverse_curve (c3, _fade_in.val());
1079 _fade_in->copy_events (*c3);
1080 reverse_curve (_inverse_fade_in.val(), _fade_in.val());
1084 _fade_in->set_interpolation(Evoral::ControlList::Curved);
1085 _inverse_fade_in->set_interpolation(Evoral::ControlList::Curved);
1087 _default_fade_in = false;
1089 send_change (PropertyChange (Properties::fade_in));
1093 AudioRegion::set_fade_out (boost::shared_ptr<AutomationList> f)
1095 _fade_out->freeze ();
1096 *(_fade_out.val()) = *f;
1098 _default_fade_out = false;
1100 send_change (PropertyChange (Properties::fade_in));
1104 AudioRegion::set_fade_out (FadeShape shape, framecnt_t len)
1106 const ARDOUR::ParameterDescriptor desc(FadeOutAutomation);
1107 boost::shared_ptr<Evoral::ControlList> c1 (new Evoral::ControlList (FadeOutAutomation, desc));
1108 boost::shared_ptr<Evoral::ControlList> c2 (new Evoral::ControlList (FadeOutAutomation, desc));
1110 _fade_out->freeze ();
1111 _fade_out->clear ();
1112 _inverse_fade_out->clear ();
1114 const int num_steps = 32;
1118 _fade_out->fast_simple_add (0.0, GAIN_COEFF_UNITY);
1119 _fade_out->fast_simple_add (len, GAIN_COEFF_SMALL);
1120 reverse_curve (_inverse_fade_out.val(), _fade_out.val());
1124 generate_db_fade (_fade_out.val(), len, num_steps, -60);
1125 generate_inverse_power_curve (_inverse_fade_out.val(), _fade_out.val());
1129 generate_db_fade (c1, len, num_steps, -1); //start off with a slow fade
1130 generate_db_fade (c2, len, num_steps, -80); //end with a fast fade
1131 merge_curves (_fade_out.val(), c1, c2);
1132 generate_inverse_power_curve (_inverse_fade_out.val(), _fade_out.val());
1135 case FadeConstantPower:
1136 //constant-power fades use a sin/cos relationship
1137 //the cutoff is abrupt but it has the benefit of being symmetrical
1138 _fade_out->fast_simple_add (0.0, GAIN_COEFF_UNITY);
1139 for (int i = 1; i < num_steps; ++i) {
1140 const float dist = i / (num_steps + 1.f);
1141 _fade_out->fast_simple_add (len * dist, cos (dist * M_PI / 2.0));
1143 _fade_out->fast_simple_add (len, GAIN_COEFF_SMALL);
1144 reverse_curve (_inverse_fade_out.val(), _fade_out.val());
1148 //start with a nearly linear cuve
1149 _fade_out->fast_simple_add (0, 1);
1150 _fade_out->fast_simple_add (0.5 * len, 0.6);
1151 //now generate a fade-out curve by successively applying a gain drop
1152 const double breakpoint = 0.7; //linear for first 70%
1153 for (int i = 2; i < 9; ++i) {
1154 const float coeff = (1.f - breakpoint) * powf (0.5, i);
1155 _fade_out->fast_simple_add (len * (breakpoint + ((GAIN_COEFF_UNITY - breakpoint) * (double)i / 9.0)), coeff);
1157 _fade_out->fast_simple_add (len, GAIN_COEFF_SMALL);
1158 reverse_curve (_inverse_fade_out.val(), _fade_out.val());
1162 _fade_out->set_interpolation(Evoral::ControlList::Curved);
1163 _inverse_fade_out->set_interpolation(Evoral::ControlList::Curved);
1165 _default_fade_out = false;
1167 send_change (PropertyChange (Properties::fade_out));
1171 AudioRegion::set_fade_in_length (framecnt_t len)
1173 if (len > _length) {
1181 bool changed = _fade_in->extend_to (len);
1184 if (_inverse_fade_in) {
1185 _inverse_fade_in->extend_to (len);
1188 _default_fade_in = false;
1189 send_change (PropertyChange (Properties::fade_in));
1194 AudioRegion::set_fade_out_length (framecnt_t len)
1196 if (len > _length) {
1204 bool changed = _fade_out->extend_to (len);
1208 if (_inverse_fade_out) {
1209 _inverse_fade_out->extend_to (len);
1211 _default_fade_out = false;
1213 send_change (PropertyChange (Properties::fade_out));
1218 AudioRegion::set_fade_in_active (bool yn)
1220 if (yn == _fade_in_active) {
1224 _fade_in_active = yn;
1225 send_change (PropertyChange (Properties::fade_in_active));
1229 AudioRegion::set_fade_out_active (bool yn)
1231 if (yn == _fade_out_active) {
1234 _fade_out_active = yn;
1235 send_change (PropertyChange (Properties::fade_out_active));
1239 AudioRegion::fade_in_is_default () const
1241 return _fade_in->size() == 2 && _fade_in->front()->when == 0 && _fade_in->back()->when == 64;
1245 AudioRegion::fade_out_is_default () const
1247 return _fade_out->size() == 2 && _fade_out->front()->when == 0 && _fade_out->back()->when == 64;
1251 AudioRegion::set_default_fade_in ()
1253 _fade_in_suspended = 0;
1254 set_fade_in (Config->get_default_fade_shape(), 64);
1258 AudioRegion::set_default_fade_out ()
1260 _fade_out_suspended = 0;
1261 set_fade_out (Config->get_default_fade_shape(), 64);
1265 AudioRegion::set_default_fades ()
1267 set_default_fade_in ();
1268 set_default_fade_out ();
1272 AudioRegion::set_default_envelope ()
1274 _envelope->freeze ();
1275 _envelope->clear ();
1276 _envelope->fast_simple_add (0, GAIN_COEFF_UNITY);
1277 _envelope->fast_simple_add (_length, GAIN_COEFF_UNITY);
1282 AudioRegion::recompute_at_end ()
1284 /* our length has changed. recompute a new final point by interpolating
1285 based on the the existing curve.
1288 _envelope->freeze ();
1289 _envelope->truncate_end (_length);
1292 suspend_property_changes();
1294 if (_left_of_split) {
1295 set_default_fade_out ();
1296 _left_of_split = false;
1297 } else if (_fade_out->back()->when > _length) {
1298 _fade_out->extend_to (_length);
1299 send_change (PropertyChange (Properties::fade_out));
1302 if (_fade_in->back()->when > _length) {
1303 _fade_in->extend_to (_length);
1304 send_change (PropertyChange (Properties::fade_in));
1307 resume_property_changes();
1311 AudioRegion::recompute_at_start ()
1313 /* as above, but the shift was from the front */
1315 _envelope->truncate_start (_length);
1317 suspend_property_changes();
1319 if (_right_of_split) {
1320 set_default_fade_in ();
1321 _right_of_split = false;
1322 } else if (_fade_in->back()->when > _length) {
1323 _fade_in->extend_to (_length);
1324 send_change (PropertyChange (Properties::fade_in));
1327 if (_fade_out->back()->when > _length) {
1328 _fade_out->extend_to (_length);
1329 send_change (PropertyChange (Properties::fade_out));
1332 resume_property_changes();
1336 AudioRegion::separate_by_channel (Session& /*session*/, vector<boost::shared_ptr<Region> >& v) const
1342 if (_sources.size() < 2) {
1346 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
1348 srcs.push_back (*i);
1352 if (_sources.size() == 2) {
1360 new_name += ('0' + n + 1);
1363 /* create a copy with just one source. prevent if from being thought of as
1364 "whole file" even if it covers the entire source file(s).
1369 plist.add (Properties::start, _start.val());
1370 plist.add (Properties::length, _length.val());
1371 plist.add (Properties::name, new_name);
1372 plist.add (Properties::layer, layer ());
1374 v.push_back(RegionFactory::create (srcs, plist));
1375 v.back()->set_whole_file (false);
1384 AudioRegion::read_raw_internal (Sample* buf, framepos_t pos, framecnt_t cnt, int channel) const
1386 return audio_source(channel)->read (buf, pos, cnt);
1390 AudioRegion::set_scale_amplitude (gain_t g)
1392 boost::shared_ptr<Playlist> pl (playlist());
1394 _scale_amplitude = g;
1396 /* tell the diskstream we're in */
1399 pl->ContentsChanged();
1402 /* tell everybody else */
1404 send_change (PropertyChange (Properties::scale_amplitude));
1407 /** @return the maximum (linear) amplitude of the region, or a -ve
1408 * number if the Progress object reports that the process was cancelled.
1411 AudioRegion::maximum_amplitude (Progress* p) const
1413 framepos_t fpos = _start;
1414 framepos_t const fend = _start + _length;
1417 framecnt_t const blocksize = 64 * 1024;
1418 Sample buf[blocksize];
1420 while (fpos < fend) {
1424 framecnt_t const to_read = min (fend - fpos, blocksize);
1426 for (n = 0; n < n_channels(); ++n) {
1430 if (read_raw_internal (buf, fpos, to_read, n) != to_read) {
1434 maxamp = compute_peak (buf, to_read, maxamp);
1439 p->set_progress (float (fpos - _start) / _length);
1440 if (p->cancelled ()) {
1449 /** Normalize using a given maximum amplitude and target, so that region
1450 * _scale_amplitude becomes target / max_amplitude.
1453 AudioRegion::normalize (float max_amplitude, float target_dB)
1455 gain_t target = dB_to_coefficient (target_dB);
1457 if (target == GAIN_COEFF_UNITY) {
1458 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1459 that we may have clipped.
1461 target -= FLT_EPSILON;
1464 if (max_amplitude < GAIN_COEFF_SMALL) {
1465 /* don't even try */
1469 if (max_amplitude == target) {
1470 /* we can't do anything useful */
1474 set_scale_amplitude (target / max_amplitude);
1478 AudioRegion::fade_in_changed ()
1480 send_change (PropertyChange (Properties::fade_in));
1484 AudioRegion::fade_out_changed ()
1486 send_change (PropertyChange (Properties::fade_out));
1490 AudioRegion::envelope_changed ()
1492 send_change (PropertyChange (Properties::envelope));
1496 AudioRegion::suspend_fade_in ()
1498 if (++_fade_in_suspended == 1) {
1499 if (fade_in_is_default()) {
1500 set_fade_in_active (false);
1506 AudioRegion::resume_fade_in ()
1508 if (--_fade_in_suspended == 0 && _fade_in_suspended) {
1509 set_fade_in_active (true);
1514 AudioRegion::suspend_fade_out ()
1516 if (++_fade_out_suspended == 1) {
1517 if (fade_out_is_default()) {
1518 set_fade_out_active (false);
1524 AudioRegion::resume_fade_out ()
1526 if (--_fade_out_suspended == 0 &&_fade_out_suspended) {
1527 set_fade_out_active (true);
1532 AudioRegion::speed_mismatch (float sr) const
1534 if (_sources.empty()) {
1535 /* impossible, but ... */
1539 float fsr = audio_source()->sample_rate();
1545 AudioRegion::source_offset_changed ()
1547 /* XXX this fixes a crash that should not occur. It does occur
1548 becauses regions are not being deleted when a session
1549 is unloaded. That bug must be fixed.
1552 if (_sources.empty()) {
1556 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(_sources.front());
1558 if (afs && afs->destructive()) {
1559 // set_start (source()->natural_position(), this);
1560 set_position (source()->natural_position());
1564 boost::shared_ptr<AudioSource>
1565 AudioRegion::audio_source (uint32_t n) const
1567 // Guaranteed to succeed (use a static cast for speed?)
1568 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1572 AudioRegion::get_related_audio_file_channel_count () const
1574 uint32_t chan_count = 0;
1575 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
1577 boost::shared_ptr<SndFileSource> sndf = boost::dynamic_pointer_cast<SndFileSource>(*i);
1580 if (sndf->channel_count() > chan_count) {
1581 chan_count = sndf->channel_count();
1584 #ifdef HAVE_COREAUDIO
1586 boost::shared_ptr<CoreAudioSource> cauf = boost::dynamic_pointer_cast<CoreAudioSource>(*i);
1588 if (cauf->channel_count() > chan_count) {
1589 chan_count = cauf->channel_count();
1593 #endif // HAVE_COREAUDIO
1600 AudioRegion::adjust_transients (frameoffset_t delta)
1602 for (AnalysisFeatureList::iterator x = _transients.begin(); x != _transients.end(); ++x) {
1603 (*x) = (*x) + delta;
1606 send_change (PropertyChange (Properties::valid_transients));
1612 AudioRegion::update_transient (framepos_t old_position, framepos_t new_position)
1614 for (AnalysisFeatureList::iterator x = _transients.begin(); x != _transients.end(); ++x) {
1615 if ((*x) == old_position) {
1616 (*x) = new_position;
1617 send_change (PropertyChange (Properties::valid_transients));
1627 AudioRegion::add_transient (framepos_t where)
1629 _transients.push_back(where);
1630 _valid_transients = true;
1632 send_change (PropertyChange (Properties::valid_transients));
1636 AudioRegion::remove_transient (framepos_t where)
1638 _transients.remove(where);
1639 _valid_transients = true;
1641 send_change (PropertyChange (Properties::valid_transients));
1645 AudioRegion::set_transients (AnalysisFeatureList& results)
1647 _transients.clear();
1648 _transients = results;
1649 _valid_transients = true;
1651 send_change (PropertyChange (Properties::valid_transients));
1657 AudioRegion::get_transients (AnalysisFeatureList& results, bool force_new)
1659 boost::shared_ptr<Playlist> pl = playlist();
1665 if (_valid_transients && !force_new) {
1666 results = _transients;
1670 SourceList::iterator s;
1672 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1673 if (!(*s)->has_been_analysed()) {
1674 cerr << "For " << name() << " source " << (*s)->name() << " has not been analyzed\n";
1679 if (s == _sources.end()) {
1680 /* all sources are analyzed, merge data from each one */
1682 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1684 /* find the set of transients within the bounds of this region */
1686 AnalysisFeatureList::iterator low = lower_bound ((*s)->transients.begin(),
1687 (*s)->transients.end(),
1690 AnalysisFeatureList::iterator high = upper_bound ((*s)->transients.begin(),
1691 (*s)->transients.end(),
1696 results.insert (results.end(), low, high);
1699 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1701 /* translate all transients to current position */
1703 for (AnalysisFeatureList::iterator x = results.begin(); x != results.end(); ++x) {
1708 _transients = results;
1709 _valid_transients = true;
1714 /* no existing/complete transient info */
1716 static bool analyse_dialog_shown = false; /* global per instance of Ardour */
1718 if (!Config->get_auto_analyse_audio()) {
1719 if (!analyse_dialog_shown) {
1720 pl->session().Dialog (string_compose (_("\
1721 You have requested an operation that requires audio analysis.\n\n\
1722 You currently have \"auto-analyse-audio\" disabled, which means \
1723 that transient data must be generated every time it is required.\n\n\
1724 If you are doing work that will require transient data on a \
1725 regular basis, you should probably enable \"auto-analyse-audio\" \
1726 then quit %1 and restart.\n\n\
1727 This dialog will not display again. But you may notice a slight delay \
1728 in this and future transient-detection operations.\n\
1730 analyse_dialog_shown = true;
1734 bool existing_results = !results.empty();
1738 TransientDetector t (pl->session().frame_rate());
1740 _transients.clear ();
1741 _valid_transients = false;
1743 for (uint32_t i = 0; i < n_channels(); ++i) {
1745 AnalysisFeatureList these_results;
1749 if (t.run ("", this, i, these_results)) {
1753 /* translate all transients to give absolute position */
1755 for (AnalysisFeatureList::iterator i = these_results.begin(); i != these_results.end(); ++i) {
1761 _transients.insert (_transients.end(), these_results.begin(), these_results.end());
1764 error << string_compose(_("Transient Analysis failed for %1."), _("Audio Region")) << endmsg;
1768 if (!results.empty()) {
1769 if (existing_results) {
1771 /* merge our transients into the existing ones, then clean up
1775 results.insert (results.end(), _transients.begin(), _transients.end());
1776 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1779 /* make sure ours are clean too */
1781 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1785 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1786 results = _transients;
1789 _valid_transients = true;
1794 /** Find areas of `silence' within a region.
1796 * @param threshold Threshold below which signal is considered silence (as a sample value)
1797 * @param min_length Minimum length of silent period to be reported.
1798 * @return Silent intervals, measured relative to the region start in the source
1802 AudioRegion::find_silence (Sample threshold, framecnt_t min_length, framecnt_t fade_length, InterThreadInfo& itt) const
1804 framecnt_t const block_size = 64 * 1024;
1805 boost::scoped_array<Sample> loudest (new Sample[block_size]);
1806 boost::scoped_array<Sample> buf (new Sample[block_size]);
1808 assert (fade_length >= 0);
1809 assert (min_length > 0);
1811 framepos_t pos = _start;
1812 framepos_t const end = _start + _length;
1814 AudioIntervalResult silent_periods;
1816 bool in_silence = true;
1817 frameoffset_t silence_start = _start;
1819 while (pos < end && !itt.cancel) {
1821 framecnt_t cur_samples = 0;
1822 /* fill `loudest' with the loudest absolute sample at each instant, across all channels */
1823 memset (loudest.get(), 0, sizeof (Sample) * block_size);
1824 for (uint32_t n = 0; n < n_channels(); ++n) {
1826 cur_samples = read_raw_internal (buf.get(), pos, block_size, n);
1827 for (framecnt_t i = 0; i < cur_samples; ++i) {
1828 loudest[i] = max (loudest[i], abs (buf[i]));
1832 /* now look for silence */
1833 for (framecnt_t i = 0; i < cur_samples; ++i) {
1834 bool const silence = abs (loudest[i]) < threshold;
1835 if (silence && !in_silence) {
1836 /* non-silence to silence */
1838 silence_start = pos + i + fade_length;
1839 } else if (!silence && in_silence) {
1840 /* silence to non-silence */
1842 frameoffset_t silence_end = pos + i - 1 - fade_length;
1844 if (silence_end - silence_start >= min_length) {
1845 silent_periods.push_back (std::make_pair (silence_start, silence_end));
1851 itt.progress = (end - pos) / (double)_length;
1853 if (cur_samples == 0) {
1854 assert (pos >= end);
1859 if (in_silence && !itt.cancel) {
1860 /* last block was silent, so finish off the last period */
1861 if (end - 1 - silence_start >= min_length + fade_length) {
1862 silent_periods.push_back (std::make_pair (silence_start, end - 1));
1868 return silent_periods;
1871 Evoral::Range<framepos_t>
1872 AudioRegion::body_range () const
1874 return Evoral::Range<framepos_t> (first_frame() + _fade_in->back()->when + 1, last_frame() - _fade_out->back()->when);
1877 boost::shared_ptr<Region>
1878 AudioRegion::get_single_other_xfade_region (bool start) const
1880 boost::shared_ptr<Playlist> pl (playlist());
1883 /* not currently in a playlist - xfade length is unbounded
1886 return boost::shared_ptr<AudioRegion> ();
1889 boost::shared_ptr<RegionList> rl;
1892 rl = pl->regions_at (position());
1894 rl = pl->regions_at (last_frame());
1897 RegionList::iterator i;
1898 boost::shared_ptr<Region> other;
1901 /* count and find the other region in a single pass through the list */
1903 for (i = rl->begin(); i != rl->end(); ++i) {
1904 if ((*i).get() != this) {
1911 /* zero or multiple regions stacked here - don't care about xfades */
1912 return boost::shared_ptr<AudioRegion> ();
1919 AudioRegion::verify_xfade_bounds (framecnt_t len, bool start)
1921 /* this is called from a UI to check on whether a new proposed
1922 length for an xfade is legal or not. it returns the legal
1923 length corresponding to @a len which may be shorter than or
1924 equal to @a len itself.
1927 boost::shared_ptr<Region> other = get_single_other_xfade_region (start);
1931 /* zero or > 2 regions here, don't care about len, but
1932 it can't be longer than the region itself.
1934 return min (length(), len);
1937 /* we overlap a single region. clamp the length of an xfade to
1938 the maximum possible duration of the overlap (if the other
1939 region were trimmed appropriately).
1943 maxlen = other->latest_possible_frame() - position();
1945 maxlen = last_frame() - other->earliest_possible_position();
1948 return min (length(), min (maxlen, len));