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 <sigc++/bind.h>
28 #include <sigc++/class_slot.h>
30 #include <glibmm/thread.h>
32 #include <pbd/basename.h>
33 #include <pbd/xml++.h>
34 #include <pbd/stacktrace.h>
35 #include <pbd/enumwriter.h>
36 #include <pbd/convert.h>
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 listen_to_my_sources ();
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)))
88 /** Basic AudioRegion constructor (one channel) */
89 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length)
90 : Region (src, start, length, PBD::basename_nosuffix(src->name()), DataType::AUDIO, 0, Region::Flag(Region::DefaultFlags|Region::External))
91 , _automatable(src->session())
92 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
93 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
94 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
96 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
98 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
104 /* Basic AudioRegion constructor (one channel) */
105 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
106 : Region (src, start, length, name, DataType::AUDIO, layer, flags)
107 , _automatable(src->session())
108 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
109 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
110 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
112 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
114 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
120 /* Basic AudioRegion constructor (many channels) */
121 AudioRegion::AudioRegion (const SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
122 : Region (srcs, start, length, name, DataType::AUDIO, layer, flags)
123 , _automatable(srcs[0]->session())
124 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
125 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
126 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
129 listen_to_my_sources ();
132 /** Create a new AudioRegion, that is part of an existing one */
133 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
134 : Region (other, offset, length, name, layer, flags)
135 , _automatable(other->session())
136 , _fade_in (new AutomationList(*other->_fade_in))
137 , _fade_out (new AutomationList(*other->_fade_out))
138 , _envelope (new AutomationList(*other->_envelope, offset, offset + length))
140 set<boost::shared_ptr<Source> > unique_srcs;
142 for (SourceList::const_iterator i= other->_sources.begin(); i != other->_sources.end(); ++i) {
143 _sources.push_back (*i);
145 pair<set<boost::shared_ptr<Source> >::iterator,bool> result;
147 result = unique_srcs.insert (*i);
150 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
152 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
157 /* return to default fades if the existing ones are too long */
159 if (_flags & LeftOfSplit) {
160 if (_fade_in->back()->when >= _length) {
161 set_default_fade_in ();
163 _fade_in_disabled = other->_fade_in_disabled;
165 set_default_fade_out ();
166 _flags = Flag (_flags & ~Region::LeftOfSplit);
169 if (_flags & RightOfSplit) {
170 if (_fade_out->back()->when >= _length) {
171 set_default_fade_out ();
173 _fade_out_disabled = other->_fade_out_disabled;
175 set_default_fade_in ();
176 _flags = Flag (_flags & ~Region::RightOfSplit);
179 _scale_amplitude = other->_scale_amplitude;
181 assert(_type == DataType::AUDIO);
183 listen_to_my_curves ();
184 listen_to_my_sources ();
187 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
189 , _automatable (other->session())
190 , _fade_in (new AutomationList (*other->_fade_in))
191 , _fade_out (new AutomationList (*other->_fade_out))
192 , _envelope (new AutomationList (*other->_envelope))
194 assert(_type == DataType::AUDIO);
195 _scale_amplitude = other->_scale_amplitude;
197 set_default_fades ();
199 listen_to_my_curves ();
200 listen_to_my_sources ();
203 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, const SourceList& srcs,
204 nframes_t length, const string& name, layer_t layer, Flag flags)
205 : Region (other, length, name, layer, flags)
206 , _automatable (other->session())
207 , _fade_in (new AutomationList (*other->_fade_in))
208 , _fade_out (new AutomationList (*other->_fade_out))
209 , _envelope (new AutomationList (*other->_envelope))
211 /* make-a-sort-of-copy-with-different-sources constructor (used by audio filter) */
213 set<boost::shared_ptr<AudioSource> > unique_srcs;
215 for (SourceList::const_iterator i=srcs.begin(); i != srcs.end(); ++i) {
217 _sources.push_back (*i);
218 _master_sources.push_back (*i);
220 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> ((*i));
222 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
226 _scale_amplitude = other->_scale_amplitude;
228 _fade_in_disabled = 0;
229 _fade_out_disabled = 0;
231 listen_to_my_curves ();
232 listen_to_my_sources ();
235 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, const XMLNode& node)
237 , _automatable(src->session())
238 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
239 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
240 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
242 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
244 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
249 if (set_state (node)) {
250 throw failed_constructor();
253 assert(_type == DataType::AUDIO);
254 listen_to_my_sources ();
257 AudioRegion::AudioRegion (SourceList& srcs, const XMLNode& node)
258 : Region (srcs, node)
259 , _automatable(srcs[0]->session())
260 , _fade_in (new AutomationList(Evoral::Parameter(FadeInAutomation)))
261 , _fade_out (new AutomationList(Evoral::Parameter(FadeOutAutomation)))
262 , _envelope (new AutomationList(Evoral::Parameter(EnvelopeAutomation)))
266 if (set_state (node)) {
267 throw failed_constructor();
270 assert(_type == DataType::AUDIO);
271 listen_to_my_sources ();
274 AudioRegion::~AudioRegion ()
279 AudioRegion::listen_to_my_sources ()
281 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
282 (*i)->AnalysisChanged.connect (mem_fun (*this, &AudioRegion::invalidate_transients));
287 AudioRegion::listen_to_my_curves ()
289 _envelope->StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
290 _fade_in->StateChanged.connect (mem_fun (*this, &AudioRegion::fade_in_changed));
291 _fade_out->StateChanged.connect (mem_fun (*this, &AudioRegion::fade_out_changed));
295 AudioRegion::set_envelope_active (bool yn)
297 if (envelope_active() != yn) {
300 snprintf (buf, sizeof (buf), "envelope active");
301 _flags = Flag (_flags|EnvelopeActive);
303 snprintf (buf, sizeof (buf), "envelope off");
304 _flags = Flag (_flags & ~EnvelopeActive);
306 send_change (EnvelopeActiveChanged);
311 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
313 if (chan_n >= _sources.size()) {
317 if (audio_source(chan_n)->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
320 if (_scale_amplitude != 1.0) {
321 for (nframes_t n = 0; n < npeaks; ++n) {
322 buf[n].max *= _scale_amplitude;
323 buf[n].min *= _scale_amplitude;
331 AudioRegion::read (Sample* buf, nframes64_t timeline_position, nframes64_t cnt, int channel) const
333 /* raw read, no fades, no gain, nada */
334 return _read_at (_sources, _length, buf, 0, 0, _position + timeline_position, cnt, channel, 0, 0, ReadOps (0));
338 AudioRegion::read_with_ops (Sample* buf, nframes64_t file_position, nframes64_t cnt, int channel, ReadOps rops) const
340 return _read_at (_sources, _length, buf, 0, 0, file_position, cnt, channel, 0, 0, rops);
344 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t file_position,
346 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
348 /* regular diskstream/butler read complete with fades etc */
349 return _read_at (_sources, _length, buf, mixdown_buffer, gain_buffer, file_position, cnt, chan_n, read_frames, skip_frames, ReadOps (~0));
353 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
354 nframes_t cnt, uint32_t chan_n) const
356 return _read_at (_master_sources, _master_sources.front()->length(), buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0);
360 AudioRegion::_read_at (const SourceList& srcs, nframes_t limit,
361 Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
362 nframes_t position, nframes_t cnt,
364 nframes_t read_frames,
365 nframes_t skip_frames,
368 nframes_t internal_offset;
369 nframes_t buf_offset;
371 bool raw = (rops == ReadOpsNone);
373 if (muted() && !raw) {
374 return 0; /* read nothing */
377 /* precondition: caller has verified that we cover the desired section */
379 if (position < _position) {
381 buf_offset = _position - position;
384 internal_offset = position - _position;
388 if (internal_offset >= limit) {
389 return 0; /* read nothing */
392 if ((to_read = min (cnt, limit - internal_offset)) == 0) {
393 return 0; /* read nothing */
396 if (opaque() || raw) {
397 /* overwrite whatever is there */
398 mixdown_buffer = buf + buf_offset;
400 mixdown_buffer += buf_offset;
403 if (rops & ReadOpsCount) {
404 _read_data_count = 0;
407 if (chan_n < n_channels()) {
409 boost::shared_ptr<AudioSource> src = audio_source(chan_n);
410 if (src->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
411 return 0; /* "read nothing" */
414 if (rops & ReadOpsCount) {
415 _read_data_count += src->read_data_count();
420 /* track is N-channel, this region has less channels; silence the ones
424 memset (mixdown_buffer, 0, sizeof (Sample) * cnt);
427 if (rops & ReadOpsFades) {
431 if ((_flags & FadeIn) && Config->get_use_region_fades()) {
433 nframes_t fade_in_length = (nframes_t) _fade_in->back()->when;
435 /* see if this read is within the fade in */
437 if (internal_offset < fade_in_length) {
441 fi_limit = min (to_read, fade_in_length - internal_offset);
444 _fade_in->curve().get_vector (internal_offset, internal_offset+fi_limit, gain_buffer, fi_limit);
446 for (nframes_t n = 0; n < fi_limit; ++n) {
447 mixdown_buffer[n] *= gain_buffer[n];
454 if ((_flags & FadeOut) && Config->get_use_region_fades()) {
456 /* see if some part of this read is within the fade out */
458 /* ................. >| REGION
464 limit - fade_out_length
467 ^internal_offset + to_read
469 we need the intersection of [internal_offset,internal_offset+to_read] with
470 [limit - fade_out_length, limit]
475 nframes_t fade_out_length = (nframes_t) _fade_out->back()->when;
476 nframes_t fade_interval_start = max(internal_offset, limit-fade_out_length);
477 nframes_t fade_interval_end = min(internal_offset + to_read, limit);
479 if (fade_interval_end > fade_interval_start) {
480 /* (part of the) the fade out is in this buffer */
482 nframes_t fo_limit = fade_interval_end - fade_interval_start;
483 nframes_t curve_offset = fade_interval_start - (limit-fade_out_length);
484 nframes_t fade_offset = fade_interval_start - internal_offset;
486 _fade_out->curve().get_vector (curve_offset, curve_offset+fo_limit, gain_buffer, fo_limit);
488 for (nframes_t n = 0, m = fade_offset; n < fo_limit; ++n, ++m) {
489 mixdown_buffer[m] *= gain_buffer[n];
496 /* Regular gain curves and scaling */
498 if ((rops & ReadOpsOwnAutomation) && envelope_active()) {
499 _envelope->curve().get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
501 if ((rops & ReadOpsOwnScaling) && _scale_amplitude != 1.0f) {
502 for (nframes_t n = 0; n < to_read; ++n) {
503 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
506 for (nframes_t n = 0; n < to_read; ++n) {
507 mixdown_buffer[n] *= gain_buffer[n];
510 } else if ((rops & ReadOpsOwnScaling) && _scale_amplitude != 1.0f) {
512 // XXX this should be using what in 2.0 would have been:
513 // Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
515 for (nframes_t n = 0; n < to_read; ++n) {
516 mixdown_buffer[n] *= _scale_amplitude;
522 /* gack. the things we do for users.
527 for (nframes_t n = 0; n < to_read; ++n) {
528 buf[n] += mixdown_buffer[n];
536 AudioRegion::state (bool full)
538 XMLNode& node (Region::state (full));
542 LocaleGuard lg (X_("POSIX"));
544 node.add_property ("flags", enum_2_string (_flags));
546 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
547 node.add_property ("scale-gain", buf);
549 // XXX these should move into Region
551 for (uint32_t n=0; n < _sources.size(); ++n) {
552 snprintf (buf2, sizeof(buf2), "source-%d", n);
553 _sources[n]->id().print (buf, sizeof (buf));
554 node.add_property (buf2, buf);
557 for (uint32_t n=0; n < _master_sources.size(); ++n) {
558 snprintf (buf2, sizeof(buf2), "master-source-%d", n);
559 _master_sources[n]->id().print (buf, sizeof (buf));
560 node.add_property (buf2, buf);
563 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
564 node.add_property ("channels", buf);
568 child = node.add_child (X_("FadeIn"));
570 if ((_flags & DefaultFadeIn)) {
571 child->add_property (X_("default"), X_("yes"));
573 child->add_child_nocopy (_fade_in->get_state ());
576 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
578 child = node.add_child (X_("FadeOut"));
580 if ((_flags & DefaultFadeOut)) {
581 child->add_property (X_("default"), X_("yes"));
583 child->add_child_nocopy (_fade_out->get_state ());
586 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
589 child = node.add_child ("Envelope");
592 bool default_env = false;
594 // If there are only two points, the points are in the start of the region and the end of the region
595 // so, if they are both at 1.0f, that means the default region.
597 if (_envelope->size() == 2 &&
598 _envelope->front()->value == 1.0f &&
599 _envelope->back()->value==1.0f) {
600 if (_envelope->front()->when == 0 && _envelope->back()->when == _length) {
606 child->add_property ("default", "yes");
608 child->add_child_nocopy (_envelope->get_state ());
612 child->add_property ("default", "yes");
615 if (full && _extra_xml) {
616 node.add_child_copy (*_extra_xml);
623 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
625 const XMLNodeList& nlist = node.children();
626 const XMLProperty *prop;
627 LocaleGuard lg (X_("POSIX"));
629 Region::set_live_state (node, what_changed, false);
631 uint32_t old_flags = _flags;
633 if ((prop = node.property ("flags")) != 0) {
634 _flags = Flag (string_2_enum (prop->value(), _flags));
636 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
638 _flags = Flag (_flags & ~Region::LeftOfSplit);
639 _flags = Flag (_flags & ~Region::RightOfSplit);
642 if ((old_flags ^ _flags) & Muted) {
643 what_changed = Change (what_changed|MuteChanged);
645 if ((old_flags ^ _flags) & Opaque) {
646 what_changed = Change (what_changed|OpacityChanged);
648 if ((old_flags ^ _flags) & Locked) {
649 what_changed = Change (what_changed|LockChanged);
652 if ((prop = node.property ("scale-gain")) != 0) {
653 _scale_amplitude = atof (prop->value().c_str());
654 what_changed = Change (what_changed|ScaleAmplitudeChanged);
656 _scale_amplitude = 1.0;
659 /* Now find envelope description and other misc child items */
661 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
668 if (child->name() == "Envelope") {
672 if ((prop = child->property ("default")) != 0 || _envelope->set_state (*child)) {
673 set_default_envelope ();
676 _envelope->set_max_xval (_length);
677 _envelope->truncate_end (_length);
679 } else if (child->name() == "FadeIn") {
683 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
684 set_default_fade_in ();
686 XMLNode* grandchild = child->child ("AutomationList");
688 _fade_in->set_state (*grandchild);
692 if ((prop = child->property ("active")) != 0) {
693 if (prop->value() == "yes") {
694 set_fade_in_active (true);
696 set_fade_in_active (true);
700 } else if (child->name() == "FadeOut") {
704 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
705 set_default_fade_out ();
707 XMLNode* grandchild = child->child ("AutomationList");
709 _fade_out->set_state (*grandchild);
713 if ((prop = child->property ("active")) != 0) {
714 if (prop->value() == "yes") {
715 set_fade_out_active (true);
717 set_fade_out_active (false);
725 send_change (what_changed);
732 AudioRegion::set_state (const XMLNode& node)
734 /* Region::set_state() calls the virtual set_live_state(),
735 which will get us back to AudioRegion::set_live_state()
736 to handle the relevant stuff.
739 return Region::set_state (node);
743 AudioRegion::set_fade_in_shape (FadeShape shape)
745 set_fade_in (shape, (nframes_t) _fade_in->back()->when);
749 AudioRegion::set_fade_out_shape (FadeShape shape)
751 set_fade_out (shape, (nframes_t) _fade_out->back()->when);
755 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
762 _fade_in->fast_simple_add (0.0, 0.0);
763 _fade_in->fast_simple_add (len, 1.0);
767 _fade_in->fast_simple_add (0, 0);
768 _fade_in->fast_simple_add (len * 0.389401, 0.0333333);
769 _fade_in->fast_simple_add (len * 0.629032, 0.0861111);
770 _fade_in->fast_simple_add (len * 0.829493, 0.233333);
771 _fade_in->fast_simple_add (len * 0.9447, 0.483333);
772 _fade_in->fast_simple_add (len * 0.976959, 0.697222);
773 _fade_in->fast_simple_add (len, 1);
777 _fade_in->fast_simple_add (0, 0);
778 _fade_in->fast_simple_add (len * 0.0207373, 0.197222);
779 _fade_in->fast_simple_add (len * 0.0645161, 0.525);
780 _fade_in->fast_simple_add (len * 0.152074, 0.802778);
781 _fade_in->fast_simple_add (len * 0.276498, 0.919444);
782 _fade_in->fast_simple_add (len * 0.481567, 0.980556);
783 _fade_in->fast_simple_add (len * 0.767281, 1);
784 _fade_in->fast_simple_add (len, 1);
788 _fade_in->fast_simple_add (0, 0);
789 _fade_in->fast_simple_add (len * 0.0737327, 0.308333);
790 _fade_in->fast_simple_add (len * 0.246544, 0.658333);
791 _fade_in->fast_simple_add (len * 0.470046, 0.886111);
792 _fade_in->fast_simple_add (len * 0.652074, 0.972222);
793 _fade_in->fast_simple_add (len * 0.771889, 0.988889);
794 _fade_in->fast_simple_add (len, 1);
798 _fade_in->fast_simple_add (0, 0);
799 _fade_in->fast_simple_add (len * 0.304147, 0.0694444);
800 _fade_in->fast_simple_add (len * 0.529954, 0.152778);
801 _fade_in->fast_simple_add (len * 0.725806, 0.333333);
802 _fade_in->fast_simple_add (len * 0.847926, 0.558333);
803 _fade_in->fast_simple_add (len * 0.919355, 0.730556);
804 _fade_in->fast_simple_add (len, 1);
809 _fade_in_shape = shape;
811 send_change (FadeInChanged);
815 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
817 _fade_out->freeze ();
822 _fade_out->fast_simple_add (len * 0, 1);
823 _fade_out->fast_simple_add (len * 0.023041, 0.697222);
824 _fade_out->fast_simple_add (len * 0.0553, 0.483333);
825 _fade_out->fast_simple_add (len * 0.170507, 0.233333);
826 _fade_out->fast_simple_add (len * 0.370968, 0.0861111);
827 _fade_out->fast_simple_add (len * 0.610599, 0.0333333);
828 _fade_out->fast_simple_add (len * 1, 0);
832 _fade_out->fast_simple_add (len * 0, 1);
833 _fade_out->fast_simple_add (len * 0.228111, 0.988889);
834 _fade_out->fast_simple_add (len * 0.347926, 0.972222);
835 _fade_out->fast_simple_add (len * 0.529954, 0.886111);
836 _fade_out->fast_simple_add (len * 0.753456, 0.658333);
837 _fade_out->fast_simple_add (len * 0.9262673, 0.308333);
838 _fade_out->fast_simple_add (len * 1, 0);
842 _fade_out->fast_simple_add (len * 0, 1);
843 _fade_out->fast_simple_add (len * 0.305556, 1);
844 _fade_out->fast_simple_add (len * 0.548611, 0.991736);
845 _fade_out->fast_simple_add (len * 0.759259, 0.931129);
846 _fade_out->fast_simple_add (len * 0.918981, 0.68595);
847 _fade_out->fast_simple_add (len * 0.976852, 0.22865);
848 _fade_out->fast_simple_add (len * 1, 0);
852 _fade_out->fast_simple_add (len * 0, 1);
853 _fade_out->fast_simple_add (len * 0.080645, 0.730556);
854 _fade_out->fast_simple_add (len * 0.277778, 0.289256);
855 _fade_out->fast_simple_add (len * 0.470046, 0.152778);
856 _fade_out->fast_simple_add (len * 0.695853, 0.0694444);
857 _fade_out->fast_simple_add (len * 1, 0);
861 _fade_out->fast_simple_add (len * 0, 1);
862 _fade_out->fast_simple_add (len * 1, 0);
867 _fade_out_shape = shape;
869 send_change (FadeOutChanged);
873 AudioRegion::set_fade_in_length (nframes_t len)
879 bool changed = _fade_in->extend_to (len);
882 _flags = Flag (_flags & ~DefaultFadeIn);
883 send_change (FadeInChanged);
888 AudioRegion::set_fade_out_length (nframes_t len)
894 bool changed = _fade_out->extend_to (len);
897 _flags = Flag (_flags & ~DefaultFadeOut);
898 send_change (FadeOutChanged);
903 AudioRegion::set_fade_in_active (bool yn)
905 if (yn == (_flags & FadeIn)) {
909 _flags = Flag (_flags|FadeIn);
911 _flags = Flag (_flags & ~FadeIn);
914 send_change (FadeInActiveChanged);
918 AudioRegion::set_fade_out_active (bool yn)
920 if (yn == (_flags & FadeOut)) {
924 _flags = Flag (_flags | FadeOut);
926 _flags = Flag (_flags & ~FadeOut);
929 send_change (FadeOutActiveChanged);
933 AudioRegion::fade_in_is_default () const
935 return _fade_in_shape == Linear && _fade_in->back()->when == 64;
939 AudioRegion::fade_out_is_default () const
941 return _fade_out_shape == Linear && _fade_out->back()->when == 64;
945 AudioRegion::set_default_fade_in ()
947 set_fade_in (Linear, 64);
951 AudioRegion::set_default_fade_out ()
953 set_fade_out (Linear, 64);
957 AudioRegion::set_default_fades ()
959 _fade_in_disabled = 0;
960 _fade_out_disabled = 0;
961 set_default_fade_in ();
962 set_default_fade_out ();
966 AudioRegion::set_default_envelope ()
968 _envelope->freeze ();
970 _envelope->fast_simple_add (0, 1.0f);
971 _envelope->fast_simple_add (_length, 1.0f);
976 AudioRegion::recompute_at_end ()
978 /* our length has changed. recompute a new final point by interpolating
979 based on the the existing curve.
982 _envelope->freeze ();
983 _envelope->truncate_end (_length);
984 _envelope->set_max_xval (_length);
987 if (_fade_in->back()->when > _length) {
988 _fade_in->extend_to (_length);
989 send_change (FadeInChanged);
992 if (_fade_out->back()->when > _length) {
993 _fade_out->extend_to (_length);
994 send_change (FadeOutChanged);
999 AudioRegion::recompute_at_start ()
1001 /* as above, but the shift was from the front */
1003 _envelope->truncate_start (_length);
1005 if (_fade_in->back()->when > _length) {
1006 _fade_in->extend_to (_length);
1007 send_change (FadeInChanged);
1010 if (_fade_out->back()->when > _length) {
1011 _fade_out->extend_to (_length);
1012 send_change (FadeOutChanged);
1017 AudioRegion::separate_by_channel (Session& session, vector<boost::shared_ptr<AudioRegion> >& v) const
1023 if (_sources.size() < 2) {
1029 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
1032 srcs.push_back (*i);
1036 if (_sources.size() == 2) {
1044 new_name += ('0' + n + 1);
1047 /* create a copy with just one source. prevent if from being thought of as "whole file" even if
1048 it covers the entire source file(s).
1051 Flag f = Flag (_flags & ~WholeFile);
1053 boost::shared_ptr<Region> r = RegionFactory::create (srcs, _start, _length, new_name, _layer, f);
1054 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
1065 AudioRegion::read_raw_internal (Sample* buf, nframes_t pos, nframes_t cnt) const
1067 return audio_source()->read (buf, pos, cnt);
1071 AudioRegion::exportme (Session& session, ARDOUR::ExportSpecification& spec)
1074 // const nframes_t blocksize = 4096;
1075 // nframes_t to_read;
1078 // spec.channels = _sources.size();
1080 // if (spec.prepare (blocksize, session.frame_rate())) {
1085 // spec.total_frames = _length;
1087 // while (spec.pos < _length && !spec.stop) {
1090 // /* step 1: interleave */
1092 // to_read = min (_length - spec.pos, blocksize);
1094 // if (spec.channels == 1) {
1096 // if (read_raw_internal (spec.dataF, _start + spec.pos, to_read) != to_read) {
1102 // Sample buf[blocksize];
1104 // for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1106 // if (audio_source(chan)->read (buf, _start + spec.pos, to_read) != to_read) {
1110 // for (nframes_t x = 0; x < to_read; ++x) {
1111 // spec.dataF[chan+(x*spec.channels)] = buf[x];
1116 // if (spec.process (to_read)) {
1120 // spec.pos += to_read;
1121 // spec.progress = (double) spec.pos /_length;
1128 // spec.running = false;
1129 // spec.status = status;
1137 AudioRegion::set_scale_amplitude (gain_t g)
1139 boost::shared_ptr<Playlist> pl (playlist());
1141 _scale_amplitude = g;
1143 /* tell the diskstream we're in */
1149 /* tell everybody else */
1151 send_change (ScaleAmplitudeChanged);
1155 AudioRegion::normalize_to (float target_dB)
1157 const nframes_t blocksize = 64 * 1024;
1158 Sample buf[blocksize];
1163 gain_t target = dB_to_coefficient (target_dB);
1165 if (target == 1.0f) {
1166 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1167 that we may have clipped.
1169 target -= FLT_EPSILON;
1173 fend = _start + _length;
1175 /* first pass: find max amplitude */
1177 while (fpos < fend) {
1181 to_read = min (fend - fpos, blocksize);
1183 for (n = 0; n < n_channels(); ++n) {
1187 if (read_raw_internal (buf, fpos, to_read) != to_read) {
1191 maxamp = compute_peak (buf, to_read, maxamp);
1197 if (maxamp == 0.0f) {
1198 /* don't even try */
1202 if (maxamp == target) {
1203 /* we can't do anything useful */
1207 /* compute scale factor */
1209 _scale_amplitude = target/maxamp;
1211 /* tell the diskstream we're in */
1213 boost::shared_ptr<Playlist> pl (playlist());
1219 /* tell everybody else */
1221 send_change (ScaleAmplitudeChanged);
1225 AudioRegion::fade_in_changed ()
1227 send_change (FadeInChanged);
1231 AudioRegion::fade_out_changed ()
1233 send_change (FadeOutChanged);
1237 AudioRegion::envelope_changed ()
1239 send_change (EnvelopeChanged);
1243 AudioRegion::suspend_fade_in ()
1245 if (++_fade_in_disabled == 1) {
1246 if (fade_in_is_default()) {
1247 set_fade_in_active (false);
1253 AudioRegion::resume_fade_in ()
1255 if (--_fade_in_disabled == 0 && _fade_in_disabled) {
1256 set_fade_in_active (true);
1261 AudioRegion::suspend_fade_out ()
1263 if (++_fade_out_disabled == 1) {
1264 if (fade_out_is_default()) {
1265 set_fade_out_active (false);
1271 AudioRegion::resume_fade_out ()
1273 if (--_fade_out_disabled == 0 &&_fade_out_disabled) {
1274 set_fade_out_active (true);
1279 AudioRegion::speed_mismatch (float sr) const
1281 if (_sources.empty()) {
1282 /* impossible, but ... */
1286 float fsr = audio_source()->sample_rate();
1292 AudioRegion::source_offset_changed ()
1294 /* XXX this fixes a crash that should not occur. It does occur
1295 becauses regions are not being deleted when a session
1296 is unloaded. That bug must be fixed.
1299 if (_sources.empty()) {
1303 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(_sources.front());
1305 if (afs && afs->destructive()) {
1306 // set_start (source()->natural_position(), this);
1307 set_position (source()->natural_position(), this);
1311 boost::shared_ptr<AudioSource>
1312 AudioRegion::audio_source (uint32_t n) const
1314 // Guaranteed to succeed (use a static cast for speed?)
1315 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1319 AudioRegion::get_transients (AnalysisFeatureList& results, bool force_new)
1321 boost::shared_ptr<Playlist> pl = playlist();
1327 if (_valid_transients && !force_new) {
1328 results = _transients;
1332 SourceList::iterator s;
1334 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1335 if (!(*s)->has_been_analysed()) {
1336 cerr << "For " << name() << " source " << (*s)->name() << " has not been analyzed\n";
1341 if (s == _sources.end()) {
1342 /* all sources are analyzed, merge data from each one */
1344 for (s = _sources.begin() ; s != _sources.end(); ++s) {
1346 /* find the set of transients within the bounds of this region */
1348 AnalysisFeatureList::iterator low = lower_bound ((*s)->transients.begin(),
1349 (*s)->transients.end(),
1352 AnalysisFeatureList::iterator high = upper_bound ((*s)->transients.begin(),
1353 (*s)->transients.end(),
1358 results.insert (results.end(), low, high);
1361 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1363 /* translate all transients to current position */
1365 for (AnalysisFeatureList::iterator x = results.begin(); x != results.end(); ++x) {
1370 _transients = results;
1371 _valid_transients = true;
1376 /* no existing/complete transient info */
1378 if (!Config->get_auto_analyse_audio()) {
1379 pl->session().Dialog (_("\
1380 You have requested an operation that requires audio analysis.\n\n\
1381 You currently have \"auto-analyse-audio\" disabled, which means\n\
1382 that transient data must be generated every time it is required.\n\n\
1383 If you are doing work that will require transient data on a\n\
1384 regular basis, you should probably enable \"auto-analyse-audio\"\n\
1385 then quit ardour and restart."));
1388 TransientDetector t (pl->session().frame_rate());
1389 bool existing_results = !results.empty();
1391 _transients.clear ();
1392 _valid_transients = false;
1394 for (uint32_t i = 0; i < n_channels(); ++i) {
1396 AnalysisFeatureList these_results;
1400 if (t.run ("", this, i, these_results)) {
1404 /* translate all transients to give absolute position */
1406 for (AnalysisFeatureList::iterator i = these_results.begin(); i != these_results.end(); ++i) {
1412 _transients.insert (_transients.end(), these_results.begin(), these_results.end());
1415 if (!results.empty()) {
1416 if (existing_results) {
1418 /* merge our transients into the existing ones, then clean up
1422 results.insert (results.end(), _transients.begin(), _transients.end());
1423 TransientDetector::cleanup_transients (results, pl->session().frame_rate(), 3.0);
1426 /* make sure ours are clean too */
1428 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1432 TransientDetector::cleanup_transients (_transients, pl->session().frame_rate(), 3.0);
1433 results = _transients;
1436 _valid_transients = true;
1443 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)
1445 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1448 uint32_t region_length_from_c (void *arg)
1451 return ((AudioRegion *) arg)->length();
1454 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1456 return ( (AudioRegion *) arg)->audio_source()->available_peaks (zoom_factor) ;