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/debug.h"
41 #include "ardour/session.h"
42 #include "ardour/dB.h"
43 #include "ardour/playlist.h"
44 #include "ardour/audiofilesource.h"
45 #include "ardour/region_factory.h"
46 #include "ardour/runtime_functions.h"
47 #include "ardour/transient_detector.h"
48 #include "ardour/progress.h"
54 using namespace ARDOUR;
58 namespace Properties {
59 PBD::PropertyDescriptor<bool> envelope_active;
60 PBD::PropertyDescriptor<bool> default_fade_in;
61 PBD::PropertyDescriptor<bool> default_fade_out;
62 PBD::PropertyDescriptor<bool> fade_in_active;
63 PBD::PropertyDescriptor<bool> fade_out_active;
64 PBD::PropertyDescriptor<float> scale_amplitude;
69 AudioRegion::make_property_quarks ()
71 Properties::envelope_active.property_id = g_quark_from_static_string (X_("envelope-active"));
72 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for envelope-active = %1\n", Properties::envelope_active.property_id));
73 Properties::default_fade_in.property_id = g_quark_from_static_string (X_("default-fade-in"));
74 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for default-fade-in = %1\n", Properties::default_fade_in.property_id));
75 Properties::default_fade_out.property_id = g_quark_from_static_string (X_("default-fade-out"));
76 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for default-fade-out = %1\n", Properties::default_fade_out.property_id));
77 Properties::fade_in_active.property_id = g_quark_from_static_string (X_("fade-in-active"));
78 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fade-in-active = %1\n", Properties::fade_in_active.property_id));
79 Properties::fade_out_active.property_id = g_quark_from_static_string (X_("fade-out-active"));
80 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for fade-out-active = %1\n", Properties::fade_out_active.property_id));
81 Properties::scale_amplitude.property_id = g_quark_from_static_string (X_("scale-amplitude"));
82 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for scale-amplitude = %1\n", Properties::scale_amplitude.property_id));
86 AudioRegion::register_properties ()
88 /* no need to register parent class properties */
90 add_property (_envelope_active);
91 add_property (_default_fade_in);
92 add_property (_default_fade_out);
93 add_property (_fade_in_active);
94 add_property (_fade_out_active);
95 add_property (_scale_amplitude);
98 #define AUDIOREGION_STATE_DEFAULT \
99 _envelope_active (Properties::envelope_active, false) \
100 , _default_fade_in (Properties::default_fade_in, true) \
101 , _default_fade_out (Properties::default_fade_out, true) \
102 , _fade_in_active (Properties::fade_in_active, true) \
103 , _fade_out_active (Properties::fade_out_active, true) \
104 , _scale_amplitude (Properties::scale_amplitude, 1.0)
106 #define AUDIOREGION_COPY_STATE(other) \
107 _envelope_active (Properties::envelope_active, other->_envelope_active) \
108 , _default_fade_in (Properties::default_fade_in, other->_default_fade_in) \
109 , _default_fade_out (Properties::default_fade_out, other->_default_fade_out) \
110 , _fade_in_active (Properties::fade_in_active, other->_fade_in_active) \
111 , _fade_out_active (Properties::fade_out_active, other->_fade_out_active) \
112 , _scale_amplitude (Properties::scale_amplitude, other->_scale_amplitude)
113 /* a Session will reset these to its chosen defaults by calling AudioRegion::set_default_fade() */
118 register_properties ();
120 suspend_property_changes();
121 set_default_fades ();
122 set_default_envelope ();
123 resume_property_changes();
125 listen_to_my_curves ();
126 connect_to_analysis_changed ();
127 connect_to_header_position_offset_changed ();
130 /** Constructor for use by derived types only */
131 AudioRegion::AudioRegion (Session& s, framepos_t start, framecnt_t len, std::string name)
132 : Region (s, start, len, name, DataType::AUDIO)
133 , AUDIOREGION_STATE_DEFAULT
135 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
136 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
137 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
138 , _fade_in_suspended (0)
139 , _fade_out_suspended (0)
140 , _fade_in_is_xfade (false)
141 , _fade_out_is_xfade (false)
144 assert (_sources.size() == _master_sources.size());
147 /** Basic AudioRegion constructor */
148 AudioRegion::AudioRegion (const SourceList& srcs)
150 , AUDIOREGION_STATE_DEFAULT
151 , _automatable(srcs[0]->session())
152 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
153 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
154 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
155 , _fade_in_suspended (0)
156 , _fade_out_suspended (0)
157 , _fade_in_is_xfade (false)
158 , _fade_out_is_xfade (false)
161 assert (_sources.size() == _master_sources.size());
164 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
166 , AUDIOREGION_COPY_STATE (other)
167 , _automatable (other->session())
168 , _fade_in (new AutomationList (*other->_fade_in))
169 , _fade_out (new AutomationList (*other->_fade_out))
170 /* As far as I can see, the _envelope's times are relative to region position, and have nothing
171 to do with sources (and hence _start). So when we copy the envelope, we just use the supplied offset.
173 , _envelope (new AutomationList (*other->_envelope, 0, other->_length))
174 , _fade_in_suspended (0)
175 , _fade_out_suspended (0)
176 , _fade_in_is_xfade (false)
177 , _fade_out_is_xfade (false)
179 /* don't use init here, because we got fade in/out from the other region
181 register_properties ();
182 listen_to_my_curves ();
183 connect_to_analysis_changed ();
184 connect_to_header_position_offset_changed ();
186 assert(_type == DataType::AUDIO);
187 assert (_sources.size() == _master_sources.size());
190 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, framecnt_t offset)
191 : Region (other, offset)
192 , AUDIOREGION_COPY_STATE (other)
193 , _automatable (other->session())
194 , _fade_in (new AutomationList (*other->_fade_in))
195 , _fade_out (new AutomationList (*other->_fade_out))
196 /* As far as I can see, the _envelope's times are relative to region position, and have nothing
197 to do with sources (and hence _start). So when we copy the envelope, we just use the supplied offset.
199 , _envelope (new AutomationList (*other->_envelope, offset, other->_length))
200 , _fade_in_suspended (0)
201 , _fade_out_suspended (0)
202 , _fade_in_is_xfade (false)
203 , _fade_out_is_xfade (false)
205 /* don't use init here, because we got fade in/out from the other region
207 register_properties ();
208 listen_to_my_curves ();
209 connect_to_analysis_changed ();
210 connect_to_header_position_offset_changed ();
212 assert(_type == DataType::AUDIO);
213 assert (_sources.size() == _master_sources.size());
216 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, const SourceList& srcs)
217 : Region (boost::static_pointer_cast<const Region>(other), srcs)
218 , AUDIOREGION_COPY_STATE (other)
219 , _automatable (other->session())
220 , _fade_in (new AutomationList (*other->_fade_in))
221 , _fade_out (new AutomationList (*other->_fade_out))
222 , _envelope (new AutomationList (*other->_envelope))
223 , _fade_in_suspended (0)
224 , _fade_out_suspended (0)
225 , _fade_in_is_xfade (false)
226 , _fade_out_is_xfade (false)
228 /* make-a-sort-of-copy-with-different-sources constructor (used by audio filter) */
230 register_properties ();
232 listen_to_my_curves ();
233 connect_to_analysis_changed ();
234 connect_to_header_position_offset_changed ();
236 assert (_sources.size() == _master_sources.size());
239 AudioRegion::AudioRegion (SourceList& srcs)
241 , AUDIOREGION_STATE_DEFAULT
242 , _automatable(srcs[0]->session())
243 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
244 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
245 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
246 , _fade_in_suspended (0)
247 , _fade_out_suspended (0)
248 , _fade_in_is_xfade (false)
249 , _fade_out_is_xfade (false)
253 assert(_type == DataType::AUDIO);
254 assert (_sources.size() == _master_sources.size());
257 AudioRegion::~AudioRegion ()
262 AudioRegion::post_set (const PropertyChange& /*ignored*/)
265 _sync_position = _start;
268 /* return to default fades if the existing ones are too long */
270 if (_left_of_split) {
271 if (_fade_in->back()->when >= _length) {
272 set_default_fade_in ();
274 set_default_fade_out ();
275 _left_of_split = false;
278 if (_right_of_split) {
279 if (_fade_out->back()->when >= _length) {
280 set_default_fade_out ();
283 set_default_fade_in ();
284 _right_of_split = false;
287 /* If _length changed, adjust our gain envelope accordingly */
288 _envelope->truncate_end (_length);
292 AudioRegion::connect_to_analysis_changed ()
294 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
295 (*i)->AnalysisChanged.connect_same_thread (*this, boost::bind (&AudioRegion::invalidate_transients, this));
300 AudioRegion::connect_to_header_position_offset_changed ()
302 set<boost::shared_ptr<Source> > unique_srcs;
304 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
306 /* connect only once to HeaderPositionOffsetChanged, even if sources are replicated
309 if (unique_srcs.find (*i) == unique_srcs.end ()) {
310 unique_srcs.insert (*i);
311 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
313 afs->HeaderPositionOffsetChanged.connect_same_thread (*this, boost::bind (&AudioRegion::source_offset_changed, this));
320 AudioRegion::listen_to_my_curves ()
322 _envelope->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::envelope_changed, this));
323 _fade_in->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::fade_in_changed, this));
324 _fade_out->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::fade_out_changed, this));
328 AudioRegion::set_envelope_active (bool yn)
330 if (envelope_active() != yn) {
331 _envelope_active = yn;
332 send_change (PropertyChange (Properties::envelope_active));
337 AudioRegion::read_peaks (PeakData *buf, framecnt_t npeaks, framecnt_t offset, framecnt_t cnt, uint32_t chan_n, double samples_per_unit) const
339 if (chan_n >= _sources.size()) {
343 if (audio_source(chan_n)->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
346 if (_scale_amplitude != 1.0f) {
347 for (framecnt_t n = 0; n < npeaks; ++n) {
348 buf[n].max *= _scale_amplitude;
349 buf[n].min *= _scale_amplitude;
356 /** @param buf Buffer to write data to (existing data will be overwritten).
357 * @param pos Position to read from as an offset from the region position.
358 * @param cnt Number of frames to read.
359 * @param channel Channel to read from.
362 AudioRegion::read (Sample* buf, framepos_t pos, framecnt_t cnt, int channel) const
364 /* raw read, no fades, no gain, nada */
365 return read_from_sources (_sources, _length, buf, _position + pos, cnt, channel);
369 AudioRegion::master_read_at (Sample *buf, Sample* /*mixdown_buffer*/, float* /*gain_buffer*/,
370 framepos_t position, framecnt_t cnt, uint32_t chan_n) const
372 /* do not read gain/scaling/fades and do not count this disk i/o in statistics */
375 return read_from_sources (
376 _master_sources, _master_sources.front()->length (_master_sources.front()->timeline_position()),
377 buf, position, cnt, chan_n
381 /** @param buf Buffer to mix data into.
382 * @param mixdown_buffer Scratch buffer for audio data.
383 * @param gain_buffer Scratch buffer for gain data.
384 * @param position Position within the session to read from.
385 * @param cnt Number of frames to read.
386 * @param chan_n Channel number to read.
389 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
392 uint32_t chan_n) const
394 /* We are reading data from this region into buf (possibly via mixdown_buffer).
395 The caller has verified that we cover the desired section.
398 /* See doc/region_read.svg for a drawing which might help to explain
404 if (n_channels() == 0) {
409 return 0; /* read nothing */
413 /* WORK OUT WHERE TO GET DATA FROM */
417 assert (position >= _position);
418 frameoffset_t const internal_offset = position - _position;
420 if (internal_offset >= _length) {
421 return 0; /* read nothing */
424 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
425 return 0; /* read nothing */
429 /* COMPUTE DETAILS OF ANY FADES INVOLVED IN THIS READ */
431 /* Amount of fade in that we are dealing with in this read */
432 framecnt_t fade_in_limit = 0;
434 /* Offset from buf / mixdown_buffer of the start
435 of any fade out that we are dealing with
437 frameoffset_t fade_out_offset = 0;
439 /* Amount of fade in that we are dealing with in this read */
440 framecnt_t fade_out_limit = 0;
442 framecnt_t fade_interval_start = 0;
446 if (_fade_in_active && _session.config.get_use_region_fades()) {
448 framecnt_t fade_in_length = (framecnt_t) _fade_in->back()->when;
450 /* see if this read is within the fade in */
452 if (internal_offset < fade_in_length) {
453 fade_in_limit = min (to_read, fade_in_length - internal_offset);
459 if (_fade_out_active && _session.config.get_use_region_fades()) {
461 /* see if some part of this read is within the fade out */
463 /* ................. >| REGION
469 _length - fade_out_length
472 ^internal_offset + to_read
474 we need the intersection of [internal_offset,internal_offset+to_read] with
475 [_length - fade_out_length, _length]
480 fade_interval_start = max (internal_offset, _length - framecnt_t (_fade_out->back()->when));
481 framecnt_t fade_interval_end = min(internal_offset + to_read, _length.val());
483 if (fade_interval_end > fade_interval_start) {
484 /* (part of the) the fade out is in this buffer */
485 fade_out_limit = fade_interval_end - fade_interval_start;
486 fade_out_offset = fade_interval_start - internal_offset;
490 /* READ DATA FROM THE SOURCE INTO mixdown_buffer.
491 We can never read directly into buf, since it may contain data
492 from a transparent region `below' this one in the stack; we
496 if (read_from_sources (_sources, _length, mixdown_buffer, position, to_read, chan_n) != to_read) {
500 /* APPLY REGULAR GAIN CURVES AND SCALING TO mixdown_buffer */
502 if (envelope_active()) {
503 _envelope->curve().get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
505 if (_scale_amplitude != 1.0f) {
506 for (framecnt_t n = 0; n < to_read; ++n) {
507 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
510 for (framecnt_t n = 0; n < to_read; ++n) {
511 mixdown_buffer[n] *= gain_buffer[n];
514 } else if (_scale_amplitude != 1.0f) {
515 apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
519 /* APPLY FADES TO THE DATA IN mixdown_buffer AND MIX THE RESULTS INTO
520 * buf. The key things to realize here: (1) the fade being applied is
521 * (as of April 26th 2012) just the inverse of the fade in curve (2)
522 * "buf" contains data from lower regions already. So this operation
523 * fades out the existing material.
526 if (fade_in_limit != 0) {
527 if (_inverse_fade_in) {
529 /* explicit inverse fade in curve (e.g. for constant
530 * power), so we have to fetch it.
533 _inverse_fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
535 /* Fade the data from lower layers out */
536 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
537 buf[n] *= gain_buffer[n];
540 /* refill gain buffer with the fade in */
542 _fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
546 /* no explicit inverse fade in, so just use (1 - fade
547 * in) for the fade out of lower layers
550 _fade_in->curve().get_vector (internal_offset, internal_offset + fade_in_limit, gain_buffer, fade_in_limit);
552 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
553 buf[n] *= 1 - gain_buffer[n];
557 /* Mix our newly-read data in, with the fade */
558 for (framecnt_t n = 0; n < fade_in_limit; ++n) {
559 buf[n] += mixdown_buffer[n] * gain_buffer[n];
563 if (fade_out_limit != 0) {
565 framecnt_t const curve_offset = fade_interval_start - (_length - _fade_out->back()->when);
567 if (_inverse_fade_out) {
569 _inverse_fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
571 /* Fade the data from lower levels out */
572 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
573 buf[m] *= gain_buffer[n];
576 /* fetch the actual fade out */
578 _fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
582 /* no explicit inverse fade out, so just use (1 - fade
583 * out) for the fade in of lower layers
586 _fade_out->curve().get_vector (curve_offset, curve_offset + fade_out_limit, gain_buffer, fade_out_limit);
588 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
589 buf[m] *= 1 - gain_buffer[n];
593 /* Mix our newly-read data out, with the fade */
594 for (framecnt_t n = 0, m = fade_out_offset; n < fade_out_limit; ++n, ++m) {
595 buf[m] += mixdown_buffer[m] * gain_buffer[n];
600 /* MIX THE REGION BODY FROM mixdown_buffer INTO buf */
602 mix_buffers_no_gain (buf + fade_in_limit, mixdown_buffer + fade_in_limit, to_read - fade_in_limit - fade_out_limit);
607 /** Read data directly from one of our sources, accounting for the situation when the track has a different channel
608 * count to the region.
610 * @param srcs Source list to get our source from.
611 * @param limit Furthest that we should read, as an offset from the region position.
612 * @param buf Buffer to write data into (existing contents of the buffer will be overwritten)
613 * @param position Position to read from, in session frames.
614 * @param cnt Number of frames to read.
615 * @param chan_n Channel to read from.
616 * @return Number of frames read.
620 AudioRegion::read_from_sources (SourceList const & srcs, framecnt_t limit, Sample* buf, framepos_t position, framecnt_t cnt, uint32_t chan_n) const
622 frameoffset_t const internal_offset = position - _position;
623 if (internal_offset >= limit) {
627 framecnt_t const to_read = min (cnt, limit - internal_offset);
632 if (chan_n < n_channels()) {
634 boost::shared_ptr<AudioSource> src = boost::dynamic_pointer_cast<AudioSource> (srcs[chan_n]);
635 if (src->read (buf, _start + internal_offset, to_read) != to_read) {
636 return 0; /* "read nothing" */
641 /* track is N-channel, this region has fewer channels; silence the ones
645 if (Config->get_replicate_missing_region_channels()) {
646 /* track is N-channel, this region has less channels, so use a relevant channel
649 uint32_t channel = n_channels() % chan_n;
650 boost::shared_ptr<AudioSource> src = boost::dynamic_pointer_cast<AudioSource> (srcs[channel]);
652 if (src->read (buf, _start + internal_offset, to_read) != to_read) {
653 return 0; /* "read nothing" */
662 AudioRegion::state ()
664 XMLNode& node (Region::state ());
667 LocaleGuard lg (X_("POSIX"));
669 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
670 node.add_property ("channels", buf);
672 Stateful::add_properties (node);
674 child = node.add_child ("Envelope");
676 bool default_env = false;
678 // If there are only two points, the points are in the start of the region and the end of the region
679 // so, if they are both at 1.0f, that means the default region.
681 if (_envelope->size() == 2 &&
682 _envelope->front()->value == 1.0f &&
683 _envelope->back()->value==1.0f) {
684 if (_envelope->front()->when == 0 && _envelope->back()->when == _length) {
690 child->add_property ("default", "yes");
692 child->add_child_nocopy (_envelope->get_state ());
695 child = node.add_child (X_("FadeIn"));
696 child->add_property ("is-xfade", (_fade_in_is_xfade ? "yes" : "no"));
698 if (_default_fade_in) {
699 child->add_property ("default", "yes");
701 child->add_child_nocopy (_fade_in->get_state ());
704 if (_inverse_fade_in) {
705 child = node.add_child (X_("InvFadeIn"));
706 child->add_child_nocopy (_inverse_fade_in->get_state ());
709 child = node.add_child (X_("FadeOut"));
710 child->add_property ("is-xfade", (_fade_out_is_xfade ? "yes" : "no"));
712 if (_default_fade_out) {
713 child->add_property ("default", "yes");
715 child->add_child_nocopy (_fade_out->get_state ());
718 if (_inverse_fade_out) {
719 child = node.add_child (X_("InvFadeOut"));
720 child->add_child_nocopy (_inverse_fade_out->get_state ());
727 AudioRegion::_set_state (const XMLNode& node, int version, PropertyChange& what_changed, bool send)
729 const XMLNodeList& nlist = node.children();
730 const XMLProperty *prop;
731 LocaleGuard lg (X_("POSIX"));
732 boost::shared_ptr<Playlist> the_playlist (_playlist.lock());
734 suspend_property_changes ();
737 the_playlist->freeze ();
741 /* this will set all our State members and stuff controlled by the Region.
742 It should NOT send any changed signals - that is our responsibility.
745 Region::_set_state (node, version, what_changed, false);
747 if ((prop = node.property ("scale-gain")) != 0) {
748 float a = atof (prop->value().c_str());
749 if (a != _scale_amplitude) {
750 _scale_amplitude = a;
751 what_changed.add (Properties::scale_amplitude);
755 /* Now find envelope description and other related child items */
757 _envelope->freeze ();
759 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
765 if (child->name() == "Envelope") {
769 if ((prop = child->property ("default")) != 0 || _envelope->set_state (*child, version)) {
770 set_default_envelope ();
773 _envelope->truncate_end (_length);
776 } else if (child->name() == "FadeIn") {
780 if (((prop = child->property ("default")) != 0 && string_is_affirmative (prop->value())) || (prop = child->property ("steepness")) != 0) {
781 set_default_fade_in ();
783 XMLNode* grandchild = child->child ("AutomationList");
785 _fade_in->set_state (*grandchild, version);
789 if ((prop = child->property ("is-xfade")) != 0) {
790 _fade_in_is_xfade = string_is_affirmative (prop->value());
792 _fade_in_is_xfade = false;
795 if ((prop = child->property ("active")) != 0) {
796 if (string_is_affirmative (prop->value())) {
797 set_fade_in_active (true);
799 set_fade_in_active (false);
803 } else if (child->name() == "FadeOut") {
807 if (((prop = child->property ("default")) != 0 && (string_is_affirmative (prop->value()))) || (prop = child->property ("steepness")) != 0) {
808 set_default_fade_out ();
810 XMLNode* grandchild = child->child ("AutomationList");
812 _fade_out->set_state (*grandchild, version);
816 if ((prop = child->property ("is-xfade")) != 0) {
817 _fade_out_is_xfade = string_is_affirmative (prop->value());
819 _fade_out_is_xfade = false;
822 if ((prop = child->property ("active")) != 0) {
823 if (string_is_affirmative (prop->value())) {
824 set_fade_out_active (true);
826 set_fade_out_active (false);
830 } else if (child->name() == "InvFadeIn") {
831 XMLNode* grandchild = child->child ("AutomationList");
833 if (!_inverse_fade_in) {
834 _inverse_fade_in.reset (new AutomationList (Evoral::Parameter (FadeInAutomation)));
836 _inverse_fade_in->set_state (*grandchild, version);
838 } else if (child->name() == "InvFadeOut") {
839 XMLNode* grandchild = child->child ("AutomationList");
841 if (!_inverse_fade_out) {
842 _inverse_fade_out.reset (new AutomationList (Evoral::Parameter (FadeOutAutomation)));
844 _inverse_fade_out->set_state (*grandchild, version);
850 resume_property_changes ();
853 send_change (what_changed);
857 the_playlist->thaw ();
864 AudioRegion::set_state (const XMLNode& node, int version)
866 PropertyChange what_changed;
867 return _set_state (node, version, what_changed, true);
871 AudioRegion::set_fade_in_shape (FadeShape shape)
873 set_fade_in (shape, (framecnt_t) _fade_in->back()->when);
877 AudioRegion::set_fade_out_shape (FadeShape shape)
879 set_fade_out (shape, (framecnt_t) _fade_out->back()->when);
883 AudioRegion::set_fade_in (boost::shared_ptr<AutomationList> f)
888 _default_fade_in = false;
890 send_change (PropertyChange (Properties::fade_in));
894 AudioRegion::set_fade_in (FadeShape shape, framecnt_t len)
901 _fade_in->fast_simple_add (0.0, 0.0);
902 _fade_in->fast_simple_add (len, 1.0);
903 _inverse_fade_in.reset ();
907 _fade_in->fast_simple_add (0, 0);
908 _fade_in->fast_simple_add (len * 0.389401, 0.0333333);
909 _fade_in->fast_simple_add (len * 0.629032, 0.0861111);
910 _fade_in->fast_simple_add (len * 0.829493, 0.233333);
911 _fade_in->fast_simple_add (len * 0.9447, 0.483333);
912 _fade_in->fast_simple_add (len * 0.976959, 0.697222);
913 _fade_in->fast_simple_add (len, 1);
914 _inverse_fade_in.reset ();
918 _fade_in->fast_simple_add (0, 0);
919 _fade_in->fast_simple_add (len * 0.0207373, 0.197222);
920 _fade_in->fast_simple_add (len * 0.0645161, 0.525);
921 _fade_in->fast_simple_add (len * 0.152074, 0.802778);
922 _fade_in->fast_simple_add (len * 0.276498, 0.919444);
923 _fade_in->fast_simple_add (len * 0.481567, 0.980556);
924 _fade_in->fast_simple_add (len * 0.767281, 1);
925 _fade_in->fast_simple_add (len, 1);
926 _inverse_fade_in.reset ();
930 _fade_in->fast_simple_add (0, 0);
931 _fade_in->fast_simple_add (len * 0.0737327, 0.308333);
932 _fade_in->fast_simple_add (len * 0.246544, 0.658333);
933 _fade_in->fast_simple_add (len * 0.470046, 0.886111);
934 _fade_in->fast_simple_add (len * 0.652074, 0.972222);
935 _fade_in->fast_simple_add (len * 0.771889, 0.988889);
936 _fade_in->fast_simple_add (len, 1);
937 _inverse_fade_in.reset ();
941 _fade_in->fast_simple_add (0, 0);
942 _fade_in->fast_simple_add (len * 0.304147, 0.0694444);
943 _fade_in->fast_simple_add (len * 0.529954, 0.152778);
944 _fade_in->fast_simple_add (len * 0.725806, 0.333333);
945 _fade_in->fast_simple_add (len * 0.847926, 0.558333);
946 _fade_in->fast_simple_add (len * 0.919355, 0.730556);
947 _fade_in->fast_simple_add (len, 1);
948 _inverse_fade_in.reset ();
951 case FadeConstantPowerMinus3dB:
952 _fade_in->fast_simple_add (0.0, 0.0);
953 _fade_in->fast_simple_add ((len * 0.166667), 0.282192);
954 _fade_in->fast_simple_add ((len * 0.333333), 0.518174);
955 _fade_in->fast_simple_add ((len * 0.500000), 0.707946);
956 _fade_in->fast_simple_add ((len * 0.666667), 0.851507);
957 _fade_in->fast_simple_add ((len * 0.833333), 0.948859);
958 _fade_in->fast_simple_add (len, 1.0);
960 /* setup complementary fade out for lower layers */
962 if (!_inverse_fade_in) {
963 _inverse_fade_in.reset (new AutomationList (Evoral::Parameter (FadeInAutomation)));
966 _inverse_fade_in->clear ();
967 _inverse_fade_in->fast_simple_add (0.0, 1.0);
968 _inverse_fade_in->fast_simple_add ((len * 0.166667), 0.948859);
969 _inverse_fade_in->fast_simple_add ((len * 0.333333), 0.851507);
970 _inverse_fade_in->fast_simple_add ((len * 0.500000), 0.707946);
971 _inverse_fade_in->fast_simple_add ((len * 0.666667), 0.518174);
972 _inverse_fade_in->fast_simple_add ((len * 0.833333), 0.282192);
973 _inverse_fade_in->fast_simple_add (len, 0.0);
977 case FadeConstantPowerMinus6dB:
978 _fade_in->fast_simple_add (0.0, 0.0);
979 _fade_in->fast_simple_add ((len * 0.166667), 0.166366);
980 _fade_in->fast_simple_add ((len * 0.333333), 0.332853);
981 _fade_in->fast_simple_add ((len * 0.500000), 0.499459);
982 _fade_in->fast_simple_add ((len * 0.666667), 0.666186);
983 _fade_in->fast_simple_add ((len * 0.833333), 0.833033);
984 _fade_in->fast_simple_add (len, 1.0);
986 /* setup complementary fade out for lower layers */
988 if (!_inverse_fade_in) {
989 _inverse_fade_in.reset (new AutomationList (Evoral::Parameter (FadeInAutomation)));
992 _inverse_fade_in->clear ();
993 _inverse_fade_in->fast_simple_add (0.0, 1.0);
994 _inverse_fade_in->fast_simple_add ((len * 0.166667), 0.833033);
995 _inverse_fade_in->fast_simple_add ((len * 0.333333), 0.666186);
996 _inverse_fade_in->fast_simple_add ((len * 0.500000), 0.499459);
997 _inverse_fade_in->fast_simple_add ((len * 0.666667), 0.332853);
998 _inverse_fade_in->fast_simple_add ((len * 0.833333), 0.166366);
999 _inverse_fade_in->fast_simple_add (len, 0.0);
1004 _default_fade_in = false;
1006 send_change (PropertyChange (Properties::fade_in));
1010 AudioRegion::set_fade_out (boost::shared_ptr<AutomationList> f)
1012 _fade_out->freeze ();
1015 _default_fade_out = false;
1017 send_change (PropertyChange (Properties::fade_in));
1021 AudioRegion::set_fade_out (FadeShape shape, framecnt_t len)
1023 _fade_out->freeze ();
1024 _fade_out->clear ();
1028 _fade_out->fast_simple_add (0.0, 1.0);
1029 _fade_out->fast_simple_add (len * 0.023041, 0.697222);
1030 _fade_out->fast_simple_add (len * 0.0553, 0.483333);
1031 _fade_out->fast_simple_add (len * 0.170507, 0.233333);
1032 _fade_out->fast_simple_add (len * 0.370968, 0.0861111);
1033 _fade_out->fast_simple_add (len * 0.610599, 0.0333333);
1034 _fade_out->fast_simple_add (1.0, 0.0);
1035 _inverse_fade_out.reset ();
1039 _fade_out->fast_simple_add (0, 1.0);
1040 _fade_out->fast_simple_add (len * 0.228111, 0.988889);
1041 _fade_out->fast_simple_add (len * 0.347926, 0.972222);
1042 _fade_out->fast_simple_add (len * 0.529954, 0.886111);
1043 _fade_out->fast_simple_add (len * 0.753456, 0.658333);
1044 _fade_out->fast_simple_add (len * 0.9262673, 0.308333);
1045 _fade_out->fast_simple_add (len, 0.0);
1046 _inverse_fade_out.reset ();
1050 _fade_out->fast_simple_add (0.0, 1.0);
1051 _fade_out->fast_simple_add (len * 0.305556, 1);
1052 _fade_out->fast_simple_add (len * 0.548611, 0.991736);
1053 _fade_out->fast_simple_add (len * 0.759259, 0.931129);
1054 _fade_out->fast_simple_add (len * 0.918981, 0.68595);
1055 _fade_out->fast_simple_add (len * 0.976852, 0.22865);
1056 _fade_out->fast_simple_add (len, 0.0);
1057 _inverse_fade_out.reset ();
1061 _fade_out->fast_simple_add (0.0, 1.0);
1062 _fade_out->fast_simple_add (len * 0.080645, 0.730556);
1063 _fade_out->fast_simple_add (len * 0.277778, 0.289256);
1064 _fade_out->fast_simple_add (len * 0.470046, 0.152778);
1065 _fade_out->fast_simple_add (len * 0.695853, 0.0694444);
1066 _fade_out->fast_simple_add (len, 0.0);
1067 _inverse_fade_out.reset ();
1071 _fade_out->fast_simple_add (0.0, 1.0);
1072 _fade_out->fast_simple_add (len, 0.0);
1073 _inverse_fade_out.reset ();
1076 case FadeConstantPowerMinus3dB:
1077 _fade_out->fast_simple_add (0.0, 1.0);
1078 _fade_out->fast_simple_add ((len * 0.166667), 0.948859);
1079 _fade_out->fast_simple_add ((len * 0.333333), 0.851507);
1080 _fade_out->fast_simple_add ((len * 0.500000), 0.707946);
1081 _fade_out->fast_simple_add ((len * 0.666667), 0.518174);
1082 _fade_out->fast_simple_add ((len * 0.833333), 0.282192);
1083 _fade_out->fast_simple_add (len, 0.0);
1085 /* setup complementary fade in for lower layers */
1087 if (!_inverse_fade_out) {
1088 _inverse_fade_out.reset (new AutomationList (Evoral::Parameter (FadeOutAutomation)));
1091 _inverse_fade_out->clear ();
1092 _inverse_fade_out->fast_simple_add (0.0, 0.0);
1093 _inverse_fade_out->fast_simple_add ((len * 0.166667), 0.282192);
1094 _inverse_fade_out->fast_simple_add ((len * 0.333333), 0.518174);
1095 _inverse_fade_out->fast_simple_add ((len * 0.500000), 0.707946);
1096 _inverse_fade_out->fast_simple_add ((len * 0.666667), 0.851507);
1097 _inverse_fade_out->fast_simple_add ((len * 0.833333), 0.948859);
1098 _inverse_fade_out->fast_simple_add (len, 1.0);
1102 case FadeConstantPowerMinus6dB:
1103 _fade_out->fast_simple_add (0.0, 1.0);
1104 _fade_out->fast_simple_add ((len * 0.166667), 0.833033);
1105 _fade_out->fast_simple_add ((len * 0.333333), 0.666186);
1106 _fade_out->fast_simple_add ((len * 0.500000), 0.499459);
1107 _fade_out->fast_simple_add ((len * 0.666667), 0.332853);
1108 _fade_out->fast_simple_add ((len * 0.833333), 0.166366);
1109 _fade_out->fast_simple_add (len, 0.0);
1111 /* setup complementary fade in for lower layers */
1113 if (!_inverse_fade_out) {
1114 _inverse_fade_out.reset (new AutomationList (Evoral::Parameter (FadeOutAutomation)));
1117 _inverse_fade_out->clear ();
1118 _inverse_fade_out->fast_simple_add (0.0, 0.0);
1119 _inverse_fade_out->fast_simple_add ((len * 0.166667), 0.166366);
1120 _inverse_fade_out->fast_simple_add ((len * 0.333333), 0.332853);
1121 _inverse_fade_out->fast_simple_add ((len * 0.500000), 0.499459);
1122 _inverse_fade_out->fast_simple_add ((len * 0.666667), 0.666186);
1123 _inverse_fade_out->fast_simple_add ((len * 0.833333), 0.833033);
1124 _inverse_fade_out->fast_simple_add (len, 1.0);
1129 _default_fade_out = false;
1131 send_change (PropertyChange (Properties::fade_in));
1135 AudioRegion::set_fade_in_length (framecnt_t len)
1137 if (len > _length) {
1145 bool changed = _fade_in->extend_to (len);
1148 if (_inverse_fade_in) {
1149 _inverse_fade_in->extend_to (len);
1152 if (_session.config.get_xfade_model() == FullCrossfade &&
1153 _session.config.get_auto_xfade() &&
1154 _fade_in_is_xfade) {
1156 /* trim a single other region below us to the new start
1160 boost::shared_ptr<Region> other = get_single_other_xfade_region (true);
1162 other->trim_end (position() + len);
1166 _default_fade_in = false;
1167 send_change (PropertyChange (Properties::fade_in));
1172 AudioRegion::set_fade_out_length (framecnt_t len)
1174 if (len > _length) {
1182 bool changed = _fade_out->extend_to (len);
1186 if (_inverse_fade_out) {
1187 _inverse_fade_out->extend_to (len);
1189 _default_fade_out = false;
1191 if (_session.config.get_xfade_model() == FullCrossfade &&
1192 _session.config.get_auto_xfade() &&
1193 _fade_out_is_xfade) {
1195 /* trim a single other region below us to the new start
1199 boost::shared_ptr<Region> other = get_single_other_xfade_region (false);
1201 other->trim_front (last_frame() - len);
1205 send_change (PropertyChange (Properties::fade_out));
1210 AudioRegion::set_fade_in_active (bool yn)
1212 if (yn == _fade_in_active) {
1216 _fade_in_active = yn;
1217 send_change (PropertyChange (Properties::fade_in_active));
1221 AudioRegion::set_fade_out_active (bool yn)
1223 if (yn == _fade_out_active) {
1226 _fade_out_active = yn;
1227 send_change (PropertyChange (Properties::fade_out_active));
1231 AudioRegion::fade_in_is_default () const
1233 return _fade_in->size() == 2 && _fade_in->front()->when == 0 && _fade_in->back()->when == 64;
1237 AudioRegion::fade_out_is_default () const
1239 return _fade_out->size() == 2 && _fade_out->front()->when == 0 && _fade_out->back()->when == 64;
1243 AudioRegion::set_default_fade_in ()
1245 _fade_in_suspended = 0;
1246 _fade_in_is_xfade = false;
1247 set_fade_in (FadeLinear, 64);
1251 AudioRegion::set_default_fade_out ()
1253 _fade_out_suspended = 0;
1254 _fade_out_is_xfade = false;
1255 set_fade_out (FadeLinear, 64);
1259 AudioRegion::set_default_fades ()
1261 set_default_fade_in ();
1262 set_default_fade_out ();
1266 AudioRegion::set_default_envelope ()
1268 _envelope->freeze ();
1269 _envelope->clear ();
1270 _envelope->fast_simple_add (0, 1.0f);
1271 _envelope->fast_simple_add (_length, 1.0f);
1276 AudioRegion::recompute_at_end ()
1278 /* our length has changed. recompute a new final point by interpolating
1279 based on the the existing curve.
1282 _envelope->freeze ();
1283 _envelope->truncate_end (_length);
1286 suspend_property_changes();
1288 if (_left_of_split) {
1289 set_default_fade_out ();
1290 _left_of_split = false;
1291 } else if (_fade_out->back()->when > _length) {
1292 _fade_out->extend_to (_length);
1293 send_change (PropertyChange (Properties::fade_out));
1296 if (_fade_in->back()->when > _length) {
1297 _fade_in->extend_to (_length);
1298 send_change (PropertyChange (Properties::fade_in));
1301 resume_property_changes();
1305 AudioRegion::recompute_at_start ()
1307 /* as above, but the shift was from the front */
1309 _envelope->truncate_start (_length);
1311 suspend_property_changes();
1313 if (_right_of_split) {
1314 set_default_fade_in ();
1315 _right_of_split = false;
1316 } else if (_fade_in->back()->when > _length) {
1317 _fade_in->extend_to (_length);
1318 send_change (PropertyChange (Properties::fade_in));
1321 if (_fade_out->back()->when > _length) {
1322 _fade_out->extend_to (_length);
1323 send_change (PropertyChange (Properties::fade_out));
1326 resume_property_changes();
1330 AudioRegion::separate_by_channel (Session& /*session*/, vector<boost::shared_ptr<Region> >& v) const
1336 if (_sources.size() < 2) {
1340 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
1342 srcs.push_back (*i);
1346 if (_sources.size() == 2) {
1354 new_name += ('0' + n + 1);
1357 /* create a copy with just one source. prevent if from being thought of as
1358 "whole file" even if it covers the entire source file(s).
1363 plist.add (Properties::start, _start.val());
1364 plist.add (Properties::length, _length.val());
1365 plist.add (Properties::name, new_name);
1366 plist.add (Properties::layer, layer ());
1368 v.push_back(RegionFactory::create (srcs, plist));
1369 v.back()->set_whole_file (false);
1378 AudioRegion::read_raw_internal (Sample* buf, framepos_t pos, framecnt_t cnt, int channel) const
1380 return audio_source(channel)->read (buf, pos, cnt);
1384 AudioRegion::set_scale_amplitude (gain_t g)
1386 boost::shared_ptr<Playlist> pl (playlist());
1388 _scale_amplitude = g;
1390 /* tell the diskstream we're in */
1393 pl->ContentsChanged();
1396 /* tell everybody else */
1398 send_change (PropertyChange (Properties::scale_amplitude));
1401 /** @return the maximum (linear) amplitude of the region, or a -ve
1402 * number if the Progress object reports that the process was cancelled.
1405 AudioRegion::maximum_amplitude (Progress* p) const
1407 framepos_t fpos = _start;
1408 framepos_t const fend = _start + _length;
1411 framecnt_t const blocksize = 64 * 1024;
1412 Sample buf[blocksize];
1414 while (fpos < fend) {
1418 framecnt_t const to_read = min (fend - fpos, blocksize);
1420 for (n = 0; n < n_channels(); ++n) {
1424 if (read_raw_internal (buf, fpos, to_read, n) != to_read) {
1428 maxamp = compute_peak (buf, to_read, maxamp);
1433 p->set_progress (float (fpos - _start) / _length);
1434 if (p->cancelled ()) {
1443 /** Normalize using a given maximum amplitude and target, so that region
1444 * _scale_amplitude becomes target / max_amplitude.
1447 AudioRegion::normalize (float max_amplitude, float target_dB)
1449 gain_t target = dB_to_coefficient (target_dB);
1451 if (target == 1.0f) {
1452 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1453 that we may have clipped.
1455 target -= FLT_EPSILON;
1458 if (max_amplitude == 0.0f) {
1459 /* don't even try */
1463 if (max_amplitude == target) {
1464 /* we can't do anything useful */
1468 set_scale_amplitude (target / max_amplitude);
1472 AudioRegion::fade_in_changed ()
1474 send_change (PropertyChange (Properties::fade_in));
1478 AudioRegion::fade_out_changed ()
1480 send_change (PropertyChange (Properties::fade_out));
1484 AudioRegion::envelope_changed ()
1486 send_change (PropertyChange (Properties::envelope));
1490 AudioRegion::suspend_fade_in ()
1492 if (++_fade_in_suspended == 1) {
1493 if (fade_in_is_default()) {
1494 set_fade_in_active (false);
1500 AudioRegion::resume_fade_in ()
1502 if (--_fade_in_suspended == 0 && _fade_in_suspended) {
1503 set_fade_in_active (true);
1508 AudioRegion::suspend_fade_out ()
1510 if (++_fade_out_suspended == 1) {
1511 if (fade_out_is_default()) {
1512 set_fade_out_active (false);
1518 AudioRegion::resume_fade_out ()
1520 if (--_fade_out_suspended == 0 &&_fade_out_suspended) {
1521 set_fade_out_active (true);
1526 AudioRegion::speed_mismatch (float sr) const
1528 if (_sources.empty()) {
1529 /* impossible, but ... */
1533 float fsr = audio_source()->sample_rate();
1539 AudioRegion::source_offset_changed ()
1541 /* XXX this fixes a crash that should not occur. It does occur
1542 becauses regions are not being deleted when a session
1543 is unloaded. That bug must be fixed.
1546 if (_sources.empty()) {
1550 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(_sources.front());
1552 if (afs && afs->destructive()) {
1553 // set_start (source()->natural_position(), this);
1554 set_position (source()->natural_position());
1558 boost::shared_ptr<AudioSource>
1559 AudioRegion::audio_source (uint32_t n) const
1561 // Guaranteed to succeed (use a static cast for speed?)
1562 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1566 AudioRegion::adjust_transients (frameoffset_t delta)
1568 for (AnalysisFeatureList::iterator x = _transients.begin(); x != _transients.end(); ++x) {
1569 (*x) = (*x) + delta;
1572 send_change (PropertyChange (Properties::valid_transients));
1578 AudioRegion::update_transient (framepos_t old_position, framepos_t new_position)
1580 for (AnalysisFeatureList::iterator x = _transients.begin(); x != _transients.end(); ++x) {
1581 if ((*x) == old_position) {
1582 (*x) = new_position;
1583 send_change (PropertyChange (Properties::valid_transients));
1593 AudioRegion::add_transient (framepos_t where)
1595 _transients.push_back(where);
1596 _valid_transients = true;
1598 send_change (PropertyChange (Properties::valid_transients));
1602 AudioRegion::remove_transient (framepos_t where)
1604 _transients.remove(where);
1605 _valid_transients = true;
1607 send_change (PropertyChange (Properties::valid_transients));
1611 AudioRegion::set_transients (AnalysisFeatureList& results)
1613 _transients.clear();
1614 _transients = results;
1615 _valid_transients = true;
1617 send_change (PropertyChange (Properties::valid_transients));
1623 AudioRegion::get_transients (AnalysisFeatureList& results, bool force_new)
1625 boost::shared_ptr<Playlist> pl = playlist();
1631 if (_valid_transients && !force_new) {
1632 results = _transients;
1636 SourceList::iterator s;
1638 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1639 if (!(*s)->has_been_analysed()) {
1640 cerr << "For " << name() << " source " << (*s)->name() << " has not been analyzed\n";
1645 if (s == _sources.end()) {
1646 /* all sources are analyzed, merge data from each one */
1648 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1650 /* find the set of transients within the bounds of this region */
1652 AnalysisFeatureList::iterator low = lower_bound ((*s)->transients.begin(),
1653 (*s)->transients.end(),
1656 AnalysisFeatureList::iterator high = upper_bound ((*s)->transients.begin(),
1657 (*s)->transients.end(),
1662 results.insert (results.end(), low, high);
1665 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1667 /* translate all transients to current position */
1669 for (AnalysisFeatureList::iterator x = results.begin(); x != results.end(); ++x) {
1674 _transients = results;
1675 _valid_transients = true;
1680 /* no existing/complete transient info */
1682 static bool analyse_dialog_shown = false; /* global per instance of Ardour */
1684 if (!Config->get_auto_analyse_audio()) {
1685 if (!analyse_dialog_shown) {
1686 pl->session().Dialog (_("\
1687 You have requested an operation that requires audio analysis.\n\n\
1688 You currently have \"auto-analyse-audio\" disabled, which means \
1689 that transient data must be generated every time it is required.\n\n\
1690 If you are doing work that will require transient data on a \
1691 regular basis, you should probably enable \"auto-analyse-audio\" \
1692 then quit ardour and restart.\n\n\
1693 This dialog will not display again. But you may notice a slight delay \
1694 in this and future transient-detection operations.\n\
1696 analyse_dialog_shown = true;
1700 TransientDetector t (pl->session().frame_rate());
1701 bool existing_results = !results.empty();
1703 _transients.clear ();
1704 _valid_transients = false;
1706 for (uint32_t i = 0; i < n_channels(); ++i) {
1708 AnalysisFeatureList these_results;
1712 if (t.run ("", this, i, these_results)) {
1716 /* translate all transients to give absolute position */
1718 for (AnalysisFeatureList::iterator i = these_results.begin(); i != these_results.end(); ++i) {
1724 _transients.insert (_transients.end(), these_results.begin(), these_results.end());
1727 if (!results.empty()) {
1728 if (existing_results) {
1730 /* merge our transients into the existing ones, then clean up
1734 results.insert (results.end(), _transients.begin(), _transients.end());
1735 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1738 /* make sure ours are clean too */
1740 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1744 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1745 results = _transients;
1748 _valid_transients = true;
1753 /** Find areas of `silence' within a region.
1755 * @param threshold Threshold below which signal is considered silence (as a sample value)
1756 * @param min_length Minimum length of silent period to be reported.
1757 * @return Silent intervals, measured relative to the region start in the source
1761 AudioRegion::find_silence (Sample threshold, framecnt_t min_length, InterThreadInfo& itt) const
1763 framecnt_t const block_size = 64 * 1024;
1764 boost::scoped_array<Sample> loudest (new Sample[block_size]);
1765 boost::scoped_array<Sample> buf (new Sample[block_size]);
1767 framepos_t pos = _start;
1768 framepos_t const end = _start + _length - 1;
1770 AudioIntervalResult silent_periods;
1772 bool in_silence = false;
1773 frameoffset_t silence_start = 0;
1775 while (pos < end && !itt.cancel) {
1777 /* fill `loudest' with the loudest absolute sample at each instant, across all channels */
1778 memset (loudest.get(), 0, sizeof (Sample) * block_size);
1779 for (uint32_t n = 0; n < n_channels(); ++n) {
1781 read_raw_internal (buf.get(), pos, block_size, n);
1782 for (framecnt_t i = 0; i < block_size; ++i) {
1783 loudest[i] = max (loudest[i], abs (buf[i]));
1787 /* now look for silence */
1788 for (framecnt_t i = 0; i < block_size; ++i) {
1789 bool const silence = abs (loudest[i]) < threshold;
1790 if (silence && !in_silence) {
1791 /* non-silence to silence */
1793 silence_start = pos + i;
1794 } else if (!silence && in_silence) {
1795 /* silence to non-silence */
1797 if (pos + i - 1 - silence_start >= min_length) {
1798 silent_periods.push_back (std::make_pair (silence_start, pos + i - 1));
1804 itt.progress = (end-pos)/(double)_length;
1807 if (in_silence && end - 1 - silence_start >= min_length) {
1808 /* last block was silent, so finish off the last period */
1809 silent_periods.push_back (std::make_pair (silence_start, end));
1814 return silent_periods;
1817 Evoral::Range<framepos_t>
1818 AudioRegion::body_range () const
1820 return Evoral::Range<framepos_t> (first_frame() + _fade_in->back()->when, last_frame() - _fade_out->back()->when);
1824 AudioRegion::set_fade_in_is_xfade (bool yn)
1826 _fade_in_is_xfade = yn;
1830 AudioRegion::set_fade_out_is_xfade (bool yn)
1832 _fade_out_is_xfade = yn;
1835 boost::shared_ptr<Region>
1836 AudioRegion::get_single_other_xfade_region (bool start) const
1838 boost::shared_ptr<Playlist> pl (playlist());
1841 /* not currently in a playlist - xfade length is unbounded
1844 return boost::shared_ptr<AudioRegion> ();
1847 boost::shared_ptr<RegionList> rl;
1850 rl = pl->regions_at (position());
1852 rl = pl->regions_at (last_frame());
1855 RegionList::iterator i;
1856 boost::shared_ptr<Region> other;
1859 /* count and find the other region in a single pass through the list */
1861 for (i = rl->begin(); i != rl->end(); ++i) {
1862 if ((*i).get() != this) {
1869 /* zero or multiple regions stacked here - don't care about xfades */
1870 return boost::shared_ptr<AudioRegion> ();
1877 AudioRegion::verify_xfade_bounds (framecnt_t len, bool start)
1879 boost::shared_ptr<Region> other = get_single_other_xfade_region (start);
1882 /* we overlap a single region. clamp the length of an xfade to
1883 the maximum possible duration of the overlap (if the other
1884 region were trimmed appropriately).
1888 maxlen = other->latest_possible_frame() - position();
1890 maxlen = last_frame() - other->earliest_possible_position();
1893 return min (maxlen, len);
1899 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)
1901 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (framecnt_t) npeaks, (framepos_t) start, (framecnt_t) cnt, n_chan,samples_per_unit);
1904 uint32_t region_length_from_c (void *arg)
1907 return ((AudioRegion *) arg)->length();
1910 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1912 return ( (AudioRegion *) arg)->audio_source()->available_peaks (zoom_factor) ;