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>
36 #include <ardour/audioregion.h>
37 #include <ardour/session.h>
38 #include <ardour/gain.h>
39 #include <ardour/dB.h>
40 #include <ardour/playlist.h>
41 #include <ardour/audiofilter.h>
42 #include <ardour/audiofilesource.h>
43 #include <ardour/destructive_filesource.h>
49 using namespace ARDOUR;
51 /* a Session will reset these to its chosen defaults by calling AudioRegion::set_default_fade() */
53 Change AudioRegion::FadeInChanged = ARDOUR::new_change();
54 Change AudioRegion::FadeOutChanged = ARDOUR::new_change();
55 Change AudioRegion::FadeInActiveChanged = ARDOUR::new_change();
56 Change AudioRegion::FadeOutActiveChanged = ARDOUR::new_change();
57 Change AudioRegion::EnvelopeActiveChanged = ARDOUR::new_change();
58 Change AudioRegion::ScaleAmplitudeChanged = ARDOUR::new_change();
59 Change AudioRegion::EnvelopeChanged = ARDOUR::new_change();
61 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length)
62 : Region (start, length, PBD::basename_nosuffix(src->name()), 0, Region::Flag(Region::DefaultFlags|Region::External)),
63 _fade_in (0.0, 2.0, 1.0, false),
64 _fade_out (0.0, 2.0, 1.0, false),
65 _envelope (0.0, 2.0, 1.0, false)
67 /* basic AudioRegion constructor */
69 sources.push_back (src);
70 master_sources.push_back (src);
71 src->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
73 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
75 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
78 _scale_amplitude = 1.0;
81 set_default_envelope ();
83 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
86 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
87 : Region (start, length, name, layer, flags),
88 _fade_in (0.0, 2.0, 1.0, false),
89 _fade_out (0.0, 2.0, 1.0, false),
90 _envelope (0.0, 2.0, 1.0, false)
92 /* basic AudioRegion constructor */
94 sources.push_back (src);
95 master_sources.push_back (src);
96 src->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
98 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
100 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
103 _scale_amplitude = 1.0;
105 set_default_fades ();
106 set_default_envelope ();
108 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
111 AudioRegion::AudioRegion (SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
112 : Region (start, length, name, layer, flags),
113 _fade_in (0.0, 2.0, 1.0, false),
114 _fade_out (0.0, 2.0, 1.0, false),
115 _envelope (0.0, 2.0, 1.0, false)
117 /* basic AudioRegion constructor */
119 for (SourceList::iterator i=srcs.begin(); i != srcs.end(); ++i) {
120 sources.push_back (*i);
121 master_sources.push_back (*i);
122 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
124 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> ((*i));
126 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
130 _scale_amplitude = 1.0;
132 set_default_fades ();
133 set_default_envelope ();
135 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
139 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
140 : Region (other, offset, length, name, layer, flags),
141 _fade_in (other->_fade_in),
142 _fade_out (other->_fade_out),
143 _envelope (other->_envelope, (double) offset, (double) offset + length)
145 /* create a new AudioRegion, that is part of an existing one */
147 set<boost::shared_ptr<AudioSource> > unique_srcs;
149 for (SourceList::const_iterator i= other->sources.begin(); i != other->sources.end(); ++i) {
150 sources.push_back (*i);
151 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
153 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
155 result = unique_srcs.insert (*i);
158 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
160 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
165 for (SourceList::const_iterator i = other->master_sources.begin(); i != other->master_sources.end(); ++i) {
166 if (unique_srcs.find (*i) == unique_srcs.end()) {
167 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
169 master_sources.push_back (*i);
172 /* return to default fades if the existing ones are too long */
173 _fade_in_disabled = 0;
174 _fade_out_disabled = 0;
177 if (_flags & LeftOfSplit) {
178 if (_fade_in.back()->when >= _length) {
179 set_default_fade_in ();
181 _fade_in_disabled = other->_fade_in_disabled;
183 set_default_fade_out ();
184 _flags = Flag (_flags & ~Region::LeftOfSplit);
187 if (_flags & RightOfSplit) {
188 if (_fade_out.back()->when >= _length) {
189 set_default_fade_out ();
191 _fade_out_disabled = other->_fade_out_disabled;
193 set_default_fade_in ();
194 _flags = Flag (_flags & ~Region::RightOfSplit);
197 _scale_amplitude = other->_scale_amplitude;
199 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
202 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
204 _fade_in (other->_fade_in),
205 _fade_out (other->_fade_out),
206 _envelope (other->_envelope)
208 /* Pure copy constructor */
210 set<boost::shared_ptr<AudioSource> > unique_srcs;
212 for (SourceList::const_iterator i = other->sources.begin(); i != other->sources.end(); ++i) {
213 sources.push_back (*i);
214 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
215 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
217 result = unique_srcs.insert (*i);
220 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
222 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
227 for (SourceList::const_iterator i = other->master_sources.begin(); i != other->master_sources.end(); ++i) {
228 master_sources.push_back (*i);
229 if (unique_srcs.find (*i) == unique_srcs.end()) {
230 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
234 _scale_amplitude = other->_scale_amplitude;
235 _envelope = other->_envelope;
237 _fade_in_disabled = 0;
238 _fade_out_disabled = 0;
240 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
243 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, const XMLNode& node)
245 _fade_in (0.0, 2.0, 1.0, false),
246 _fade_out (0.0, 2.0, 1.0, false),
247 _envelope (0.0, 2.0, 1.0, false)
249 sources.push_back (src);
250 master_sources.push_back (src);
251 src->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
253 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
255 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
258 set_default_fades ();
260 if (set_state (node)) {
261 throw failed_constructor();
264 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
267 AudioRegion::AudioRegion (SourceList& srcs, const XMLNode& node)
269 _fade_in (0.0, 2.0, 1.0, false),
270 _fade_out (0.0, 2.0, 1.0, false),
271 _envelope (0.0, 2.0, 1.0, false)
273 set<boost::shared_ptr<AudioSource> > unique_srcs;
275 for (SourceList::iterator i=srcs.begin(); i != srcs.end(); ++i) {
276 sources.push_back (*i);
277 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
278 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
280 result = unique_srcs.insert (*i);
283 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
285 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
290 for (SourceList::iterator i = srcs.begin(); i != srcs.end(); ++i) {
291 master_sources.push_back (*i);
292 if (unique_srcs.find (*i) == unique_srcs.end()) {
293 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
297 set_default_fades ();
298 _scale_amplitude = 1.0;
300 if (set_state (node)) {
301 throw failed_constructor();
304 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
307 AudioRegion::~AudioRegion ()
310 GoingAway (); /* EMIT SIGNAL */
314 AudioRegion::verify_length (nframes_t len)
316 if (boost::dynamic_pointer_cast<DestructiveFileSource>(source())) {
320 for (uint32_t n=0; n < sources.size(); ++n) {
321 if (_start > sources[n]->length() - len) {
329 AudioRegion::verify_start_and_length (nframes_t new_start, nframes_t new_length)
331 if (boost::dynamic_pointer_cast<DestructiveFileSource>(source())) {
335 for (uint32_t n=0; n < sources.size(); ++n) {
336 if (new_length > sources[n]->length() - new_start) {
343 AudioRegion::verify_start (nframes_t pos)
345 if (boost::dynamic_pointer_cast<DestructiveFileSource>(source())) {
349 for (uint32_t n=0; n < sources.size(); ++n) {
350 if (pos > sources[n]->length() - _length) {
358 AudioRegion::verify_start_mutable (nframes_t& new_start)
360 if (boost::dynamic_pointer_cast<DestructiveFileSource>(source())) {
364 for (uint32_t n=0; n < sources.size(); ++n) {
365 if (new_start > sources[n]->length() - _length) {
366 new_start = sources[n]->length() - _length;
372 AudioRegion::set_envelope_active (bool yn)
374 if (envelope_active() != yn) {
377 snprintf (buf, sizeof (buf), "envelope active");
378 _flags = Flag (_flags|EnvelopeActive);
380 snprintf (buf, sizeof (buf), "envelope off");
381 _flags = Flag (_flags & ~EnvelopeActive);
383 send_change (EnvelopeActiveChanged);
388 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
390 if (chan_n >= sources.size()) {
394 if (sources[chan_n]->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
397 if (_scale_amplitude != 1.0) {
398 for (nframes_t n = 0; n < npeaks; ++n) {
399 buf[n].max *= _scale_amplitude;
400 buf[n].min *= _scale_amplitude;
408 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
410 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
412 return _read_at (sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, read_frames, skip_frames);
416 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
417 nframes_t cnt, uint32_t chan_n) const
419 return _read_at (master_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0);
423 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
424 nframes_t position, nframes_t cnt,
425 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
427 nframes_t internal_offset;
428 nframes_t buf_offset;
431 /* precondition: caller has verified that we cover the desired section */
433 if (chan_n >= sources.size()) {
434 return 0; /* read nothing */
437 if (position < _position) {
439 buf_offset = _position - position;
442 internal_offset = position - _position;
446 if (internal_offset >= _length) {
447 return 0; /* read nothing */
450 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
451 return 0; /* read nothing */
455 /* overwrite whatever is there */
456 mixdown_buffer = buf + buf_offset;
458 mixdown_buffer += buf_offset;
462 return 0; /* read nothing */
465 _read_data_count = 0;
467 if (srcs[chan_n]->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
468 return 0; /* "read nothing" */
471 _read_data_count += srcs[chan_n]->read_data_count();
475 if (_flags & FadeIn) {
477 nframes_t fade_in_length = (nframes_t) _fade_in.back()->when;
479 /* see if this read is within the fade in */
481 if (internal_offset < fade_in_length) {
485 limit = min (to_read, fade_in_length - internal_offset);
487 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
489 for (nframes_t n = 0; n < limit; ++n) {
490 mixdown_buffer[n] *= gain_buffer[n];
497 if (_flags & FadeOut) {
499 /* see if some part of this read is within the fade out */
501 /* ................. >| REGION
507 _length - fade_out_length
510 ^internal_offset + to_read
512 we need the intersection of [internal_offset,internal_offset+to_read] with
513 [_length - fade_out_length, _length]
518 nframes_t fade_out_length = (nframes_t) _fade_out.back()->when;
519 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
520 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
522 if (fade_interval_end > fade_interval_start) {
523 /* (part of the) the fade out is in this buffer */
525 nframes_t limit = fade_interval_end - fade_interval_start;
526 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
527 nframes_t fade_offset = fade_interval_start - internal_offset;
529 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
531 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
532 mixdown_buffer[m] *= gain_buffer[n];
538 /* Regular gain curves */
540 if (envelope_active()) {
541 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
543 if (_scale_amplitude != 1.0f) {
544 for (nframes_t n = 0; n < to_read; ++n) {
545 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
548 for (nframes_t n = 0; n < to_read; ++n) {
549 mixdown_buffer[n] *= gain_buffer[n];
552 } else if (_scale_amplitude != 1.0f) {
553 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
558 /* gack. the things we do for users.
563 for (nframes_t n = 0; n < to_read; ++n) {
564 buf[n] += mixdown_buffer[n];
572 AudioRegion::state (bool full)
574 XMLNode& node (Region::state (full));
578 LocaleGuard lg (X_("POSIX"));
580 snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
581 node.add_property ("flags", buf);
582 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
583 node.add_property ("scale-gain", buf);
585 for (uint32_t n=0; n < sources.size(); ++n) {
586 snprintf (buf2, sizeof(buf2), "source-%d", n);
587 sources[n]->id().print (buf, sizeof (buf));
588 node.add_property (buf2, buf);
591 snprintf (buf, sizeof (buf), "%u", (uint32_t) sources.size());
592 node.add_property ("channels", buf);
596 child = node.add_child (X_("FadeIn"));
598 if ((_flags & DefaultFadeIn)) {
599 child->add_property (X_("default"), X_("yes"));
601 child->add_child_nocopy (_fade_in.get_state ());
604 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
606 child = node.add_child (X_("FadeOut"));
608 if ((_flags & DefaultFadeOut)) {
609 child->add_property (X_("default"), X_("yes"));
611 child->add_child_nocopy (_fade_out.get_state ());
614 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
617 child = node.add_child ("Envelope");
620 bool default_env = false;
622 // If there are only two points, the points are in the start of the region and the end of the region
623 // so, if they are both at 1.0f, that means the default region.
625 if (_envelope.size() == 2 &&
626 _envelope.front()->value == 1.0f &&
627 _envelope.back()->value==1.0f) {
628 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
634 child->add_property ("default", "yes");
636 child->add_child_nocopy (_envelope.get_state ());
640 child->add_property ("default", "yes");
643 if (full && _extra_xml) {
644 node.add_child_copy (*_extra_xml);
651 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
653 const XMLNodeList& nlist = node.children();
654 const XMLProperty *prop;
655 LocaleGuard lg (X_("POSIX"));
657 Region::set_live_state (node, what_changed, false);
659 uint32_t old_flags = _flags;
661 if ((prop = node.property ("flags")) != 0) {
662 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
664 _flags = Flag (_flags & ~Region::LeftOfSplit);
665 _flags = Flag (_flags & ~Region::RightOfSplit);
668 if ((old_flags ^ _flags) & Muted) {
669 what_changed = Change (what_changed|MuteChanged);
671 if ((old_flags ^ _flags) & Opaque) {
672 what_changed = Change (what_changed|OpacityChanged);
674 if ((old_flags ^ _flags) & Locked) {
675 what_changed = Change (what_changed|LockChanged);
678 if ((prop = node.property ("scale-gain")) != 0) {
679 _scale_amplitude = atof (prop->value().c_str());
681 _scale_amplitude = 1.0;
684 /* Now find envelope description and other misc child items */
686 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
693 if (child->name() == "Envelope") {
697 if ((prop = child->property ("default")) != 0) {
698 set_default_envelope ();
700 _envelope.set_state (*child);
703 _envelope.set_max_xval (_length);
704 _envelope.truncate_end (_length);
706 } else if (child->name() == "FadeIn") {
710 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
711 set_default_fade_in ();
713 _fade_in.set_state (*child);
716 } else if (child->name() == "FadeOut") {
720 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
721 set_default_fade_out ();
723 _fade_out.set_state (*child);
729 send_change (what_changed);
736 AudioRegion::set_state (const XMLNode& node)
738 /* Region::set_state() calls the virtual set_live_state(),
739 which will get us back to AudioRegion::set_live_state()
740 to handle the relevant stuff.
743 return Region::set_state (node);
747 AudioRegion::set_fade_in_shape (FadeShape shape)
749 set_fade_in (shape, (nframes_t) _fade_in.back()->when);
753 AudioRegion::set_fade_out_shape (FadeShape shape)
755 set_fade_out (shape, (nframes_t) _fade_out.back()->when);
759 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
766 _fade_in.add (0.0, 0.0);
767 _fade_in.add (len, 1.0);
772 _fade_in.add (len * 0.389401, 0.0333333);
773 _fade_in.add (len * 0.629032, 0.0861111);
774 _fade_in.add (len * 0.829493, 0.233333);
775 _fade_in.add (len * 0.9447, 0.483333);
776 _fade_in.add (len * 0.976959, 0.697222);
777 _fade_in.add (len, 1);
782 _fade_in.add (len * 0.0207373, 0.197222);
783 _fade_in.add (len * 0.0645161, 0.525);
784 _fade_in.add (len * 0.152074, 0.802778);
785 _fade_in.add (len * 0.276498, 0.919444);
786 _fade_in.add (len * 0.481567, 0.980556);
787 _fade_in.add (len * 0.767281, 1);
788 _fade_in.add (len, 1);
793 _fade_in.add (len * 0.0737327, 0.308333);
794 _fade_in.add (len * 0.246544, 0.658333);
795 _fade_in.add (len * 0.470046, 0.886111);
796 _fade_in.add (len * 0.652074, 0.972222);
797 _fade_in.add (len * 0.771889, 0.988889);
798 _fade_in.add (len, 1);
803 _fade_in.add (len * 0.304147, 0.0694444);
804 _fade_in.add (len * 0.529954, 0.152778);
805 _fade_in.add (len * 0.725806, 0.333333);
806 _fade_in.add (len * 0.847926, 0.558333);
807 _fade_in.add (len * 0.919355, 0.730556);
808 _fade_in.add (len, 1);
813 _fade_in_shape = shape;
815 send_change (FadeInChanged);
819 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
826 _fade_out.add (len * 0, 1);
827 _fade_out.add (len * 0.023041, 0.697222);
828 _fade_out.add (len * 0.0553, 0.483333);
829 _fade_out.add (len * 0.170507, 0.233333);
830 _fade_out.add (len * 0.370968, 0.0861111);
831 _fade_out.add (len * 0.610599, 0.0333333);
832 _fade_out.add (len * 1, 0);
836 _fade_out.add (len * 0, 1);
837 _fade_out.add (len * 0.228111, 0.988889);
838 _fade_out.add (len * 0.347926, 0.972222);
839 _fade_out.add (len * 0.529954, 0.886111);
840 _fade_out.add (len * 0.753456, 0.658333);
841 _fade_out.add (len * 0.9262673, 0.308333);
842 _fade_out.add (len * 1, 0);
846 _fade_out.add (len * 0, 1);
847 _fade_out.add (len * 0.305556, 1);
848 _fade_out.add (len * 0.548611, 0.991736);
849 _fade_out.add (len * 0.759259, 0.931129);
850 _fade_out.add (len * 0.918981, 0.68595);
851 _fade_out.add (len * 0.976852, 0.22865);
852 _fade_out.add (len * 1, 0);
856 _fade_out.add (len * 0, 1);
857 _fade_out.add (len * 0.080645, 0.730556);
858 _fade_out.add (len * 0.277778, 0.289256);
859 _fade_out.add (len * 0.470046, 0.152778);
860 _fade_out.add (len * 0.695853, 0.0694444);
861 _fade_out.add (len * 1, 0);
865 _fade_out.add (len * 0, 1);
866 _fade_out.add (len * 1, 0);
871 _fade_out_shape = shape;
873 send_change (FadeOutChanged);
877 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)
890 bool changed = _fade_out.extend_to (len);
893 _flags = Flag (_flags & ~DefaultFadeOut);
896 send_change (FadeOutChanged);
900 AudioRegion::set_fade_in_active (bool yn)
902 if (yn == (_flags & FadeIn)) {
906 _flags = Flag (_flags|FadeIn);
908 _flags = Flag (_flags & ~FadeIn);
911 send_change (FadeInActiveChanged);
915 AudioRegion::set_fade_out_active (bool yn)
917 if (yn == (_flags & FadeOut)) {
921 _flags = Flag (_flags | FadeOut);
923 _flags = Flag (_flags & ~FadeOut);
926 send_change (FadeOutActiveChanged);
930 AudioRegion::set_default_fade_in ()
932 set_fade_in (Linear, 64);
936 AudioRegion::set_default_fade_out ()
938 set_fade_out (Linear, 64);
942 AudioRegion::set_default_fades ()
944 _fade_in_disabled = 0;
945 _fade_out_disabled = 0;
946 set_default_fade_in ();
947 set_default_fade_out ();
951 AudioRegion::set_default_envelope ()
955 _envelope.add (0, 1.0f);
956 _envelope.add (_length, 1.0f);
961 AudioRegion::recompute_at_end ()
963 /* our length has changed. recompute a new final point by interpolating
964 based on the the existing curve.
968 _envelope.truncate_end (_length);
969 _envelope.set_max_xval (_length);
972 if (_fade_in.back()->when > _length) {
973 _fade_in.extend_to (_length);
974 send_change (FadeInChanged);
977 if (_fade_out.back()->when > _length) {
978 _fade_out.extend_to (_length);
979 send_change (FadeOutChanged);
984 AudioRegion::recompute_at_start ()
986 /* as above, but the shift was from the front */
988 _envelope.truncate_start (_length);
990 if (_fade_in.back()->when > _length) {
991 _fade_in.extend_to (_length);
992 send_change (FadeInChanged);
995 if (_fade_out.back()->when > _length) {
996 _fade_out.extend_to (_length);
997 send_change (FadeOutChanged);
1002 AudioRegion::separate_by_channel (Session& session, vector<AudioRegion*>& v) const
1007 for (SourceList::const_iterator i = master_sources.begin(); i != master_sources.end(); ++i) {
1010 srcs.push_back (*i);
1012 /* generate a new name */
1014 if (session.region_name (new_name, _name)) {
1018 /* create a copy with just one source */
1020 v.push_back (new AudioRegion (srcs, _start, _length, new_name, _layer, _flags));
1027 AudioRegion::source_deleted ()
1034 AudioRegion::master_source_names ()
1036 SourceList::iterator i;
1038 vector<string> names;
1039 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1040 names.push_back((*i)->name());
1047 AudioRegion::source_equivalent (boost::shared_ptr<const Region> o) const
1049 boost::shared_ptr<const AudioRegion> other = boost::dynamic_pointer_cast<const AudioRegion>(o);
1054 SourceList::const_iterator i;
1055 SourceList::const_iterator io;
1057 for (i = sources.begin(), io = other->sources.begin(); i != sources.end() && io != other->sources.end(); ++i, ++io) {
1058 if ((*i)->id() != (*io)->id()) {
1063 for (i = master_sources.begin(), io = other->master_sources.begin(); i != master_sources.end() && io != other->master_sources.end(); ++i, ++io) {
1064 if ((*i)->id() != (*io)->id()) {
1073 AudioRegion::apply (AudioFilter& filter)
1075 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
1076 return filter.run (ar);
1080 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
1082 const nframes_t blocksize = 4096;
1086 spec.channels = sources.size();
1088 if (spec.prepare (blocksize, session.frame_rate())) {
1093 spec.total_frames = _length;
1095 while (spec.pos < _length && !spec.stop) {
1098 /* step 1: interleave */
1100 to_read = min (_length - spec.pos, blocksize);
1102 if (spec.channels == 1) {
1104 if (sources.front()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
1110 Sample buf[blocksize];
1112 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1114 if (sources[chan]->read (buf, _start + spec.pos, to_read) != to_read) {
1118 for (nframes_t x = 0; x < to_read; ++x) {
1119 spec.dataF[chan+(x*spec.channels)] = buf[x];
1124 if (spec.process (to_read)) {
1128 spec.pos += to_read;
1129 spec.progress = (double) spec.pos /_length;
1136 spec.running = false;
1137 spec.status = status;
1143 boost::shared_ptr<Region>
1144 AudioRegion::get_parent()
1146 boost::shared_ptr<Region> r;
1149 boost::shared_ptr<AudioRegion> ar;
1150 boost::shared_ptr<AudioRegion> grrr2 = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
1152 if (grrr2 && (ar = _playlist->session().find_whole_file_parent (grrr2))) {
1153 return boost::static_pointer_cast<Region> (ar);
1161 AudioRegion::set_scale_amplitude (gain_t g)
1163 _scale_amplitude = g;
1165 /* tell the diskstream we're in */
1168 _playlist->Modified();
1171 /* tell everybody else */
1173 send_change (ScaleAmplitudeChanged);
1177 AudioRegion::normalize_to (float target_dB)
1179 const nframes_t blocksize = 64 * 1024;
1180 Sample buf[blocksize];
1185 gain_t target = dB_to_coefficient (target_dB);
1187 if (target == 1.0f) {
1188 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1189 that we may have clipped.
1191 target -= FLT_EPSILON;
1195 fend = _start + _length;
1197 /* first pass: find max amplitude */
1199 while (fpos < fend) {
1203 to_read = min (fend - fpos, blocksize);
1205 for (n = 0; n < n_channels(); ++n) {
1209 if (source (n)->read (buf, fpos, to_read) != to_read) {
1213 maxamp = Session::compute_peak (buf, to_read, maxamp);
1219 if (maxamp == 0.0f) {
1220 /* don't even try */
1224 if (maxamp == target) {
1225 /* we can't do anything useful */
1229 /* compute scale factor */
1231 _scale_amplitude = target/maxamp;
1233 /* tell the diskstream we're in */
1236 _playlist->Modified();
1239 /* tell everybody else */
1241 send_change (ScaleAmplitudeChanged);
1245 AudioRegion::envelope_changed (Change ignored)
1247 send_change (EnvelopeChanged);
1251 AudioRegion::suspend_fade_in ()
1253 if (++_fade_in_disabled == 1) {
1254 set_fade_in_active (false);
1259 AudioRegion::resume_fade_in ()
1261 if (_fade_in_disabled && --_fade_in_disabled == 0) {
1262 set_fade_in_active (true);
1267 AudioRegion::suspend_fade_out ()
1269 if (++_fade_out_disabled == 1) {
1270 set_fade_out_active (false);
1275 AudioRegion::resume_fade_out ()
1277 if (_fade_out_disabled && --_fade_out_disabled == 0) {
1278 set_fade_out_active (true);
1283 AudioRegion::speed_mismatch (float sr) const
1285 if (sources.empty()) {
1286 /* impossible, but ... */
1290 float fsr = sources.front()->sample_rate();
1296 AudioRegion::source_offset_changed ()
1298 if (boost::dynamic_pointer_cast<DestructiveFileSource>(sources.front())) {
1299 set_start (source()->natural_position(), this);
1300 set_position (source()->natural_position(), this);
1306 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)
1308 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1311 uint32_t region_length_from_c (void *arg)
1314 return ((AudioRegion *) arg)->length();
1317 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1319 return ( (AudioRegion *) arg)->source()->available_peaks (zoom_factor) ;