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>
29 #include <glibmm/threads.h>
31 #include "pbd/basename.h"
32 #include "pbd/xml++.h"
33 #include "pbd/stacktrace.h"
34 #include "pbd/enumwriter.h"
35 #include "pbd/convert.h"
37 #include "evoral/Curve.hpp"
39 #include "ardour/audioregion.h"
40 #include "ardour/session.h"
41 #include "ardour/dB.h"
42 #include "ardour/debug.h"
43 #include "ardour/event_type_map.h"
44 #include "ardour/playlist.h"
45 #include "ardour/audiofilesource.h"
46 #include "ardour/region_factory.h"
47 #include "ardour/runtime_functions.h"
48 #include "ardour/transient_detector.h"
49 #include "ardour/parameter_descriptor.h"
50 #include "ardour/progress.h"
56 using namespace ARDOUR;
60 namespace Properties {
61 PBD::PropertyDescriptor<bool> envelope_active;
62 PBD::PropertyDescriptor<bool> default_fade_in;
63 PBD::PropertyDescriptor<bool> default_fade_out;
64 PBD::PropertyDescriptor<bool> fade_in_active;
65 PBD::PropertyDescriptor<bool> fade_out_active;
66 PBD::PropertyDescriptor<float> scale_amplitude;
67 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > fade_in;
68 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > inverse_fade_in;
69 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > fade_out;
70 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > inverse_fade_out;
71 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > envelope;
75 static const double VERY_SMALL_SIGNAL = 0.0000001; //-140dB
77 /* Curve manipulations */
80 reverse_curve (boost::shared_ptr<Evoral::ControlList> dst, boost::shared_ptr<const Evoral::ControlList> src)
82 size_t len = src->back()->when;
83 for (Evoral::ControlList::const_reverse_iterator it = src->rbegin(); it!=src->rend(); it++) {
84 dst->fast_simple_add (len - (*it)->when, (*it)->value);
89 generate_inverse_power_curve (boost::shared_ptr<Evoral::ControlList> dst, boost::shared_ptr<const Evoral::ControlList> src)
91 // calc inverse curve using sum of squares
92 for (Evoral::ControlList::const_iterator it = src->begin(); it!=src->end(); ++it ) {
93 float value = (*it)->value;
94 value = 1 - powf(value,2);
96 dst->fast_simple_add ( (*it)->when, value );
101 generate_db_fade (boost::shared_ptr<Evoral::ControlList> dst, double len, int num_steps, float dB_drop)
104 dst->fast_simple_add (0, 1);
106 //generate a fade-out curve by successively applying a gain drop
107 float fade_speed = dB_to_coefficient(dB_drop / (float) num_steps);
108 for (int i = 1; i < (num_steps-1); i++) {
110 for (int j = 0; j < i; j++) {
113 dst->fast_simple_add (len*(double)i/(double)num_steps, coeff);
116 dst->fast_simple_add (len, VERY_SMALL_SIGNAL);
120 merge_curves (boost::shared_ptr<Evoral::ControlList> dst,
121 boost::shared_ptr<const Evoral::ControlList> curve1,
122 boost::shared_ptr<const Evoral::ControlList> curve2)
124 Evoral::ControlList::EventList::size_type size = curve1->size();
126 //curve lengths must match for now
127 if (size != curve2->size()) {
131 Evoral::ControlList::const_iterator c1 = curve1->begin();
133 for (Evoral::ControlList::const_iterator c2 = curve2->begin(); c2!=curve2->end(); c2++ ) {
134 float v1 = accurate_coefficient_to_dB((*c1)->value);
135 float v2 = accurate_coefficient_to_dB((*c2)->value);
137 double interp = v1 * ( 1.0-( (double)count / (double)size) );
138 interp += v2 * ( (double)count / (double)size );
140 interp = dB_to_coefficient(interp);
141 dst->fast_simple_add ( (*c1)->when, interp );
148 AudioRegion::make_property_quarks ()
150 Properties::envelope_active.property_id = g_quark_from_static_string (X_("envelope-active"));
151 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for envelope-active = %1\n", Properties::envelope_active.property_id));
152 Properties::default_fade_in.property_id = g_quark_from_static_string (X_("default-fade-in"));
153 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for default-fade-in = %1\n", Properties::default_fade_in.property_id));
154 Properties::default_fade_out.property_id = g_quark_from_static_string (X_("default-fade-out"));
155 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for default-fade-out = %1\n", Properties::default_fade_out.property_id));
156 Properties::fade_in_active.property_id = g_quark_from_static_string (X_("fade-in-active"));
157 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fade-in-active = %1\n", Properties::fade_in_active.property_id));
158 Properties::fade_out_active.property_id = g_quark_from_static_string (X_("fade-out-active"));
159 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fade-out-active = %1\n", Properties::fade_out_active.property_id));
160 Properties::scale_amplitude.property_id = g_quark_from_static_string (X_("scale-amplitude"));
161 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for scale-amplitude = %1\n", Properties::scale_amplitude.property_id));
162 Properties::fade_in.property_id = g_quark_from_static_string (X_("FadeIn"));
163 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for FadeIn = %1\n", Properties::fade_in.property_id));
164 Properties::inverse_fade_in.property_id = g_quark_from_static_string (X_("InverseFadeIn"));
165 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for InverseFadeIn = %1\n", Properties::inverse_fade_in.property_id));
166 Properties::fade_out.property_id = g_quark_from_static_string (X_("FadeOut"));
167 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for FadeOut = %1\n", Properties::fade_out.property_id));
168 Properties::inverse_fade_out.property_id = g_quark_from_static_string (X_("InverseFadeOut"));
169 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for InverseFadeOut = %1\n", Properties::inverse_fade_out.property_id));
170 Properties::envelope.property_id = g_quark_from_static_string (X_("Envelope"));
171 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for Envelope = %1\n", Properties::envelope.property_id));
175 AudioRegion::register_properties ()
177 /* no need to register parent class properties */
179 add_property (_envelope_active);
180 add_property (_default_fade_in);
181 add_property (_default_fade_out);
182 add_property (_fade_in_active);
183 add_property (_fade_out_active);
184 add_property (_scale_amplitude);
185 add_property (_fade_in);
186 add_property (_inverse_fade_in);
187 add_property (_fade_out);
188 add_property (_inverse_fade_out);
189 add_property (_envelope);
192 #define AUDIOREGION_STATE_DEFAULT \
193 _envelope_active (Properties::envelope_active, false) \
194 , _default_fade_in (Properties::default_fade_in, true) \
195 , _default_fade_out (Properties::default_fade_out, true) \
196 , _fade_in_active (Properties::fade_in_active, true) \
197 , _fade_out_active (Properties::fade_out_active, true) \
198 , _scale_amplitude (Properties::scale_amplitude, 1.0) \
199 , _fade_in (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation)))) \
200 , _inverse_fade_in (Properties::inverse_fade_in, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation)))) \
201 , _fade_out (Properties::fade_out, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeOutAutomation)))) \
202 , _inverse_fade_out (Properties::inverse_fade_out, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeOutAutomation))))
204 #define AUDIOREGION_COPY_STATE(other) \
205 _envelope_active (Properties::envelope_active, other->_envelope_active) \
206 , _default_fade_in (Properties::default_fade_in, other->_default_fade_in) \
207 , _default_fade_out (Properties::default_fade_out, other->_default_fade_out) \
208 , _fade_in_active (Properties::fade_in_active, other->_fade_in_active) \
209 , _fade_out_active (Properties::fade_out_active, other->_fade_out_active) \
210 , _scale_amplitude (Properties::scale_amplitude, other->_scale_amplitude) \
211 , _fade_in (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_fade_in.val()))) \
212 , _inverse_fade_in (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_inverse_fade_in.val()))) \
213 , _fade_out (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_fade_out.val()))) \
214 , _inverse_fade_out (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_inverse_fade_out.val())))
215 /* a Session will reset these to its chosen defaults by calling AudioRegion::set_default_fade() */
220 register_properties ();
222 suspend_property_changes();
223 set_default_fades ();
224 set_default_envelope ();
225 resume_property_changes();
227 listen_to_my_curves ();
228 connect_to_analysis_changed ();
229 connect_to_header_position_offset_changed ();
232 /** Constructor for use by derived types only */
233 AudioRegion::AudioRegion (Session& s, framepos_t start, framecnt_t len, std::string name)
234 : Region (s, start, len, name, DataType::AUDIO)
235 , AUDIOREGION_STATE_DEFAULT
236 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter(EnvelopeAutomation))))
238 , _fade_in_suspended (0)
239 , _fade_out_suspended (0)
242 assert (_sources.size() == _master_sources.size());
245 /** Basic AudioRegion constructor */
246 AudioRegion::AudioRegion (const SourceList& srcs)
248 , AUDIOREGION_STATE_DEFAULT
249 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter(EnvelopeAutomation))))
250 , _automatable(srcs[0]->session())
251 , _fade_in_suspended (0)
252 , _fade_out_suspended (0)
255 assert (_sources.size() == _master_sources.size());
258 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
260 , AUDIOREGION_COPY_STATE (other)
261 /* As far as I can see, the _envelope's times are relative to region position, and have nothing
262 to do with sources (and hence _start). So when we copy the envelope, we just use the supplied offset.
264 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (*other->_envelope.val(), 0, other->_length)))
265 , _automatable (other->session())
266 , _fade_in_suspended (0)
267 , _fade_out_suspended (0)
269 /* don't use init here, because we got fade in/out from the other region
271 register_properties ();
272 listen_to_my_curves ();
273 connect_to_analysis_changed ();
274 connect_to_header_position_offset_changed ();
276 assert(_type == DataType::AUDIO);
277 assert (_sources.size() == _master_sources.size());
280 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, framecnt_t offset)
281 : Region (other, offset)
282 , AUDIOREGION_COPY_STATE (other)
283 /* As far as I can see, the _envelope's times are relative to region position, and have nothing
284 to do with sources (and hence _start). So when we copy the envelope, we just use the supplied offset.
286 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (*other->_envelope.val(), offset, other->_length)))
287 , _automatable (other->session())
288 , _fade_in_suspended (0)
289 , _fade_out_suspended (0)
291 /* don't use init here, because we got fade in/out from the other region
293 register_properties ();
294 listen_to_my_curves ();
295 connect_to_analysis_changed ();
296 connect_to_header_position_offset_changed ();
298 assert(_type == DataType::AUDIO);
299 assert (_sources.size() == _master_sources.size());
302 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, const SourceList& srcs)
303 : Region (boost::static_pointer_cast<const Region>(other), srcs)
304 , AUDIOREGION_COPY_STATE (other)
305 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (*other->_envelope.val())))
306 , _automatable (other->session())
307 , _fade_in_suspended (0)
308 , _fade_out_suspended (0)
310 /* make-a-sort-of-copy-with-different-sources constructor (used by audio filter) */
312 register_properties ();
314 listen_to_my_curves ();
315 connect_to_analysis_changed ();
316 connect_to_header_position_offset_changed ();
318 assert (_sources.size() == _master_sources.size());
321 AudioRegion::AudioRegion (SourceList& srcs)
323 , AUDIOREGION_STATE_DEFAULT
324 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList(Evoral::Parameter(EnvelopeAutomation))))
325 , _automatable(srcs[0]->session())
326 , _fade_in_suspended (0)
327 , _fade_out_suspended (0)
331 assert(_type == DataType::AUDIO);
332 assert (_sources.size() == _master_sources.size());
335 AudioRegion::~AudioRegion ()
340 AudioRegion::post_set (const PropertyChange& /*ignored*/)
343 _sync_position = _start;
346 /* return to default fades if the existing ones are too long */
348 if (_left_of_split) {
349 if (_fade_in->back()->when >= _length) {
350 set_default_fade_in ();
352 set_default_fade_out ();
353 _left_of_split = false;
356 if (_right_of_split) {
357 if (_fade_out->back()->when >= _length) {
358 set_default_fade_out ();
361 set_default_fade_in ();
362 _right_of_split = false;
365 /* If _length changed, adjust our gain envelope accordingly */
366 _envelope->truncate_end (_length);
370 AudioRegion::connect_to_analysis_changed ()
372 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
373 (*i)->AnalysisChanged.connect_same_thread (*this, boost::bind (&AudioRegion::invalidate_transients, this));
378 AudioRegion::connect_to_header_position_offset_changed ()
380 set<boost::shared_ptr<Source> > unique_srcs;
382 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
384 /* connect only once to HeaderPositionOffsetChanged, even if sources are replicated
387 if (unique_srcs.find (*i) == unique_srcs.end ()) {
388 unique_srcs.insert (*i);
389 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
391 afs->HeaderPositionOffsetChanged.connect_same_thread (*this, boost::bind (&AudioRegion::source_offset_changed, this));
398 AudioRegion::listen_to_my_curves ()
400 _envelope->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::envelope_changed, this));
401 _fade_in->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::fade_in_changed, this));
402 _fade_out->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::fade_out_changed, this));
406 AudioRegion::set_envelope_active (bool yn)
408 if (envelope_active() != yn) {
409 _envelope_active = yn;
410 send_change (PropertyChange (Properties::envelope_active));
414 /** @param buf Buffer to put peak data in.
415 * @param npeaks Number of peaks to read (ie the number of PeakDatas in buf)
416 * @param offset Start position, as an offset from the start of this region's source.
417 * @param cnt Number of samples to read.
418 * @param chan_n Channel.
419 * @param frames_per_pixel Number of samples to use to generate one peak value.
423 AudioRegion::read_peaks (PeakData *buf, framecnt_t npeaks, framecnt_t offset, framecnt_t cnt, uint32_t chan_n, double frames_per_pixel) const
425 if (chan_n >= _sources.size()) {
429 if (audio_source(chan_n)->read_peaks (buf, npeaks, offset, cnt, frames_per_pixel)) {
432 if (_scale_amplitude != 1.0f) {
433 for (framecnt_t n = 0; n < npeaks; ++n) {
434 buf[n].max *= _scale_amplitude;
435 buf[n].min *= _scale_amplitude;
442 /** @param buf Buffer to write data to (existing data will be overwritten).
443 * @param pos Position to read from as an offset from the region position.
444 * @param cnt Number of frames to read.
445 * @param channel Channel to read from.
448 AudioRegion::read (Sample* buf, framepos_t pos, framecnt_t cnt, int channel) const
450 /* raw read, no fades, no gain, nada */
451 return read_from_sources (_sources, _length, buf, _position + pos, cnt, channel);
455 AudioRegion::master_read_at (Sample *buf, Sample* /*mixdown_buffer*/, float* /*gain_buffer*/,
456 framepos_t position, framecnt_t cnt, uint32_t chan_n) const
458 /* do not read gain/scaling/fades and do not count this disk i/o in statistics */
461 return read_from_sources (
462 _master_sources, _master_sources.front()->length (_master_sources.front()->timeline_position()),
463 buf, position, cnt, chan_n
467 /** @param buf Buffer to mix data into.
468 * @param mixdown_buffer Scratch buffer for audio data.
469 * @param gain_buffer Scratch buffer for gain data.
470 * @param position Position within the session to read from.
471 * @param cnt Number of frames to read.
472 * @param chan_n Channel number to read.
475 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
478 uint32_t chan_n) const
480 /* We are reading data from this region into buf (possibly via mixdown_buffer).
481 The caller has verified that we cover the desired section.
484 /* See doc/region_read.svg for a drawing which might help to explain
490 if (n_channels() == 0) {
494 /* WORK OUT WHERE TO GET DATA FROM */
498 assert (position >= _position);
499 frameoffset_t const internal_offset = position - _position;
501 if (internal_offset >= _length) {
502 return 0; /* read nothing */
505 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
506 return 0; /* read nothing */
510 /* COMPUTE DETAILS OF ANY FADES INVOLVED IN THIS READ */
512 /* Amount (length) of fade in that we are dealing with in this read */
513 framecnt_t fade_in_limit = 0;
515 /* Offset from buf / mixdown_buffer of the start
516 of any fade out that we are dealing with
518 frameoffset_t fade_out_offset = 0;
520 /* Amount (length) of fade out that we are dealing with in this read */
521 framecnt_t fade_out_limit = 0;
523 framecnt_t fade_interval_start = 0;
527 if (_fade_in_active && _session.config.get_use_region_fades()) {
529 framecnt_t fade_in_length = (framecnt_t) _fade_in->back()->when;
531 /* see if this read is within the fade in */
533 if (internal_offset < fade_in_length) {
534 fade_in_limit = min (to_read, fade_in_length - internal_offset);
540 if (_fade_out_active && _session.config.get_use_region_fades()) {
542 /* see if some part of this read is within the fade out */
544 /* ................. >| REGION
550 * _length - fade_out_length
554 * ^internal_offset + to_read
556 * we need the intersection of [internal_offset,internal_offset+to_read] with
557 * [_length - fade_out_length, _length]
561 fade_interval_start = max (internal_offset, _length - framecnt_t (_fade_out->back()->when));
562 framecnt_t fade_interval_end = min(internal_offset + to_read, _length.val());
564 if (fade_interval_end > fade_interval_start) {
565 /* (part of the) the fade out is in this buffer */
566 fade_out_limit = fade_interval_end - fade_interval_start;
567 fade_out_offset = fade_interval_start - internal_offset;
571 /* READ DATA FROM THE SOURCE INTO mixdown_buffer.
572 We can never read directly into buf, since it may contain data
573 from a region `below' this one in the stack, and our fades (if they exist)
574 may need to mix with the existing data.
577 if (read_from_sources (_sources, _length, mixdown_buffer, position, to_read, chan_n) != to_read) {
581 /* APPLY REGULAR GAIN CURVES AND SCALING TO mixdown_buffer */
583 if (envelope_active()) {
584 _envelope->curve().get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
586 if (_scale_amplitude != 1.0f) {
587 for (framecnt_t n = 0; n < to_read; ++n) {
588 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
591 for (framecnt_t n = 0; n < to_read; ++n) {
592 mixdown_buffer[n] *= gain_buffer[n];
595 } else if (_scale_amplitude != 1.0f) {
596 apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
599 /* APPLY FADES TO THE DATA IN mixdown_buffer AND MIX THE RESULTS INTO
600 * buf. The key things to realize here: (1) the fade being applied is
601 * (as of April 26th 2012) just the inverse of the fade in curve (2)
602 * "buf" contains data from lower regions already. So this operation
603 * fades out the existing material.
606 if (fade_in_limit != 0) {
609 if (_inverse_fade_in) {
611 /* explicit inverse fade in curve (e.g. for constant
612 * power), so we have to fetch it.
615 _inverse_fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
617 /* Fade the data from lower layers out */
618 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
619 buf[n] *= gain_buffer[n];
622 /* refill gain buffer with the fade in */
624 _fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
628 /* no explicit inverse fade in, so just use (1 - fade
629 * in) for the fade out of lower layers
632 _fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
634 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
635 buf[n] *= 1 - gain_buffer[n];
639 _fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
642 /* Mix our newly-read data in, with the fade */
643 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
644 buf[n] += mixdown_buffer[n] * gain_buffer[n];
648 if (fade_out_limit != 0) {
650 framecnt_t const curve_offset = fade_interval_start - (_length - _fade_out->back()->when);
653 if (_inverse_fade_out) {
655 _inverse_fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
657 /* Fade the data from lower levels in */
658 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
659 buf[m] *= gain_buffer[n];
662 /* fetch the actual fade out */
664 _fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
668 /* no explicit inverse fade out (which is
669 * actually a fade in), so just use (1 - fade
670 * out) for the fade in of lower layers
673 _fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
675 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
676 buf[m] *= 1 - gain_buffer[n];
680 _fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
683 /* Mix our newly-read data with whatever was already there,
684 with the fade out applied to our data.
686 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
687 buf[m] += mixdown_buffer[m] * gain_buffer[n];
691 /* MIX OR COPY THE REGION BODY FROM mixdown_buffer INTO buf */
693 framecnt_t const N = to_read - fade_in_limit - fade_out_limit;
696 DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("Region %1 memcpy into buf @ %2 + %3, from mixdown buffer @ %4 + %5, len = %6 cnt was %7\n",
697 name(), buf, fade_in_limit, mixdown_buffer, fade_in_limit, N, cnt));
698 memcpy (buf + fade_in_limit, mixdown_buffer + fade_in_limit, N * sizeof (Sample));
700 mix_buffers_no_gain (buf + fade_in_limit, mixdown_buffer + fade_in_limit, N);
707 /** Read data directly from one of our sources, accounting for the situation when the track has a different channel
708 * count to the region.
710 * @param srcs Source list to get our source from.
711 * @param limit Furthest that we should read, as an offset from the region position.
712 * @param buf Buffer to write data into (existing contents of the buffer will be overwritten)
713 * @param position Position to read from, in session frames.
714 * @param cnt Number of frames to read.
715 * @param chan_n Channel to read from.
716 * @return Number of frames read.
720 AudioRegion::read_from_sources (SourceList const & srcs, framecnt_t limit, Sample* buf, framepos_t position, framecnt_t cnt, uint32_t chan_n) const
722 frameoffset_t const internal_offset = position - _position;
723 if (internal_offset >= limit) {
727 framecnt_t const to_read = min (cnt, limit - internal_offset);
732 if (chan_n < n_channels()) {
734 boost::shared_ptr<AudioSource> src = boost::dynamic_pointer_cast<AudioSource> (srcs[chan_n]);
735 if (src->read (buf, _start + internal_offset, to_read) != to_read) {
736 return 0; /* "read nothing" */
741 /* track is N-channel, this region has fewer channels; silence the ones
745 if (Config->get_replicate_missing_region_channels()) {
747 /* copy an existing channel's data in for this non-existant one */
749 uint32_t channel = chan_n % n_channels();
750 boost::shared_ptr<AudioSource> src = boost::dynamic_pointer_cast<AudioSource> (srcs[channel]);
752 if (src->read (buf, _start + internal_offset, to_read) != to_read) {
753 return 0; /* "read nothing" */
759 memset (buf, 0, sizeof (Sample) * to_read);
767 AudioRegion::get_basic_state ()
769 XMLNode& node (Region::state ());
771 LocaleGuard lg (X_("C"));
773 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
774 node.add_property ("channels", buf);
780 AudioRegion::state ()
782 XMLNode& node (get_basic_state());
784 LocaleGuard lg (X_("C"));
786 child = node.add_child ("Envelope");
788 bool default_env = false;
790 // If there are only two points, the points are in the start of the region and the end of the region
791 // so, if they are both at 1.0f, that means the default region.
793 if (_envelope->size() == 2 &&
794 _envelope->front()->value == 1.0f &&
795 _envelope->back()->value==1.0f) {
796 if (_envelope->front()->when == 0 && _envelope->back()->when == _length) {
802 child->add_property ("default", "yes");
804 child->add_child_nocopy (_envelope->get_state ());
807 child = node.add_child (X_("FadeIn"));
809 if (_default_fade_in) {
810 child->add_property ("default", "yes");
812 child->add_child_nocopy (_fade_in->get_state ());
815 if (_inverse_fade_in) {
816 child = node.add_child (X_("InverseFadeIn"));
817 child->add_child_nocopy (_inverse_fade_in->get_state ());
820 child = node.add_child (X_("FadeOut"));
822 if (_default_fade_out) {
823 child->add_property ("default", "yes");
825 child->add_child_nocopy (_fade_out->get_state ());
828 if (_inverse_fade_out) {
829 child = node.add_child (X_("InverseFadeOut"));
830 child->add_child_nocopy (_inverse_fade_out->get_state ());
837 AudioRegion::_set_state (const XMLNode& node, int version, PropertyChange& what_changed, bool send)
839 const XMLNodeList& nlist = node.children();
840 const XMLProperty *prop;
841 LocaleGuard lg (X_("C"));
842 boost::shared_ptr<Playlist> the_playlist (_playlist.lock());
844 suspend_property_changes ();
847 the_playlist->freeze ();
851 /* this will set all our State members and stuff controlled by the Region.
852 It should NOT send any changed signals - that is our responsibility.
855 Region::_set_state (node, version, what_changed, false);
857 if ((prop = node.property ("scale-gain")) != 0) {
858 float a = atof (prop->value().c_str());
859 if (a != _scale_amplitude) {
860 _scale_amplitude = a;
861 what_changed.add (Properties::scale_amplitude);
865 /* Now find envelope description and other related child items */
867 _envelope->freeze ();
869 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
875 if (child->name() == "Envelope") {
879 if ((prop = child->property ("default")) != 0 || _envelope->set_state (*child, version)) {
880 set_default_envelope ();
883 _envelope->truncate_end (_length);
886 } else if (child->name() == "FadeIn") {
890 if (((prop = child->property ("default")) != 0 && string_is_affirmative (prop->value())) || (prop = child->property ("steepness")) != 0) {
891 set_default_fade_in ();
893 XMLNode* grandchild = child->child ("AutomationList");
895 _fade_in->set_state (*grandchild, version);
899 if ((prop = child->property ("active")) != 0) {
900 if (string_is_affirmative (prop->value())) {
901 set_fade_in_active (true);
903 set_fade_in_active (false);
907 } else if (child->name() == "FadeOut") {
911 if (((prop = child->property ("default")) != 0 && (string_is_affirmative (prop->value()))) || (prop = child->property ("steepness")) != 0) {
912 set_default_fade_out ();
914 XMLNode* grandchild = child->child ("AutomationList");
916 _fade_out->set_state (*grandchild, version);
920 if ((prop = child->property ("active")) != 0) {
921 if (string_is_affirmative (prop->value())) {
922 set_fade_out_active (true);
924 set_fade_out_active (false);
928 } else if ( (child->name() == "InverseFadeIn") || (child->name() == "InvFadeIn") ) {
929 XMLNode* grandchild = child->child ("AutomationList");
931 _inverse_fade_in->set_state (*grandchild, version);
933 } else if ( (child->name() == "InverseFadeOut") || (child->name() == "InvFadeOut") ) {
934 XMLNode* grandchild = child->child ("AutomationList");
936 _inverse_fade_out->set_state (*grandchild, version);
942 resume_property_changes ();
945 send_change (what_changed);
949 the_playlist->thaw ();
956 AudioRegion::set_state (const XMLNode& node, int version)
958 PropertyChange what_changed;
959 return _set_state (node, version, what_changed, true);
963 AudioRegion::fade_range (framepos_t start, framepos_t end)
967 switch (coverage (start, end)) {
968 case Evoral::OverlapStart:
971 set_fade_in (FadeConstantPower, e - s);
973 case Evoral::OverlapEnd:
975 e = _position + _length;
976 set_fade_out (FadeConstantPower, e - s);
978 case Evoral::OverlapInternal:
979 /* needs addressing, perhaps. Difficult to do if we can't
980 * control one edge of the fade relative to the relevant edge
981 * of the region, which we cannot - fades are currently assumed
982 * to start/end at the start/end of the region
991 AudioRegion::set_fade_in_shape (FadeShape shape)
993 set_fade_in (shape, (framecnt_t) _fade_in->back()->when);
997 AudioRegion::set_fade_out_shape (FadeShape shape)
999 set_fade_out (shape, (framecnt_t) _fade_out->back()->when);
1003 AudioRegion::set_fade_in (boost::shared_ptr<AutomationList> f)
1005 _fade_in->freeze ();
1006 *(_fade_in.val()) = *f;
1008 _default_fade_in = false;
1010 send_change (PropertyChange (Properties::fade_in));
1014 AudioRegion::set_fade_in (FadeShape shape, framecnt_t len)
1016 const ARDOUR::ParameterDescriptor desc(FadeInAutomation);
1017 boost::shared_ptr<Evoral::ControlList> c1 (new Evoral::ControlList (FadeInAutomation, desc));
1018 boost::shared_ptr<Evoral::ControlList> c2 (new Evoral::ControlList (FadeInAutomation, desc));
1019 boost::shared_ptr<Evoral::ControlList> c3 (new Evoral::ControlList (FadeInAutomation, desc));
1021 _fade_in->freeze ();
1023 _inverse_fade_in->clear ();
1025 const int num_steps = 32;
1029 _fade_in->fast_simple_add (0.0, VERY_SMALL_SIGNAL);
1030 _fade_in->fast_simple_add (len, 1.0);
1031 reverse_curve (_inverse_fade_in.val(), _fade_in.val());
1035 generate_db_fade (_fade_in.val(), len, num_steps, -60);
1036 reverse_curve (c1, _fade_in.val());
1037 _fade_in->copy_events (*c1);
1038 generate_inverse_power_curve (_inverse_fade_in.val(), _fade_in.val());
1042 generate_db_fade (c1, len, num_steps, -1); // start off with a slow fade
1043 generate_db_fade (c2, len, num_steps, -80); // end with a fast fade
1044 merge_curves (_fade_in.val(), c1, c2);
1045 reverse_curve (c3, _fade_in.val());
1046 _fade_in->copy_events (*c3);
1047 generate_inverse_power_curve (_inverse_fade_in.val(), _fade_in.val());
1050 case FadeConstantPower:
1051 _fade_in->fast_simple_add (0.0, VERY_SMALL_SIGNAL);
1052 for (int i = 1; i < num_steps; ++i) {
1053 const float dist = i / (num_steps + 1.f);
1054 _fade_in->fast_simple_add (len * dist, sin (dist * M_PI / 2.0));
1056 _fade_in->fast_simple_add (len, 1.0);
1057 reverse_curve (_inverse_fade_in.val(), _fade_in.val());
1061 //start with a nearly linear cuve
1062 _fade_in->fast_simple_add (0, 1);
1063 _fade_in->fast_simple_add (0.5 * len, 0.6);
1064 //now generate a fade-out curve by successively applying a gain drop
1065 const double breakpoint = 0.7; //linear for first 70%
1066 for (int i = 2; i < 9; ++i) {
1067 const float coeff = (1.f - breakpoint) * powf (0.5, i);
1068 _fade_in->fast_simple_add (len * (breakpoint + ((1.0 - breakpoint) * (double)i / 9.0)), coeff);
1070 _fade_in->fast_simple_add (len, VERY_SMALL_SIGNAL);
1071 reverse_curve (c3, _fade_in.val());
1072 _fade_in->copy_events (*c3);
1073 reverse_curve (_inverse_fade_in.val(), _fade_in.val());
1077 _fade_in->set_interpolation(Evoral::ControlList::Curved);
1078 _inverse_fade_in->set_interpolation(Evoral::ControlList::Curved);
1080 _default_fade_in = false;
1082 send_change (PropertyChange (Properties::fade_in));
1086 AudioRegion::set_fade_out (boost::shared_ptr<AutomationList> f)
1088 _fade_out->freeze ();
1089 *(_fade_out.val()) = *f;
1091 _default_fade_out = false;
1093 send_change (PropertyChange (Properties::fade_in));
1097 AudioRegion::set_fade_out (FadeShape shape, framecnt_t len)
1099 const ARDOUR::ParameterDescriptor desc(FadeOutAutomation);
1100 boost::shared_ptr<Evoral::ControlList> c1 (new Evoral::ControlList (FadeOutAutomation, desc));
1101 boost::shared_ptr<Evoral::ControlList> c2 (new Evoral::ControlList (FadeOutAutomation, desc));
1103 _fade_out->freeze ();
1104 _fade_out->clear ();
1105 _inverse_fade_out->clear ();
1107 const int num_steps = 32;
1111 _fade_out->fast_simple_add (0.0, 1.0);
1112 _fade_out->fast_simple_add (len, VERY_SMALL_SIGNAL);
1113 reverse_curve (_inverse_fade_out.val(), _fade_out.val());
1117 generate_db_fade (_fade_out.val(), len, num_steps, -60);
1118 generate_inverse_power_curve (_inverse_fade_out.val(), _fade_out.val());
1122 generate_db_fade (c1, len, num_steps, -1); //start off with a slow fade
1123 generate_db_fade (c2, len, num_steps, -80); //end with a fast fade
1124 merge_curves (_fade_out.val(), c1, c2);
1125 generate_inverse_power_curve (_inverse_fade_out.val(), _fade_out.val());
1128 case FadeConstantPower:
1129 //constant-power fades use a sin/cos relationship
1130 //the cutoff is abrupt but it has the benefit of being symmetrical
1131 _fade_out->fast_simple_add (0.0, 1.0);
1132 for (int i = 1; i < num_steps; ++i) {
1133 const float dist = i / (num_steps + 1.f);
1134 _fade_out->fast_simple_add (len * dist, cos (dist * M_PI / 2.0));
1136 _fade_out->fast_simple_add (len, VERY_SMALL_SIGNAL);
1137 reverse_curve (_inverse_fade_out.val(), _fade_out.val());
1141 //start with a nearly linear cuve
1142 _fade_out->fast_simple_add (0, 1);
1143 _fade_out->fast_simple_add (0.5 * len, 0.6);
1144 //now generate a fade-out curve by successively applying a gain drop
1145 const double breakpoint = 0.7; //linear for first 70%
1146 for (int i = 2; i < 9; ++i) {
1147 const float coeff = (1.f - breakpoint) * powf (0.5, i);
1148 _fade_out->fast_simple_add (len * (breakpoint + ((1.0 - breakpoint) * (double)i / 9.0)), coeff);
1150 _fade_out->fast_simple_add (len, VERY_SMALL_SIGNAL);
1151 reverse_curve (_inverse_fade_out.val(), _fade_out.val());
1155 _fade_out->set_interpolation(Evoral::ControlList::Curved);
1156 _inverse_fade_out->set_interpolation(Evoral::ControlList::Curved);
1158 _default_fade_out = false;
1160 send_change (PropertyChange (Properties::fade_out));
1164 AudioRegion::set_fade_in_length (framecnt_t len)
1166 if (len > _length) {
1174 bool changed = _fade_in->extend_to (len);
1177 if (_inverse_fade_in) {
1178 _inverse_fade_in->extend_to (len);
1181 _default_fade_in = false;
1182 send_change (PropertyChange (Properties::fade_in));
1187 AudioRegion::set_fade_out_length (framecnt_t len)
1189 if (len > _length) {
1197 bool changed = _fade_out->extend_to (len);
1201 if (_inverse_fade_out) {
1202 _inverse_fade_out->extend_to (len);
1204 _default_fade_out = false;
1206 send_change (PropertyChange (Properties::fade_out));
1211 AudioRegion::set_fade_in_active (bool yn)
1213 if (yn == _fade_in_active) {
1217 _fade_in_active = yn;
1218 send_change (PropertyChange (Properties::fade_in_active));
1222 AudioRegion::set_fade_out_active (bool yn)
1224 if (yn == _fade_out_active) {
1227 _fade_out_active = yn;
1228 send_change (PropertyChange (Properties::fade_out_active));
1232 AudioRegion::fade_in_is_default () const
1234 return _fade_in->size() == 2 && _fade_in->front()->when == 0 && _fade_in->back()->when == 64;
1238 AudioRegion::fade_out_is_default () const
1240 return _fade_out->size() == 2 && _fade_out->front()->when == 0 && _fade_out->back()->when == 64;
1244 AudioRegion::set_default_fade_in ()
1246 _fade_in_suspended = 0;
1247 set_fade_in (Config->get_default_fade_shape(), 64);
1251 AudioRegion::set_default_fade_out ()
1253 _fade_out_suspended = 0;
1254 set_fade_out (Config->get_default_fade_shape(), 64);
1258 AudioRegion::set_default_fades ()
1260 set_default_fade_in ();
1261 set_default_fade_out ();
1265 AudioRegion::set_default_envelope ()
1267 _envelope->freeze ();
1268 _envelope->clear ();
1269 _envelope->fast_simple_add (0, 1.0f);
1270 _envelope->fast_simple_add (_length, 1.0f);
1275 AudioRegion::recompute_at_end ()
1277 /* our length has changed. recompute a new final point by interpolating
1278 based on the the existing curve.
1281 _envelope->freeze ();
1282 _envelope->truncate_end (_length);
1285 suspend_property_changes();
1287 if (_left_of_split) {
1288 set_default_fade_out ();
1289 _left_of_split = false;
1290 } else if (_fade_out->back()->when > _length) {
1291 _fade_out->extend_to (_length);
1292 send_change (PropertyChange (Properties::fade_out));
1295 if (_fade_in->back()->when > _length) {
1296 _fade_in->extend_to (_length);
1297 send_change (PropertyChange (Properties::fade_in));
1300 resume_property_changes();
1304 AudioRegion::recompute_at_start ()
1306 /* as above, but the shift was from the front */
1308 _envelope->truncate_start (_length);
1310 suspend_property_changes();
1312 if (_right_of_split) {
1313 set_default_fade_in ();
1314 _right_of_split = false;
1315 } else if (_fade_in->back()->when > _length) {
1316 _fade_in->extend_to (_length);
1317 send_change (PropertyChange (Properties::fade_in));
1320 if (_fade_out->back()->when > _length) {
1321 _fade_out->extend_to (_length);
1322 send_change (PropertyChange (Properties::fade_out));
1325 resume_property_changes();
1329 AudioRegion::separate_by_channel (Session& /*session*/, vector<boost::shared_ptr<Region> >& v) const
1335 if (_sources.size() < 2) {
1339 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
1341 srcs.push_back (*i);
1345 if (_sources.size() == 2) {
1353 new_name += ('0' + n + 1);
1356 /* create a copy with just one source. prevent if from being thought of as
1357 "whole file" even if it covers the entire source file(s).
1362 plist.add (Properties::start, _start.val());
1363 plist.add (Properties::length, _length.val());
1364 plist.add (Properties::name, new_name);
1365 plist.add (Properties::layer, layer ());
1367 v.push_back(RegionFactory::create (srcs, plist));
1368 v.back()->set_whole_file (false);
1377 AudioRegion::read_raw_internal (Sample* buf, framepos_t pos, framecnt_t cnt, int channel) const
1379 return audio_source(channel)->read (buf, pos, cnt);
1383 AudioRegion::set_scale_amplitude (gain_t g)
1385 boost::shared_ptr<Playlist> pl (playlist());
1387 _scale_amplitude = g;
1389 /* tell the diskstream we're in */
1392 pl->ContentsChanged();
1395 /* tell everybody else */
1397 send_change (PropertyChange (Properties::scale_amplitude));
1400 /** @return the maximum (linear) amplitude of the region, or a -ve
1401 * number if the Progress object reports that the process was cancelled.
1404 AudioRegion::maximum_amplitude (Progress* p) const
1406 framepos_t fpos = _start;
1407 framepos_t const fend = _start + _length;
1410 framecnt_t const blocksize = 64 * 1024;
1411 Sample buf[blocksize];
1413 while (fpos < fend) {
1417 framecnt_t const to_read = min (fend - fpos, blocksize);
1419 for (n = 0; n < n_channels(); ++n) {
1423 if (read_raw_internal (buf, fpos, to_read, n) != to_read) {
1427 maxamp = compute_peak (buf, to_read, maxamp);
1432 p->set_progress (float (fpos - _start) / _length);
1433 if (p->cancelled ()) {
1442 /** Normalize using a given maximum amplitude and target, so that region
1443 * _scale_amplitude becomes target / max_amplitude.
1446 AudioRegion::normalize (float max_amplitude, float target_dB)
1448 gain_t target = dB_to_coefficient (target_dB);
1450 if (target == 1.0f) {
1451 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1452 that we may have clipped.
1454 target -= FLT_EPSILON;
1457 if (max_amplitude == 0.0f) {
1458 /* don't even try */
1462 if (max_amplitude == target) {
1463 /* we can't do anything useful */
1467 set_scale_amplitude (target / max_amplitude);
1471 AudioRegion::fade_in_changed ()
1473 send_change (PropertyChange (Properties::fade_in));
1477 AudioRegion::fade_out_changed ()
1479 send_change (PropertyChange (Properties::fade_out));
1483 AudioRegion::envelope_changed ()
1485 send_change (PropertyChange (Properties::envelope));
1489 AudioRegion::suspend_fade_in ()
1491 if (++_fade_in_suspended == 1) {
1492 if (fade_in_is_default()) {
1493 set_fade_in_active (false);
1499 AudioRegion::resume_fade_in ()
1501 if (--_fade_in_suspended == 0 && _fade_in_suspended) {
1502 set_fade_in_active (true);
1507 AudioRegion::suspend_fade_out ()
1509 if (++_fade_out_suspended == 1) {
1510 if (fade_out_is_default()) {
1511 set_fade_out_active (false);
1517 AudioRegion::resume_fade_out ()
1519 if (--_fade_out_suspended == 0 &&_fade_out_suspended) {
1520 set_fade_out_active (true);
1525 AudioRegion::speed_mismatch (float sr) const
1527 if (_sources.empty()) {
1528 /* impossible, but ... */
1532 float fsr = audio_source()->sample_rate();
1538 AudioRegion::source_offset_changed ()
1540 /* XXX this fixes a crash that should not occur. It does occur
1541 becauses regions are not being deleted when a session
1542 is unloaded. That bug must be fixed.
1545 if (_sources.empty()) {
1549 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(_sources.front());
1551 if (afs && afs->destructive()) {
1552 // set_start (source()->natural_position(), this);
1553 set_position (source()->natural_position());
1557 boost::shared_ptr<AudioSource>
1558 AudioRegion::audio_source (uint32_t n) const
1560 // Guaranteed to succeed (use a static cast for speed?)
1561 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1565 AudioRegion::adjust_transients (frameoffset_t delta)
1567 for (AnalysisFeatureList::iterator x = _transients.begin(); x != _transients.end(); ++x) {
1568 (*x) = (*x) + delta;
1571 send_change (PropertyChange (Properties::valid_transients));
1577 AudioRegion::update_transient (framepos_t old_position, framepos_t new_position)
1579 for (AnalysisFeatureList::iterator x = _transients.begin(); x != _transients.end(); ++x) {
1580 if ((*x) == old_position) {
1581 (*x) = new_position;
1582 send_change (PropertyChange (Properties::valid_transients));
1592 AudioRegion::add_transient (framepos_t where)
1594 _transients.push_back(where);
1595 _valid_transients = true;
1597 send_change (PropertyChange (Properties::valid_transients));
1601 AudioRegion::remove_transient (framepos_t where)
1603 _transients.remove(where);
1604 _valid_transients = true;
1606 send_change (PropertyChange (Properties::valid_transients));
1610 AudioRegion::set_transients (AnalysisFeatureList& results)
1612 _transients.clear();
1613 _transients = results;
1614 _valid_transients = true;
1616 send_change (PropertyChange (Properties::valid_transients));
1622 AudioRegion::get_transients (AnalysisFeatureList& results, bool force_new)
1624 boost::shared_ptr<Playlist> pl = playlist();
1630 if (_valid_transients && !force_new) {
1631 results = _transients;
1635 SourceList::iterator s;
1637 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1638 if (!(*s)->has_been_analysed()) {
1639 cerr << "For " << name() << " source " << (*s)->name() << " has not been analyzed\n";
1644 if (s == _sources.end()) {
1645 /* all sources are analyzed, merge data from each one */
1647 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1649 /* find the set of transients within the bounds of this region */
1651 AnalysisFeatureList::iterator low = lower_bound ((*s)->transients.begin(),
1652 (*s)->transients.end(),
1655 AnalysisFeatureList::iterator high = upper_bound ((*s)->transients.begin(),
1656 (*s)->transients.end(),
1661 results.insert (results.end(), low, high);
1664 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1666 /* translate all transients to current position */
1668 for (AnalysisFeatureList::iterator x = results.begin(); x != results.end(); ++x) {
1673 _transients = results;
1674 _valid_transients = true;
1679 /* no existing/complete transient info */
1681 static bool analyse_dialog_shown = false; /* global per instance of Ardour */
1683 if (!Config->get_auto_analyse_audio()) {
1684 if (!analyse_dialog_shown) {
1685 pl->session().Dialog (string_compose (_("\
1686 You have requested an operation that requires audio analysis.\n\n\
1687 You currently have \"auto-analyse-audio\" disabled, which means \
1688 that transient data must be generated every time it is required.\n\n\
1689 If you are doing work that will require transient data on a \
1690 regular basis, you should probably enable \"auto-analyse-audio\" \
1691 then quit %1 and restart.\n\n\
1692 This dialog will not display again. But you may notice a slight delay \
1693 in this and future transient-detection operations.\n\
1695 analyse_dialog_shown = true;
1699 bool existing_results = !results.empty();
1703 TransientDetector t (pl->session().frame_rate());
1705 _transients.clear ();
1706 _valid_transients = false;
1708 for (uint32_t i = 0; i < n_channels(); ++i) {
1710 AnalysisFeatureList these_results;
1714 if (t.run ("", this, i, these_results)) {
1718 /* translate all transients to give absolute position */
1720 for (AnalysisFeatureList::iterator i = these_results.begin(); i != these_results.end(); ++i) {
1726 _transients.insert (_transients.end(), these_results.begin(), these_results.end());
1729 error << string_compose(_("Transient Analysis failed for %1."), _("Audio Region")) << endmsg;
1733 if (!results.empty()) {
1734 if (existing_results) {
1736 /* merge our transients into the existing ones, then clean up
1740 results.insert (results.end(), _transients.begin(), _transients.end());
1741 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1744 /* make sure ours are clean too */
1746 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1750 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1751 results = _transients;
1754 _valid_transients = true;
1759 /** Find areas of `silence' within a region.
1761 * @param threshold Threshold below which signal is considered silence (as a sample value)
1762 * @param min_length Minimum length of silent period to be reported.
1763 * @return Silent intervals, measured relative to the region start in the source
1767 AudioRegion::find_silence (Sample threshold, framecnt_t min_length, InterThreadInfo& itt) const
1769 framecnt_t const block_size = 64 * 1024;
1770 boost::scoped_array<Sample> loudest (new Sample[block_size]);
1771 boost::scoped_array<Sample> buf (new Sample[block_size]);
1773 framepos_t pos = _start;
1774 framepos_t const end = _start + _length - 1;
1776 AudioIntervalResult silent_periods;
1778 bool in_silence = false;
1779 frameoffset_t silence_start = 0;
1781 while (pos < end && !itt.cancel) {
1783 /* fill `loudest' with the loudest absolute sample at each instant, across all channels */
1784 memset (loudest.get(), 0, sizeof (Sample) * block_size);
1785 for (uint32_t n = 0; n < n_channels(); ++n) {
1787 read_raw_internal (buf.get(), pos, block_size, n);
1788 for (framecnt_t i = 0; i < block_size; ++i) {
1789 loudest[i] = max (loudest[i], abs (buf[i]));
1793 /* now look for silence */
1794 for (framecnt_t i = 0; i < block_size; ++i) {
1795 bool const silence = abs (loudest[i]) < threshold;
1796 if (silence && !in_silence) {
1797 /* non-silence to silence */
1799 silence_start = pos + i;
1800 } else if (!silence && in_silence) {
1801 /* silence to non-silence */
1803 if (pos + i - 1 - silence_start >= min_length) {
1804 silent_periods.push_back (std::make_pair (silence_start, pos + i - 1));
1810 itt.progress = (end-pos)/(double)_length;
1813 if (in_silence && end - 1 - silence_start >= min_length) {
1814 /* last block was silent, so finish off the last period */
1815 silent_periods.push_back (std::make_pair (silence_start, end));
1820 return silent_periods;
1823 Evoral::Range<framepos_t>
1824 AudioRegion::body_range () const
1826 return Evoral::Range<framepos_t> (first_frame() + _fade_in->back()->when + 1, last_frame() - _fade_out->back()->when);
1829 boost::shared_ptr<Region>
1830 AudioRegion::get_single_other_xfade_region (bool start) const
1832 boost::shared_ptr<Playlist> pl (playlist());
1835 /* not currently in a playlist - xfade length is unbounded
1838 return boost::shared_ptr<AudioRegion> ();
1841 boost::shared_ptr<RegionList> rl;
1844 rl = pl->regions_at (position());
1846 rl = pl->regions_at (last_frame());
1849 RegionList::iterator i;
1850 boost::shared_ptr<Region> other;
1853 /* count and find the other region in a single pass through the list */
1855 for (i = rl->begin(); i != rl->end(); ++i) {
1856 if ((*i).get() != this) {
1863 /* zero or multiple regions stacked here - don't care about xfades */
1864 return boost::shared_ptr<AudioRegion> ();
1871 AudioRegion::verify_xfade_bounds (framecnt_t len, bool start)
1873 /* this is called from a UI to check on whether a new proposed
1874 length for an xfade is legal or not. it returns the legal
1875 length corresponding to @a len which may be shorter than or
1876 equal to @a len itself.
1879 boost::shared_ptr<Region> other = get_single_other_xfade_region (start);
1883 /* zero or > 2 regions here, don't care about len, but
1884 it can't be longer than the region itself.
1886 return min (length(), len);
1889 /* we overlap a single region. clamp the length of an xfade to
1890 the maximum possible duration of the overlap (if the other
1891 region were trimmed appropriately).
1895 maxlen = other->latest_possible_frame() - position();
1897 maxlen = last_frame() - other->earliest_possible_position();
1900 return min (length(), min (maxlen, len));