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"
52 #include "ardour/sndfilesource.h"
53 #include "ardour/coreaudiosource.h"
59 using namespace ARDOUR;
63 namespace Properties {
64 PBD::PropertyDescriptor<bool> envelope_active;
65 PBD::PropertyDescriptor<bool> default_fade_in;
66 PBD::PropertyDescriptor<bool> default_fade_out;
67 PBD::PropertyDescriptor<bool> fade_in_active;
68 PBD::PropertyDescriptor<bool> fade_out_active;
69 PBD::PropertyDescriptor<float> scale_amplitude;
70 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > fade_in;
71 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > inverse_fade_in;
72 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > fade_out;
73 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > inverse_fade_out;
74 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > envelope;
78 /* Curve manipulations */
81 reverse_curve (boost::shared_ptr<Evoral::ControlList> dst, boost::shared_ptr<const Evoral::ControlList> src)
83 size_t len = src->back()->when;
84 for (Evoral::ControlList::const_reverse_iterator it = src->rbegin(); it!=src->rend(); it++) {
85 dst->fast_simple_add (len - (*it)->when, (*it)->value);
90 generate_inverse_power_curve (boost::shared_ptr<Evoral::ControlList> dst, boost::shared_ptr<const Evoral::ControlList> src)
92 // calc inverse curve using sum of squares
93 for (Evoral::ControlList::const_iterator it = src->begin(); it!=src->end(); ++it ) {
94 float value = (*it)->value;
95 value = 1 - powf(value,2);
97 dst->fast_simple_add ( (*it)->when, value );
102 generate_db_fade (boost::shared_ptr<Evoral::ControlList> dst, double len, int num_steps, float dB_drop)
105 dst->fast_simple_add (0, 1);
107 //generate a fade-out curve by successively applying a gain drop
108 float fade_speed = dB_to_coefficient(dB_drop / (float) num_steps);
109 for (int i = 1; i < (num_steps-1); i++) {
110 float coeff = GAIN_COEFF_UNITY;
111 for (int j = 0; j < i; j++) {
114 dst->fast_simple_add (len*(double)i/(double)num_steps, coeff);
117 dst->fast_simple_add (len, GAIN_COEFF_SMALL);
121 merge_curves (boost::shared_ptr<Evoral::ControlList> dst,
122 boost::shared_ptr<const Evoral::ControlList> curve1,
123 boost::shared_ptr<const Evoral::ControlList> curve2)
125 Evoral::ControlList::EventList::size_type size = curve1->size();
127 //curve lengths must match for now
128 if (size != curve2->size()) {
132 Evoral::ControlList::const_iterator c1 = curve1->begin();
134 for (Evoral::ControlList::const_iterator c2 = curve2->begin(); c2!=curve2->end(); c2++ ) {
135 float v1 = accurate_coefficient_to_dB((*c1)->value);
136 float v2 = accurate_coefficient_to_dB((*c2)->value);
138 double interp = v1 * ( 1.0-( (double)count / (double)size) );
139 interp += v2 * ( (double)count / (double)size );
141 interp = dB_to_coefficient(interp);
142 dst->fast_simple_add ( (*c1)->when, interp );
149 AudioRegion::make_property_quarks ()
151 Properties::envelope_active.property_id = g_quark_from_static_string (X_("envelope-active"));
152 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for envelope-active = %1\n", Properties::envelope_active.property_id));
153 Properties::default_fade_in.property_id = g_quark_from_static_string (X_("default-fade-in"));
154 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for default-fade-in = %1\n", Properties::default_fade_in.property_id));
155 Properties::default_fade_out.property_id = g_quark_from_static_string (X_("default-fade-out"));
156 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for default-fade-out = %1\n", Properties::default_fade_out.property_id));
157 Properties::fade_in_active.property_id = g_quark_from_static_string (X_("fade-in-active"));
158 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fade-in-active = %1\n", Properties::fade_in_active.property_id));
159 Properties::fade_out_active.property_id = g_quark_from_static_string (X_("fade-out-active"));
160 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fade-out-active = %1\n", Properties::fade_out_active.property_id));
161 Properties::scale_amplitude.property_id = g_quark_from_static_string (X_("scale-amplitude"));
162 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for scale-amplitude = %1\n", Properties::scale_amplitude.property_id));
163 Properties::fade_in.property_id = g_quark_from_static_string (X_("FadeIn"));
164 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for FadeIn = %1\n", Properties::fade_in.property_id));
165 Properties::inverse_fade_in.property_id = g_quark_from_static_string (X_("InverseFadeIn"));
166 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for InverseFadeIn = %1\n", Properties::inverse_fade_in.property_id));
167 Properties::fade_out.property_id = g_quark_from_static_string (X_("FadeOut"));
168 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for FadeOut = %1\n", Properties::fade_out.property_id));
169 Properties::inverse_fade_out.property_id = g_quark_from_static_string (X_("InverseFadeOut"));
170 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for InverseFadeOut = %1\n", Properties::inverse_fade_out.property_id));
171 Properties::envelope.property_id = g_quark_from_static_string (X_("Envelope"));
172 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for Envelope = %1\n", Properties::envelope.property_id));
176 AudioRegion::register_properties ()
178 /* no need to register parent class properties */
180 add_property (_envelope_active);
181 add_property (_default_fade_in);
182 add_property (_default_fade_out);
183 add_property (_fade_in_active);
184 add_property (_fade_out_active);
185 add_property (_scale_amplitude);
186 add_property (_fade_in);
187 add_property (_inverse_fade_in);
188 add_property (_fade_out);
189 add_property (_inverse_fade_out);
190 add_property (_envelope);
193 #define AUDIOREGION_STATE_DEFAULT \
194 _envelope_active (Properties::envelope_active, false) \
195 , _default_fade_in (Properties::default_fade_in, true) \
196 , _default_fade_out (Properties::default_fade_out, true) \
197 , _fade_in_active (Properties::fade_in_active, true) \
198 , _fade_out_active (Properties::fade_out_active, true) \
199 , _scale_amplitude (Properties::scale_amplitude, 1.0) \
200 , _fade_in (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation)))) \
201 , _inverse_fade_in (Properties::inverse_fade_in, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation)))) \
202 , _fade_out (Properties::fade_out, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeOutAutomation)))) \
203 , _inverse_fade_out (Properties::inverse_fade_out, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeOutAutomation))))
205 #define AUDIOREGION_COPY_STATE(other) \
206 _envelope_active (Properties::envelope_active, other->_envelope_active) \
207 , _default_fade_in (Properties::default_fade_in, other->_default_fade_in) \
208 , _default_fade_out (Properties::default_fade_out, other->_default_fade_out) \
209 , _fade_in_active (Properties::fade_in_active, other->_fade_in_active) \
210 , _fade_out_active (Properties::fade_out_active, other->_fade_out_active) \
211 , _scale_amplitude (Properties::scale_amplitude, other->_scale_amplitude) \
212 , _fade_in (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_fade_in.val()))) \
213 , _inverse_fade_in (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_inverse_fade_in.val()))) \
214 , _fade_out (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_fade_out.val()))) \
215 , _inverse_fade_out (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_inverse_fade_out.val())))
216 /* a Session will reset these to its chosen defaults by calling AudioRegion::set_default_fade() */
221 register_properties ();
223 suspend_property_changes();
224 set_default_fades ();
225 set_default_envelope ();
226 resume_property_changes();
228 listen_to_my_curves ();
229 connect_to_analysis_changed ();
230 connect_to_header_position_offset_changed ();
233 /** Constructor for use by derived types only */
234 AudioRegion::AudioRegion (Session& s, framepos_t start, framecnt_t len, std::string name)
235 : Region (s, start, len, name, DataType::AUDIO)
236 , AUDIOREGION_STATE_DEFAULT
237 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter(EnvelopeAutomation))))
239 , _fade_in_suspended (0)
240 , _fade_out_suspended (0)
243 assert (_sources.size() == _master_sources.size());
246 /** Basic AudioRegion constructor */
247 AudioRegion::AudioRegion (const SourceList& srcs)
249 , AUDIOREGION_STATE_DEFAULT
250 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter(EnvelopeAutomation))))
251 , _automatable(srcs[0]->session())
252 , _fade_in_suspended (0)
253 , _fade_out_suspended (0)
256 assert (_sources.size() == _master_sources.size());
259 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
261 , AUDIOREGION_COPY_STATE (other)
262 /* As far as I can see, the _envelope's times are relative to region position, and have nothing
263 to do with sources (and hence _start). So when we copy the envelope, we just use the supplied offset.
265 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (*other->_envelope.val(), 0, other->_length)))
266 , _automatable (other->session())
267 , _fade_in_suspended (0)
268 , _fade_out_suspended (0)
270 /* don't use init here, because we got fade in/out from the other region
272 register_properties ();
273 listen_to_my_curves ();
274 connect_to_analysis_changed ();
275 connect_to_header_position_offset_changed ();
277 assert(_type == DataType::AUDIO);
278 assert (_sources.size() == _master_sources.size());
281 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, framecnt_t offset)
282 : Region (other, offset)
283 , AUDIOREGION_COPY_STATE (other)
284 /* As far as I can see, the _envelope's times are relative to region position, and have nothing
285 to do with sources (and hence _start). So when we copy the envelope, we just use the supplied offset.
287 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (*other->_envelope.val(), offset, other->_length)))
288 , _automatable (other->session())
289 , _fade_in_suspended (0)
290 , _fade_out_suspended (0)
292 /* don't use init here, because we got fade in/out from the other region
294 register_properties ();
295 listen_to_my_curves ();
296 connect_to_analysis_changed ();
297 connect_to_header_position_offset_changed ();
299 assert(_type == DataType::AUDIO);
300 assert (_sources.size() == _master_sources.size());
303 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, const SourceList& srcs)
304 : Region (boost::static_pointer_cast<const Region>(other), srcs)
305 , AUDIOREGION_COPY_STATE (other)
306 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (*other->_envelope.val())))
307 , _automatable (other->session())
308 , _fade_in_suspended (0)
309 , _fade_out_suspended (0)
311 /* make-a-sort-of-copy-with-different-sources constructor (used by audio filter) */
313 register_properties ();
315 listen_to_my_curves ();
316 connect_to_analysis_changed ();
317 connect_to_header_position_offset_changed ();
319 assert (_sources.size() == _master_sources.size());
322 AudioRegion::AudioRegion (SourceList& srcs)
324 , AUDIOREGION_STATE_DEFAULT
325 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList(Evoral::Parameter(EnvelopeAutomation))))
326 , _automatable(srcs[0]->session())
327 , _fade_in_suspended (0)
328 , _fade_out_suspended (0)
332 assert(_type == DataType::AUDIO);
333 assert (_sources.size() == _master_sources.size());
336 AudioRegion::~AudioRegion ()
341 AudioRegion::post_set (const PropertyChange& /*ignored*/)
344 _sync_position = _start;
347 /* return to default fades if the existing ones are too long */
349 if (_left_of_split) {
350 if (_fade_in->back()->when >= _length) {
351 set_default_fade_in ();
353 set_default_fade_out ();
354 _left_of_split = false;
357 if (_right_of_split) {
358 if (_fade_out->back()->when >= _length) {
359 set_default_fade_out ();
362 set_default_fade_in ();
363 _right_of_split = false;
366 /* If _length changed, adjust our gain envelope accordingly */
367 _envelope->truncate_end (_length);
371 AudioRegion::connect_to_analysis_changed ()
373 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
374 (*i)->AnalysisChanged.connect_same_thread (*this, boost::bind (&AudioRegion::invalidate_transients, this));
379 AudioRegion::connect_to_header_position_offset_changed ()
381 set<boost::shared_ptr<Source> > unique_srcs;
383 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
385 /* connect only once to HeaderPositionOffsetChanged, even if sources are replicated
388 if (unique_srcs.find (*i) == unique_srcs.end ()) {
389 unique_srcs.insert (*i);
390 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
392 afs->HeaderPositionOffsetChanged.connect_same_thread (*this, boost::bind (&AudioRegion::source_offset_changed, this));
399 AudioRegion::listen_to_my_curves ()
401 _envelope->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::envelope_changed, this));
402 _fade_in->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::fade_in_changed, this));
403 _fade_out->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::fade_out_changed, this));
407 AudioRegion::set_envelope_active (bool yn)
409 if (envelope_active() != yn) {
410 _envelope_active = yn;
411 send_change (PropertyChange (Properties::envelope_active));
415 /** @param buf Buffer to put peak data in.
416 * @param npeaks Number of peaks to read (ie the number of PeakDatas in buf)
417 * @param offset Start position, as an offset from the start of this region's source.
418 * @param cnt Number of samples to read.
419 * @param chan_n Channel.
420 * @param frames_per_pixel Number of samples to use to generate one peak value.
424 AudioRegion::read_peaks (PeakData *buf, framecnt_t npeaks, framecnt_t offset, framecnt_t cnt, uint32_t chan_n, double frames_per_pixel) const
426 if (chan_n >= _sources.size()) {
430 if (audio_source(chan_n)->read_peaks (buf, npeaks, offset, cnt, frames_per_pixel)) {
434 if (_scale_amplitude != 1.0f) {
435 for (framecnt_t n = 0; n < npeaks; ++n) {
436 buf[n].max *= _scale_amplitude;
437 buf[n].min *= _scale_amplitude;
444 /** @param buf Buffer to write data to (existing data will be overwritten).
445 * @param pos Position to read from as an offset from the region position.
446 * @param cnt Number of frames to read.
447 * @param channel Channel to read from.
450 AudioRegion::read (Sample* buf, framepos_t pos, framecnt_t cnt, int channel) const
452 /* raw read, no fades, no gain, nada */
453 return read_from_sources (_sources, _length, buf, _position + pos, cnt, channel);
457 AudioRegion::master_read_at (Sample *buf, Sample* /*mixdown_buffer*/, float* /*gain_buffer*/,
458 framepos_t position, framecnt_t cnt, uint32_t chan_n) const
460 /* do not read gain/scaling/fades and do not count this disk i/o in statistics */
463 return read_from_sources (
464 _master_sources, _master_sources.front()->length (_master_sources.front()->timeline_position()),
465 buf, position, cnt, chan_n
469 /** @param buf Buffer to mix data into.
470 * @param mixdown_buffer Scratch buffer for audio data.
471 * @param gain_buffer Scratch buffer for gain data.
472 * @param position Position within the session to read from.
473 * @param cnt Number of frames to read.
474 * @param chan_n Channel number to read.
477 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
480 uint32_t chan_n) const
482 /* We are reading data from this region into buf (possibly via mixdown_buffer).
483 The caller has verified that we cover the desired section.
486 /* See doc/region_read.svg for a drawing which might help to explain
492 if (n_channels() == 0) {
496 /* WORK OUT WHERE TO GET DATA FROM */
500 assert (position >= _position);
501 frameoffset_t const internal_offset = position - _position;
503 if (internal_offset >= _length) {
504 return 0; /* read nothing */
507 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
508 return 0; /* read nothing */
512 /* COMPUTE DETAILS OF ANY FADES INVOLVED IN THIS READ */
514 /* Amount (length) of fade in that we are dealing with in this read */
515 framecnt_t fade_in_limit = 0;
517 /* Offset from buf / mixdown_buffer of the start
518 of any fade out that we are dealing with
520 frameoffset_t fade_out_offset = 0;
522 /* Amount (length) of fade out that we are dealing with in this read */
523 framecnt_t fade_out_limit = 0;
525 framecnt_t fade_interval_start = 0;
529 if (_fade_in_active && _session.config.get_use_region_fades()) {
531 framecnt_t fade_in_length = (framecnt_t) _fade_in->back()->when;
533 /* see if this read is within the fade in */
535 if (internal_offset < fade_in_length) {
536 fade_in_limit = min (to_read, fade_in_length - internal_offset);
542 if (_fade_out_active && _session.config.get_use_region_fades()) {
544 /* see if some part of this read is within the fade out */
546 /* ................. >| REGION
552 * _length - fade_out_length
556 * ^internal_offset + to_read
558 * we need the intersection of [internal_offset,internal_offset+to_read] with
559 * [_length - fade_out_length, _length]
563 fade_interval_start = max (internal_offset, _length - framecnt_t (_fade_out->back()->when));
564 framecnt_t fade_interval_end = min(internal_offset + to_read, _length.val());
566 if (fade_interval_end > fade_interval_start) {
567 /* (part of the) the fade out is in this buffer */
568 fade_out_limit = fade_interval_end - fade_interval_start;
569 fade_out_offset = fade_interval_start - internal_offset;
573 /* READ DATA FROM THE SOURCE INTO mixdown_buffer.
574 We can never read directly into buf, since it may contain data
575 from a region `below' this one in the stack, and our fades (if they exist)
576 may need to mix with the existing data.
579 if (read_from_sources (_sources, _length, mixdown_buffer, position, to_read, chan_n) != to_read) {
583 /* APPLY REGULAR GAIN CURVES AND SCALING TO mixdown_buffer */
585 if (envelope_active()) {
586 _envelope->curve().get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
588 if (_scale_amplitude != 1.0f) {
589 for (framecnt_t n = 0; n < to_read; ++n) {
590 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
593 for (framecnt_t n = 0; n < to_read; ++n) {
594 mixdown_buffer[n] *= gain_buffer[n];
597 } else if (_scale_amplitude != 1.0f) {
598 apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
601 /* APPLY FADES TO THE DATA IN mixdown_buffer AND MIX THE RESULTS INTO
602 * buf. The key things to realize here: (1) the fade being applied is
603 * (as of April 26th 2012) just the inverse of the fade in curve (2)
604 * "buf" contains data from lower regions already. So this operation
605 * fades out the existing material.
608 if (fade_in_limit != 0) {
611 if (_inverse_fade_in) {
613 /* explicit inverse fade in curve (e.g. for constant
614 * power), so we have to fetch it.
617 _inverse_fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
619 /* Fade the data from lower layers out */
620 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
621 buf[n] *= gain_buffer[n];
624 /* refill gain buffer with the fade in */
626 _fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
630 /* no explicit inverse fade in, so just use (1 - fade
631 * in) for the fade out of lower layers
634 _fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
636 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
637 buf[n] *= 1 - gain_buffer[n];
641 _fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
644 /* Mix our newly-read data in, with the fade */
645 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
646 buf[n] += mixdown_buffer[n] * gain_buffer[n];
650 if (fade_out_limit != 0) {
652 framecnt_t const curve_offset = fade_interval_start - (_length - _fade_out->back()->when);
655 if (_inverse_fade_out) {
657 _inverse_fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
659 /* Fade the data from lower levels in */
660 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
661 buf[m] *= gain_buffer[n];
664 /* fetch the actual fade out */
666 _fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
670 /* no explicit inverse fade out (which is
671 * actually a fade in), so just use (1 - fade
672 * out) for the fade in of lower layers
675 _fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
677 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
678 buf[m] *= 1 - gain_buffer[n];
682 _fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
685 /* Mix our newly-read data with whatever was already there,
686 with the fade out applied to our data.
688 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
689 buf[m] += mixdown_buffer[m] * gain_buffer[n];
693 /* MIX OR COPY THE REGION BODY FROM mixdown_buffer INTO buf */
695 framecnt_t const N = to_read - fade_in_limit - fade_out_limit;
698 DEBUG_TRACE (DEBUG::AudioPlayback, string_compose ("Region %1 memcpy into buf @ %2 + %3, from mixdown buffer @ %4 + %5, len = %6 cnt was %7\n",
699 name(), buf, fade_in_limit, mixdown_buffer, fade_in_limit, N, cnt));
700 memcpy (buf + fade_in_limit, mixdown_buffer + fade_in_limit, N * sizeof (Sample));
702 mix_buffers_no_gain (buf + fade_in_limit, mixdown_buffer + fade_in_limit, N);
709 /** Read data directly from one of our sources, accounting for the situation when the track has a different channel
710 * count to the region.
712 * @param srcs Source list to get our source from.
713 * @param limit Furthest that we should read, as an offset from the region position.
714 * @param buf Buffer to write data into (existing contents of the buffer will be overwritten)
715 * @param position Position to read from, in session frames.
716 * @param cnt Number of frames to read.
717 * @param chan_n Channel to read from.
718 * @return Number of frames read.
722 AudioRegion::read_from_sources (SourceList const & srcs, framecnt_t limit, Sample* buf, framepos_t position, framecnt_t cnt, uint32_t chan_n) const
724 frameoffset_t const internal_offset = position - _position;
725 if (internal_offset >= limit) {
729 framecnt_t const to_read = min (cnt, limit - internal_offset);
734 if (chan_n < n_channels()) {
736 boost::shared_ptr<AudioSource> src = boost::dynamic_pointer_cast<AudioSource> (srcs[chan_n]);
737 if (src->read (buf, _start + internal_offset, to_read) != to_read) {
738 return 0; /* "read nothing" */
743 /* track is N-channel, this region has fewer channels; silence the ones
747 if (Config->get_replicate_missing_region_channels()) {
749 /* copy an existing channel's data in for this non-existant one */
751 uint32_t channel = chan_n % n_channels();
752 boost::shared_ptr<AudioSource> src = boost::dynamic_pointer_cast<AudioSource> (srcs[channel]);
754 if (src->read (buf, _start + internal_offset, to_read) != to_read) {
755 return 0; /* "read nothing" */
761 memset (buf, 0, sizeof (Sample) * to_read);
769 AudioRegion::get_basic_state ()
771 XMLNode& node (Region::state ());
773 LocaleGuard lg (X_("C"));
775 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
776 node.add_property ("channels", buf);
782 AudioRegion::state ()
784 XMLNode& node (get_basic_state());
786 LocaleGuard lg (X_("C"));
788 child = node.add_child ("Envelope");
790 bool default_env = false;
792 // If there are only two points, the points are in the start of the region and the end of the region
793 // so, if they are both at 1.0f, that means the default region.
795 if (_envelope->size() == 2 &&
796 _envelope->front()->value == GAIN_COEFF_UNITY &&
797 _envelope->back()->value==GAIN_COEFF_UNITY) {
798 if (_envelope->front()->when == 0 && _envelope->back()->when == _length) {
804 child->add_property ("default", "yes");
806 child->add_child_nocopy (_envelope->get_state ());
809 child = node.add_child (X_("FadeIn"));
811 if (_default_fade_in) {
812 child->add_property ("default", "yes");
814 child->add_child_nocopy (_fade_in->get_state ());
817 if (_inverse_fade_in) {
818 child = node.add_child (X_("InverseFadeIn"));
819 child->add_child_nocopy (_inverse_fade_in->get_state ());
822 child = node.add_child (X_("FadeOut"));
824 if (_default_fade_out) {
825 child->add_property ("default", "yes");
827 child->add_child_nocopy (_fade_out->get_state ());
830 if (_inverse_fade_out) {
831 child = node.add_child (X_("InverseFadeOut"));
832 child->add_child_nocopy (_inverse_fade_out->get_state ());
839 AudioRegion::_set_state (const XMLNode& node, int version, PropertyChange& what_changed, bool send)
841 const XMLNodeList& nlist = node.children();
842 const XMLProperty *prop;
843 LocaleGuard lg (X_("C"));
844 boost::shared_ptr<Playlist> the_playlist (_playlist.lock());
846 suspend_property_changes ();
849 the_playlist->freeze ();
853 /* this will set all our State members and stuff controlled by the Region.
854 It should NOT send any changed signals - that is our responsibility.
857 Region::_set_state (node, version, what_changed, false);
859 if ((prop = node.property ("scale-gain")) != 0) {
860 float a = atof (prop->value().c_str());
861 if (a != _scale_amplitude) {
862 _scale_amplitude = a;
863 what_changed.add (Properties::scale_amplitude);
867 /* Now find envelope description and other related child items */
869 _envelope->freeze ();
871 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
877 if (child->name() == "Envelope") {
881 if ((prop = child->property ("default")) != 0 || _envelope->set_state (*child, version)) {
882 set_default_envelope ();
885 _envelope->truncate_end (_length);
888 } else if (child->name() == "FadeIn") {
892 if (((prop = child->property ("default")) != 0 && string_is_affirmative (prop->value())) || (prop = child->property ("steepness")) != 0) {
893 set_default_fade_in ();
895 XMLNode* grandchild = child->child ("AutomationList");
897 _fade_in->set_state (*grandchild, version);
901 if ((prop = child->property ("active")) != 0) {
902 if (string_is_affirmative (prop->value())) {
903 set_fade_in_active (true);
905 set_fade_in_active (false);
909 } else if (child->name() == "FadeOut") {
913 if (((prop = child->property ("default")) != 0 && (string_is_affirmative (prop->value()))) || (prop = child->property ("steepness")) != 0) {
914 set_default_fade_out ();
916 XMLNode* grandchild = child->child ("AutomationList");
918 _fade_out->set_state (*grandchild, version);
922 if ((prop = child->property ("active")) != 0) {
923 if (string_is_affirmative (prop->value())) {
924 set_fade_out_active (true);
926 set_fade_out_active (false);
930 } else if ( (child->name() == "InverseFadeIn") || (child->name() == "InvFadeIn") ) {
931 XMLNode* grandchild = child->child ("AutomationList");
933 _inverse_fade_in->set_state (*grandchild, version);
935 } else if ( (child->name() == "InverseFadeOut") || (child->name() == "InvFadeOut") ) {
936 XMLNode* grandchild = child->child ("AutomationList");
938 _inverse_fade_out->set_state (*grandchild, version);
944 resume_property_changes ();
947 send_change (what_changed);
951 the_playlist->thaw ();
958 AudioRegion::set_state (const XMLNode& node, int version)
960 PropertyChange what_changed;
961 return _set_state (node, version, what_changed, true);
965 AudioRegion::fade_range (framepos_t start, framepos_t end)
969 switch (coverage (start, end)) {
970 case Evoral::OverlapStart:
974 set_fade_in (FadeConstantPower, e - s);
976 case Evoral::OverlapEnd:
979 e = _position + _length;
980 set_fade_out (FadeConstantPower, e - s);
982 case Evoral::OverlapInternal:
983 /* needs addressing, perhaps. Difficult to do if we can't
984 * control one edge of the fade relative to the relevant edge
985 * of the region, which we cannot - fades are currently assumed
986 * to start/end at the start/end of the region
995 AudioRegion::set_fade_in_shape (FadeShape shape)
997 set_fade_in (shape, (framecnt_t) _fade_in->back()->when);
1001 AudioRegion::set_fade_out_shape (FadeShape shape)
1003 set_fade_out (shape, (framecnt_t) _fade_out->back()->when);
1007 AudioRegion::set_fade_in (boost::shared_ptr<AutomationList> f)
1009 _fade_in->freeze ();
1010 *(_fade_in.val()) = *f;
1012 _default_fade_in = false;
1014 send_change (PropertyChange (Properties::fade_in));
1018 AudioRegion::set_fade_in (FadeShape shape, framecnt_t len)
1020 const ARDOUR::ParameterDescriptor desc(FadeInAutomation);
1021 boost::shared_ptr<Evoral::ControlList> c1 (new Evoral::ControlList (FadeInAutomation, desc));
1022 boost::shared_ptr<Evoral::ControlList> c2 (new Evoral::ControlList (FadeInAutomation, desc));
1023 boost::shared_ptr<Evoral::ControlList> c3 (new Evoral::ControlList (FadeInAutomation, desc));
1025 _fade_in->freeze ();
1027 _inverse_fade_in->clear ();
1029 const int num_steps = 32;
1033 _fade_in->fast_simple_add (0.0, GAIN_COEFF_SMALL);
1034 _fade_in->fast_simple_add (len, GAIN_COEFF_UNITY);
1035 reverse_curve (_inverse_fade_in.val(), _fade_in.val());
1039 generate_db_fade (_fade_in.val(), len, num_steps, -60);
1040 reverse_curve (c1, _fade_in.val());
1041 _fade_in->copy_events (*c1);
1042 generate_inverse_power_curve (_inverse_fade_in.val(), _fade_in.val());
1046 generate_db_fade (c1, len, num_steps, -1); // start off with a slow fade
1047 generate_db_fade (c2, len, num_steps, -80); // end with a fast fade
1048 merge_curves (_fade_in.val(), c1, c2);
1049 reverse_curve (c3, _fade_in.val());
1050 _fade_in->copy_events (*c3);
1051 generate_inverse_power_curve (_inverse_fade_in.val(), _fade_in.val());
1054 case FadeConstantPower:
1055 _fade_in->fast_simple_add (0.0, GAIN_COEFF_SMALL);
1056 for (int i = 1; i < num_steps; ++i) {
1057 const float dist = i / (num_steps + 1.f);
1058 _fade_in->fast_simple_add (len * dist, sin (dist * M_PI / 2.0));
1060 _fade_in->fast_simple_add (len, GAIN_COEFF_UNITY);
1061 reverse_curve (_inverse_fade_in.val(), _fade_in.val());
1065 //start with a nearly linear cuve
1066 _fade_in->fast_simple_add (0, 1);
1067 _fade_in->fast_simple_add (0.5 * len, 0.6);
1068 //now generate a fade-out curve by successively applying a gain drop
1069 const double breakpoint = 0.7; //linear for first 70%
1070 for (int i = 2; i < 9; ++i) {
1071 const float coeff = (1.f - breakpoint) * powf (0.5, i);
1072 _fade_in->fast_simple_add (len * (breakpoint + ((GAIN_COEFF_UNITY - breakpoint) * (double)i / 9.0)), coeff);
1074 _fade_in->fast_simple_add (len, GAIN_COEFF_SMALL);
1075 reverse_curve (c3, _fade_in.val());
1076 _fade_in->copy_events (*c3);
1077 reverse_curve (_inverse_fade_in.val(), _fade_in.val());
1081 _fade_in->set_interpolation(Evoral::ControlList::Curved);
1082 _inverse_fade_in->set_interpolation(Evoral::ControlList::Curved);
1084 _default_fade_in = false;
1086 send_change (PropertyChange (Properties::fade_in));
1090 AudioRegion::set_fade_out (boost::shared_ptr<AutomationList> f)
1092 _fade_out->freeze ();
1093 *(_fade_out.val()) = *f;
1095 _default_fade_out = false;
1097 send_change (PropertyChange (Properties::fade_in));
1101 AudioRegion::set_fade_out (FadeShape shape, framecnt_t len)
1103 const ARDOUR::ParameterDescriptor desc(FadeOutAutomation);
1104 boost::shared_ptr<Evoral::ControlList> c1 (new Evoral::ControlList (FadeOutAutomation, desc));
1105 boost::shared_ptr<Evoral::ControlList> c2 (new Evoral::ControlList (FadeOutAutomation, desc));
1107 _fade_out->freeze ();
1108 _fade_out->clear ();
1109 _inverse_fade_out->clear ();
1111 const int num_steps = 32;
1115 _fade_out->fast_simple_add (0.0, GAIN_COEFF_UNITY);
1116 _fade_out->fast_simple_add (len, GAIN_COEFF_SMALL);
1117 reverse_curve (_inverse_fade_out.val(), _fade_out.val());
1121 generate_db_fade (_fade_out.val(), len, num_steps, -60);
1122 generate_inverse_power_curve (_inverse_fade_out.val(), _fade_out.val());
1126 generate_db_fade (c1, len, num_steps, -1); //start off with a slow fade
1127 generate_db_fade (c2, len, num_steps, -80); //end with a fast fade
1128 merge_curves (_fade_out.val(), c1, c2);
1129 generate_inverse_power_curve (_inverse_fade_out.val(), _fade_out.val());
1132 case FadeConstantPower:
1133 //constant-power fades use a sin/cos relationship
1134 //the cutoff is abrupt but it has the benefit of being symmetrical
1135 _fade_out->fast_simple_add (0.0, GAIN_COEFF_UNITY);
1136 for (int i = 1; i < num_steps; ++i) {
1137 const float dist = i / (num_steps + 1.f);
1138 _fade_out->fast_simple_add (len * dist, cos (dist * M_PI / 2.0));
1140 _fade_out->fast_simple_add (len, GAIN_COEFF_SMALL);
1141 reverse_curve (_inverse_fade_out.val(), _fade_out.val());
1145 //start with a nearly linear cuve
1146 _fade_out->fast_simple_add (0, 1);
1147 _fade_out->fast_simple_add (0.5 * len, 0.6);
1148 //now generate a fade-out curve by successively applying a gain drop
1149 const double breakpoint = 0.7; //linear for first 70%
1150 for (int i = 2; i < 9; ++i) {
1151 const float coeff = (1.f - breakpoint) * powf (0.5, i);
1152 _fade_out->fast_simple_add (len * (breakpoint + ((GAIN_COEFF_UNITY - breakpoint) * (double)i / 9.0)), coeff);
1154 _fade_out->fast_simple_add (len, GAIN_COEFF_SMALL);
1155 reverse_curve (_inverse_fade_out.val(), _fade_out.val());
1159 _fade_out->set_interpolation(Evoral::ControlList::Curved);
1160 _inverse_fade_out->set_interpolation(Evoral::ControlList::Curved);
1162 _default_fade_out = false;
1164 send_change (PropertyChange (Properties::fade_out));
1168 AudioRegion::set_fade_in_length (framecnt_t len)
1170 if (len > _length) {
1178 bool changed = _fade_in->extend_to (len);
1181 if (_inverse_fade_in) {
1182 _inverse_fade_in->extend_to (len);
1185 _default_fade_in = false;
1186 send_change (PropertyChange (Properties::fade_in));
1191 AudioRegion::set_fade_out_length (framecnt_t len)
1193 if (len > _length) {
1201 bool changed = _fade_out->extend_to (len);
1205 if (_inverse_fade_out) {
1206 _inverse_fade_out->extend_to (len);
1208 _default_fade_out = false;
1210 send_change (PropertyChange (Properties::fade_out));
1215 AudioRegion::set_fade_in_active (bool yn)
1217 if (yn == _fade_in_active) {
1221 _fade_in_active = yn;
1222 send_change (PropertyChange (Properties::fade_in_active));
1226 AudioRegion::set_fade_out_active (bool yn)
1228 if (yn == _fade_out_active) {
1231 _fade_out_active = yn;
1232 send_change (PropertyChange (Properties::fade_out_active));
1236 AudioRegion::fade_in_is_default () const
1238 return _fade_in->size() == 2 && _fade_in->front()->when == 0 && _fade_in->back()->when == 64;
1242 AudioRegion::fade_out_is_default () const
1244 return _fade_out->size() == 2 && _fade_out->front()->when == 0 && _fade_out->back()->when == 64;
1248 AudioRegion::set_default_fade_in ()
1250 _fade_in_suspended = 0;
1251 set_fade_in (Config->get_default_fade_shape(), 64);
1255 AudioRegion::set_default_fade_out ()
1257 _fade_out_suspended = 0;
1258 set_fade_out (Config->get_default_fade_shape(), 64);
1262 AudioRegion::set_default_fades ()
1264 set_default_fade_in ();
1265 set_default_fade_out ();
1269 AudioRegion::set_default_envelope ()
1271 _envelope->freeze ();
1272 _envelope->clear ();
1273 _envelope->fast_simple_add (0, GAIN_COEFF_UNITY);
1274 _envelope->fast_simple_add (_length, GAIN_COEFF_UNITY);
1279 AudioRegion::recompute_at_end ()
1281 /* our length has changed. recompute a new final point by interpolating
1282 based on the the existing curve.
1285 _envelope->freeze ();
1286 _envelope->truncate_end (_length);
1289 suspend_property_changes();
1291 if (_left_of_split) {
1292 set_default_fade_out ();
1293 _left_of_split = false;
1294 } else if (_fade_out->back()->when > _length) {
1295 _fade_out->extend_to (_length);
1296 send_change (PropertyChange (Properties::fade_out));
1299 if (_fade_in->back()->when > _length) {
1300 _fade_in->extend_to (_length);
1301 send_change (PropertyChange (Properties::fade_in));
1304 resume_property_changes();
1308 AudioRegion::recompute_at_start ()
1310 /* as above, but the shift was from the front */
1312 _envelope->truncate_start (_length);
1314 suspend_property_changes();
1316 if (_right_of_split) {
1317 set_default_fade_in ();
1318 _right_of_split = false;
1319 } else if (_fade_in->back()->when > _length) {
1320 _fade_in->extend_to (_length);
1321 send_change (PropertyChange (Properties::fade_in));
1324 if (_fade_out->back()->when > _length) {
1325 _fade_out->extend_to (_length);
1326 send_change (PropertyChange (Properties::fade_out));
1329 resume_property_changes();
1333 AudioRegion::separate_by_channel (Session& /*session*/, vector<boost::shared_ptr<Region> >& v) const
1339 if (_sources.size() < 2) {
1343 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
1345 srcs.push_back (*i);
1349 if (_sources.size() == 2) {
1357 new_name += ('0' + n + 1);
1360 /* create a copy with just one source. prevent if from being thought of as
1361 "whole file" even if it covers the entire source file(s).
1366 plist.add (Properties::start, _start.val());
1367 plist.add (Properties::length, _length.val());
1368 plist.add (Properties::name, new_name);
1369 plist.add (Properties::layer, layer ());
1371 v.push_back(RegionFactory::create (srcs, plist));
1372 v.back()->set_whole_file (false);
1381 AudioRegion::read_raw_internal (Sample* buf, framepos_t pos, framecnt_t cnt, int channel) const
1383 return audio_source(channel)->read (buf, pos, cnt);
1387 AudioRegion::set_scale_amplitude (gain_t g)
1389 boost::shared_ptr<Playlist> pl (playlist());
1391 _scale_amplitude = g;
1393 /* tell the diskstream we're in */
1396 pl->ContentsChanged();
1399 /* tell everybody else */
1401 send_change (PropertyChange (Properties::scale_amplitude));
1404 /** @return the maximum (linear) amplitude of the region, or a -ve
1405 * number if the Progress object reports that the process was cancelled.
1408 AudioRegion::maximum_amplitude (Progress* p) const
1410 framepos_t fpos = _start;
1411 framepos_t const fend = _start + _length;
1414 framecnt_t const blocksize = 64 * 1024;
1415 Sample buf[blocksize];
1417 while (fpos < fend) {
1421 framecnt_t const to_read = min (fend - fpos, blocksize);
1423 for (n = 0; n < n_channels(); ++n) {
1427 if (read_raw_internal (buf, fpos, to_read, n) != to_read) {
1431 maxamp = compute_peak (buf, to_read, maxamp);
1436 p->set_progress (float (fpos - _start) / _length);
1437 if (p->cancelled ()) {
1446 /** Normalize using a given maximum amplitude and target, so that region
1447 * _scale_amplitude becomes target / max_amplitude.
1450 AudioRegion::normalize (float max_amplitude, float target_dB)
1452 gain_t target = dB_to_coefficient (target_dB);
1454 if (target == GAIN_COEFF_UNITY) {
1455 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1456 that we may have clipped.
1458 target -= FLT_EPSILON;
1461 if (max_amplitude < GAIN_COEFF_SMALL) {
1462 /* don't even try */
1466 if (max_amplitude == target) {
1467 /* we can't do anything useful */
1471 set_scale_amplitude (target / max_amplitude);
1475 AudioRegion::fade_in_changed ()
1477 send_change (PropertyChange (Properties::fade_in));
1481 AudioRegion::fade_out_changed ()
1483 send_change (PropertyChange (Properties::fade_out));
1487 AudioRegion::envelope_changed ()
1489 send_change (PropertyChange (Properties::envelope));
1493 AudioRegion::suspend_fade_in ()
1495 if (++_fade_in_suspended == 1) {
1496 if (fade_in_is_default()) {
1497 set_fade_in_active (false);
1503 AudioRegion::resume_fade_in ()
1505 if (--_fade_in_suspended == 0 && _fade_in_suspended) {
1506 set_fade_in_active (true);
1511 AudioRegion::suspend_fade_out ()
1513 if (++_fade_out_suspended == 1) {
1514 if (fade_out_is_default()) {
1515 set_fade_out_active (false);
1521 AudioRegion::resume_fade_out ()
1523 if (--_fade_out_suspended == 0 &&_fade_out_suspended) {
1524 set_fade_out_active (true);
1529 AudioRegion::speed_mismatch (float sr) const
1531 if (_sources.empty()) {
1532 /* impossible, but ... */
1536 float fsr = audio_source()->sample_rate();
1542 AudioRegion::source_offset_changed ()
1544 /* XXX this fixes a crash that should not occur. It does occur
1545 becauses regions are not being deleted when a session
1546 is unloaded. That bug must be fixed.
1549 if (_sources.empty()) {
1553 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(_sources.front());
1555 if (afs && afs->destructive()) {
1556 // set_start (source()->natural_position(), this);
1557 set_position (source()->natural_position());
1561 boost::shared_ptr<AudioSource>
1562 AudioRegion::audio_source (uint32_t n) const
1564 // Guaranteed to succeed (use a static cast for speed?)
1565 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1569 AudioRegion::get_related_audio_file_channel_count () const
1571 uint32_t chan_count = 0;
1572 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
1574 boost::shared_ptr<SndFileSource> sndf = dynamic_pointer_cast<SndFileSource>(*i);
1577 if (sndf->channel_count() > chan_count) {
1578 chan_count = sndf->channel_count();
1581 #ifdef HAVE_COREAUDIO
1583 boost::shared_ptr<CoreAudioSource> cauf = dynamic_pointer_cast<CoreAudioSource>(*i);
1585 if (cauf->channel_count() > chan_count) {
1586 chan_count = cauf->channel_count();
1590 #endif // HAVE_COREAUDIO
1597 AudioRegion::adjust_transients (frameoffset_t delta)
1599 for (AnalysisFeatureList::iterator x = _transients.begin(); x != _transients.end(); ++x) {
1600 (*x) = (*x) + delta;
1603 send_change (PropertyChange (Properties::valid_transients));
1609 AudioRegion::update_transient (framepos_t old_position, framepos_t new_position)
1611 for (AnalysisFeatureList::iterator x = _transients.begin(); x != _transients.end(); ++x) {
1612 if ((*x) == old_position) {
1613 (*x) = new_position;
1614 send_change (PropertyChange (Properties::valid_transients));
1624 AudioRegion::add_transient (framepos_t where)
1626 _transients.push_back(where);
1627 _valid_transients = true;
1629 send_change (PropertyChange (Properties::valid_transients));
1633 AudioRegion::remove_transient (framepos_t where)
1635 _transients.remove(where);
1636 _valid_transients = true;
1638 send_change (PropertyChange (Properties::valid_transients));
1642 AudioRegion::set_transients (AnalysisFeatureList& results)
1644 _transients.clear();
1645 _transients = results;
1646 _valid_transients = true;
1648 send_change (PropertyChange (Properties::valid_transients));
1654 AudioRegion::get_transients (AnalysisFeatureList& results, bool force_new)
1656 boost::shared_ptr<Playlist> pl = playlist();
1662 if (_valid_transients && !force_new) {
1663 results = _transients;
1667 SourceList::iterator s;
1669 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1670 if (!(*s)->has_been_analysed()) {
1671 cerr << "For " << name() << " source " << (*s)->name() << " has not been analyzed\n";
1676 if (s == _sources.end()) {
1677 /* all sources are analyzed, merge data from each one */
1679 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1681 /* find the set of transients within the bounds of this region */
1683 AnalysisFeatureList::iterator low = lower_bound ((*s)->transients.begin(),
1684 (*s)->transients.end(),
1687 AnalysisFeatureList::iterator high = upper_bound ((*s)->transients.begin(),
1688 (*s)->transients.end(),
1693 results.insert (results.end(), low, high);
1696 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1698 /* translate all transients to current position */
1700 for (AnalysisFeatureList::iterator x = results.begin(); x != results.end(); ++x) {
1705 _transients = results;
1706 _valid_transients = true;
1711 /* no existing/complete transient info */
1713 static bool analyse_dialog_shown = false; /* global per instance of Ardour */
1715 if (!Config->get_auto_analyse_audio()) {
1716 if (!analyse_dialog_shown) {
1717 pl->session().Dialog (string_compose (_("\
1718 You have requested an operation that requires audio analysis.\n\n\
1719 You currently have \"auto-analyse-audio\" disabled, which means \
1720 that transient data must be generated every time it is required.\n\n\
1721 If you are doing work that will require transient data on a \
1722 regular basis, you should probably enable \"auto-analyse-audio\" \
1723 then quit %1 and restart.\n\n\
1724 This dialog will not display again. But you may notice a slight delay \
1725 in this and future transient-detection operations.\n\
1727 analyse_dialog_shown = true;
1731 bool existing_results = !results.empty();
1735 TransientDetector t (pl->session().frame_rate());
1737 _transients.clear ();
1738 _valid_transients = false;
1740 for (uint32_t i = 0; i < n_channels(); ++i) {
1742 AnalysisFeatureList these_results;
1746 if (t.run ("", this, i, these_results)) {
1750 /* translate all transients to give absolute position */
1752 for (AnalysisFeatureList::iterator i = these_results.begin(); i != these_results.end(); ++i) {
1758 _transients.insert (_transients.end(), these_results.begin(), these_results.end());
1761 error << string_compose(_("Transient Analysis failed for %1."), _("Audio Region")) << endmsg;
1765 if (!results.empty()) {
1766 if (existing_results) {
1768 /* merge our transients into the existing ones, then clean up
1772 results.insert (results.end(), _transients.begin(), _transients.end());
1773 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1776 /* make sure ours are clean too */
1778 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1782 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1783 results = _transients;
1786 _valid_transients = true;
1791 /** Find areas of `silence' within a region.
1793 * @param threshold Threshold below which signal is considered silence (as a sample value)
1794 * @param min_length Minimum length of silent period to be reported.
1795 * @return Silent intervals, measured relative to the region start in the source
1799 AudioRegion::find_silence (Sample threshold, framecnt_t min_length, InterThreadInfo& itt) const
1801 framecnt_t const block_size = 64 * 1024;
1802 boost::scoped_array<Sample> loudest (new Sample[block_size]);
1803 boost::scoped_array<Sample> buf (new Sample[block_size]);
1805 framepos_t pos = _start;
1806 framepos_t const end = _start + _length - 1;
1808 AudioIntervalResult silent_periods;
1810 bool in_silence = false;
1811 frameoffset_t silence_start = 0;
1813 while (pos < end && !itt.cancel) {
1815 /* fill `loudest' with the loudest absolute sample at each instant, across all channels */
1816 memset (loudest.get(), 0, sizeof (Sample) * block_size);
1817 for (uint32_t n = 0; n < n_channels(); ++n) {
1819 read_raw_internal (buf.get(), pos, block_size, n);
1820 for (framecnt_t i = 0; i < block_size; ++i) {
1821 loudest[i] = max (loudest[i], abs (buf[i]));
1825 /* now look for silence */
1826 for (framecnt_t i = 0; i < block_size; ++i) {
1827 bool const silence = abs (loudest[i]) < threshold;
1828 if (silence && !in_silence) {
1829 /* non-silence to silence */
1831 silence_start = pos + i;
1832 } else if (!silence && in_silence) {
1833 /* silence to non-silence */
1835 if (pos + i - 1 - silence_start >= min_length) {
1836 silent_periods.push_back (std::make_pair (silence_start, pos + i - 1));
1842 itt.progress = (end-pos)/(double)_length;
1845 if (in_silence && end - 1 - silence_start >= min_length) {
1846 /* last block was silent, so finish off the last period */
1847 silent_periods.push_back (std::make_pair (silence_start, end));
1852 return silent_periods;
1855 Evoral::Range<framepos_t>
1856 AudioRegion::body_range () const
1858 return Evoral::Range<framepos_t> (first_frame() + _fade_in->back()->when + 1, last_frame() - _fade_out->back()->when);
1861 boost::shared_ptr<Region>
1862 AudioRegion::get_single_other_xfade_region (bool start) const
1864 boost::shared_ptr<Playlist> pl (playlist());
1867 /* not currently in a playlist - xfade length is unbounded
1870 return boost::shared_ptr<AudioRegion> ();
1873 boost::shared_ptr<RegionList> rl;
1876 rl = pl->regions_at (position());
1878 rl = pl->regions_at (last_frame());
1881 RegionList::iterator i;
1882 boost::shared_ptr<Region> other;
1885 /* count and find the other region in a single pass through the list */
1887 for (i = rl->begin(); i != rl->end(); ++i) {
1888 if ((*i).get() != this) {
1895 /* zero or multiple regions stacked here - don't care about xfades */
1896 return boost::shared_ptr<AudioRegion> ();
1903 AudioRegion::verify_xfade_bounds (framecnt_t len, bool start)
1905 /* this is called from a UI to check on whether a new proposed
1906 length for an xfade is legal or not. it returns the legal
1907 length corresponding to @a len which may be shorter than or
1908 equal to @a len itself.
1911 boost::shared_ptr<Region> other = get_single_other_xfade_region (start);
1915 /* zero or > 2 regions here, don't care about len, but
1916 it can't be longer than the region itself.
1918 return min (length(), len);
1921 /* we overlap a single region. clamp the length of an xfade to
1922 the maximum possible duration of the overlap (if the other
1923 region were trimmed appropriately).
1927 maxlen = other->latest_possible_frame() - position();
1929 maxlen = last_frame() - other->earliest_possible_position();
1932 return min (length(), min (maxlen, len));