2 Copyright (C) 2000-2001 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/audiofilter.h>
44 #include <ardour/audiofilesource.h>
45 #include <ardour/region_factory.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();
65 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length)
66 : Region (start, length, PBD::basename_nosuffix(src->name()), 0, Region::Flag(Region::DefaultFlags|Region::External)),
67 _fade_in (0.0, 2.0, 1.0, false),
68 _fade_out (0.0, 2.0, 1.0, false),
69 _envelope (0.0, 2.0, 1.0, false)
71 /* basic AudioRegion constructor */
73 sources.push_back (src);
74 master_sources.push_back (src);
75 src->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
77 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
79 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
82 _scale_amplitude = 1.0;
85 set_default_envelope ();
87 listen_to_my_curves ();
90 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
91 : Region (start, length, name, layer, flags),
92 _fade_in (0.0, 2.0, 1.0, false),
93 _fade_out (0.0, 2.0, 1.0, false),
94 _envelope (0.0, 2.0, 1.0, false)
96 /* basic AudioRegion constructor */
98 sources.push_back (src);
99 master_sources.push_back (src);
100 src->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
102 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
104 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
107 _scale_amplitude = 1.0;
109 set_default_fades ();
110 set_default_envelope ();
112 listen_to_my_curves ();
115 AudioRegion::AudioRegion (const SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
116 : Region (start, length, name, layer, flags),
117 _fade_in (0.0, 2.0, 1.0, false),
118 _fade_out (0.0, 2.0, 1.0, false),
119 _envelope (0.0, 2.0, 1.0, false)
121 /* basic AudioRegion constructor */
123 for (SourceList::const_iterator i=srcs.begin(); i != srcs.end(); ++i) {
124 sources.push_back (*i);
125 master_sources.push_back (*i);
126 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
128 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> ((*i));
130 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
134 _scale_amplitude = 1.0;
136 set_default_fades ();
137 set_default_envelope ();
139 listen_to_my_curves ();
143 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
144 : Region (other, offset, length, name, layer, flags),
145 _fade_in (other->_fade_in),
146 _fade_out (other->_fade_out),
147 _envelope (other->_envelope, (double) offset, (double) offset + length)
149 /* create a new AudioRegion, that is part of an existing one */
151 set<boost::shared_ptr<AudioSource> > unique_srcs;
153 for (SourceList::const_iterator i= other->sources.begin(); i != other->sources.end(); ++i) {
154 sources.push_back (*i);
155 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
157 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
159 result = unique_srcs.insert (*i);
162 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
164 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
169 for (SourceList::const_iterator i = other->master_sources.begin(); i != other->master_sources.end(); ++i) {
170 if (unique_srcs.find (*i) == unique_srcs.end()) {
171 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
173 master_sources.push_back (*i);
177 /* return to default fades if the existing ones are too long */
178 _fade_in_disabled = 0;
179 _fade_out_disabled = 0;
182 if (_flags & LeftOfSplit) {
183 if (_fade_in.back()->when >= _length) {
184 set_default_fade_in ();
186 _fade_in_disabled = other->_fade_in_disabled;
188 set_default_fade_out ();
189 _flags = Flag (_flags & ~Region::LeftOfSplit);
192 if (_flags & RightOfSplit) {
193 if (_fade_out.back()->when >= _length) {
194 set_default_fade_out ();
196 _fade_out_disabled = other->_fade_out_disabled;
198 set_default_fade_in ();
199 _flags = Flag (_flags & ~Region::RightOfSplit);
202 _scale_amplitude = other->_scale_amplitude;
204 listen_to_my_curves ();
207 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
209 _fade_in (other->_fade_in),
210 _fade_out (other->_fade_out),
211 _envelope (other->_envelope)
213 /* Pure copy constructor */
215 set<boost::shared_ptr<AudioSource> > unique_srcs;
217 for (SourceList::const_iterator i = other->sources.begin(); i != other->sources.end(); ++i) {
218 sources.push_back (*i);
219 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
220 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
222 result = unique_srcs.insert (*i);
225 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
227 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
232 for (SourceList::const_iterator i = other->master_sources.begin(); i != other->master_sources.end(); ++i) {
233 master_sources.push_back (*i);
234 if (unique_srcs.find (*i) == unique_srcs.end()) {
235 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
239 _scale_amplitude = other->_scale_amplitude;
240 _envelope = other->_envelope;
242 _fade_in_disabled = 0;
243 _fade_out_disabled = 0;
245 listen_to_my_curves ();
248 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, const XMLNode& node)
250 _fade_in (0.0, 2.0, 1.0, false),
251 _fade_out (0.0, 2.0, 1.0, false),
252 _envelope (0.0, 2.0, 1.0, false)
254 sources.push_back (src);
255 master_sources.push_back (src);
256 src->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
258 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
260 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
263 set_default_fades ();
265 if (set_state (node)) {
266 throw failed_constructor();
269 listen_to_my_curves ();
272 AudioRegion::AudioRegion (SourceList& srcs, const XMLNode& node)
274 _fade_in (0.0, 2.0, 1.0, false),
275 _fade_out (0.0, 2.0, 1.0, false),
276 _envelope (0.0, 2.0, 1.0, false)
278 set<boost::shared_ptr<AudioSource> > unique_srcs;
280 for (SourceList::iterator i=srcs.begin(); i != srcs.end(); ++i) {
281 sources.push_back (*i);
282 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
283 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
285 result = unique_srcs.insert (*i);
288 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
290 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
295 for (SourceList::iterator i = srcs.begin(); i != srcs.end(); ++i) {
296 master_sources.push_back (*i);
297 if (unique_srcs.find (*i) == unique_srcs.end()) {
298 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
302 set_default_fades ();
303 _scale_amplitude = 1.0;
305 if (set_state (node)) {
306 throw failed_constructor();
309 listen_to_my_curves ();
312 AudioRegion::~AudioRegion ()
314 boost::shared_ptr<Playlist> pl (playlist());
317 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
318 (*i)->remove_playlist (pl);
323 GoingAway (); /* EMIT SIGNAL */
327 AudioRegion::listen_to_my_curves ()
329 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
330 _fade_in.StateChanged.connect (mem_fun (*this, &AudioRegion::fade_in_changed));
331 _fade_out.StateChanged.connect (mem_fun (*this, &AudioRegion::fade_out_changed));
335 AudioRegion::verify_length (nframes_t& len)
337 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
339 if (afs && afs->destructive()) {
343 nframes_t maxlen = 0;
345 for (uint32_t n=0; n < sources.size(); ++n) {
346 maxlen = max (maxlen, sources[n]->length() - _start);
349 len = min (len, maxlen);
355 AudioRegion::verify_start_and_length (nframes_t new_start, nframes_t& new_length)
357 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
359 if (afs && afs->destructive()) {
363 nframes_t maxlen = 0;
365 for (uint32_t n=0; n < sources.size(); ++n) {
366 maxlen = max (maxlen, sources[n]->length() - new_start);
369 new_length = min (new_length, maxlen);
374 AudioRegion::verify_start (nframes_t pos)
376 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
378 if (afs && afs->destructive()) {
382 for (uint32_t n=0; n < sources.size(); ++n) {
383 if (pos > sources[n]->length() - _length) {
391 AudioRegion::verify_start_mutable (nframes_t& new_start)
393 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
395 if (afs && afs->destructive()) {
399 for (uint32_t n=0; n < sources.size(); ++n) {
400 if (new_start > sources[n]->length() - _length) {
401 new_start = sources[n]->length() - _length;
407 AudioRegion::set_envelope_active (bool yn)
409 if (envelope_active() != yn) {
412 snprintf (buf, sizeof (buf), "envelope active");
413 _flags = Flag (_flags|EnvelopeActive);
415 snprintf (buf, sizeof (buf), "envelope off");
416 _flags = Flag (_flags & ~EnvelopeActive);
418 send_change (EnvelopeActiveChanged);
423 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
425 if (chan_n >= sources.size()) {
429 if (sources[chan_n]->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
432 if (_scale_amplitude != 1.0) {
433 for (nframes_t n = 0; n < npeaks; ++n) {
434 buf[n].max *= _scale_amplitude;
435 buf[n].min *= _scale_amplitude;
443 AudioRegion::read (Sample* buf, nframes64_t position, nframes64_t cnt, int channel) const
445 /* raw read, no fades, no gain, nada */
446 return _read_at (sources, buf, 0, 0, _position + position, cnt, channel, 0, 0, true);
450 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
452 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
454 /* regular diskstream/butler read complete with fades etc */
455 return _read_at (sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, read_frames, skip_frames, false);
459 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
460 nframes_t cnt, uint32_t chan_n) const
462 return _read_at (master_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0);
466 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
467 nframes_t position, nframes_t cnt,
469 nframes_t read_frames,
470 nframes_t skip_frames,
473 nframes_t internal_offset;
474 nframes_t buf_offset;
477 if (muted() && !raw) {
478 return 0; /* read nothing */
481 /* precondition: caller has verified that we cover the desired section */
483 if (position < _position) {
485 buf_offset = _position - position;
488 internal_offset = position - _position;
492 if (internal_offset >= _length) {
493 return 0; /* read nothing */
496 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
497 return 0; /* read nothing */
500 if (opaque() || raw) {
501 /* overwrite whatever is there */
502 mixdown_buffer = buf + buf_offset;
504 mixdown_buffer += buf_offset;
508 _read_data_count = 0;
511 if (chan_n < n_channels()) {
513 if (srcs[chan_n]->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
515 return 0; /* "read nothing" */
519 _read_data_count += srcs[chan_n]->read_data_count();
524 /* track is N-channel, this region has less channels; silence the ones
528 memset (mixdown_buffer, 0, sizeof (Sample) * cnt);
530 /* no fades required */
541 if (_flags & FadeIn) {
543 nframes_t fade_in_length = (nframes_t) _fade_in.back()->when;
545 /* see if this read is within the fade in */
547 if (internal_offset < fade_in_length) {
551 limit = min (to_read, fade_in_length - internal_offset);
553 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
555 for (nframes_t n = 0; n < limit; ++n) {
556 mixdown_buffer[n] *= gain_buffer[n];
563 if (_flags & FadeOut) {
565 /* see if some part of this read is within the fade out */
567 /* ................. >| REGION
573 _length - fade_out_length
576 ^internal_offset + to_read
578 we need the intersection of [internal_offset,internal_offset+to_read] with
579 [_length - fade_out_length, _length]
584 nframes_t fade_out_length = (nframes_t) _fade_out.back()->when;
585 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
586 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
588 if (fade_interval_end > fade_interval_start) {
589 /* (part of the) the fade out is in this buffer */
591 nframes_t limit = fade_interval_end - fade_interval_start;
592 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
593 nframes_t fade_offset = fade_interval_start - internal_offset;
595 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
597 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
598 mixdown_buffer[m] *= gain_buffer[n];
604 /* Regular gain curves */
606 if (envelope_active()) {
607 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
609 if (_scale_amplitude != 1.0f) {
610 for (nframes_t n = 0; n < to_read; ++n) {
611 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
614 for (nframes_t n = 0; n < to_read; ++n) {
615 mixdown_buffer[n] *= gain_buffer[n];
618 } else if (_scale_amplitude != 1.0f) {
619 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
626 /* gack. the things we do for users.
631 for (nframes_t n = 0; n < to_read; ++n) {
632 buf[n] += mixdown_buffer[n];
641 AudioRegion::state (bool full)
643 XMLNode& node (Region::state (full));
647 LocaleGuard lg (X_("POSIX"));
649 node.add_property ("flags", enum_2_string (_flags));
651 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
652 node.add_property ("scale-gain", buf);
654 for (uint32_t n=0; n < sources.size(); ++n) {
655 snprintf (buf2, sizeof(buf2), "source-%d", n);
656 sources[n]->id().print (buf, sizeof (buf));
657 node.add_property (buf2, buf);
660 snprintf (buf, sizeof (buf), "%u", (uint32_t) sources.size());
661 node.add_property ("channels", buf);
665 child = node.add_child (X_("FadeIn"));
667 if ((_flags & DefaultFadeIn)) {
668 child->add_property (X_("default"), X_("yes"));
670 child->add_child_nocopy (_fade_in.get_state ());
673 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
675 child = node.add_child (X_("FadeOut"));
677 if ((_flags & DefaultFadeOut)) {
678 child->add_property (X_("default"), X_("yes"));
680 child->add_child_nocopy (_fade_out.get_state ());
683 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
686 child = node.add_child ("Envelope");
689 bool default_env = false;
691 // If there are only two points, the points are in the start of the region and the end of the region
692 // so, if they are both at 1.0f, that means the default region.
694 if (_envelope.size() == 2 &&
695 _envelope.front()->value == 1.0f &&
696 _envelope.back()->value==1.0f) {
697 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
703 child->add_property ("default", "yes");
705 child->add_child_nocopy (_envelope.get_state ());
709 child->add_property ("default", "yes");
712 for (uint32_t n=0; n < master_sources.size(); ++n) {
713 snprintf (buf2, sizeof(buf2), "master-source-%d", n);
714 master_sources[n]->id().print (buf, sizeof (buf));
715 node.add_property (buf2, buf);
718 if (full && _extra_xml) {
719 node.add_child_copy (*_extra_xml);
726 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
728 const XMLNodeList& nlist = node.children();
729 const XMLProperty *prop;
730 LocaleGuard lg (X_("POSIX"));
732 Region::set_live_state (node, what_changed, false);
734 uint32_t old_flags = _flags;
736 if ((prop = node.property ("flags")) != 0) {
737 _flags = Flag (string_2_enum (prop->value(), _flags));
739 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
741 _flags = Flag (_flags & ~Region::LeftOfSplit);
742 _flags = Flag (_flags & ~Region::RightOfSplit);
745 if ((old_flags ^ _flags) & Muted) {
746 what_changed = Change (what_changed|MuteChanged);
748 if ((old_flags ^ _flags) & Opaque) {
749 what_changed = Change (what_changed|OpacityChanged);
751 if ((old_flags ^ _flags) & Locked) {
752 what_changed = Change (what_changed|LockChanged);
755 if ((prop = node.property ("scale-gain")) != 0) {
756 _scale_amplitude = atof (prop->value().c_str());
757 what_changed = Change (what_changed|ScaleAmplitudeChanged);
759 _scale_amplitude = 1.0;
762 /* Now find envelope description and other misc child items */
764 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
771 if (child->name() == "Envelope") {
775 if ((prop = child->property ("default")) != 0 || _envelope.set_state (*child)) {
776 set_default_envelope ();
779 _envelope.set_max_xval (_length);
780 _envelope.truncate_end (_length);
782 } else if (child->name() == "FadeIn") {
786 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
787 set_default_fade_in ();
789 XMLNode* grandchild = child->child ("AutomationList");
791 _fade_in.set_state (*grandchild);
795 if ((prop = child->property ("active")) != 0) {
796 if (prop->value() == "yes") {
797 set_fade_in_active (true);
799 set_fade_in_active (true);
803 } else if (child->name() == "FadeOut") {
807 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
808 set_default_fade_out ();
810 XMLNode* grandchild = child->child ("AutomationList");
812 _fade_out.set_state (*grandchild);
816 if ((prop = child->property ("active")) != 0) {
817 if (prop->value() == "yes") {
818 set_fade_out_active (true);
820 set_fade_out_active (false);
828 send_change (what_changed);
835 AudioRegion::set_state (const XMLNode& node)
837 /* Region::set_state() calls the virtual set_live_state(),
838 which will get us back to AudioRegion::set_live_state()
839 to handle the relevant stuff.
842 return Region::set_state (node);
846 AudioRegion::set_fade_in_shape (FadeShape shape)
848 set_fade_in (shape, (nframes_t) _fade_in.back()->when);
852 AudioRegion::set_fade_out_shape (FadeShape shape)
854 set_fade_out (shape, (nframes_t) _fade_out.back()->when);
858 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
865 _fade_in.fast_simple_add (0.0, 0.0);
866 _fade_in.fast_simple_add (len, 1.0);
870 _fade_in.fast_simple_add (0, 0);
871 _fade_in.fast_simple_add (len * 0.389401, 0.0333333);
872 _fade_in.fast_simple_add (len * 0.629032, 0.0861111);
873 _fade_in.fast_simple_add (len * 0.829493, 0.233333);
874 _fade_in.fast_simple_add (len * 0.9447, 0.483333);
875 _fade_in.fast_simple_add (len * 0.976959, 0.697222);
876 _fade_in.fast_simple_add (len, 1);
880 _fade_in.fast_simple_add (0, 0);
881 _fade_in.fast_simple_add (len * 0.0207373, 0.197222);
882 _fade_in.fast_simple_add (len * 0.0645161, 0.525);
883 _fade_in.fast_simple_add (len * 0.152074, 0.802778);
884 _fade_in.fast_simple_add (len * 0.276498, 0.919444);
885 _fade_in.fast_simple_add (len * 0.481567, 0.980556);
886 _fade_in.fast_simple_add (len * 0.767281, 1);
887 _fade_in.fast_simple_add (len, 1);
891 _fade_in.fast_simple_add (0, 0);
892 _fade_in.fast_simple_add (len * 0.0737327, 0.308333);
893 _fade_in.fast_simple_add (len * 0.246544, 0.658333);
894 _fade_in.fast_simple_add (len * 0.470046, 0.886111);
895 _fade_in.fast_simple_add (len * 0.652074, 0.972222);
896 _fade_in.fast_simple_add (len * 0.771889, 0.988889);
897 _fade_in.fast_simple_add (len, 1);
901 _fade_in.fast_simple_add (0, 0);
902 _fade_in.fast_simple_add (len * 0.304147, 0.0694444);
903 _fade_in.fast_simple_add (len * 0.529954, 0.152778);
904 _fade_in.fast_simple_add (len * 0.725806, 0.333333);
905 _fade_in.fast_simple_add (len * 0.847926, 0.558333);
906 _fade_in.fast_simple_add (len * 0.919355, 0.730556);
907 _fade_in.fast_simple_add (len, 1);
912 _fade_in_shape = shape;
914 send_change (FadeInChanged);
918 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
925 _fade_out.fast_simple_add (len * 0, 1);
926 _fade_out.fast_simple_add (len * 0.023041, 0.697222);
927 _fade_out.fast_simple_add (len * 0.0553, 0.483333);
928 _fade_out.fast_simple_add (len * 0.170507, 0.233333);
929 _fade_out.fast_simple_add (len * 0.370968, 0.0861111);
930 _fade_out.fast_simple_add (len * 0.610599, 0.0333333);
931 _fade_out.fast_simple_add (len * 1, 0);
935 _fade_out.fast_simple_add (len * 0, 1);
936 _fade_out.fast_simple_add (len * 0.228111, 0.988889);
937 _fade_out.fast_simple_add (len * 0.347926, 0.972222);
938 _fade_out.fast_simple_add (len * 0.529954, 0.886111);
939 _fade_out.fast_simple_add (len * 0.753456, 0.658333);
940 _fade_out.fast_simple_add (len * 0.9262673, 0.308333);
941 _fade_out.fast_simple_add (len * 1, 0);
945 _fade_out.fast_simple_add (len * 0, 1);
946 _fade_out.fast_simple_add (len * 0.305556, 1);
947 _fade_out.fast_simple_add (len * 0.548611, 0.991736);
948 _fade_out.fast_simple_add (len * 0.759259, 0.931129);
949 _fade_out.fast_simple_add (len * 0.918981, 0.68595);
950 _fade_out.fast_simple_add (len * 0.976852, 0.22865);
951 _fade_out.fast_simple_add (len * 1, 0);
955 _fade_out.fast_simple_add (len * 0, 1);
956 _fade_out.fast_simple_add (len * 0.080645, 0.730556);
957 _fade_out.fast_simple_add (len * 0.277778, 0.289256);
958 _fade_out.fast_simple_add (len * 0.470046, 0.152778);
959 _fade_out.fast_simple_add (len * 0.695853, 0.0694444);
960 _fade_out.fast_simple_add (len * 1, 0);
964 _fade_out.fast_simple_add (len * 0, 1);
965 _fade_out.fast_simple_add (len * 1, 0);
970 _fade_out_shape = shape;
972 send_change (FadeOutChanged);
976 AudioRegion::set_fade_in_length (nframes_t len)
982 bool changed = _fade_in.extend_to (len);
985 _flags = Flag (_flags & ~DefaultFadeIn);
986 send_change (FadeInChanged);
991 AudioRegion::set_fade_out_length (nframes_t len)
997 bool changed = _fade_out.extend_to (len);
1000 _flags = Flag (_flags & ~DefaultFadeOut);
1001 send_change (FadeOutChanged);
1006 AudioRegion::set_fade_in_active (bool yn)
1008 if (yn == (_flags & FadeIn)) {
1012 _flags = Flag (_flags|FadeIn);
1014 _flags = Flag (_flags & ~FadeIn);
1017 send_change (FadeInActiveChanged);
1021 AudioRegion::set_fade_out_active (bool yn)
1023 if (yn == (_flags & FadeOut)) {
1027 _flags = Flag (_flags | FadeOut);
1029 _flags = Flag (_flags & ~FadeOut);
1032 send_change (FadeOutActiveChanged);
1036 AudioRegion::fade_in_is_default () const
1038 return _fade_in_shape == Linear && _fade_in.back()->when == 64;
1042 AudioRegion::fade_out_is_default () const
1044 return _fade_out_shape == Linear && _fade_out.back()->when == 64;
1048 AudioRegion::set_default_fade_in ()
1050 set_fade_in (Linear, 64);
1054 AudioRegion::set_default_fade_out ()
1056 set_fade_out (Linear, 64);
1060 AudioRegion::set_default_fades ()
1062 _fade_in_disabled = 0;
1063 _fade_out_disabled = 0;
1064 set_default_fade_in ();
1065 set_default_fade_out ();
1069 AudioRegion::set_default_envelope ()
1071 _envelope.freeze ();
1073 _envelope.fast_simple_add (0, 1.0f);
1074 _envelope.fast_simple_add (_length, 1.0f);
1079 AudioRegion::recompute_at_end ()
1081 /* our length has changed. recompute a new final point by interpolating
1082 based on the the existing curve.
1085 _envelope.freeze ();
1086 _envelope.truncate_end (_length);
1087 _envelope.set_max_xval (_length);
1090 if (_fade_in.back()->when > _length) {
1091 _fade_in.extend_to (_length);
1092 send_change (FadeInChanged);
1095 if (_fade_out.back()->when > _length) {
1096 _fade_out.extend_to (_length);
1097 send_change (FadeOutChanged);
1102 AudioRegion::recompute_at_start ()
1104 /* as above, but the shift was from the front */
1106 _envelope.truncate_start (_length);
1108 if (_fade_in.back()->when > _length) {
1109 _fade_in.extend_to (_length);
1110 send_change (FadeInChanged);
1113 if (_fade_out.back()->when > _length) {
1114 _fade_out.extend_to (_length);
1115 send_change (FadeOutChanged);
1120 AudioRegion::separate_by_channel (Session& session, vector<boost::shared_ptr<AudioRegion> >& v) const
1126 if (sources.size() < 2) {
1132 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1135 srcs.push_back (*i);
1139 if (sources.size() == 2) {
1147 new_name += ('0' + n + 1);
1150 /* create a copy with just one source. prevent if from being thought of as "whole file" even if
1151 it covers the entire source file(s).
1154 Flag f = Flag (_flags & ~WholeFile);
1156 boost::shared_ptr<Region> r = RegionFactory::create (srcs, _start, _length, new_name, _layer, f);
1157 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
1168 AudioRegion::source_deleted ()
1175 AudioRegion::master_source_names ()
1177 SourceList::iterator i;
1179 vector<string> names;
1180 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1181 names.push_back((*i)->name());
1188 AudioRegion::set_master_sources (SourceList& srcs)
1190 master_sources = srcs;
1194 AudioRegion::source_equivalent (boost::shared_ptr<const Region> o) const
1196 boost::shared_ptr<const AudioRegion> other = boost::dynamic_pointer_cast<const AudioRegion>(o);
1201 SourceList::const_iterator i;
1202 SourceList::const_iterator io;
1204 for (i = sources.begin(), io = other->sources.begin(); i != sources.end() && io != other->sources.end(); ++i, ++io) {
1205 if ((*i)->id() != (*io)->id()) {
1210 for (i = master_sources.begin(), io = other->master_sources.begin(); i != master_sources.end() && io != other->master_sources.end(); ++i, ++io) {
1211 if ((*i)->id() != (*io)->id()) {
1220 AudioRegion::apply (AudioFilter& filter)
1222 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
1223 return filter.run (ar);
1227 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
1229 const nframes_t blocksize = 4096;
1233 spec.channels = sources.size();
1235 if (spec.prepare (blocksize, session.frame_rate())) {
1240 spec.total_frames = _length;
1242 while (spec.pos < _length && !spec.stop) {
1245 /* step 1: interleave */
1247 to_read = min (_length - spec.pos, blocksize);
1249 if (spec.channels == 1) {
1251 if (sources.front()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
1257 Sample buf[blocksize];
1259 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1261 if (sources[chan]->read (buf, _start + spec.pos, to_read) != to_read) {
1265 for (nframes_t x = 0; x < to_read; ++x) {
1266 spec.dataF[chan+(x*spec.channels)] = buf[x];
1271 if (spec.process (to_read)) {
1275 spec.pos += to_read;
1276 spec.progress = (double) spec.pos /_length;
1283 spec.running = false;
1284 spec.status = status;
1290 boost::shared_ptr<Region>
1291 AudioRegion::get_parent() const
1293 boost::shared_ptr<Playlist> pl (playlist());
1296 boost::shared_ptr<AudioRegion> ar;
1297 boost::shared_ptr<AudioRegion const> grrr2 = boost::dynamic_pointer_cast<AudioRegion const> (shared_from_this());
1299 if (grrr2 && (ar = pl->session().find_whole_file_parent (grrr2))) {
1300 return boost::static_pointer_cast<Region> (ar);
1304 return boost::shared_ptr<Region>();
1308 AudioRegion::set_scale_amplitude (gain_t g)
1310 boost::shared_ptr<Playlist> pl (playlist());
1312 _scale_amplitude = g;
1314 /* tell the diskstream we're in */
1320 /* tell everybody else */
1322 send_change (ScaleAmplitudeChanged);
1326 AudioRegion::normalize_to (float target_dB)
1328 const nframes_t blocksize = 64 * 1024;
1329 Sample buf[blocksize];
1334 gain_t target = dB_to_coefficient (target_dB);
1336 if (target == 1.0f) {
1337 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1338 that we may have clipped.
1340 target -= FLT_EPSILON;
1344 fend = _start + _length;
1346 /* first pass: find max amplitude */
1348 while (fpos < fend) {
1352 to_read = min (fend - fpos, blocksize);
1354 for (n = 0; n < n_channels(); ++n) {
1358 if (source (n)->read (buf, fpos, to_read) != to_read) {
1362 maxamp = Session::compute_peak (buf, to_read, maxamp);
1368 if (maxamp == 0.0f) {
1369 /* don't even try */
1373 if (maxamp == target) {
1374 /* we can't do anything useful */
1378 /* compute scale factor */
1380 _scale_amplitude = target/maxamp;
1382 /* tell the diskstream we're in */
1384 boost::shared_ptr<Playlist> pl (playlist());
1390 /* tell everybody else */
1392 send_change (ScaleAmplitudeChanged);
1396 AudioRegion::fade_in_changed ()
1398 send_change (FadeInChanged);
1402 AudioRegion::fade_out_changed ()
1404 send_change (FadeOutChanged);
1408 AudioRegion::envelope_changed ()
1410 send_change (EnvelopeChanged);
1414 AudioRegion::suspend_fade_in ()
1416 if (++_fade_in_disabled == 1) {
1417 if (fade_in_is_default()) {
1418 set_fade_in_active (false);
1424 AudioRegion::resume_fade_in ()
1426 if (--_fade_in_disabled == 0 && _fade_in_disabled) {
1427 set_fade_in_active (true);
1432 AudioRegion::suspend_fade_out ()
1434 if (++_fade_out_disabled == 1) {
1435 if (fade_out_is_default()) {
1436 set_fade_out_active (false);
1442 AudioRegion::resume_fade_out ()
1444 if (--_fade_out_disabled == 0 &&_fade_out_disabled) {
1445 set_fade_out_active (true);
1450 AudioRegion::speed_mismatch (float sr) const
1452 if (sources.empty()) {
1453 /* impossible, but ... */
1457 float fsr = sources.front()->sample_rate();
1463 AudioRegion::source_offset_changed ()
1465 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(sources.front());
1467 if (afs && afs->destructive()) {
1468 // set_start (source()->natural_position(), this);
1469 set_position (source()->natural_position(), this);
1474 AudioRegion::set_playlist (boost::weak_ptr<Playlist> wpl)
1476 boost::shared_ptr<Playlist> old_playlist = (_playlist.lock());
1478 boost::shared_ptr<Playlist> pl (wpl.lock());
1480 if (old_playlist == pl) {
1484 Region::set_playlist (wpl);
1488 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1489 (*i)->remove_playlist (_playlist);
1490 (*i)->add_playlist (pl);
1493 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1494 (*i)->add_playlist (pl);
1499 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1500 (*i)->remove_playlist (old_playlist);
1507 AudioRegion::get_transients (vector<nframes64_t>& results)
1513 TransientDetector t (playlist()->session().frame_rate());
1514 bool existing_results = !results.empty();
1516 for (uint32_t i = 0; i < n_channels(); ++i) {
1518 vector<nframes64_t> these_results;
1522 if (t.run ("", this, i, these_results)) {
1526 /* translate all transients to give absolute position */
1528 for (vector<nframes64_t>::iterator i = these_results.begin(); i != these_results.end(); ++i) {
1534 results.insert (results.end(), these_results.begin(), these_results.end());
1537 if (!results.empty() && (existing_results || n_channels() > 1)) {
1539 /* now resort to bring transients from different channels together */
1541 sort (results.begin(), results.end());
1543 /* remove duplicates or other things that are too close */
1545 vector<nframes64_t>::iterator i = results.begin();
1546 nframes64_t curr = (*i);
1548 /* XXX force a 3msec gap - use a config variable */
1550 nframes64_t gap_frames = (nframes64_t) floor (3.0 * (playlist()->session().frame_rate() / 1000.0));
1554 while (i != results.end()) {
1555 if (((*i) == curr) || (((*i) - curr) < gap_frames)) {
1556 i = results.erase (i);
1570 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)
1572 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1575 uint32_t region_length_from_c (void *arg)
1578 return ((AudioRegion *) arg)->length();
1581 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1583 return ( (AudioRegion *) arg)->source()->available_peaks (zoom_factor) ;