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 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
311 (*i)->remove_playlist (_playlist);
316 GoingAway (); /* EMIT SIGNAL */
320 AudioRegion::verify_length (nframes_t len)
322 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
324 if (afs && afs->destructive()) {
328 for (uint32_t n=0; n < sources.size(); ++n) {
329 if (_start > sources[n]->length() - len) {
337 AudioRegion::verify_start_and_length (nframes_t new_start, nframes_t new_length)
339 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
341 if (afs && afs->destructive()) {
345 for (uint32_t n=0; n < sources.size(); ++n) {
346 if (new_length > sources[n]->length() - new_start) {
353 AudioRegion::verify_start (nframes_t pos)
355 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
357 if (afs && afs->destructive()) {
361 for (uint32_t n=0; n < sources.size(); ++n) {
362 if (pos > sources[n]->length() - _length) {
370 AudioRegion::verify_start_mutable (nframes_t& new_start)
372 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
374 if (afs && afs->destructive()) {
378 for (uint32_t n=0; n < sources.size(); ++n) {
379 if (new_start > sources[n]->length() - _length) {
380 new_start = sources[n]->length() - _length;
386 AudioRegion::set_envelope_active (bool yn)
388 if (envelope_active() != yn) {
391 snprintf (buf, sizeof (buf), "envelope active");
392 _flags = Flag (_flags|EnvelopeActive);
394 snprintf (buf, sizeof (buf), "envelope off");
395 _flags = Flag (_flags & ~EnvelopeActive);
397 send_change (EnvelopeActiveChanged);
402 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
404 if (chan_n >= sources.size()) {
408 if (sources[chan_n]->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
411 if (_scale_amplitude != 1.0) {
412 for (nframes_t n = 0; n < npeaks; ++n) {
413 buf[n].max *= _scale_amplitude;
414 buf[n].min *= _scale_amplitude;
422 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
424 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
426 return _read_at (sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, read_frames, skip_frames);
430 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
431 nframes_t cnt, uint32_t chan_n) const
433 return _read_at (master_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0);
437 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
438 nframes_t position, nframes_t cnt,
439 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
441 nframes_t internal_offset;
442 nframes_t buf_offset;
445 /* precondition: caller has verified that we cover the desired section */
447 if (chan_n >= sources.size()) {
448 return 0; /* read nothing */
451 if (position < _position) {
453 buf_offset = _position - position;
456 internal_offset = position - _position;
460 if (internal_offset >= _length) {
461 return 0; /* read nothing */
464 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
465 return 0; /* read nothing */
469 /* overwrite whatever is there */
470 mixdown_buffer = buf + buf_offset;
472 mixdown_buffer += buf_offset;
476 return 0; /* read nothing */
479 _read_data_count = 0;
481 if (srcs[chan_n]->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
482 return 0; /* "read nothing" */
485 _read_data_count += srcs[chan_n]->read_data_count();
489 if (_flags & FadeIn) {
491 nframes_t fade_in_length = (nframes_t) _fade_in.back()->when;
493 /* see if this read is within the fade in */
495 if (internal_offset < fade_in_length) {
499 limit = min (to_read, fade_in_length - internal_offset);
501 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
503 for (nframes_t n = 0; n < limit; ++n) {
504 mixdown_buffer[n] *= gain_buffer[n];
511 if (_flags & FadeOut) {
513 /* see if some part of this read is within the fade out */
515 /* ................. >| REGION
521 _length - fade_out_length
524 ^internal_offset + to_read
526 we need the intersection of [internal_offset,internal_offset+to_read] with
527 [_length - fade_out_length, _length]
532 nframes_t fade_out_length = (nframes_t) _fade_out.back()->when;
533 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
534 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
536 if (fade_interval_end > fade_interval_start) {
537 /* (part of the) the fade out is in this buffer */
539 nframes_t limit = fade_interval_end - fade_interval_start;
540 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
541 nframes_t fade_offset = fade_interval_start - internal_offset;
543 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
545 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
546 mixdown_buffer[m] *= gain_buffer[n];
552 /* Regular gain curves */
554 if (envelope_active()) {
555 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
557 if (_scale_amplitude != 1.0f) {
558 for (nframes_t n = 0; n < to_read; ++n) {
559 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
562 for (nframes_t n = 0; n < to_read; ++n) {
563 mixdown_buffer[n] *= gain_buffer[n];
566 } else if (_scale_amplitude != 1.0f) {
567 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
572 /* gack. the things we do for users.
577 for (nframes_t n = 0; n < to_read; ++n) {
578 buf[n] += mixdown_buffer[n];
586 AudioRegion::state (bool full)
588 XMLNode& node (Region::state (full));
592 LocaleGuard lg (X_("POSIX"));
594 snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
595 node.add_property ("flags", buf);
596 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
597 node.add_property ("scale-gain", buf);
599 for (uint32_t n=0; n < sources.size(); ++n) {
600 snprintf (buf2, sizeof(buf2), "source-%d", n);
601 sources[n]->id().print (buf, sizeof (buf));
602 node.add_property (buf2, buf);
605 snprintf (buf, sizeof (buf), "%u", (uint32_t) sources.size());
606 node.add_property ("channels", buf);
610 child = node.add_child (X_("FadeIn"));
612 if ((_flags & DefaultFadeIn)) {
613 child->add_property (X_("default"), X_("yes"));
615 child->add_child_nocopy (_fade_in.get_state ());
618 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
620 child = node.add_child (X_("FadeOut"));
622 if ((_flags & DefaultFadeOut)) {
623 child->add_property (X_("default"), X_("yes"));
625 child->add_child_nocopy (_fade_out.get_state ());
628 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
631 child = node.add_child ("Envelope");
634 bool default_env = false;
636 // If there are only two points, the points are in the start of the region and the end of the region
637 // so, if they are both at 1.0f, that means the default region.
639 if (_envelope.size() == 2 &&
640 _envelope.front()->value == 1.0f &&
641 _envelope.back()->value==1.0f) {
642 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
648 child->add_property ("default", "yes");
650 child->add_child_nocopy (_envelope.get_state ());
654 child->add_property ("default", "yes");
657 if (full && _extra_xml) {
658 node.add_child_copy (*_extra_xml);
665 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
667 const XMLNodeList& nlist = node.children();
668 const XMLProperty *prop;
669 LocaleGuard lg (X_("POSIX"));
671 Region::set_live_state (node, what_changed, false);
673 uint32_t old_flags = _flags;
675 if ((prop = node.property ("flags")) != 0) {
676 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
678 _flags = Flag (_flags & ~Region::LeftOfSplit);
679 _flags = Flag (_flags & ~Region::RightOfSplit);
682 if ((old_flags ^ _flags) & Muted) {
683 what_changed = Change (what_changed|MuteChanged);
685 if ((old_flags ^ _flags) & Opaque) {
686 what_changed = Change (what_changed|OpacityChanged);
688 if ((old_flags ^ _flags) & Locked) {
689 what_changed = Change (what_changed|LockChanged);
692 if ((prop = node.property ("scale-gain")) != 0) {
693 _scale_amplitude = atof (prop->value().c_str());
695 _scale_amplitude = 1.0;
698 /* Now find envelope description and other misc child items */
700 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
707 if (child->name() == "Envelope") {
711 if ((prop = child->property ("default")) != 0 || _envelope.set_state (*child)) {
712 set_default_envelope ();
715 _envelope.set_max_xval (_length);
716 _envelope.truncate_end (_length);
718 } else if (child->name() == "FadeIn") {
722 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_in.set_state (*child)) {
723 set_default_fade_in ();
726 } else if (child->name() == "FadeOut") {
730 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_out.set_state (*child)) {
731 set_default_fade_out ();
737 send_change (what_changed);
744 AudioRegion::set_state (const XMLNode& node)
746 /* Region::set_state() calls the virtual set_live_state(),
747 which will get us back to AudioRegion::set_live_state()
748 to handle the relevant stuff.
751 return Region::set_state (node);
755 AudioRegion::set_fade_in_shape (FadeShape shape)
757 set_fade_in (shape, (nframes_t) _fade_in.back()->when);
761 AudioRegion::set_fade_out_shape (FadeShape shape)
763 set_fade_out (shape, (nframes_t) _fade_out.back()->when);
767 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
774 _fade_in.add (0.0, 0.0);
775 _fade_in.add (len, 1.0);
780 _fade_in.add (len * 0.389401, 0.0333333);
781 _fade_in.add (len * 0.629032, 0.0861111);
782 _fade_in.add (len * 0.829493, 0.233333);
783 _fade_in.add (len * 0.9447, 0.483333);
784 _fade_in.add (len * 0.976959, 0.697222);
785 _fade_in.add (len, 1);
790 _fade_in.add (len * 0.0207373, 0.197222);
791 _fade_in.add (len * 0.0645161, 0.525);
792 _fade_in.add (len * 0.152074, 0.802778);
793 _fade_in.add (len * 0.276498, 0.919444);
794 _fade_in.add (len * 0.481567, 0.980556);
795 _fade_in.add (len * 0.767281, 1);
796 _fade_in.add (len, 1);
801 _fade_in.add (len * 0.0737327, 0.308333);
802 _fade_in.add (len * 0.246544, 0.658333);
803 _fade_in.add (len * 0.470046, 0.886111);
804 _fade_in.add (len * 0.652074, 0.972222);
805 _fade_in.add (len * 0.771889, 0.988889);
806 _fade_in.add (len, 1);
811 _fade_in.add (len * 0.304147, 0.0694444);
812 _fade_in.add (len * 0.529954, 0.152778);
813 _fade_in.add (len * 0.725806, 0.333333);
814 _fade_in.add (len * 0.847926, 0.558333);
815 _fade_in.add (len * 0.919355, 0.730556);
816 _fade_in.add (len, 1);
821 _fade_in_shape = shape;
823 send_change (FadeInChanged);
827 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
834 _fade_out.add (len * 0, 1);
835 _fade_out.add (len * 0.023041, 0.697222);
836 _fade_out.add (len * 0.0553, 0.483333);
837 _fade_out.add (len * 0.170507, 0.233333);
838 _fade_out.add (len * 0.370968, 0.0861111);
839 _fade_out.add (len * 0.610599, 0.0333333);
840 _fade_out.add (len * 1, 0);
844 _fade_out.add (len * 0, 1);
845 _fade_out.add (len * 0.228111, 0.988889);
846 _fade_out.add (len * 0.347926, 0.972222);
847 _fade_out.add (len * 0.529954, 0.886111);
848 _fade_out.add (len * 0.753456, 0.658333);
849 _fade_out.add (len * 0.9262673, 0.308333);
850 _fade_out.add (len * 1, 0);
854 _fade_out.add (len * 0, 1);
855 _fade_out.add (len * 0.305556, 1);
856 _fade_out.add (len * 0.548611, 0.991736);
857 _fade_out.add (len * 0.759259, 0.931129);
858 _fade_out.add (len * 0.918981, 0.68595);
859 _fade_out.add (len * 0.976852, 0.22865);
860 _fade_out.add (len * 1, 0);
864 _fade_out.add (len * 0, 1);
865 _fade_out.add (len * 0.080645, 0.730556);
866 _fade_out.add (len * 0.277778, 0.289256);
867 _fade_out.add (len * 0.470046, 0.152778);
868 _fade_out.add (len * 0.695853, 0.0694444);
869 _fade_out.add (len * 1, 0);
873 _fade_out.add (len * 0, 1);
874 _fade_out.add (len * 1, 0);
879 _fade_out_shape = shape;
881 send_change (FadeOutChanged);
885 AudioRegion::set_fade_in_length (nframes_t len)
887 bool changed = _fade_in.extend_to (len);
890 _flags = Flag (_flags & ~DefaultFadeIn);
891 send_change (FadeInChanged);
896 AudioRegion::set_fade_out_length (nframes_t len)
898 bool changed = _fade_out.extend_to (len);
901 _flags = Flag (_flags & ~DefaultFadeOut);
904 send_change (FadeOutChanged);
908 AudioRegion::set_fade_in_active (bool yn)
910 if (yn == (_flags & FadeIn)) {
914 _flags = Flag (_flags|FadeIn);
916 _flags = Flag (_flags & ~FadeIn);
919 send_change (FadeInActiveChanged);
923 AudioRegion::set_fade_out_active (bool yn)
925 if (yn == (_flags & FadeOut)) {
929 _flags = Flag (_flags | FadeOut);
931 _flags = Flag (_flags & ~FadeOut);
934 send_change (FadeOutActiveChanged);
938 AudioRegion::set_default_fade_in ()
940 set_fade_in (Linear, 64);
944 AudioRegion::set_default_fade_out ()
946 set_fade_out (Linear, 64);
950 AudioRegion::set_default_fades ()
952 _fade_in_disabled = 0;
953 _fade_out_disabled = 0;
954 set_default_fade_in ();
955 set_default_fade_out ();
959 AudioRegion::set_default_envelope ()
963 _envelope.add (0, 1.0f);
964 _envelope.add (_length, 1.0f);
969 AudioRegion::recompute_at_end ()
971 /* our length has changed. recompute a new final point by interpolating
972 based on the the existing curve.
976 _envelope.truncate_end (_length);
977 _envelope.set_max_xval (_length);
980 if (_fade_in.back()->when > _length) {
981 _fade_in.extend_to (_length);
982 send_change (FadeInChanged);
985 if (_fade_out.back()->when > _length) {
986 _fade_out.extend_to (_length);
987 send_change (FadeOutChanged);
992 AudioRegion::recompute_at_start ()
994 /* as above, but the shift was from the front */
996 _envelope.truncate_start (_length);
998 if (_fade_in.back()->when > _length) {
999 _fade_in.extend_to (_length);
1000 send_change (FadeInChanged);
1003 if (_fade_out.back()->when > _length) {
1004 _fade_out.extend_to (_length);
1005 send_change (FadeOutChanged);
1010 AudioRegion::separate_by_channel (Session& session, vector<AudioRegion*>& v) const
1015 for (SourceList::const_iterator i = master_sources.begin(); i != master_sources.end(); ++i) {
1018 srcs.push_back (*i);
1020 /* generate a new name */
1022 if (session.region_name (new_name, _name)) {
1026 /* create a copy with just one source */
1028 v.push_back (new AudioRegion (srcs, _start, _length, new_name, _layer, _flags));
1035 AudioRegion::source_deleted ()
1042 AudioRegion::master_source_names ()
1044 SourceList::iterator i;
1046 vector<string> names;
1047 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1048 names.push_back((*i)->name());
1055 AudioRegion::source_equivalent (boost::shared_ptr<const Region> o) const
1057 boost::shared_ptr<const AudioRegion> other = boost::dynamic_pointer_cast<const AudioRegion>(o);
1062 SourceList::const_iterator i;
1063 SourceList::const_iterator io;
1065 for (i = sources.begin(), io = other->sources.begin(); i != sources.end() && io != other->sources.end(); ++i, ++io) {
1066 if ((*i)->id() != (*io)->id()) {
1071 for (i = master_sources.begin(), io = other->master_sources.begin(); i != master_sources.end() && io != other->master_sources.end(); ++i, ++io) {
1072 if ((*i)->id() != (*io)->id()) {
1081 AudioRegion::apply (AudioFilter& filter)
1083 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
1084 return filter.run (ar);
1088 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
1090 const nframes_t blocksize = 4096;
1094 spec.channels = sources.size();
1096 if (spec.prepare (blocksize, session.frame_rate())) {
1101 spec.total_frames = _length;
1103 while (spec.pos < _length && !spec.stop) {
1106 /* step 1: interleave */
1108 to_read = min (_length - spec.pos, blocksize);
1110 if (spec.channels == 1) {
1112 if (sources.front()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
1118 Sample buf[blocksize];
1120 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1122 if (sources[chan]->read (buf, _start + spec.pos, to_read) != to_read) {
1126 for (nframes_t x = 0; x < to_read; ++x) {
1127 spec.dataF[chan+(x*spec.channels)] = buf[x];
1132 if (spec.process (to_read)) {
1136 spec.pos += to_read;
1137 spec.progress = (double) spec.pos /_length;
1144 spec.running = false;
1145 spec.status = status;
1151 boost::shared_ptr<Region>
1152 AudioRegion::get_parent()
1155 boost::shared_ptr<AudioRegion> ar;
1156 boost::shared_ptr<AudioRegion> grrr2 = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
1158 if (grrr2 && (ar = _playlist->session().find_whole_file_parent (grrr2))) {
1159 return boost::static_pointer_cast<Region> (ar);
1163 return boost::shared_ptr<Region>();
1167 AudioRegion::set_scale_amplitude (gain_t g)
1169 _scale_amplitude = g;
1171 /* tell the diskstream we're in */
1174 _playlist->Modified();
1177 /* tell everybody else */
1179 send_change (ScaleAmplitudeChanged);
1183 AudioRegion::normalize_to (float target_dB)
1185 const nframes_t blocksize = 64 * 1024;
1186 Sample buf[blocksize];
1191 gain_t target = dB_to_coefficient (target_dB);
1193 if (target == 1.0f) {
1194 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1195 that we may have clipped.
1197 target -= FLT_EPSILON;
1201 fend = _start + _length;
1203 /* first pass: find max amplitude */
1205 while (fpos < fend) {
1209 to_read = min (fend - fpos, blocksize);
1211 for (n = 0; n < n_channels(); ++n) {
1215 if (source (n)->read (buf, fpos, to_read) != to_read) {
1219 maxamp = Session::compute_peak (buf, to_read, maxamp);
1225 if (maxamp == 0.0f) {
1226 /* don't even try */
1230 if (maxamp == target) {
1231 /* we can't do anything useful */
1235 /* compute scale factor */
1237 _scale_amplitude = target/maxamp;
1239 /* tell the diskstream we're in */
1242 _playlist->Modified();
1245 /* tell everybody else */
1247 send_change (ScaleAmplitudeChanged);
1251 AudioRegion::envelope_changed (Change ignored)
1253 send_change (EnvelopeChanged);
1257 AudioRegion::suspend_fade_in ()
1259 if (++_fade_in_disabled == 1) {
1260 set_fade_in_active (false);
1265 AudioRegion::resume_fade_in ()
1267 if (_fade_in_disabled && --_fade_in_disabled == 0) {
1268 set_fade_in_active (true);
1273 AudioRegion::suspend_fade_out ()
1275 if (++_fade_out_disabled == 1) {
1276 set_fade_out_active (false);
1281 AudioRegion::resume_fade_out ()
1283 if (_fade_out_disabled && --_fade_out_disabled == 0) {
1284 set_fade_out_active (true);
1289 AudioRegion::speed_mismatch (float sr) const
1291 if (sources.empty()) {
1292 /* impossible, but ... */
1296 float fsr = sources.front()->sample_rate();
1302 AudioRegion::source_offset_changed ()
1304 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(sources.front());
1306 if (afs && afs->destructive()) {
1307 // set_start (source()->natural_position(), this);
1308 set_position (source()->natural_position(), this);
1313 AudioRegion::set_playlist (Playlist* pl)
1315 if (pl == _playlist) {
1319 Playlist* old_playlist = _playlist;
1321 Region::set_playlist (pl);
1325 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1326 (*i)->remove_playlist (old_playlist);
1327 (*i)->add_playlist (_playlist);
1330 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1331 (*i)->add_playlist (_playlist);
1336 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1337 (*i)->remove_playlist (old_playlist);
1345 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)
1347 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1350 uint32_t region_length_from_c (void *arg)
1353 return ((AudioRegion *) arg)->length();
1356 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1358 return ( (AudioRegion *) arg)->source()->available_peaks (zoom_factor) ;