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/session.h"
40 #include "ardour/gain.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"
52 using namespace ARDOUR;
55 /* a Session will reset these to its chosen defaults by calling AudioRegion::set_default_fade() */
57 Change AudioRegion::FadeInChanged = ARDOUR::new_change();
58 Change AudioRegion::FadeOutChanged = ARDOUR::new_change();
59 Change AudioRegion::FadeInActiveChanged = ARDOUR::new_change();
60 Change AudioRegion::FadeOutActiveChanged = ARDOUR::new_change();
61 Change AudioRegion::EnvelopeActiveChanged = ARDOUR::new_change();
62 Change AudioRegion::ScaleAmplitudeChanged = ARDOUR::new_change();
63 Change AudioRegion::EnvelopeChanged = ARDOUR::new_change();
68 _scale_amplitude = 1.0;
71 set_default_envelope ();
73 listen_to_my_curves ();
74 connect_to_analysis_changed ();
77 /** Constructor for use by derived types only */
78 AudioRegion::AudioRegion (Session& s, nframes_t start, nframes_t length, string name)
79 : Region (s, start, length, name, DataType::AUDIO)
81 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
82 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
83 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
86 assert (_sources.size() == _master_sources.size());
89 /** Basic AudioRegion constructor (one channel) */
90 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length)
91 : Region (src, start, length, PBD::basename_nosuffix(src->name()), DataType::AUDIO, 0, Region::Flag(Region::DefaultFlags|Region::External))
92 , _automatable(src->session())
93 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
94 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
95 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
97 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
99 afs->HeaderPositionOffsetChanged.connect_same_thread (*this, boost::bind (&AudioRegion::source_offset_changed, this));
103 assert (_sources.size() == _master_sources.size());
106 /* Basic AudioRegion constructor (one channel) */
107 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
108 : Region (src, start, length, name, DataType::AUDIO, layer, flags)
109 , _automatable(src->session())
110 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
111 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
112 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
114 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
116 afs->HeaderPositionOffsetChanged.connect_same_thread (*this, boost::bind (&AudioRegion::source_offset_changed, this));
120 assert (_sources.size() == _master_sources.size());
123 /** Basic AudioRegion constructor (many channels) */
124 AudioRegion::AudioRegion (const SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
125 : Region (srcs, start, length, name, DataType::AUDIO, layer, flags)
126 , _automatable(srcs[0]->session())
127 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
128 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
129 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
132 connect_to_analysis_changed ();
133 assert (_sources.size() == _master_sources.size());
136 /** Create a new AudioRegion, that is part of an existing one */
137 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
138 : Region (other, offset, length, name, layer, flags)
139 , _automatable(other->session())
140 , _fade_in (new AutomationList(*other->_fade_in))
141 , _fade_out (new AutomationList(*other->_fade_out))
142 , _envelope (new AutomationList(*other->_envelope, offset, offset + length))
144 connect_to_header_position_offset_changed ();
146 /* return to default fades if the existing ones are too long */
148 if (_flags & LeftOfSplit) {
149 if (_fade_in->back()->when >= _length) {
150 set_default_fade_in ();
152 _fade_in_disabled = other->_fade_in_disabled;
154 set_default_fade_out ();
155 _flags = Flag (_flags & ~Region::LeftOfSplit);
158 if (_flags & RightOfSplit) {
159 if (_fade_out->back()->when >= _length) {
160 set_default_fade_out ();
162 _fade_out_disabled = other->_fade_out_disabled;
164 set_default_fade_in ();
165 _flags = Flag (_flags & ~Region::RightOfSplit);
168 _scale_amplitude = other->_scale_amplitude;
170 assert(_type == DataType::AUDIO);
172 listen_to_my_curves ();
173 connect_to_analysis_changed ();
175 assert (_sources.size() == _master_sources.size());
178 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
180 , _automatable (other->session())
181 , _fade_in (new AutomationList (*other->_fade_in))
182 , _fade_out (new AutomationList (*other->_fade_out))
183 , _envelope (new AutomationList (*other->_envelope))
185 assert(_type == DataType::AUDIO);
186 _scale_amplitude = other->_scale_amplitude;
188 listen_to_my_curves ();
189 connect_to_analysis_changed ();
191 assert (_sources.size() == _master_sources.size());
194 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, const SourceList& /*srcs*/,
195 nframes_t length, const string& name, layer_t layer, Flag flags)
196 : Region (other, length, name, layer, flags)
197 , _automatable (other->session())
198 , _fade_in (new AutomationList (*other->_fade_in))
199 , _fade_out (new AutomationList (*other->_fade_out))
200 , _envelope (new AutomationList (*other->_envelope))
202 /* make-a-sort-of-copy-with-different-sources constructor (used by audio filter) */
204 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
206 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> ((*i));
208 afs->HeaderPositionOffsetChanged.connect_same_thread (*this, boost::bind (&AudioRegion::source_offset_changed, this));
212 _scale_amplitude = other->_scale_amplitude;
214 _fade_in_disabled = 0;
215 _fade_out_disabled = 0;
217 listen_to_my_curves ();
218 connect_to_analysis_changed ();
220 assert (_sources.size() == _master_sources.size());
223 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, const XMLNode& node)
225 , _automatable(src->session())
226 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
227 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
228 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
230 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
232 afs->HeaderPositionOffsetChanged.connect_same_thread (*this, boost::bind (&AudioRegion::source_offset_changed, this));
237 if (set_state (node, Stateful::loading_state_version)) {
238 throw failed_constructor();
241 assert(_type == DataType::AUDIO);
242 connect_to_analysis_changed ();
244 assert (_sources.size() == _master_sources.size());
247 AudioRegion::AudioRegion (SourceList& srcs, const XMLNode& node)
248 : Region (srcs, node)
249 , _automatable(srcs[0]->session())
250 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
251 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
252 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
256 if (set_state (node, Stateful::loading_state_version)) {
257 throw failed_constructor();
260 assert(_type == DataType::AUDIO);
261 connect_to_analysis_changed ();
262 assert (_sources.size() == _master_sources.size());
265 AudioRegion::~AudioRegion ()
270 AudioRegion::connect_to_analysis_changed ()
272 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
273 (*i)->AnalysisChanged.connect_same_thread (*this, boost::bind (&AudioRegion::invalidate_transients, this));
278 AudioRegion::connect_to_header_position_offset_changed ()
280 set<boost::shared_ptr<Source> > unique_srcs;
282 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
284 if (unique_srcs.find (*i) == unique_srcs.end ()) {
285 unique_srcs.insert (*i);
286 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
288 afs->HeaderPositionOffsetChanged.connect_same_thread (*this, boost::bind (&AudioRegion::source_offset_changed, this));
295 AudioRegion::listen_to_my_curves ()
297 cerr << _name << ": listeing my own curves\n";
299 _envelope->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::envelope_changed, this));
300 _fade_in->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::fade_in_changed, this));
301 _fade_out->StateChanged.connect_same_thread (*this, boost::bind (&AudioRegion::fade_out_changed, this));
305 AudioRegion::set_envelope_active (bool yn)
307 if (envelope_active() != yn) {
310 snprintf (buf, sizeof (buf), "envelope active");
311 _flags = Flag (_flags|EnvelopeActive);
313 snprintf (buf, sizeof (buf), "envelope off");
314 _flags = Flag (_flags & ~EnvelopeActive);
316 send_change (EnvelopeActiveChanged);
321 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
323 if (chan_n >= _sources.size()) {
327 if (audio_source(chan_n)->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
330 if (_scale_amplitude != 1.0) {
331 for (nframes_t n = 0; n < npeaks; ++n) {
332 buf[n].max *= _scale_amplitude;
333 buf[n].min *= _scale_amplitude;
341 AudioRegion::read (Sample* buf, sframes_t timeline_position, nframes_t cnt, int channel) const
343 /* raw read, no fades, no gain, nada */
344 return _read_at (_sources, _length, buf, 0, 0, _position + timeline_position, cnt, channel, 0, 0, ReadOps (0));
348 AudioRegion::read_with_ops (Sample* buf, sframes_t file_position, nframes_t cnt, int channel, ReadOps rops) const
350 return _read_at (_sources, _length, buf, 0, 0, file_position, cnt, channel, 0, 0, rops);
354 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
355 sframes_t file_position, nframes_t cnt, uint32_t chan_n,
356 nframes_t read_frames, nframes_t skip_frames) const
358 /* regular diskstream/butler read complete with fades etc */
359 return _read_at (_sources, _length, buf, mixdown_buffer, gain_buffer,
360 file_position, cnt, chan_n, read_frames, skip_frames, ReadOps (~0));
364 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
365 sframes_t position, nframes_t cnt, uint32_t chan_n) const
367 /* do not read gain/scaling/fades and do not count this disk i/o in statistics */
369 return _read_at (_master_sources, _master_sources.front()->length(_master_sources.front()->timeline_position()),
370 buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0, ReadOps (0));
374 AudioRegion::_read_at (const SourceList& /*srcs*/, nframes_t limit,
375 Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
376 sframes_t position, nframes_t cnt,
378 nframes_t /*read_frames*/,
379 nframes_t /*skip_frames*/,
382 nframes_t internal_offset;
383 nframes_t buf_offset;
385 bool raw = (rops == ReadOpsNone);
387 if (muted() && !raw) {
388 return 0; /* read nothing */
391 /* precondition: caller has verified that we cover the desired section */
393 if (position < _position) {
395 buf_offset = _position - position;
398 internal_offset = position - _position;
402 if (internal_offset >= limit) {
403 return 0; /* read nothing */
406 if ((to_read = min (cnt, limit - internal_offset)) == 0) {
407 return 0; /* read nothing */
410 if (opaque() || raw) {
411 /* overwrite whatever is there */
412 mixdown_buffer = buf + buf_offset;
414 mixdown_buffer += buf_offset;
417 if (rops & ReadOpsCount) {
418 _read_data_count = 0;
421 if (chan_n < n_channels()) {
423 boost::shared_ptr<AudioSource> src = audio_source(chan_n);
424 if (src->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
425 return 0; /* "read nothing" */
428 if (rops & ReadOpsCount) {
429 _read_data_count += src->read_data_count();
434 /* track is N-channel, this region has less channels; silence the ones
438 memset (mixdown_buffer, 0, sizeof (Sample) * cnt);
441 if (rops & ReadOpsFades) {
445 if ((_flags & FadeIn) && _session.config.get_use_region_fades()) {
447 nframes_t fade_in_length = (nframes_t) _fade_in->back()->when;
449 /* see if this read is within the fade in */
451 if (internal_offset < fade_in_length) {
455 fi_limit = min (to_read, fade_in_length - internal_offset);
458 _fade_in->curve().get_vector (internal_offset, internal_offset+fi_limit, gain_buffer, fi_limit);
460 for (nframes_t n = 0; n < fi_limit; ++n) {
461 mixdown_buffer[n] *= gain_buffer[n];
468 if ((_flags & FadeOut) && _session.config.get_use_region_fades()) {
470 /* see if some part of this read is within the fade out */
472 /* ................. >| REGION
478 limit - fade_out_length
481 ^internal_offset + to_read
483 we need the intersection of [internal_offset,internal_offset+to_read] with
484 [limit - fade_out_length, limit]
489 nframes_t fade_out_length = (nframes_t) _fade_out->back()->when;
490 nframes_t fade_interval_start = max(internal_offset, limit-fade_out_length);
491 nframes_t fade_interval_end = min(internal_offset + to_read, limit);
493 if (fade_interval_end > fade_interval_start) {
494 /* (part of the) the fade out is in this buffer */
496 nframes_t fo_limit = fade_interval_end - fade_interval_start;
497 nframes_t curve_offset = fade_interval_start - (limit-fade_out_length);
498 nframes_t fade_offset = fade_interval_start - internal_offset;
500 _fade_out->curve().get_vector (curve_offset, curve_offset+fo_limit, gain_buffer, fo_limit);
502 for (nframes_t n = 0, m = fade_offset; n < fo_limit; ++n, ++m) {
503 mixdown_buffer[m] *= gain_buffer[n];
510 /* Regular gain curves and scaling */
512 if ((rops & ReadOpsOwnAutomation) && envelope_active()) {
513 _envelope->curve().get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
515 if ((rops & ReadOpsOwnScaling) && _scale_amplitude != 1.0f) {
516 for (nframes_t n = 0; n < to_read; ++n) {
517 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
520 for (nframes_t n = 0; n < to_read; ++n) {
521 mixdown_buffer[n] *= gain_buffer[n];
524 } else if ((rops & ReadOpsOwnScaling) && _scale_amplitude != 1.0f) {
526 // XXX this should be using what in 2.0 would have been:
527 // Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
529 for (nframes_t n = 0; n < to_read; ++n) {
530 mixdown_buffer[n] *= _scale_amplitude;
536 /* gack. the things we do for users.
541 for (nframes_t n = 0; n < to_read; ++n) {
542 buf[n] += mixdown_buffer[n];
550 AudioRegion::state (bool full)
552 XMLNode& node (Region::state (full));
556 LocaleGuard lg (X_("POSIX"));
558 node.add_property ("flags", enum_2_string (_flags));
560 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
561 node.add_property ("scale-gain", buf);
563 // XXX these should move into Region
565 for (uint32_t n=0; n < _sources.size(); ++n) {
566 snprintf (buf2, sizeof(buf2), "source-%d", n);
567 _sources[n]->id().print (buf, sizeof (buf));
568 node.add_property (buf2, buf);
571 for (uint32_t n=0; n < _master_sources.size(); ++n) {
572 snprintf (buf2, sizeof(buf2), "master-source-%d", n);
573 _master_sources[n]->id().print (buf, sizeof (buf));
574 node.add_property (buf2, buf);
577 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
578 node.add_property ("channels", buf);
582 child = node.add_child (X_("FadeIn"));
584 if ((_flags & DefaultFadeIn)) {
585 child->add_property (X_("default"), X_("yes"));
587 child->add_child_nocopy (_fade_in->get_state ());
590 child->add_property (X_("active"), fade_in_active () ? X_("yes") : X_("no"));
592 child = node.add_child (X_("FadeOut"));
594 if ((_flags & DefaultFadeOut)) {
595 child->add_property (X_("default"), X_("yes"));
597 child->add_child_nocopy (_fade_out->get_state ());
600 child->add_property (X_("active"), fade_out_active () ? X_("yes") : X_("no"));
603 child = node.add_child ("Envelope");
606 bool default_env = false;
608 // If there are only two points, the points are in the start of the region and the end of the region
609 // so, if they are both at 1.0f, that means the default region.
611 if (_envelope->size() == 2 &&
612 _envelope->front()->value == 1.0f &&
613 _envelope->back()->value==1.0f) {
614 if (_envelope->front()->when == 0 && _envelope->back()->when == _length) {
620 child->add_property ("default", "yes");
622 child->add_child_nocopy (_envelope->get_state ());
626 child->add_property ("default", "yes");
629 if (full && _extra_xml) {
630 node.add_child_copy (*_extra_xml);
637 AudioRegion::set_live_state (const XMLNode& node, int version, Change& what_changed, bool send)
639 const XMLNodeList& nlist = node.children();
640 const XMLProperty *prop;
641 LocaleGuard lg (X_("POSIX"));
642 boost::shared_ptr<Playlist> the_playlist (_playlist.lock());
646 the_playlist->freeze ();
649 Region::set_live_state (node, version, what_changed, false);
650 cerr << "After region SLS, wc = " << what_changed << endl;
652 uint32_t old_flags = _flags;
654 if ((prop = node.property ("flags")) != 0) {
655 _flags = Flag (string_2_enum (prop->value(), _flags));
657 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
659 _flags = Flag (_flags & ~Region::LeftOfSplit);
660 _flags = Flag (_flags & ~Region::RightOfSplit);
663 /* leave this flag setting in place, no matter what */
665 if ((old_flags & DoNotSendPropertyChanges)) {
666 _flags = Flag (_flags | DoNotSendPropertyChanges);
669 /* find out if any flags changed that we signal about */
671 if ((old_flags ^ _flags) & Muted) {
672 what_changed = Change (what_changed|MuteChanged);
673 cerr << _name << " mute changed\n";
675 if ((old_flags ^ _flags) & Opaque) {
676 what_changed = Change (what_changed|OpacityChanged);
677 cerr << _name << " opacity changed\n";
679 if ((old_flags ^ _flags) & Locked) {
680 what_changed = Change (what_changed|LockChanged);
681 cerr << _name << " lock changed\n";
684 if ((prop = node.property ("scale-gain")) != 0) {
685 float a = atof (prop->value().c_str());
686 if (a != _scale_amplitude) {
687 _scale_amplitude = a;
688 what_changed = Change (what_changed|ScaleAmplitudeChanged);
689 cerr << _name << " amp changed\n";
692 _scale_amplitude = 1.0;
695 /* Now find envelope description and other misc child items */
697 _envelope->freeze ();
699 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
707 if (child->name() == "Envelope") {
711 if ((prop = child->property ("default")) != 0 || _envelope->set_state (*child, version)) {
712 set_default_envelope ();
715 _envelope->set_max_xval (_length);
716 _envelope->truncate_end (_length);
718 cerr << _name << " envelope changd\n";
721 } else if (child->name() == "FadeIn") {
725 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
726 set_default_fade_in ();
728 XMLNode* grandchild = child->child ("AutomationList");
730 _fade_in->set_state (*grandchild, version);
734 if ((prop = child->property ("active")) != 0) {
735 if (string_is_affirmative (prop->value())) {
736 set_fade_in_active (true);
738 set_fade_in_active (false);
741 cerr << _name << " fadein changd\n";
743 } else if (child->name() == "FadeOut") {
747 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
748 set_default_fade_out ();
750 XMLNode* grandchild = child->child ("AutomationList");
752 _fade_out->set_state (*grandchild, version);
756 if ((prop = child->property ("active")) != 0) {
757 if (string_is_affirmative (prop->value())) {
758 set_fade_out_active (true);
760 set_fade_out_active (false);
763 cerr << _name << " fadeout changd\n";
773 cerr << _name << ": audio final change: " << hex << what_changed << dec << endl;
774 send_change (what_changed);
778 the_playlist->thaw ();
785 AudioRegion::set_state (const XMLNode& node, int version)
787 /* Region::set_state() calls the virtual set_live_state(),
788 which will get us back to AudioRegion::set_live_state()
789 to handle the relevant stuff.
792 return Region::set_state (node, version);
796 AudioRegion::set_fade_in_shape (FadeShape shape)
798 set_fade_in (shape, (nframes_t) _fade_in->back()->when);
802 AudioRegion::set_fade_out_shape (FadeShape shape)
804 set_fade_out (shape, (nframes_t) _fade_out->back()->when);
808 AudioRegion::set_fade_in (boost::shared_ptr<AutomationList> f)
814 send_change (FadeInChanged);
818 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
825 _fade_in->fast_simple_add (0.0, 0.0);
826 _fade_in->fast_simple_add (len, 1.0);
830 _fade_in->fast_simple_add (0, 0);
831 _fade_in->fast_simple_add (len * 0.389401, 0.0333333);
832 _fade_in->fast_simple_add (len * 0.629032, 0.0861111);
833 _fade_in->fast_simple_add (len * 0.829493, 0.233333);
834 _fade_in->fast_simple_add (len * 0.9447, 0.483333);
835 _fade_in->fast_simple_add (len * 0.976959, 0.697222);
836 _fade_in->fast_simple_add (len, 1);
840 _fade_in->fast_simple_add (0, 0);
841 _fade_in->fast_simple_add (len * 0.0207373, 0.197222);
842 _fade_in->fast_simple_add (len * 0.0645161, 0.525);
843 _fade_in->fast_simple_add (len * 0.152074, 0.802778);
844 _fade_in->fast_simple_add (len * 0.276498, 0.919444);
845 _fade_in->fast_simple_add (len * 0.481567, 0.980556);
846 _fade_in->fast_simple_add (len * 0.767281, 1);
847 _fade_in->fast_simple_add (len, 1);
851 _fade_in->fast_simple_add (0, 0);
852 _fade_in->fast_simple_add (len * 0.0737327, 0.308333);
853 _fade_in->fast_simple_add (len * 0.246544, 0.658333);
854 _fade_in->fast_simple_add (len * 0.470046, 0.886111);
855 _fade_in->fast_simple_add (len * 0.652074, 0.972222);
856 _fade_in->fast_simple_add (len * 0.771889, 0.988889);
857 _fade_in->fast_simple_add (len, 1);
861 _fade_in->fast_simple_add (0, 0);
862 _fade_in->fast_simple_add (len * 0.304147, 0.0694444);
863 _fade_in->fast_simple_add (len * 0.529954, 0.152778);
864 _fade_in->fast_simple_add (len * 0.725806, 0.333333);
865 _fade_in->fast_simple_add (len * 0.847926, 0.558333);
866 _fade_in->fast_simple_add (len * 0.919355, 0.730556);
867 _fade_in->fast_simple_add (len, 1);
875 AudioRegion::set_fade_out (boost::shared_ptr<AutomationList> f)
877 _fade_out->freeze ();
881 send_change (FadeInChanged);
885 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
887 _fade_out->freeze ();
892 _fade_out->fast_simple_add (len * 0, 1);
893 _fade_out->fast_simple_add (len * 0.023041, 0.697222);
894 _fade_out->fast_simple_add (len * 0.0553, 0.483333);
895 _fade_out->fast_simple_add (len * 0.170507, 0.233333);
896 _fade_out->fast_simple_add (len * 0.370968, 0.0861111);
897 _fade_out->fast_simple_add (len * 0.610599, 0.0333333);
898 _fade_out->fast_simple_add (len * 1, 0);
902 _fade_out->fast_simple_add (len * 0, 1);
903 _fade_out->fast_simple_add (len * 0.228111, 0.988889);
904 _fade_out->fast_simple_add (len * 0.347926, 0.972222);
905 _fade_out->fast_simple_add (len * 0.529954, 0.886111);
906 _fade_out->fast_simple_add (len * 0.753456, 0.658333);
907 _fade_out->fast_simple_add (len * 0.9262673, 0.308333);
908 _fade_out->fast_simple_add (len * 1, 0);
912 _fade_out->fast_simple_add (len * 0, 1);
913 _fade_out->fast_simple_add (len * 0.305556, 1);
914 _fade_out->fast_simple_add (len * 0.548611, 0.991736);
915 _fade_out->fast_simple_add (len * 0.759259, 0.931129);
916 _fade_out->fast_simple_add (len * 0.918981, 0.68595);
917 _fade_out->fast_simple_add (len * 0.976852, 0.22865);
918 _fade_out->fast_simple_add (len * 1, 0);
922 _fade_out->fast_simple_add (len * 0, 1);
923 _fade_out->fast_simple_add (len * 0.080645, 0.730556);
924 _fade_out->fast_simple_add (len * 0.277778, 0.289256);
925 _fade_out->fast_simple_add (len * 0.470046, 0.152778);
926 _fade_out->fast_simple_add (len * 0.695853, 0.0694444);
927 _fade_out->fast_simple_add (len * 1, 0);
931 _fade_out->fast_simple_add (len * 0, 1);
932 _fade_out->fast_simple_add (len * 1, 0);
940 AudioRegion::set_fade_in_length (nframes_t len)
946 bool changed = _fade_in->extend_to (len);
949 _flags = Flag (_flags & ~DefaultFadeIn);
950 send_change (FadeInChanged);
955 AudioRegion::set_fade_out_length (nframes_t len)
961 bool changed = _fade_out->extend_to (len);
964 _flags = Flag (_flags & ~DefaultFadeOut);
965 send_change (FadeOutChanged);
970 AudioRegion::set_fade_in_active (bool yn)
972 if (yn == (_flags & FadeIn)) {
976 _flags = Flag (_flags|FadeIn);
978 _flags = Flag (_flags & ~FadeIn);
981 send_change (FadeInActiveChanged);
985 AudioRegion::set_fade_out_active (bool yn)
987 if (yn == (_flags & FadeOut)) {
991 _flags = Flag (_flags | FadeOut);
993 _flags = Flag (_flags & ~FadeOut);
996 send_change (FadeOutActiveChanged);
1000 AudioRegion::fade_in_is_default () const
1002 return _fade_in->size() == 2 && _fade_in->front()->when == 0 && _fade_in->back()->when == 64;
1006 AudioRegion::fade_out_is_default () const
1008 return _fade_out->size() == 2 && _fade_out->front()->when == 0 && _fade_out->back()->when == 64;
1012 AudioRegion::set_default_fade_in ()
1014 _fade_in_disabled = 0;
1015 set_fade_in (Linear, 64);
1019 AudioRegion::set_default_fade_out ()
1021 _fade_out_disabled = 0;
1022 set_fade_out (Linear, 64);
1026 AudioRegion::set_default_fades ()
1028 set_default_fade_in ();
1029 set_default_fade_out ();
1033 AudioRegion::set_default_envelope ()
1035 _envelope->freeze ();
1036 _envelope->clear ();
1037 _envelope->fast_simple_add (0, 1.0f);
1038 _envelope->fast_simple_add (_length, 1.0f);
1043 AudioRegion::recompute_at_end ()
1045 /* our length has changed. recompute a new final point by interpolating
1046 based on the the existing curve.
1049 _envelope->freeze ();
1050 _envelope->truncate_end (_length);
1051 _envelope->set_max_xval (_length);
1054 if (_fade_in->back()->when > _length) {
1055 _fade_in->extend_to (_length);
1056 send_change (FadeInChanged);
1059 if (_fade_out->back()->when > _length) {
1060 _fade_out->extend_to (_length);
1061 send_change (FadeOutChanged);
1066 AudioRegion::recompute_at_start ()
1068 /* as above, but the shift was from the front */
1070 _envelope->truncate_start (_length);
1072 if (_fade_in->back()->when > _length) {
1073 _fade_in->extend_to (_length);
1074 send_change (FadeInChanged);
1077 if (_fade_out->back()->when > _length) {
1078 _fade_out->extend_to (_length);
1079 send_change (FadeOutChanged);
1084 AudioRegion::separate_by_channel (Session& /*session*/, vector<boost::shared_ptr<Region> >& v) const
1090 if (_sources.size() < 2) {
1094 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
1096 srcs.push_back (*i);
1100 if (_sources.size() == 2) {
1108 new_name += ('0' + n + 1);
1111 /* create a copy with just one source. prevent if from being thought of as
1112 "whole file" even if it covers the entire source file(s).
1115 Flag f = Flag (_flags & ~WholeFile);
1117 v.push_back(RegionFactory::create (srcs, _start, _length, new_name, _layer, f));
1126 AudioRegion::read_raw_internal (Sample* buf, sframes_t pos, nframes_t cnt, int channel) const
1128 return audio_source()->read (buf, pos, cnt, channel);
1132 AudioRegion::exportme (Session& /*session*/, ARDOUR::ExportSpecification& /*spec*/)
1135 // const nframes_t blocksize = 4096;
1136 // nframes_t to_read;
1139 // spec.channels = _sources.size();
1141 // if (spec.prepare (blocksize, session.frame_rate())) {
1146 // spec.total_frames = _length;
1148 // while (spec.pos < _length && !spec.stop) {
1151 // /* step 1: interleave */
1153 // to_read = min (_length - spec.pos, blocksize);
1155 // if (spec.channels == 1) {
1157 // if (read_raw_internal (spec.dataF, _start + spec.pos, to_read) != to_read) {
1163 // Sample buf[blocksize];
1165 // for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1167 // if (audio_source(chan)->read (buf, _start + spec.pos, to_read) != to_read) {
1171 // for (nframes_t x = 0; x < to_read; ++x) {
1172 // spec.dataF[chan+(x*spec.channels)] = buf[x];
1177 // if (spec.process (to_read)) {
1181 // spec.pos += to_read;
1182 // spec.progress = (double) spec.pos /_length;
1189 // spec.running = false;
1190 // spec.status = status;
1198 AudioRegion::set_scale_amplitude (gain_t g)
1200 boost::shared_ptr<Playlist> pl (playlist());
1202 _scale_amplitude = g;
1204 /* tell the diskstream we're in */
1207 pl->ContentsChanged();
1210 /* tell everybody else */
1212 send_change (ScaleAmplitudeChanged);
1216 AudioRegion::normalize_to (float target_dB)
1218 const nframes_t blocksize = 64 * 1024;
1219 Sample buf[blocksize];
1224 gain_t target = dB_to_coefficient (target_dB);
1226 if (target == 1.0f) {
1227 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1228 that we may have clipped.
1230 target -= FLT_EPSILON;
1234 fend = _start + _length;
1236 /* first pass: find max amplitude */
1238 while (fpos < fend) {
1242 to_read = min (fend - fpos, blocksize);
1244 for (n = 0; n < n_channels(); ++n) {
1248 if (read_raw_internal (buf, fpos, to_read, 0) != to_read) {
1252 maxamp = compute_peak (buf, to_read, maxamp);
1258 if (maxamp == 0.0f) {
1259 /* don't even try */
1263 if (maxamp == target) {
1264 /* we can't do anything useful */
1268 /* compute scale factor */
1270 _scale_amplitude = target/maxamp;
1272 /* tell the diskstream we're in */
1274 boost::shared_ptr<Playlist> pl (playlist());
1277 pl->ContentsChanged();
1280 /* tell everybody else */
1282 send_change (ScaleAmplitudeChanged);
1286 AudioRegion::fade_in_changed ()
1288 send_change (FadeInChanged);
1292 AudioRegion::fade_out_changed ()
1294 send_change (FadeOutChanged);
1298 AudioRegion::envelope_changed ()
1300 send_change (EnvelopeChanged);
1304 AudioRegion::suspend_fade_in ()
1306 if (++_fade_in_disabled == 1) {
1307 if (fade_in_is_default()) {
1308 set_fade_in_active (false);
1314 AudioRegion::resume_fade_in ()
1316 if (--_fade_in_disabled == 0 && _fade_in_disabled) {
1317 set_fade_in_active (true);
1322 AudioRegion::suspend_fade_out ()
1324 if (++_fade_out_disabled == 1) {
1325 if (fade_out_is_default()) {
1326 set_fade_out_active (false);
1332 AudioRegion::resume_fade_out ()
1334 if (--_fade_out_disabled == 0 &&_fade_out_disabled) {
1335 set_fade_out_active (true);
1340 AudioRegion::speed_mismatch (float sr) const
1342 if (_sources.empty()) {
1343 /* impossible, but ... */
1347 float fsr = audio_source()->sample_rate();
1353 AudioRegion::source_offset_changed ()
1355 /* XXX this fixes a crash that should not occur. It does occur
1356 becauses regions are not being deleted when a session
1357 is unloaded. That bug must be fixed.
1360 if (_sources.empty()) {
1364 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(_sources.front());
1366 if (afs && afs->destructive()) {
1367 // set_start (source()->natural_position(), this);
1368 set_position (source()->natural_position(), this);
1372 boost::shared_ptr<AudioSource>
1373 AudioRegion::audio_source (uint32_t n) const
1375 // Guaranteed to succeed (use a static cast for speed?)
1376 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1380 AudioRegion::get_transients (AnalysisFeatureList& results, bool force_new)
1382 boost::shared_ptr<Playlist> pl = playlist();
1388 if (_valid_transients && !force_new) {
1389 results = _transients;
1393 SourceList::iterator s;
1395 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1396 if (!(*s)->has_been_analysed()) {
1397 cerr << "For " << name() << " source " << (*s)->name() << " has not been analyzed\n";
1402 if (s == _sources.end()) {
1403 /* all sources are analyzed, merge data from each one */
1405 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1407 /* find the set of transients within the bounds of this region */
1409 AnalysisFeatureList::iterator low = lower_bound ((*s)->transients.begin(),
1410 (*s)->transients.end(),
1413 AnalysisFeatureList::iterator high = upper_bound ((*s)->transients.begin(),
1414 (*s)->transients.end(),
1419 results.insert (results.end(), low, high);
1422 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1424 /* translate all transients to current position */
1426 for (AnalysisFeatureList::iterator x = results.begin(); x != results.end(); ++x) {
1431 _transients = results;
1432 _valid_transients = true;
1437 /* no existing/complete transient info */
1439 if (!Config->get_auto_analyse_audio()) {
1440 pl->session().Dialog (_("\
1441 You have requested an operation that requires audio analysis.\n\n\
1442 You currently have \"auto-analyse-audio\" disabled, which means\n\
1443 that transient data must be generated every time it is required.\n\n\
1444 If you are doing work that will require transient data on a\n\
1445 regular basis, you should probably enable \"auto-analyse-audio\"\n\
1446 then quit ardour and restart."));
1449 TransientDetector t (pl->session().frame_rate());
1450 bool existing_results = !results.empty();
1452 _transients.clear ();
1453 _valid_transients = false;
1455 for (uint32_t i = 0; i < n_channels(); ++i) {
1457 AnalysisFeatureList these_results;
1461 if (t.run ("", this, i, these_results)) {
1465 /* translate all transients to give absolute position */
1467 for (AnalysisFeatureList::iterator i = these_results.begin(); i != these_results.end(); ++i) {
1473 _transients.insert (_transients.end(), these_results.begin(), these_results.end());
1476 if (!results.empty()) {
1477 if (existing_results) {
1479 /* merge our transients into the existing ones, then clean up
1483 results.insert (results.end(), _transients.begin(), _transients.end());
1484 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1487 /* make sure ours are clean too */
1489 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1493 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1494 results = _transients;
1497 _valid_transients = true;
1502 /** Find areas of `silence' within a region.
1504 * @param threshold Threshold below which signal is considered silence (as a sample value)
1505 * @param min_length Minimum length of silent period to be reported.
1506 * @return Silent periods; first of pair is the offset within the region, second is the length of the period
1509 std::list<std::pair<nframes_t, nframes_t> >
1510 AudioRegion::find_silence (Sample threshold, nframes_t min_length) const
1512 nframes_t const block_size = 64 * 1024;
1513 Sample loudest[block_size];
1514 Sample buf[block_size];
1516 nframes_t pos = _start;
1517 nframes_t const end = _start + _length - 1;
1519 std::list<std::pair<nframes_t, nframes_t> > silent_periods;
1521 bool in_silence = false;
1522 nframes_t silence_start = 0;
1527 /* fill `loudest' with the loudest absolute sample at each instant, across all channels */
1528 memset (loudest, 0, sizeof (Sample) * block_size);
1529 for (uint32_t n = 0; n < n_channels(); ++n) {
1531 read_raw_internal (buf, pos, block_size, n);
1532 for (nframes_t i = 0; i < block_size; ++i) {
1533 loudest[i] = max (loudest[i], abs (buf[i]));
1537 /* now look for silence */
1538 for (nframes_t i = 0; i < block_size; ++i) {
1539 silence = abs (loudest[i]) < threshold;
1540 if (silence && !in_silence) {
1541 /* non-silence to silence */
1543 silence_start = pos + i;
1544 } else if (!silence && in_silence) {
1545 /* silence to non-silence */
1547 if (pos + i - 1 - silence_start >= min_length) {
1548 silent_periods.push_back (std::make_pair (silence_start, pos + i - 1));
1556 if (in_silence && end - 1 - silence_start >= min_length) {
1557 /* last block was silent, so finish off the last period */
1558 silent_periods.push_back (std::make_pair (silence_start, end));
1561 return silent_periods;
1567 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)
1569 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1572 uint32_t region_length_from_c (void *arg)
1575 return ((AudioRegion *) arg)->length();
1578 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1580 return ( (AudioRegion *) arg)->audio_source()->available_peaks (zoom_factor) ;