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.
28 #include <glibmm/thread.h>
30 #include "pbd/basename.h"
31 #include "pbd/xml++.h"
32 #include "pbd/stacktrace.h"
33 #include "pbd/enumwriter.h"
34 #include "pbd/convert.h"
36 #include "evoral/Curve.hpp"
38 #include "ardour/audioregion.h"
39 #include "ardour/debug.h"
40 #include "ardour/session.h"
41 #include "ardour/gain.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)
142 assert (_sources.size() == _master_sources.size());
145 /** Basic AudioRegion constructor */
146 AudioRegion::AudioRegion (const SourceList& srcs)
148 , AUDIOREGION_STATE_DEFAULT
149 , _automatable(srcs[0]->session())
150 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
151 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
152 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
153 , _fade_in_suspended (0)
154 , _fade_out_suspended (0)
157 assert (_sources.size() == _master_sources.size());
160 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, framecnt_t offset, bool offset_relative)
161 : Region (other, offset, offset_relative)
162 , AUDIOREGION_COPY_STATE (other)
163 , _automatable (other->session())
164 , _fade_in (new AutomationList (*other->_fade_in))
165 , _fade_out (new AutomationList (*other->_fade_out))
166 /* As far as I can see, the _envelope's times are relative to region position, and have nothing
167 to do with sources (and hence _start). So when we copy the envelope, we just use the supplied offset.
169 , _envelope (new AutomationList (*other->_envelope, offset, other->_length))
170 , _fade_in_suspended (0)
171 , _fade_out_suspended (0)
173 /* don't use init here, because we got fade in/out from the other region
175 register_properties ();
176 listen_to_my_curves ();
177 connect_to_analysis_changed ();
178 connect_to_header_position_offset_changed ();
180 assert(_type == DataType::AUDIO);
181 assert (_sources.size() == _master_sources.size());
184 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, const SourceList& srcs)
185 : Region (boost::static_pointer_cast<const Region>(other), srcs)
186 , AUDIOREGION_COPY_STATE (other)
187 , _automatable (other->session())
188 , _fade_in (new AutomationList (*other->_fade_in))
189 , _fade_out (new AutomationList (*other->_fade_out))
190 , _envelope (new AutomationList (*other->_envelope))
191 , _fade_in_suspended (0)
192 , _fade_out_suspended (0)
194 /* make-a-sort-of-copy-with-different-sources constructor (used by audio filter) */
196 register_properties ();
198 listen_to_my_curves ();
199 connect_to_analysis_changed ();
200 connect_to_header_position_offset_changed ();
202 assert (_sources.size() == _master_sources.size());
205 AudioRegion::AudioRegion (SourceList& srcs)
207 , AUDIOREGION_STATE_DEFAULT
208 , _automatable(srcs[0]->session())
209 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
210 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
211 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
212 , _fade_in_suspended (0)
213 , _fade_out_suspended (0)
217 assert(_type == DataType::AUDIO);
218 assert (_sources.size() == _master_sources.size());
221 AudioRegion::~AudioRegion ()
226 AudioRegion::post_set ()
229 _sync_position = _start;
232 /* return to default fades if the existing ones are too long */
234 if (_left_of_split) {
235 if (_fade_in->back()->when >= _length) {
236 set_default_fade_in ();
238 set_default_fade_out ();
239 _left_of_split = false;
242 if (_right_of_split) {
243 if (_fade_out->back()->when >= _length) {
244 set_default_fade_out ();
247 set_default_fade_in ();
248 _right_of_split = false;
251 /* If _length changed, adjust our gain envelope accordingly */
252 _envelope->truncate_end (_length);
256 AudioRegion::connect_to_analysis_changed ()
258 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
259 (*i)->AnalysisChanged.connect_same_thread (*this, boost::bind (&AudioRegion::invalidate_transients, this));
264 AudioRegion::connect_to_header_position_offset_changed ()
266 set<boost::shared_ptr<Source> > unique_srcs;
268 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
270 /* connect only once to HeaderPositionOffsetChanged, even if sources are replicated
273 if (unique_srcs.find (*i) == unique_srcs.end ()) {
274 unique_srcs.insert (*i);
275 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
277 afs->HeaderPositionOffsetChanged.connect_same_thread (*this, boost::bind (&AudioRegion::source_offset_changed, this));
284 AudioRegion::listen_to_my_curves ()
286 _envelope->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::envelope_changed, this));
287 _fade_in->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::fade_in_changed, this));
288 _fade_out->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::fade_out_changed, this));
292 AudioRegion::set_envelope_active (bool yn)
294 if (envelope_active() != yn) {
295 _envelope_active = yn;
296 send_change (PropertyChange (Properties::envelope_active));
301 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
303 if (chan_n >= _sources.size()) {
307 if (audio_source(chan_n)->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
310 if (_scale_amplitude != 1.0f) {
311 for (nframes_t n = 0; n < npeaks; ++n) {
312 buf[n].max *= _scale_amplitude;
313 buf[n].min *= _scale_amplitude;
321 AudioRegion::read (Sample* buf, framepos_t timeline_position, framecnt_t cnt, int channel) const
323 /* raw read, no fades, no gain, nada */
324 return _read_at (_sources, _length, buf, 0, 0, _position + timeline_position, cnt, channel, 0, 0, ReadOps (0));
328 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
329 framepos_t file_position, framecnt_t cnt, uint32_t chan_n,
330 framecnt_t read_frames, framecnt_t skip_frames) const
332 /* regular diskstream/butler read complete with fades etc */
333 return _read_at (_sources, _length, buf, mixdown_buffer, gain_buffer,
334 file_position, cnt, chan_n, read_frames, skip_frames, ReadOps (~0));
338 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
339 framepos_t position, framecnt_t cnt, uint32_t chan_n) const
341 /* do not read gain/scaling/fades and do not count this disk i/o in statistics */
343 return _read_at (_master_sources, _master_sources.front()->length(_master_sources.front()->timeline_position()),
344 buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0, ReadOps (0));
348 AudioRegion::_read_at (const SourceList& /*srcs*/, framecnt_t limit,
349 Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
353 framecnt_t /*read_frames*/,
354 framecnt_t /*skip_frames*/,
357 frameoffset_t internal_offset;
358 frameoffset_t buf_offset;
360 bool raw = (rops == ReadOpsNone);
362 if (n_channels() == 0) {
366 if (muted() && !raw) {
367 return 0; /* read nothing */
370 /* precondition: caller has verified that we cover the desired section */
372 if (position < _position) {
374 buf_offset = _position - position;
377 internal_offset = position - _position;
381 if (internal_offset >= limit) {
382 return 0; /* read nothing */
385 if ((to_read = min (cnt, limit - internal_offset)) == 0) {
386 return 0; /* read nothing */
389 if (opaque() || raw) {
390 /* overwrite whatever is there */
391 mixdown_buffer = buf + buf_offset;
393 mixdown_buffer += buf_offset;
396 if (rops & ReadOpsCount) {
397 _read_data_count = 0;
400 if (chan_n < n_channels()) {
402 boost::shared_ptr<AudioSource> src = audio_source(chan_n);
403 if (src->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
404 return 0; /* "read nothing" */
407 if (rops & ReadOpsCount) {
408 _read_data_count += src->read_data_count();
413 /* track is N-channel, this region has less channels; silence the ones
417 if (Config->get_replicate_missing_region_channels()) {
418 /* track is N-channel, this region has less channels, so use a relevant channel
421 uint32_t channel = n_channels() % chan_n;
422 boost::shared_ptr<AudioSource> src = audio_source (channel);
424 if (src->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
425 return 0; /* "read nothing" */
428 /* adjust read data count appropriately since this was a duplicate read */
429 src->dec_read_data_count (to_read);
431 memset (mixdown_buffer, 0, sizeof (Sample) * cnt);
435 if (rops & ReadOpsFades) {
439 if (_fade_in_active && _session.config.get_use_region_fades()) {
441 nframes_t fade_in_length = (nframes_t) _fade_in->back()->when;
443 /* see if this read is within the fade in */
445 if (internal_offset < fade_in_length) {
449 fi_limit = min (to_read, fade_in_length - internal_offset);
452 _fade_in->curve().get_vector (internal_offset, internal_offset+fi_limit, gain_buffer, fi_limit);
454 for (nframes_t n = 0; n < fi_limit; ++n) {
455 mixdown_buffer[n] *= gain_buffer[n];
462 if (_fade_out_active && _session.config.get_use_region_fades()) {
464 /* see if some part of this read is within the fade out */
466 /* ................. >| REGION
472 limit - fade_out_length
475 ^internal_offset + to_read
477 we need the intersection of [internal_offset,internal_offset+to_read] with
478 [limit - fade_out_length, limit]
483 nframes_t fade_out_length = (nframes_t) _fade_out->back()->when;
484 nframes_t fade_interval_start = max(internal_offset, limit-fade_out_length);
485 nframes_t fade_interval_end = min(internal_offset + to_read, limit);
487 if (fade_interval_end > fade_interval_start) {
488 /* (part of the) the fade out is in this buffer */
490 nframes_t fo_limit = fade_interval_end - fade_interval_start;
491 nframes_t curve_offset = fade_interval_start - (limit-fade_out_length);
492 nframes_t fade_offset = fade_interval_start - internal_offset;
494 _fade_out->curve().get_vector (curve_offset, curve_offset+fo_limit, gain_buffer, fo_limit);
496 for (nframes_t n = 0, m = fade_offset; n < fo_limit; ++n, ++m) {
497 mixdown_buffer[m] *= gain_buffer[n];
504 /* Regular gain curves and scaling */
506 if ((rops & ReadOpsOwnAutomation) && envelope_active()) {
507 _envelope->curve().get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
509 if ((rops & ReadOpsOwnScaling) && _scale_amplitude != 1.0f) {
510 for (nframes_t n = 0; n < to_read; ++n) {
511 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
514 for (nframes_t n = 0; n < to_read; ++n) {
515 mixdown_buffer[n] *= gain_buffer[n];
518 } else if ((rops & ReadOpsOwnScaling) && _scale_amplitude != 1.0f) {
520 // XXX this should be using what in 2.0 would have been:
521 // Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
523 for (nframes_t n = 0; n < to_read; ++n) {
524 mixdown_buffer[n] *= _scale_amplitude;
528 if (!opaque() && (buf != mixdown_buffer)) {
530 /* gack. the things we do for users.
535 for (nframes_t n = 0; n < to_read; ++n) {
536 buf[n] += mixdown_buffer[n];
544 AudioRegion::state ()
546 XMLNode& node (Region::state ());
549 LocaleGuard lg (X_("POSIX"));
551 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
552 node.add_property ("channels", buf);
554 Stateful::add_properties (node);
556 child = node.add_child ("Envelope");
558 bool default_env = false;
560 // If there are only two points, the points are in the start of the region and the end of the region
561 // so, if they are both at 1.0f, that means the default region.
563 if (_envelope->size() == 2 &&
564 _envelope->front()->value == 1.0f &&
565 _envelope->back()->value==1.0f) {
566 if (_envelope->front()->when == 0 && _envelope->back()->when == _length) {
572 child->add_property ("default", "yes");
574 child->add_child_nocopy (_envelope->get_state ());
577 child = node.add_child (X_("FadeIn"));
579 if (_default_fade_in) {
580 child->add_property ("default", "yes");
582 child->add_child_nocopy (_fade_in->get_state ());
585 child = node.add_child (X_("FadeOut"));
587 if (_default_fade_out) {
588 child->add_property ("default", "yes");
590 child->add_child_nocopy (_fade_out->get_state ());
597 AudioRegion::_set_state (const XMLNode& node, int version, PropertyChange& what_changed, bool send)
599 const XMLNodeList& nlist = node.children();
600 const XMLProperty *prop;
601 LocaleGuard lg (X_("POSIX"));
602 boost::shared_ptr<Playlist> the_playlist (_playlist.lock());
604 suspend_property_changes ();
607 the_playlist->freeze ();
611 /* this will set all our State members and stuff controlled by the Region.
612 It should NOT send any changed signals - that is our responsibility.
615 Region::_set_state (node, version, what_changed, false);
617 if ((prop = node.property ("scale-gain")) != 0) {
618 float a = atof (prop->value().c_str());
619 if (a != _scale_amplitude) {
620 _scale_amplitude = a;
621 what_changed.add (Properties::scale_amplitude);
625 /* Now find envelope description and other related child items */
627 _envelope->freeze ();
629 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
635 if (child->name() == "Envelope") {
639 if ((prop = child->property ("default")) != 0 || _envelope->set_state (*child, version)) {
640 set_default_envelope ();
643 _envelope->set_max_xval (_length);
644 _envelope->truncate_end (_length);
647 } else if (child->name() == "FadeIn") {
651 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
652 set_default_fade_in ();
654 XMLNode* grandchild = child->child ("AutomationList");
656 _fade_in->set_state (*grandchild, version);
660 if ((prop = child->property ("active")) != 0) {
661 if (string_is_affirmative (prop->value())) {
662 set_fade_in_active (true);
664 set_fade_in_active (false);
668 } else if (child->name() == "FadeOut") {
672 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
673 set_default_fade_out ();
675 XMLNode* grandchild = child->child ("AutomationList");
677 _fade_out->set_state (*grandchild, version);
681 if ((prop = child->property ("active")) != 0) {
682 if (string_is_affirmative (prop->value())) {
683 set_fade_out_active (true);
685 set_fade_out_active (false);
693 resume_property_changes ();
696 send_change (what_changed);
700 the_playlist->thaw ();
707 AudioRegion::set_state (const XMLNode& node, int version)
709 PropertyChange what_changed;
710 return _set_state (node, version, what_changed, true);
714 AudioRegion::set_fade_in_shape (FadeShape shape)
716 set_fade_in (shape, (nframes_t) _fade_in->back()->when);
720 AudioRegion::set_fade_out_shape (FadeShape shape)
722 set_fade_out (shape, (nframes_t) _fade_out->back()->when);
726 AudioRegion::set_fade_in (boost::shared_ptr<AutomationList> f)
732 send_change (PropertyChange (Properties::fade_in));
736 AudioRegion::set_fade_in (FadeShape shape, framecnt_t len)
743 _fade_in->fast_simple_add (0.0, 0.0);
744 _fade_in->fast_simple_add (len, 1.0);
748 _fade_in->fast_simple_add (0, 0);
749 _fade_in->fast_simple_add (len * 0.389401, 0.0333333);
750 _fade_in->fast_simple_add (len * 0.629032, 0.0861111);
751 _fade_in->fast_simple_add (len * 0.829493, 0.233333);
752 _fade_in->fast_simple_add (len * 0.9447, 0.483333);
753 _fade_in->fast_simple_add (len * 0.976959, 0.697222);
754 _fade_in->fast_simple_add (len, 1);
758 _fade_in->fast_simple_add (0, 0);
759 _fade_in->fast_simple_add (len * 0.0207373, 0.197222);
760 _fade_in->fast_simple_add (len * 0.0645161, 0.525);
761 _fade_in->fast_simple_add (len * 0.152074, 0.802778);
762 _fade_in->fast_simple_add (len * 0.276498, 0.919444);
763 _fade_in->fast_simple_add (len * 0.481567, 0.980556);
764 _fade_in->fast_simple_add (len * 0.767281, 1);
765 _fade_in->fast_simple_add (len, 1);
769 _fade_in->fast_simple_add (0, 0);
770 _fade_in->fast_simple_add (len * 0.0737327, 0.308333);
771 _fade_in->fast_simple_add (len * 0.246544, 0.658333);
772 _fade_in->fast_simple_add (len * 0.470046, 0.886111);
773 _fade_in->fast_simple_add (len * 0.652074, 0.972222);
774 _fade_in->fast_simple_add (len * 0.771889, 0.988889);
775 _fade_in->fast_simple_add (len, 1);
779 _fade_in->fast_simple_add (0, 0);
780 _fade_in->fast_simple_add (len * 0.304147, 0.0694444);
781 _fade_in->fast_simple_add (len * 0.529954, 0.152778);
782 _fade_in->fast_simple_add (len * 0.725806, 0.333333);
783 _fade_in->fast_simple_add (len * 0.847926, 0.558333);
784 _fade_in->fast_simple_add (len * 0.919355, 0.730556);
785 _fade_in->fast_simple_add (len, 1);
790 send_change (PropertyChange (Properties::fade_in));
794 AudioRegion::set_fade_out (boost::shared_ptr<AutomationList> f)
796 _fade_out->freeze ();
800 send_change (PropertyChange (Properties::fade_in));
804 AudioRegion::set_fade_out (FadeShape shape, framecnt_t len)
806 _fade_out->freeze ();
811 _fade_out->fast_simple_add (len * 0, 1);
812 _fade_out->fast_simple_add (len * 0.023041, 0.697222);
813 _fade_out->fast_simple_add (len * 0.0553, 0.483333);
814 _fade_out->fast_simple_add (len * 0.170507, 0.233333);
815 _fade_out->fast_simple_add (len * 0.370968, 0.0861111);
816 _fade_out->fast_simple_add (len * 0.610599, 0.0333333);
817 _fade_out->fast_simple_add (len * 1, 0);
821 _fade_out->fast_simple_add (len * 0, 1);
822 _fade_out->fast_simple_add (len * 0.228111, 0.988889);
823 _fade_out->fast_simple_add (len * 0.347926, 0.972222);
824 _fade_out->fast_simple_add (len * 0.529954, 0.886111);
825 _fade_out->fast_simple_add (len * 0.753456, 0.658333);
826 _fade_out->fast_simple_add (len * 0.9262673, 0.308333);
827 _fade_out->fast_simple_add (len * 1, 0);
831 _fade_out->fast_simple_add (len * 0, 1);
832 _fade_out->fast_simple_add (len * 0.305556, 1);
833 _fade_out->fast_simple_add (len * 0.548611, 0.991736);
834 _fade_out->fast_simple_add (len * 0.759259, 0.931129);
835 _fade_out->fast_simple_add (len * 0.918981, 0.68595);
836 _fade_out->fast_simple_add (len * 0.976852, 0.22865);
837 _fade_out->fast_simple_add (len * 1, 0);
841 _fade_out->fast_simple_add (len * 0, 1);
842 _fade_out->fast_simple_add (len * 0.080645, 0.730556);
843 _fade_out->fast_simple_add (len * 0.277778, 0.289256);
844 _fade_out->fast_simple_add (len * 0.470046, 0.152778);
845 _fade_out->fast_simple_add (len * 0.695853, 0.0694444);
846 _fade_out->fast_simple_add (len * 1, 0);
850 _fade_out->fast_simple_add (len * 0, 1);
851 _fade_out->fast_simple_add (len * 1, 0);
856 send_change (PropertyChange (Properties::fade_in));
860 AudioRegion::set_fade_in_length (framecnt_t len)
866 bool changed = _fade_in->extend_to (len);
869 _default_fade_in = false;
870 send_change (PropertyChange (Properties::fade_in));
875 AudioRegion::set_fade_out_length (framecnt_t len)
881 bool changed = _fade_out->extend_to (len);
884 _default_fade_out = false;
885 send_change (PropertyChange (Properties::fade_out));
890 AudioRegion::set_fade_in_active (bool yn)
892 if (yn == _fade_in_active) {
896 _fade_in_active = yn;
897 send_change (PropertyChange (Properties::fade_in_active));
901 AudioRegion::set_fade_out_active (bool yn)
903 if (yn == _fade_out_active) {
906 _fade_out_active = yn;
907 send_change (PropertyChange (Properties::fade_out_active));
911 AudioRegion::fade_in_is_default () const
913 return _fade_in->size() == 2 && _fade_in->front()->when == 0 && _fade_in->back()->when == 64;
917 AudioRegion::fade_out_is_default () const
919 return _fade_out->size() == 2 && _fade_out->front()->when == 0 && _fade_out->back()->when == 64;
923 AudioRegion::set_default_fade_in ()
925 _fade_in_suspended = 0;
926 set_fade_in (FadeLinear, 64);
930 AudioRegion::set_default_fade_out ()
932 _fade_out_suspended = 0;
933 set_fade_out (FadeLinear, 64);
937 AudioRegion::set_default_fades ()
939 set_default_fade_in ();
940 set_default_fade_out ();
944 AudioRegion::set_default_envelope ()
946 _envelope->freeze ();
948 _envelope->fast_simple_add (0, 1.0f);
949 _envelope->fast_simple_add (_length, 1.0f);
954 AudioRegion::recompute_at_end ()
956 /* our length has changed. recompute a new final point by interpolating
957 based on the the existing curve.
960 _envelope->freeze ();
961 _envelope->truncate_end (_length);
962 _envelope->set_max_xval (_length);
965 suspend_property_changes();
967 if (_left_of_split) {
968 set_default_fade_out ();
969 _left_of_split = false;
970 } else if (_fade_out->back()->when > _length) {
971 _fade_out->extend_to (_length);
972 send_change (PropertyChange (Properties::fade_out));
975 if (_fade_in->back()->when > _length) {
976 _fade_in->extend_to (_length);
977 send_change (PropertyChange (Properties::fade_in));
980 resume_property_changes();
984 AudioRegion::recompute_at_start ()
986 /* as above, but the shift was from the front */
988 _envelope->truncate_start (_length);
990 suspend_property_changes();
992 if (_right_of_split) {
993 set_default_fade_in ();
994 _right_of_split = false;
995 } else if (_fade_in->back()->when > _length) {
996 _fade_in->extend_to (_length);
997 send_change (PropertyChange (Properties::fade_in));
1000 if (_fade_out->back()->when > _length) {
1001 _fade_out->extend_to (_length);
1002 send_change (PropertyChange (Properties::fade_out));
1005 resume_property_changes();
1009 AudioRegion::separate_by_channel (Session& /*session*/, vector<boost::shared_ptr<Region> >& v) const
1015 if (_sources.size() < 2) {
1019 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
1021 srcs.push_back (*i);
1025 if (_sources.size() == 2) {
1033 new_name += ('0' + n + 1);
1036 /* create a copy with just one source. prevent if from being thought of as
1037 "whole file" even if it covers the entire source file(s).
1042 plist.add (Properties::start, _start.val());
1043 plist.add (Properties::length, _length.val());
1044 plist.add (Properties::name, new_name);
1045 plist.add (Properties::layer, _layer.val());
1047 v.push_back(RegionFactory::create (srcs, plist));
1048 v.back()->set_whole_file (false);
1057 AudioRegion::read_raw_internal (Sample* buf, framepos_t pos, framecnt_t cnt, int channel) const
1059 return audio_source()->read (buf, pos, cnt, channel);
1063 AudioRegion::exportme (Session& /*session*/, ARDOUR::ExportSpecification& /*spec*/)
1066 // const nframes_t blocksize = 4096;
1067 // nframes_t to_read;
1070 // spec.channels = _sources.size();
1072 // if (spec.prepare (blocksize, session.frame_rate())) {
1077 // spec.total_frames = _length;
1079 // while (spec.pos < _length && !spec.stop) {
1082 // /* step 1: interleave */
1084 // to_read = min (_length - spec.pos, blocksize);
1086 // if (spec.channels == 1) {
1088 // if (read_raw_internal (spec.dataF, _start + spec.pos, to_read) != to_read) {
1094 // Sample buf[blocksize];
1096 // for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1098 // if (audio_source(chan)->read (buf, _start + spec.pos, to_read) != to_read) {
1102 // for (nframes_t x = 0; x < to_read; ++x) {
1103 // spec.dataF[chan+(x*spec.channels)] = buf[x];
1108 // if (spec.process (to_read)) {
1112 // spec.pos += to_read;
1113 // spec.progress = (double) spec.pos /_length;
1120 // spec.running = false;
1121 // spec.status = status;
1129 AudioRegion::set_scale_amplitude (gain_t g)
1131 boost::shared_ptr<Playlist> pl (playlist());
1133 _scale_amplitude = g;
1135 /* tell the diskstream we're in */
1138 pl->ContentsChanged();
1141 /* tell everybody else */
1143 send_change (PropertyChange (Properties::scale_amplitude));
1146 /** @return the maximum (linear) amplitude of the region */
1148 AudioRegion::maximum_amplitude (Progress* p) const
1150 framepos_t fpos = _start;
1151 framepos_t const fend = _start + _length;
1154 framecnt_t const blocksize = 64 * 1024;
1155 Sample buf[blocksize];
1157 while (fpos < fend) {
1161 framecnt_t const to_read = min (fend - fpos, blocksize);
1163 for (n = 0; n < n_channels(); ++n) {
1167 if (read_raw_internal (buf, fpos, to_read, 0) != to_read) {
1171 maxamp = compute_peak (buf, to_read, maxamp);
1175 p->set_progress (float (fpos - _start) / _length);
1181 /** Normalize using a given maximum amplitude and target, so that region
1182 * _scale_amplitude becomes target / max_amplitude.
1185 AudioRegion::normalize (float max_amplitude, float target_dB)
1187 gain_t target = dB_to_coefficient (target_dB);
1189 if (target == 1.0f) {
1190 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1191 that we may have clipped.
1193 target -= FLT_EPSILON;
1196 if (max_amplitude == 0.0f) {
1197 /* don't even try */
1201 if (max_amplitude == target) {
1202 /* we can't do anything useful */
1206 set_scale_amplitude (target / max_amplitude);
1210 AudioRegion::fade_in_changed ()
1212 send_change (PropertyChange (Properties::fade_in));
1216 AudioRegion::fade_out_changed ()
1218 send_change (PropertyChange (Properties::fade_out));
1222 AudioRegion::envelope_changed ()
1224 send_change (PropertyChange (Properties::envelope));
1228 AudioRegion::suspend_fade_in ()
1230 if (++_fade_in_suspended == 1) {
1231 if (fade_in_is_default()) {
1232 set_fade_in_active (false);
1238 AudioRegion::resume_fade_in ()
1240 if (--_fade_in_suspended == 0 && _fade_in_suspended) {
1241 set_fade_in_active (true);
1246 AudioRegion::suspend_fade_out ()
1248 if (++_fade_out_suspended == 1) {
1249 if (fade_out_is_default()) {
1250 set_fade_out_active (false);
1256 AudioRegion::resume_fade_out ()
1258 if (--_fade_out_suspended == 0 &&_fade_out_suspended) {
1259 set_fade_out_active (true);
1264 AudioRegion::speed_mismatch (float sr) const
1266 if (_sources.empty()) {
1267 /* impossible, but ... */
1271 float fsr = audio_source()->sample_rate();
1277 AudioRegion::source_offset_changed ()
1279 /* XXX this fixes a crash that should not occur. It does occur
1280 becauses regions are not being deleted when a session
1281 is unloaded. That bug must be fixed.
1284 if (_sources.empty()) {
1288 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(_sources.front());
1290 if (afs && afs->destructive()) {
1291 // set_start (source()->natural_position(), this);
1292 set_position (source()->natural_position(), this);
1296 boost::shared_ptr<AudioSource>
1297 AudioRegion::audio_source (uint32_t n) const
1299 // Guaranteed to succeed (use a static cast for speed?)
1300 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1304 AudioRegion::adjust_transients (frameoffset_t delta)
1306 for (AnalysisFeatureList::iterator x = _transients.begin(); x != _transients.end(); ++x) {
1307 (*x) = (*x) + delta;
1310 send_change (PropertyChange (Properties::valid_transients));
1316 AudioRegion::update_transient (framepos_t old_position, framepos_t new_position)
1318 for (AnalysisFeatureList::iterator x = _transients.begin(); x != _transients.end(); ++x) {
1319 if ((*x) == old_position) {
1320 (*x) = new_position;
1321 send_change (PropertyChange (Properties::valid_transients));
1331 AudioRegion::add_transient (framepos_t where)
1333 _transients.push_back(where);
1334 _valid_transients = true;
1336 send_change (PropertyChange (Properties::valid_transients));
1340 AudioRegion::remove_transient (framepos_t where)
1342 _transients.remove(where);
1343 _valid_transients = true;
1345 send_change (PropertyChange (Properties::valid_transients));
1349 AudioRegion::set_transients (AnalysisFeatureList& results)
1351 _transients.clear();
1352 _transients = results;
1353 _valid_transients = true;
1355 send_change (PropertyChange (Properties::valid_transients));
1361 AudioRegion::get_transients (AnalysisFeatureList& results, bool force_new)
1363 boost::shared_ptr<Playlist> pl = playlist();
1369 if (_valid_transients && !force_new) {
1370 results = _transients;
1374 SourceList::iterator s;
1376 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1377 if (!(*s)->has_been_analysed()) {
1378 cerr << "For " << name() << " source " << (*s)->name() << " has not been analyzed\n";
1383 if (s == _sources.end()) {
1384 /* all sources are analyzed, merge data from each one */
1386 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1388 /* find the set of transients within the bounds of this region */
1390 AnalysisFeatureList::iterator low = lower_bound ((*s)->transients.begin(),
1391 (*s)->transients.end(),
1394 AnalysisFeatureList::iterator high = upper_bound ((*s)->transients.begin(),
1395 (*s)->transients.end(),
1400 results.insert (results.end(), low, high);
1403 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1405 /* translate all transients to current position */
1407 for (AnalysisFeatureList::iterator x = results.begin(); x != results.end(); ++x) {
1412 _transients = results;
1413 _valid_transients = true;
1418 /* no existing/complete transient info */
1420 static bool analyse_dialog_shown = false; /* global per instance of Ardour */
1422 if (!Config->get_auto_analyse_audio()) {
1423 if (!analyse_dialog_shown) {
1424 pl->session().Dialog (_("\
1425 You have requested an operation that requires audio analysis.\n\n\
1426 You currently have \"auto-analyse-audio\" disabled, which means \
1427 that transient data must be generated every time it is required.\n\n\
1428 If you are doing work that will require transient data on a \
1429 regular basis, you should probably enable \"auto-analyse-audio\" \
1430 then quit ardour and restart.\n\n\
1431 This dialog will not display again. But you may notice a slight delay \
1432 in this and future transient-detection operations.\n\
1434 analyse_dialog_shown = true;
1438 TransientDetector t (pl->session().frame_rate());
1439 bool existing_results = !results.empty();
1441 _transients.clear ();
1442 _valid_transients = false;
1444 for (uint32_t i = 0; i < n_channels(); ++i) {
1446 AnalysisFeatureList these_results;
1450 if (t.run ("", this, i, these_results)) {
1454 /* translate all transients to give absolute position */
1456 for (AnalysisFeatureList::iterator i = these_results.begin(); i != these_results.end(); ++i) {
1462 _transients.insert (_transients.end(), these_results.begin(), these_results.end());
1465 if (!results.empty()) {
1466 if (existing_results) {
1468 /* merge our transients into the existing ones, then clean up
1472 results.insert (results.end(), _transients.begin(), _transients.end());
1473 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1476 /* make sure ours are clean too */
1478 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1482 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1483 results = _transients;
1486 _valid_transients = true;
1491 /** Find areas of `silence' within a region.
1493 * @param threshold Threshold below which signal is considered silence (as a sample value)
1494 * @param min_length Minimum length of silent period to be reported.
1495 * @return Silent periods; first of pair is the offset within the region, second is the length of the period
1498 std::list<std::pair<frameoffset_t, framecnt_t> >
1499 AudioRegion::find_silence (Sample threshold, framecnt_t min_length, InterThreadInfo& itt) const
1501 framecnt_t const block_size = 64 * 1024;
1502 Sample loudest[block_size];
1503 Sample buf[block_size];
1505 framepos_t pos = _start;
1506 framepos_t const end = _start + _length - 1;
1508 std::list<std::pair<frameoffset_t, framecnt_t> > silent_periods;
1510 bool in_silence = false;
1511 frameoffset_t silence_start = 0;
1514 while (pos < end && !itt.cancel) {
1516 /* fill `loudest' with the loudest absolute sample at each instant, across all channels */
1517 memset (loudest, 0, sizeof (Sample) * block_size);
1518 for (uint32_t n = 0; n < n_channels(); ++n) {
1520 read_raw_internal (buf, pos, block_size, n);
1521 for (framecnt_t i = 0; i < block_size; ++i) {
1522 loudest[i] = max (loudest[i], abs (buf[i]));
1526 /* now look for silence */
1527 for (framecnt_t i = 0; i < block_size; ++i) {
1528 silence = abs (loudest[i]) < threshold;
1529 if (silence && !in_silence) {
1530 /* non-silence to silence */
1532 silence_start = pos + i;
1533 } else if (!silence && in_silence) {
1534 /* silence to non-silence */
1536 if (pos + i - 1 - silence_start >= min_length) {
1537 silent_periods.push_back (std::make_pair (silence_start, pos + i - 1));
1543 itt.progress = (end-pos)/(double)_length;
1546 if (in_silence && end - 1 - silence_start >= min_length) {
1547 /* last block was silent, so finish off the last period */
1548 silent_periods.push_back (std::make_pair (silence_start, end));
1553 return silent_periods;
1560 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)
1562 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (framecnt_t) npeaks, (framepos_t) start, (framecnt_t) cnt, n_chan,samples_per_unit);
1565 uint32_t region_length_from_c (void *arg)
1568 return ((AudioRegion *) arg)->length();
1571 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1573 return ( (AudioRegion *) arg)->audio_source()->available_peaks (zoom_factor) ;