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/thread.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/playlist.h"
43 #include "ardour/audiofilesource.h"
44 #include "ardour/region_factory.h"
45 #include "ardour/runtime_functions.h"
46 #include "ardour/transient_detector.h"
47 #include "ardour/progress.h"
53 using namespace ARDOUR;
57 namespace Properties {
58 PBD::PropertyDescriptor<bool> envelope_active;
59 PBD::PropertyDescriptor<bool> default_fade_in;
60 PBD::PropertyDescriptor<bool> default_fade_out;
61 PBD::PropertyDescriptor<bool> fade_in_active;
62 PBD::PropertyDescriptor<bool> fade_out_active;
63 PBD::PropertyDescriptor<float> scale_amplitude;
64 PBD::PropertyDescriptor<bool> fade_out_is_xfade;
65 PBD::PropertyDescriptor<bool> fade_out_is_short;
66 PBD::PropertyDescriptor<bool> fade_in_is_xfade;
67 PBD::PropertyDescriptor<bool> fade_in_is_short;
68 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > fade_in;
69 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > inverse_fade_in;
70 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > fade_out;
71 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > inverse_fade_out;
72 PBD::PropertyDescriptor<boost::shared_ptr<AutomationList> > envelope;
76 static const double VERY_SMALL_SIGNAL = 0.0000001; //-140dB
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;
85 for (Evoral::ControlList::const_iterator it = src->begin(); it!=src->end(); it++) {
86 dst->add (len - (*it)->when, (*it)->value);
91 generate_inverse_power_curve (boost::shared_ptr<Evoral::ControlList> dst, boost::shared_ptr<const Evoral::ControlList> src)
93 // calc inverse curve using sum of squares
94 for (Evoral::ControlList::const_iterator it = src->begin(); it!=src->end(); ++it ) {
95 float value = (*it)->value;
96 value = 1 - powf(value,2);
98 dst->fast_simple_add ( (*it)->when, value );
103 generate_db_fade (boost::shared_ptr<Evoral::ControlList> dst, double len, int num_steps, float dB_drop)
106 dst->fast_simple_add (0, 1);
108 //generate a fade-out curve by successively applying a gain drop
109 float fade_speed = dB_to_coefficient(dB_drop / (float) num_steps);
110 for (int i = 1; i < (num_steps-1); i++) {
112 for (int j = 0; j < i; j++) {
115 dst->fast_simple_add (len*(double)i/(double)num_steps, coeff);
118 dst->fast_simple_add (len, VERY_SMALL_SIGNAL);
122 merge_curves (boost::shared_ptr<Evoral::ControlList> dst,
123 boost::shared_ptr<const Evoral::ControlList> curve1,
124 boost::shared_ptr<const Evoral::ControlList> curve2)
126 Evoral::ControlList::EventList::size_type size = curve1->size();
128 //curve lengths must match for now
129 if (size != curve2->size()) {
133 Evoral::ControlList::const_iterator c1 = curve1->begin();
135 for (Evoral::ControlList::const_iterator c2 = curve2->begin(); c2!=curve2->end(); c2++ ) {
136 float v1 = accurate_coefficient_to_dB((*c1)->value);
137 float v2 = accurate_coefficient_to_dB((*c2)->value);
139 double interp = v1 * ( 1.0-( (double)count / (double)size) );
140 interp += v2 * ( (double)count / (double)size );
142 interp = dB_to_coefficient(interp);
143 dst->add ( (*c1)->when, interp );
150 AudioRegion::make_property_quarks ()
152 Properties::envelope_active.property_id = g_quark_from_static_string (X_("envelope-active"));
153 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for envelope-active = %1\n", Properties::envelope_active.property_id));
154 Properties::default_fade_in.property_id = g_quark_from_static_string (X_("default-fade-in"));
155 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for default-fade-in = %1\n", Properties::default_fade_in.property_id));
156 Properties::default_fade_out.property_id = g_quark_from_static_string (X_("default-fade-out"));
157 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for default-fade-out = %1\n", Properties::default_fade_out.property_id));
158 Properties::fade_in_active.property_id = g_quark_from_static_string (X_("fade-in-active"));
159 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fade-in-active = %1\n", Properties::fade_in_active.property_id));
160 Properties::fade_out_active.property_id = g_quark_from_static_string (X_("fade-out-active"));
161 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fade-out-active = %1\n", Properties::fade_out_active.property_id));
162 Properties::scale_amplitude.property_id = g_quark_from_static_string (X_("scale-amplitude"));
163 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for scale-amplitude = %1\n", Properties::scale_amplitude.property_id));
164 Properties::fade_out_is_xfade.property_id = g_quark_from_static_string (X_("fade-out-is-xfade"));
165 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fade-out-is-xfade = %1\n", Properties::fade_out_is_xfade.property_id));
166 Properties::fade_out_is_short.property_id = g_quark_from_static_string (X_("fade-out-is-short"));
167 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fade-out-is-short = %1\n", Properties::fade_out_is_short.property_id));
168 Properties::fade_in_is_xfade.property_id = g_quark_from_static_string (X_("fade-in-is-xfade"));
169 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fade-in-is-xfade = %1\n", Properties::fade_in_is_xfade.property_id));
170 Properties::fade_in_is_short.property_id = g_quark_from_static_string (X_("fade-in-is-short"));
171 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fade-in-is-short = %1\n", Properties::fade_in_is_short.property_id));
172 Properties::fade_in.property_id = g_quark_from_static_string (X_("FadeIn"));
173 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for FadeIn = %1\n", Properties::fade_in.property_id));
174 Properties::inverse_fade_in.property_id = g_quark_from_static_string (X_("InverseFadeIn"));
175 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for InverseFadeIn = %1\n", Properties::inverse_fade_in.property_id));
176 Properties::fade_out.property_id = g_quark_from_static_string (X_("FadeOut"));
177 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for FadeOut = %1\n", Properties::fade_out.property_id));
178 Properties::inverse_fade_out.property_id = g_quark_from_static_string (X_("InverseFadeOut"));
179 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for InverseFadeOut = %1\n", Properties::inverse_fade_out.property_id));
180 Properties::envelope.property_id = g_quark_from_static_string (X_("Envelope"));
181 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for Envelope = %1\n", Properties::envelope.property_id));
185 AudioRegion::register_properties ()
187 /* no need to register parent class properties */
189 add_property (_envelope_active);
190 add_property (_default_fade_in);
191 add_property (_default_fade_out);
192 add_property (_fade_in_active);
193 add_property (_fade_out_active);
194 add_property (_scale_amplitude);
195 add_property (_fade_out_is_xfade);
196 add_property (_fade_out_is_short);
197 add_property (_fade_in_is_xfade);
198 add_property (_fade_in_is_short);
199 add_property (_fade_in);
200 add_property (_inverse_fade_in);
201 add_property (_fade_out);
202 add_property (_inverse_fade_out);
203 add_property (_envelope);
206 #define AUDIOREGION_STATE_DEFAULT \
207 _envelope_active (Properties::envelope_active, false) \
208 , _default_fade_in (Properties::default_fade_in, true) \
209 , _default_fade_out (Properties::default_fade_out, true) \
210 , _fade_in_active (Properties::fade_in_active, true) \
211 , _fade_out_active (Properties::fade_out_active, true) \
212 , _scale_amplitude (Properties::scale_amplitude, 1.0) \
213 , _fade_in_is_xfade (Properties::fade_in_is_xfade, false) \
214 , _fade_out_is_xfade (Properties::fade_out_is_xfade, false) \
215 , _fade_in_is_short (Properties::fade_in_is_short, false) \
216 , _fade_out_is_short (Properties::fade_out_is_short, false) \
217 , _fade_in (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation)))) \
218 , _inverse_fade_in (Properties::inverse_fade_in, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeInAutomation)))) \
219 , _fade_out (Properties::fade_out, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeOutAutomation)))) \
220 , _inverse_fade_out (Properties::inverse_fade_out, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter (FadeOutAutomation))))
222 #define AUDIOREGION_COPY_STATE(other) \
223 _envelope_active (Properties::envelope_active, other->_envelope_active) \
224 , _default_fade_in (Properties::default_fade_in, other->_default_fade_in) \
225 , _default_fade_out (Properties::default_fade_out, other->_default_fade_out) \
226 , _fade_in_active (Properties::fade_in_active, other->_fade_in_active) \
227 , _fade_out_active (Properties::fade_out_active, other->_fade_out_active) \
228 , _scale_amplitude (Properties::scale_amplitude, other->_scale_amplitude) \
229 , _fade_in_is_xfade (Properties::fade_in_is_xfade, other->_fade_in_is_xfade) \
230 , _fade_out_is_xfade (Properties::fade_out_is_xfade, other->_fade_out_is_xfade) \
231 , _fade_in_is_short (Properties::fade_in_is_short, other->_fade_in_is_short) \
232 , _fade_out_is_short (Properties::fade_out_is_short, other->_fade_out_is_short) \
233 , _fade_in (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_fade_in.val()))) \
234 , _inverse_fade_in (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_inverse_fade_in.val()))) \
235 , _fade_out (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_fade_out.val()))) \
236 , _inverse_fade_out (Properties::fade_in, boost::shared_ptr<AutomationList> (new AutomationList (*other->_inverse_fade_out.val())))
237 /* a Session will reset these to its chosen defaults by calling AudioRegion::set_default_fade() */
242 register_properties ();
244 suspend_property_changes();
245 set_default_fades ();
246 set_default_envelope ();
247 resume_property_changes();
249 listen_to_my_curves ();
250 connect_to_analysis_changed ();
251 connect_to_header_position_offset_changed ();
254 /** Constructor for use by derived types only */
255 AudioRegion::AudioRegion (Session& s, framepos_t start, framecnt_t len, std::string name)
256 : Region (s, start, len, name, DataType::AUDIO)
257 , AUDIOREGION_STATE_DEFAULT
258 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter(EnvelopeAutomation))))
260 , _fade_in_suspended (0)
261 , _fade_out_suspended (0)
264 assert (_sources.size() == _master_sources.size());
267 /** Basic AudioRegion constructor */
268 AudioRegion::AudioRegion (const SourceList& srcs)
270 , AUDIOREGION_STATE_DEFAULT
271 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (Evoral::Parameter(EnvelopeAutomation))))
272 , _automatable(srcs[0]->session())
273 , _fade_in_suspended (0)
274 , _fade_out_suspended (0)
277 assert (_sources.size() == _master_sources.size());
280 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
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(), 0, 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, framecnt_t offset)
303 : Region (other, offset)
304 , AUDIOREGION_COPY_STATE (other)
305 /* As far as I can see, the _envelope's times are relative to region position, and have nothing
306 to do with sources (and hence _start). So when we copy the envelope, we just use the supplied offset.
308 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (*other->_envelope.val(), offset, other->_length)))
309 , _automatable (other->session())
310 , _fade_in_suspended (0)
311 , _fade_out_suspended (0)
313 /* don't use init here, because we got fade in/out from the other region
315 register_properties ();
316 listen_to_my_curves ();
317 connect_to_analysis_changed ();
318 connect_to_header_position_offset_changed ();
320 assert(_type == DataType::AUDIO);
321 assert (_sources.size() == _master_sources.size());
324 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, const SourceList& srcs)
325 : Region (boost::static_pointer_cast<const Region>(other), srcs)
326 , AUDIOREGION_COPY_STATE (other)
327 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList (*other->_envelope.val())))
328 , _automatable (other->session())
329 , _fade_in_suspended (0)
330 , _fade_out_suspended (0)
332 /* make-a-sort-of-copy-with-different-sources constructor (used by audio filter) */
334 register_properties ();
336 listen_to_my_curves ();
337 connect_to_analysis_changed ();
338 connect_to_header_position_offset_changed ();
340 assert (_sources.size() == _master_sources.size());
343 AudioRegion::AudioRegion (SourceList& srcs)
345 , AUDIOREGION_STATE_DEFAULT
346 , _envelope (Properties::envelope, boost::shared_ptr<AutomationList> (new AutomationList(Evoral::Parameter(EnvelopeAutomation))))
347 , _automatable(srcs[0]->session())
348 , _fade_in_suspended (0)
349 , _fade_out_suspended (0)
353 assert(_type == DataType::AUDIO);
354 assert (_sources.size() == _master_sources.size());
357 AudioRegion::~AudioRegion ()
362 AudioRegion::post_set (const PropertyChange& /*ignored*/)
365 _sync_position = _start;
368 /* return to default fades if the existing ones are too long */
370 if (_left_of_split) {
371 if (_fade_in->back()->when >= _length) {
372 set_default_fade_in ();
374 set_default_fade_out ();
375 _left_of_split = false;
378 if (_right_of_split) {
379 if (_fade_out->back()->when >= _length) {
380 set_default_fade_out ();
383 set_default_fade_in ();
384 _right_of_split = false;
387 /* If _length changed, adjust our gain envelope accordingly */
388 _envelope->truncate_end (_length);
392 AudioRegion::connect_to_analysis_changed ()
394 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
395 (*i)->AnalysisChanged.connect_same_thread (*this, boost::bind (&AudioRegion::invalidate_transients, this));
400 AudioRegion::connect_to_header_position_offset_changed ()
402 set<boost::shared_ptr<Source> > unique_srcs;
404 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
406 /* connect only once to HeaderPositionOffsetChanged, even if sources are replicated
409 if (unique_srcs.find (*i) == unique_srcs.end ()) {
410 unique_srcs.insert (*i);
411 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
413 afs->HeaderPositionOffsetChanged.connect_same_thread (*this, boost::bind (&AudioRegion::source_offset_changed, this));
420 AudioRegion::listen_to_my_curves ()
422 _envelope->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::envelope_changed, this));
423 _fade_in->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::fade_in_changed, this));
424 _fade_out->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::fade_out_changed, this));
428 AudioRegion::set_envelope_active (bool yn)
430 if (envelope_active() != yn) {
431 _envelope_active = yn;
432 send_change (PropertyChange (Properties::envelope_active));
437 AudioRegion::read_peaks (PeakData *buf, framecnt_t npeaks, framecnt_t offset, framecnt_t cnt, uint32_t chan_n, double samples_per_unit) const
439 if (chan_n >= _sources.size()) {
443 if (audio_source(chan_n)->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
446 if (_scale_amplitude != 1.0f) {
447 for (framecnt_t n = 0; n < npeaks; ++n) {
448 buf[n].max *= _scale_amplitude;
449 buf[n].min *= _scale_amplitude;
456 /** @param buf Buffer to write data to (existing data will be overwritten).
457 * @param pos Position to read from as an offset from the region position.
458 * @param cnt Number of frames to read.
459 * @param channel Channel to read from.
462 AudioRegion::read (Sample* buf, framepos_t pos, framecnt_t cnt, int channel) const
464 /* raw read, no fades, no gain, nada */
465 return read_from_sources (_sources, _length, buf, _position + pos, cnt, channel);
469 AudioRegion::master_read_at (Sample *buf, Sample* /*mixdown_buffer*/, float* /*gain_buffer*/,
470 framepos_t position, framecnt_t cnt, uint32_t chan_n) const
472 /* do not read gain/scaling/fades and do not count this disk i/o in statistics */
475 return read_from_sources (
476 _master_sources, _master_sources.front()->length (_master_sources.front()->timeline_position()),
477 buf, position, cnt, chan_n
481 /** @param buf Buffer to mix data into.
482 * @param mixdown_buffer Scratch buffer for audio data.
483 * @param gain_buffer Scratch buffer for gain data.
484 * @param position Position within the session to read from.
485 * @param cnt Number of frames to read.
486 * @param chan_n Channel number to read.
489 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
492 uint32_t chan_n) const
494 /* We are reading data from this region into buf (possibly via mixdown_buffer).
495 The caller has verified that we cover the desired section.
498 /* See doc/region_read.svg for a drawing which might help to explain
504 if (n_channels() == 0) {
509 return 0; /* read nothing */
513 /* WORK OUT WHERE TO GET DATA FROM */
517 assert (position >= _position);
518 frameoffset_t const internal_offset = position - _position;
520 if (internal_offset >= _length) {
521 return 0; /* read nothing */
524 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
525 return 0; /* read nothing */
529 /* COMPUTE DETAILS OF ANY FADES INVOLVED IN THIS READ */
531 /* Amount (length) of fade in that we are dealing with in this read */
532 framecnt_t fade_in_limit = 0;
534 /* Offset from buf / mixdown_buffer of the start
535 of any fade out that we are dealing with
537 frameoffset_t fade_out_offset = 0;
539 /* Amount (length) of fade out that we are dealing with in this read */
540 framecnt_t fade_out_limit = 0;
542 framecnt_t fade_interval_start = 0;
546 if (_fade_in_active && _session.config.get_use_region_fades()) {
548 framecnt_t fade_in_length = (framecnt_t) _fade_in->back()->when;
550 /* see if this read is within the fade in */
552 if (internal_offset < fade_in_length) {
553 fade_in_limit = min (to_read, fade_in_length - internal_offset);
559 if (_fade_out_active && _session.config.get_use_region_fades()) {
561 /* see if some part of this read is within the fade out */
563 /* ................. >| REGION
569 _length - fade_out_length
572 ^internal_offset + to_read
574 we need the intersection of [internal_offset,internal_offset+to_read] with
575 [_length - fade_out_length, _length]
580 fade_interval_start = max (internal_offset, _length - framecnt_t (_fade_out->back()->when));
581 framecnt_t fade_interval_end = min(internal_offset + to_read, _length.val());
583 if (fade_interval_end > fade_interval_start) {
584 /* (part of the) the fade out is in this buffer */
585 fade_out_limit = fade_interval_end - fade_interval_start;
586 fade_out_offset = fade_interval_start - internal_offset;
590 /* READ DATA FROM THE SOURCE INTO mixdown_buffer.
591 We can never read directly into buf, since it may contain data
592 from a region `below' this one in the stack, and our fades (if they exist)
593 may need to mix with the existing data.
596 if (read_from_sources (_sources, _length, mixdown_buffer, position, to_read, chan_n) != to_read) {
600 /* APPLY REGULAR GAIN CURVES AND SCALING TO mixdown_buffer */
602 if (envelope_active()) {
603 _envelope->curve().get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
605 if (_scale_amplitude != 1.0f) {
606 for (framecnt_t n = 0; n < to_read; ++n) {
607 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
610 for (framecnt_t n = 0; n < to_read; ++n) {
611 mixdown_buffer[n] *= gain_buffer[n];
614 } else if (_scale_amplitude != 1.0f) {
615 apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
618 /* APPLY FADES TO THE DATA IN mixdown_buffer AND MIX THE RESULTS INTO
619 * buf. The key things to realize here: (1) the fade being applied is
620 * (as of April 26th 2012) just the inverse of the fade in curve (2)
621 * "buf" contains data from lower regions already. So this operation
622 * fades out the existing material.
625 if (fade_in_limit != 0) {
628 if (_inverse_fade_in) {
630 /* explicit inverse fade in curve (e.g. for constant
631 * power), so we have to fetch it.
634 _inverse_fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
636 /* Fade the data from lower layers out */
637 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
638 buf[n] *= gain_buffer[n];
641 /* refill gain buffer with the fade in */
643 _fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
647 /* no explicit inverse fade in, so just use (1 - fade
648 * in) for the fade out of lower layers
651 _fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
653 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
654 buf[n] *= 1 - gain_buffer[n];
658 _fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
661 /* Mix our newly-read data in, with the fade */
662 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
663 buf[n] += mixdown_buffer[n] * gain_buffer[n];
667 if (fade_out_limit != 0) {
669 framecnt_t const curve_offset = fade_interval_start - (_length - _fade_out->back()->when);
672 if (_inverse_fade_out) {
674 _inverse_fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
676 /* Fade the data from lower levels in */
677 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
678 buf[m] *= gain_buffer[n];
681 /* fetch the actual fade out */
683 _fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
687 /* no explicit inverse fade out (which is
688 * actually a fade in), so just use (1 - fade
689 * out) for the fade in of lower layers
692 _fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
694 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
695 buf[m] *= 1 - gain_buffer[n];
699 _fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
702 /* Mix our newly-read data with whatever was already there,
703 with the fade out applied to our data.
705 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
706 buf[m] += mixdown_buffer[m] * gain_buffer[n];
710 /* MIX OR COPY THE REGION BODY FROM mixdown_buffer INTO buf */
712 framecnt_t const N = to_read - fade_in_limit - fade_out_limit;
715 memcpy (buf + fade_in_limit, mixdown_buffer + fade_in_limit, N * sizeof (Sample));
717 mix_buffers_no_gain (buf + fade_in_limit, mixdown_buffer + fade_in_limit, N);
724 /** Read data directly from one of our sources, accounting for the situation when the track has a different channel
725 * count to the region.
727 * @param srcs Source list to get our source from.
728 * @param limit Furthest that we should read, as an offset from the region position.
729 * @param buf Buffer to write data into (existing contents of the buffer will be overwritten)
730 * @param position Position to read from, in session frames.
731 * @param cnt Number of frames to read.
732 * @param chan_n Channel to read from.
733 * @return Number of frames read.
737 AudioRegion::read_from_sources (SourceList const & srcs, framecnt_t limit, Sample* buf, framepos_t position, framecnt_t cnt, uint32_t chan_n) const
739 frameoffset_t const internal_offset = position - _position;
740 if (internal_offset >= limit) {
744 framecnt_t const to_read = min (cnt, limit - internal_offset);
749 if (chan_n < n_channels()) {
751 boost::shared_ptr<AudioSource> src = boost::dynamic_pointer_cast<AudioSource> (srcs[chan_n]);
752 if (src->read (buf, _start + internal_offset, to_read) != to_read) {
753 return 0; /* "read nothing" */
758 /* track is N-channel, this region has fewer channels; silence the ones
762 if (Config->get_replicate_missing_region_channels()) {
764 /* copy an existing channel's data in for this non-existant one */
766 uint32_t channel = n_channels() % chan_n;
767 boost::shared_ptr<AudioSource> src = boost::dynamic_pointer_cast<AudioSource> (srcs[channel]);
769 if (src->read (buf, _start + internal_offset, to_read) != to_read) {
770 return 0; /* "read nothing" */
776 memset (buf, 0, sizeof (Sample) * to_read);
784 AudioRegion::state ()
786 XMLNode& node (Region::state ());
789 LocaleGuard lg (X_("POSIX"));
791 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
792 node.add_property ("channels", buf);
794 Stateful::add_properties (node);
796 child = node.add_child ("Envelope");
798 bool default_env = false;
800 // If there are only two points, the points are in the start of the region and the end of the region
801 // so, if they are both at 1.0f, that means the default region.
803 if (_envelope->size() == 2 &&
804 _envelope->front()->value == 1.0f &&
805 _envelope->back()->value==1.0f) {
806 if (_envelope->front()->when == 0 && _envelope->back()->when == _length) {
812 child->add_property ("default", "yes");
814 child->add_child_nocopy (_envelope->get_state ());
817 child = node.add_child (X_("FadeIn"));
819 if (_default_fade_in) {
820 child->add_property ("default", "yes");
822 child->add_child_nocopy (_fade_in->get_state ());
825 if (_inverse_fade_in) {
826 child = node.add_child (X_("InvFadeIn"));
827 child->add_child_nocopy (_inverse_fade_in->get_state ());
830 child = node.add_child (X_("FadeOut"));
832 if (_default_fade_out) {
833 child->add_property ("default", "yes");
835 child->add_child_nocopy (_fade_out->get_state ());
838 if (_inverse_fade_out) {
839 child = node.add_child (X_("InvFadeOut"));
840 child->add_child_nocopy (_inverse_fade_out->get_state ());
847 AudioRegion::_set_state (const XMLNode& node, int version, PropertyChange& what_changed, bool send)
849 const XMLNodeList& nlist = node.children();
850 const XMLProperty *prop;
851 LocaleGuard lg (X_("POSIX"));
852 boost::shared_ptr<Playlist> the_playlist (_playlist.lock());
854 suspend_property_changes ();
857 the_playlist->freeze ();
861 /* this will set all our State members and stuff controlled by the Region.
862 It should NOT send any changed signals - that is our responsibility.
865 Region::_set_state (node, version, what_changed, false);
867 if ((prop = node.property ("scale-gain")) != 0) {
868 float a = atof (prop->value().c_str());
869 if (a != _scale_amplitude) {
870 _scale_amplitude = a;
871 what_changed.add (Properties::scale_amplitude);
875 /* Now find envelope description and other related child items */
877 _envelope->freeze ();
879 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
885 if (child->name() == "Envelope") {
889 if ((prop = child->property ("default")) != 0 || _envelope->set_state (*child, version)) {
890 set_default_envelope ();
893 _envelope->truncate_end (_length);
896 } else if (child->name() == "FadeIn") {
900 if (((prop = child->property ("default")) != 0 && string_is_affirmative (prop->value())) || (prop = child->property ("steepness")) != 0) {
901 set_default_fade_in ();
903 XMLNode* grandchild = child->child ("AutomationList");
905 _fade_in->set_state (*grandchild, version);
909 if ((prop = child->property ("active")) != 0) {
910 if (string_is_affirmative (prop->value())) {
911 set_fade_in_active (true);
913 set_fade_in_active (false);
919 if ((prop = child->property ("is-xfade")) != 0) {
920 _fade_in_is_xfade = string_is_affirmative (prop->value());
923 } else if (child->name() == "FadeOut") {
927 if (((prop = child->property ("default")) != 0 && (string_is_affirmative (prop->value()))) || (prop = child->property ("steepness")) != 0) {
928 set_default_fade_out ();
930 XMLNode* grandchild = child->child ("AutomationList");
932 _fade_out->set_state (*grandchild, version);
936 if ((prop = child->property ("active")) != 0) {
937 if (string_is_affirmative (prop->value())) {
938 set_fade_out_active (true);
940 set_fade_out_active (false);
946 if ((prop = child->property ("is-xfade")) != 0) {
947 _fade_out_is_xfade = string_is_affirmative (prop->value());
950 } else if (child->name() == "InvFadeIn") {
951 XMLNode* grandchild = child->child ("AutomationList");
953 _inverse_fade_in->set_state (*grandchild, version);
955 } else if (child->name() == "InvFadeOut") {
956 XMLNode* grandchild = child->child ("AutomationList");
958 _inverse_fade_out->set_state (*grandchild, version);
964 resume_property_changes ();
967 send_change (what_changed);
971 the_playlist->thaw ();
978 AudioRegion::set_state (const XMLNode& node, int version)
980 PropertyChange what_changed;
981 return _set_state (node, version, what_changed, true);
985 AudioRegion::set_fade_in_shape (FadeShape shape)
987 set_fade_in (shape, (framecnt_t) _fade_in->back()->when);
991 AudioRegion::set_fade_out_shape (FadeShape shape)
993 set_fade_out (shape, (framecnt_t) _fade_out->back()->when);
997 AudioRegion::set_fade_in (boost::shared_ptr<AutomationList> f)
1000 *(_fade_in.val()) = *f;
1002 _default_fade_in = false;
1004 send_change (PropertyChange (Properties::fade_in));
1008 AudioRegion::set_fade_in (FadeShape shape, framecnt_t len)
1010 boost::shared_ptr<Evoral::ControlList> c1 (new Evoral::ControlList (FadeInAutomation));
1011 boost::shared_ptr<Evoral::ControlList> c2 (new Evoral::ControlList (FadeInAutomation));
1012 boost::shared_ptr<Evoral::ControlList> c3 (new Evoral::ControlList (FadeInAutomation));
1014 _fade_in->freeze ();
1016 _inverse_fade_in->clear ();
1020 _fade_in->fast_simple_add (0.0, 0.0);
1021 _fade_in->fast_simple_add (len, 1.0);
1022 reverse_curve (_inverse_fade_in.val(), _fade_in.val());
1026 generate_db_fade (_fade_in.val(), len, 10, -60);
1027 reverse_curve (c1, _fade_in.val());
1028 _fade_in->copy_events (*c1);
1029 generate_inverse_power_curve (_inverse_fade_in.val(), _fade_in.val());
1033 generate_db_fade (c1, len, 10, -1); // start off with a slow fade
1034 generate_db_fade (c2, len, 10, -80); // end with a fast fade
1035 merge_curves (_fade_in.val(), c1, c2);
1036 reverse_curve (c3, _fade_in.val());
1037 _fade_in->copy_events (*c3);
1038 generate_inverse_power_curve (_inverse_fade_in.val(), _fade_in.val());
1041 case FadeConstantPower:
1042 for (int i = 0; i < 9; ++i) {
1043 float dist = (float) i / 10.0f;
1044 _fade_in->fast_simple_add (len*dist, sin (dist*M_PI/2));
1046 _fade_in->fast_simple_add (len, 1.0);
1047 reverse_curve (_inverse_fade_in.val(), _fade_in.val());
1051 //start with a nearly linear cuve
1052 _fade_in->fast_simple_add (0, 1);
1053 _fade_in->fast_simple_add (0.5*len, 0.6);
1054 //now generate a fade-out curve by successively applying a gain drop
1055 const float breakpoint = 0.7; //linear for first 70%
1056 const int num_steps = 9;
1057 for (int i = 2; i < num_steps; i++) {
1058 float coeff = (1.0-breakpoint);
1059 for (int j = 0; j < i; j++) {
1060 coeff *= 0.5; //6dB drop per step
1062 _fade_in->fast_simple_add (len* (breakpoint+((1.0-breakpoint)*(double)i/(double)num_steps)), coeff);
1064 _fade_in->fast_simple_add (len, VERY_SMALL_SIGNAL);
1065 reverse_curve (c3, _fade_in.val());
1066 _fade_in->copy_events (*c3);
1067 reverse_curve (_inverse_fade_in.val(), _fade_in.val());
1071 _default_fade_in = false;
1073 send_change (PropertyChange (Properties::fade_in));
1077 AudioRegion::set_fade_out (boost::shared_ptr<AutomationList> f)
1079 _fade_out->freeze ();
1080 *(_fade_out.val()) = *f;
1082 _default_fade_out = false;
1084 send_change (PropertyChange (Properties::fade_in));
1088 AudioRegion::set_fade_out (FadeShape shape, framecnt_t len)
1090 boost::shared_ptr<Evoral::ControlList> c1 (new Evoral::ControlList (FadeOutAutomation));
1091 boost::shared_ptr<Evoral::ControlList> c2 (new Evoral::ControlList (FadeOutAutomation));
1093 _fade_out->freeze ();
1094 _fade_out->clear ();
1095 _inverse_fade_out->clear ();
1099 _fade_out->fast_simple_add (0.0, 1.0);
1100 _fade_out->fast_simple_add (len, VERY_SMALL_SIGNAL);
1101 reverse_curve (_inverse_fade_out.val(), _fade_out.val());
1105 generate_db_fade (_fade_out.val(), len, 10, -60);
1106 generate_inverse_power_curve (_inverse_fade_out.val(), _fade_out.val());
1110 generate_db_fade (c1, len, 10, -1); //start off with a slow fade
1111 generate_db_fade (c2, len, 10, -80); //end with a fast fade
1112 merge_curves (_fade_out.val(), c1, c2);
1113 generate_inverse_power_curve (_inverse_fade_out.val(), _fade_out.val());
1116 case FadeConstantPower:
1117 //constant-power fades use a sin/cos relationship
1118 //the cutoff is abrupt but it has the benefit of being symmetrical
1119 _fade_out->fast_simple_add (0.0, 1.0);
1120 for (int i = 1; i < 9; i++ ) {
1121 float dist = (float)i/10.0;
1122 _fade_out->fast_simple_add ((len * dist), cos(dist*M_PI/2));
1124 _fade_out->fast_simple_add (len, VERY_SMALL_SIGNAL);
1125 reverse_curve (_inverse_fade_out.val(), _fade_out.val());
1129 //start with a nearly linear cuve
1130 _fade_out->fast_simple_add (0, 1);
1131 _fade_out->fast_simple_add (0.5*len, 0.6);
1133 //now generate a fade-out curve by successively applying a gain drop
1134 const float breakpoint = 0.7; //linear for first 70%
1135 const int num_steps = 9;
1136 for (int i = 2; i < num_steps; i++) {
1137 float coeff = (1.0-breakpoint);
1138 for (int j = 0; j < i; j++) {
1139 coeff *= 0.5; //6dB drop per step
1141 _fade_out->fast_simple_add (len* (breakpoint+((1.0-breakpoint)*(double)i/(double)num_steps)), coeff);
1143 _fade_out->fast_simple_add (len, VERY_SMALL_SIGNAL);
1144 reverse_curve (_inverse_fade_out.val(), _fade_out.val());
1148 _default_fade_out = false;
1150 send_change (PropertyChange (Properties::fade_out));
1154 AudioRegion::set_fade_in_length (framecnt_t len)
1156 if (len > _length) {
1164 bool changed = _fade_in->extend_to (len);
1167 if (_inverse_fade_in) {
1168 _inverse_fade_in->extend_to (len);
1171 if (_session.config.get_xfade_model() == FullCrossfade &&
1172 _session.config.get_auto_xfade() &&
1173 _fade_in_is_xfade && !_fade_in_is_short) {
1175 /* trim a single other region below us to the new start
1179 boost::shared_ptr<Region> other = get_single_other_xfade_region (true);
1181 other->trim_end (position() + 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 if (_session.config.get_xfade_model() == FullCrossfade &&
1211 _session.config.get_auto_xfade() &&
1212 _fade_out_is_xfade && !_fade_out_is_short) {
1214 /* trim a single other region below us to the new start
1218 boost::shared_ptr<Region> other = get_single_other_xfade_region (false);
1220 other->trim_front (last_frame() - len);
1224 send_change (PropertyChange (Properties::fade_out));
1229 AudioRegion::set_fade_in_active (bool yn)
1231 if (yn == _fade_in_active) {
1235 _fade_in_active = yn;
1236 send_change (PropertyChange (Properties::fade_in_active));
1240 AudioRegion::set_fade_out_active (bool yn)
1242 if (yn == _fade_out_active) {
1245 _fade_out_active = yn;
1246 send_change (PropertyChange (Properties::fade_out_active));
1250 AudioRegion::fade_in_is_default () const
1252 return _fade_in->size() == 2 && _fade_in->front()->when == 0 && _fade_in->back()->when == 64;
1256 AudioRegion::fade_out_is_default () const
1258 return _fade_out->size() == 2 && _fade_out->front()->when == 0 && _fade_out->back()->when == 64;
1262 AudioRegion::set_default_fade_in ()
1264 _fade_in_suspended = 0;
1265 _fade_in_is_xfade = false;
1266 _fade_in_is_short = true;
1267 set_fade_in (FadeLinear, 64);
1271 AudioRegion::set_default_fade_out ()
1273 _fade_out_suspended = 0;
1274 _fade_out_is_xfade = false;
1275 _fade_out_is_short = true;
1276 set_fade_out (FadeLinear, 64);
1280 AudioRegion::set_default_fades ()
1282 set_default_fade_in ();
1283 set_default_fade_out ();
1287 AudioRegion::set_default_envelope ()
1289 _envelope->freeze ();
1290 _envelope->clear ();
1291 _envelope->fast_simple_add (0, 1.0f);
1292 _envelope->fast_simple_add (_length, 1.0f);
1297 AudioRegion::recompute_at_end ()
1299 /* our length has changed. recompute a new final point by interpolating
1300 based on the the existing curve.
1303 _envelope->freeze ();
1304 _envelope->truncate_end (_length);
1307 suspend_property_changes();
1309 if (_left_of_split) {
1310 set_default_fade_out ();
1311 _left_of_split = false;
1312 } else if (_fade_out->back()->when > _length) {
1313 _fade_out->extend_to (_length);
1314 send_change (PropertyChange (Properties::fade_out));
1317 if (_fade_in->back()->when > _length) {
1318 _fade_in->extend_to (_length);
1319 send_change (PropertyChange (Properties::fade_in));
1322 resume_property_changes();
1326 AudioRegion::recompute_at_start ()
1328 /* as above, but the shift was from the front */
1330 _envelope->truncate_start (_length);
1332 suspend_property_changes();
1334 if (_right_of_split) {
1335 set_default_fade_in ();
1336 _right_of_split = false;
1337 } else if (_fade_in->back()->when > _length) {
1338 _fade_in->extend_to (_length);
1339 send_change (PropertyChange (Properties::fade_in));
1342 if (_fade_out->back()->when > _length) {
1343 _fade_out->extend_to (_length);
1344 send_change (PropertyChange (Properties::fade_out));
1347 resume_property_changes();
1351 AudioRegion::separate_by_channel (Session& /*session*/, vector<boost::shared_ptr<Region> >& v) const
1357 if (_sources.size() < 2) {
1361 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
1363 srcs.push_back (*i);
1367 if (_sources.size() == 2) {
1375 new_name += ('0' + n + 1);
1378 /* create a copy with just one source. prevent if from being thought of as
1379 "whole file" even if it covers the entire source file(s).
1384 plist.add (Properties::start, _start.val());
1385 plist.add (Properties::length, _length.val());
1386 plist.add (Properties::name, new_name);
1387 plist.add (Properties::layer, layer ());
1389 v.push_back(RegionFactory::create (srcs, plist));
1390 v.back()->set_whole_file (false);
1399 AudioRegion::read_raw_internal (Sample* buf, framepos_t pos, framecnt_t cnt, int channel) const
1401 return audio_source(channel)->read (buf, pos, cnt);
1405 AudioRegion::set_scale_amplitude (gain_t g)
1407 boost::shared_ptr<Playlist> pl (playlist());
1409 _scale_amplitude = g;
1411 /* tell the diskstream we're in */
1414 pl->ContentsChanged();
1417 /* tell everybody else */
1419 send_change (PropertyChange (Properties::scale_amplitude));
1422 /** @return the maximum (linear) amplitude of the region, or a -ve
1423 * number if the Progress object reports that the process was cancelled.
1426 AudioRegion::maximum_amplitude (Progress* p) const
1428 framepos_t fpos = _start;
1429 framepos_t const fend = _start + _length;
1432 framecnt_t const blocksize = 64 * 1024;
1433 Sample buf[blocksize];
1435 while (fpos < fend) {
1439 framecnt_t const to_read = min (fend - fpos, blocksize);
1441 for (n = 0; n < n_channels(); ++n) {
1445 if (read_raw_internal (buf, fpos, to_read, n) != to_read) {
1449 maxamp = compute_peak (buf, to_read, maxamp);
1454 p->set_progress (float (fpos - _start) / _length);
1455 if (p->cancelled ()) {
1464 /** Normalize using a given maximum amplitude and target, so that region
1465 * _scale_amplitude becomes target / max_amplitude.
1468 AudioRegion::normalize (float max_amplitude, float target_dB)
1470 gain_t target = dB_to_coefficient (target_dB);
1472 if (target == 1.0f) {
1473 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1474 that we may have clipped.
1476 target -= FLT_EPSILON;
1479 if (max_amplitude == 0.0f) {
1480 /* don't even try */
1484 if (max_amplitude == target) {
1485 /* we can't do anything useful */
1489 set_scale_amplitude (target / max_amplitude);
1493 AudioRegion::fade_in_changed ()
1495 send_change (PropertyChange (Properties::fade_in));
1499 AudioRegion::fade_out_changed ()
1501 send_change (PropertyChange (Properties::fade_out));
1505 AudioRegion::envelope_changed ()
1507 send_change (PropertyChange (Properties::envelope));
1511 AudioRegion::suspend_fade_in ()
1513 if (++_fade_in_suspended == 1) {
1514 if (fade_in_is_default()) {
1515 set_fade_in_active (false);
1521 AudioRegion::resume_fade_in ()
1523 if (--_fade_in_suspended == 0 && _fade_in_suspended) {
1524 set_fade_in_active (true);
1529 AudioRegion::suspend_fade_out ()
1531 if (++_fade_out_suspended == 1) {
1532 if (fade_out_is_default()) {
1533 set_fade_out_active (false);
1539 AudioRegion::resume_fade_out ()
1541 if (--_fade_out_suspended == 0 &&_fade_out_suspended) {
1542 set_fade_out_active (true);
1547 AudioRegion::speed_mismatch (float sr) const
1549 if (_sources.empty()) {
1550 /* impossible, but ... */
1554 float fsr = audio_source()->sample_rate();
1560 AudioRegion::source_offset_changed ()
1562 /* XXX this fixes a crash that should not occur. It does occur
1563 becauses regions are not being deleted when a session
1564 is unloaded. That bug must be fixed.
1567 if (_sources.empty()) {
1571 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(_sources.front());
1573 if (afs && afs->destructive()) {
1574 // set_start (source()->natural_position(), this);
1575 set_position (source()->natural_position());
1579 boost::shared_ptr<AudioSource>
1580 AudioRegion::audio_source (uint32_t n) const
1582 // Guaranteed to succeed (use a static cast for speed?)
1583 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1587 AudioRegion::adjust_transients (frameoffset_t delta)
1589 for (AnalysisFeatureList::iterator x = _transients.begin(); x != _transients.end(); ++x) {
1590 (*x) = (*x) + delta;
1593 send_change (PropertyChange (Properties::valid_transients));
1599 AudioRegion::update_transient (framepos_t old_position, framepos_t new_position)
1601 for (AnalysisFeatureList::iterator x = _transients.begin(); x != _transients.end(); ++x) {
1602 if ((*x) == old_position) {
1603 (*x) = new_position;
1604 send_change (PropertyChange (Properties::valid_transients));
1614 AudioRegion::add_transient (framepos_t where)
1616 _transients.push_back(where);
1617 _valid_transients = true;
1619 send_change (PropertyChange (Properties::valid_transients));
1623 AudioRegion::remove_transient (framepos_t where)
1625 _transients.remove(where);
1626 _valid_transients = true;
1628 send_change (PropertyChange (Properties::valid_transients));
1632 AudioRegion::set_transients (AnalysisFeatureList& results)
1634 _transients.clear();
1635 _transients = results;
1636 _valid_transients = true;
1638 send_change (PropertyChange (Properties::valid_transients));
1644 AudioRegion::get_transients (AnalysisFeatureList& results, bool force_new)
1646 boost::shared_ptr<Playlist> pl = playlist();
1652 if (_valid_transients && !force_new) {
1653 results = _transients;
1657 SourceList::iterator s;
1659 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1660 if (!(*s)->has_been_analysed()) {
1661 cerr << "For " << name() << " source " << (*s)->name() << " has not been analyzed\n";
1666 if (s == _sources.end()) {
1667 /* all sources are analyzed, merge data from each one */
1669 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1671 /* find the set of transients within the bounds of this region */
1673 AnalysisFeatureList::iterator low = lower_bound ((*s)->transients.begin(),
1674 (*s)->transients.end(),
1677 AnalysisFeatureList::iterator high = upper_bound ((*s)->transients.begin(),
1678 (*s)->transients.end(),
1683 results.insert (results.end(), low, high);
1686 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1688 /* translate all transients to current position */
1690 for (AnalysisFeatureList::iterator x = results.begin(); x != results.end(); ++x) {
1695 _transients = results;
1696 _valid_transients = true;
1701 /* no existing/complete transient info */
1703 static bool analyse_dialog_shown = false; /* global per instance of Ardour */
1705 if (!Config->get_auto_analyse_audio()) {
1706 if (!analyse_dialog_shown) {
1707 pl->session().Dialog (_("\
1708 You have requested an operation that requires audio analysis.\n\n\
1709 You currently have \"auto-analyse-audio\" disabled, which means \
1710 that transient data must be generated every time it is required.\n\n\
1711 If you are doing work that will require transient data on a \
1712 regular basis, you should probably enable \"auto-analyse-audio\" \
1713 then quit ardour and restart.\n\n\
1714 This dialog will not display again. But you may notice a slight delay \
1715 in this and future transient-detection operations.\n\
1717 analyse_dialog_shown = true;
1721 TransientDetector t (pl->session().frame_rate());
1722 bool existing_results = !results.empty();
1724 _transients.clear ();
1725 _valid_transients = false;
1727 for (uint32_t i = 0; i < n_channels(); ++i) {
1729 AnalysisFeatureList these_results;
1733 if (t.run ("", this, i, these_results)) {
1737 /* translate all transients to give absolute position */
1739 for (AnalysisFeatureList::iterator i = these_results.begin(); i != these_results.end(); ++i) {
1745 _transients.insert (_transients.end(), these_results.begin(), these_results.end());
1748 if (!results.empty()) {
1749 if (existing_results) {
1751 /* merge our transients into the existing ones, then clean up
1755 results.insert (results.end(), _transients.begin(), _transients.end());
1756 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1759 /* make sure ours are clean too */
1761 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1765 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1766 results = _transients;
1769 _valid_transients = true;
1774 /** Find areas of `silence' within a region.
1776 * @param threshold Threshold below which signal is considered silence (as a sample value)
1777 * @param min_length Minimum length of silent period to be reported.
1778 * @return Silent intervals, measured relative to the region start in the source
1782 AudioRegion::find_silence (Sample threshold, framecnt_t min_length, InterThreadInfo& itt) const
1784 framecnt_t const block_size = 64 * 1024;
1785 boost::scoped_array<Sample> loudest (new Sample[block_size]);
1786 boost::scoped_array<Sample> buf (new Sample[block_size]);
1788 framepos_t pos = _start;
1789 framepos_t const end = _start + _length - 1;
1791 AudioIntervalResult silent_periods;
1793 bool in_silence = false;
1794 frameoffset_t silence_start = 0;
1796 while (pos < end && !itt.cancel) {
1798 /* fill `loudest' with the loudest absolute sample at each instant, across all channels */
1799 memset (loudest.get(), 0, sizeof (Sample) * block_size);
1800 for (uint32_t n = 0; n < n_channels(); ++n) {
1802 read_raw_internal (buf.get(), pos, block_size, n);
1803 for (framecnt_t i = 0; i < block_size; ++i) {
1804 loudest[i] = max (loudest[i], abs (buf[i]));
1808 /* now look for silence */
1809 for (framecnt_t i = 0; i < block_size; ++i) {
1810 bool const silence = abs (loudest[i]) < threshold;
1811 if (silence && !in_silence) {
1812 /* non-silence to silence */
1814 silence_start = pos + i;
1815 } else if (!silence && in_silence) {
1816 /* silence to non-silence */
1818 if (pos + i - 1 - silence_start >= min_length) {
1819 silent_periods.push_back (std::make_pair (silence_start, pos + i - 1));
1825 itt.progress = (end-pos)/(double)_length;
1828 if (in_silence && end - 1 - silence_start >= min_length) {
1829 /* last block was silent, so finish off the last period */
1830 silent_periods.push_back (std::make_pair (silence_start, end));
1835 return silent_periods;
1838 Evoral::Range<framepos_t>
1839 AudioRegion::body_range () const
1841 return Evoral::Range<framepos_t> (first_frame() + _fade_in->back()->when + 1, last_frame() - _fade_out->back()->when);
1845 AudioRegion::set_fade_in_is_xfade (bool yn)
1847 if (yn == _fade_in_is_xfade) {
1851 _fade_in_is_xfade = yn;
1852 send_change (PropertyChange (Properties::fade_in_is_xfade));
1856 AudioRegion::set_fade_out_is_xfade (bool yn)
1858 if (yn == _fade_out_is_xfade) {
1862 _fade_out_is_xfade = yn;
1863 send_change (PropertyChange (Properties::fade_out_is_xfade));
1867 AudioRegion::set_fade_in_is_short (bool yn)
1869 if (yn == _fade_in_is_short) {
1873 _fade_in_is_short = yn;
1874 send_change (PropertyChange (Properties::fade_in_is_short));
1879 AudioRegion::set_fade_out_is_short (bool yn)
1881 if (yn == _fade_out_is_short) {
1885 _fade_out_is_short = yn;
1886 send_change (PropertyChange (Properties::fade_out_is_short));
1889 boost::shared_ptr<Region>
1890 AudioRegion::get_single_other_xfade_region (bool start) const
1892 boost::shared_ptr<Playlist> pl (playlist());
1895 /* not currently in a playlist - xfade length is unbounded
1898 return boost::shared_ptr<AudioRegion> ();
1901 boost::shared_ptr<RegionList> rl;
1904 rl = pl->regions_at (position());
1906 rl = pl->regions_at (last_frame());
1909 RegionList::iterator i;
1910 boost::shared_ptr<Region> other;
1913 /* count and find the other region in a single pass through the list */
1915 for (i = rl->begin(); i != rl->end(); ++i) {
1916 if ((*i).get() != this) {
1923 /* zero or multiple regions stacked here - don't care about xfades */
1924 return boost::shared_ptr<AudioRegion> ();
1931 AudioRegion::verify_xfade_bounds (framecnt_t len, bool start)
1933 /* this is called from a UI to check on whether a new proposed
1934 length for an xfade is legal or not. it returns the legal
1935 length corresponding to @a len which may be shorter than or
1936 equal to @a len itself.
1939 boost::shared_ptr<Region> other = get_single_other_xfade_region (start);
1943 /* zero or > 2 regions here, don't care about len, but
1944 it can't be longer than the region itself.
1946 return min (length(), len);
1949 /* we overlap a single region. clamp the length of an xfade to
1950 the maximum possible duration of the overlap (if the other
1951 region were trimmed appropriately).
1955 maxlen = other->latest_possible_frame() - position();
1957 maxlen = last_frame() - other->earliest_possible_position();
1960 return min (length(), min (maxlen, len));
1966 int region_read_peaks_from_c (void *arg, uint32_t npeaks, uint32_t start, uint32_t cnt, intptr_t data, uint32_t n_chan, double samples_per_unit)
1968 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (framecnt_t) npeaks, (framepos_t) start, (framecnt_t) cnt, n_chan,samples_per_unit);
1971 uint32_t region_length_from_c (void *arg)
1974 return ((AudioRegion *) arg)->length();
1977 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1979 return ( (AudioRegion *) arg)->audio_source()->available_peaks (zoom_factor) ;