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.
26 #include <sigc++/bind.h>
27 #include <sigc++/class_slot.h>
29 #include <glibmm/thread.h>
31 #include <pbd/basename.h>
32 #include <pbd/xml++.h>
33 #include <pbd/stacktrace.h>
34 #include <pbd/enumwriter.h>
35 #include <pbd/convert.h>
37 #include <ardour/audioregion.h>
38 #include <ardour/session.h>
39 #include <ardour/gain.h>
40 #include <ardour/dB.h>
41 #include <ardour/playlist.h>
42 #include <ardour/audiofilter.h>
43 #include <ardour/audiofilesource.h>
44 #include <ardour/region_factory.h>
50 using namespace ARDOUR;
53 /* a Session will reset these to its chosen defaults by calling AudioRegion::set_default_fade() */
55 Change AudioRegion::FadeInChanged = ARDOUR::new_change();
56 Change AudioRegion::FadeOutChanged = ARDOUR::new_change();
57 Change AudioRegion::FadeInActiveChanged = ARDOUR::new_change();
58 Change AudioRegion::FadeOutActiveChanged = ARDOUR::new_change();
59 Change AudioRegion::EnvelopeActiveChanged = ARDOUR::new_change();
60 Change AudioRegion::ScaleAmplitudeChanged = ARDOUR::new_change();
61 Change AudioRegion::EnvelopeChanged = ARDOUR::new_change();
63 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length)
64 : Region (start, length, PBD::basename_nosuffix(src->name()), 0, Region::Flag(Region::DefaultFlags|Region::External)),
65 _fade_in (0.0, 2.0, 1.0, false),
66 _fade_out (0.0, 2.0, 1.0, false),
67 _envelope (0.0, 2.0, 1.0, false)
69 /* basic AudioRegion constructor */
71 sources.push_back (src);
72 master_sources.push_back (src);
73 src->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
75 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
77 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
80 _scale_amplitude = 1.0;
83 set_default_envelope ();
85 listen_to_my_curves ();
88 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
89 : Region (start, length, name, layer, flags),
90 _fade_in (0.0, 2.0, 1.0, false),
91 _fade_out (0.0, 2.0, 1.0, false),
92 _envelope (0.0, 2.0, 1.0, false)
94 /* basic AudioRegion constructor */
96 sources.push_back (src);
97 master_sources.push_back (src);
98 src->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
100 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
102 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
105 _scale_amplitude = 1.0;
107 set_default_fades ();
108 set_default_envelope ();
110 listen_to_my_curves ();
113 AudioRegion::AudioRegion (SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
114 : Region (start, length, name, layer, flags),
115 _fade_in (0.0, 2.0, 1.0, false),
116 _fade_out (0.0, 2.0, 1.0, false),
117 _envelope (0.0, 2.0, 1.0, false)
119 /* basic AudioRegion constructor */
121 for (SourceList::iterator i=srcs.begin(); i != srcs.end(); ++i) {
122 sources.push_back (*i);
123 master_sources.push_back (*i);
124 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
126 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> ((*i));
128 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
132 _scale_amplitude = 1.0;
134 set_default_fades ();
135 set_default_envelope ();
137 listen_to_my_curves ();
141 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
142 : Region (other, offset, length, name, layer, flags),
143 _fade_in (other->_fade_in),
144 _fade_out (other->_fade_out),
145 _envelope (other->_envelope, (double) offset, (double) offset + length)
147 /* create a new AudioRegion, that is part of an existing one */
149 set<boost::shared_ptr<AudioSource> > unique_srcs;
151 for (SourceList::const_iterator i= other->sources.begin(); i != other->sources.end(); ++i) {
152 sources.push_back (*i);
153 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
155 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
157 result = unique_srcs.insert (*i);
160 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
162 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
167 for (SourceList::const_iterator i = other->master_sources.begin(); i != other->master_sources.end(); ++i) {
168 if (unique_srcs.find (*i) == unique_srcs.end()) {
169 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
171 master_sources.push_back (*i);
175 /* return to default fades if the existing ones are too long */
176 _fade_in_disabled = 0;
177 _fade_out_disabled = 0;
180 if (_flags & LeftOfSplit) {
181 if (_fade_in.back()->when >= _length) {
182 set_default_fade_in ();
184 _fade_in_disabled = other->_fade_in_disabled;
186 set_default_fade_out ();
187 _flags = Flag (_flags & ~Region::LeftOfSplit);
190 if (_flags & RightOfSplit) {
191 if (_fade_out.back()->when >= _length) {
192 set_default_fade_out ();
194 _fade_out_disabled = other->_fade_out_disabled;
196 set_default_fade_in ();
197 _flags = Flag (_flags & ~Region::RightOfSplit);
200 _scale_amplitude = other->_scale_amplitude;
202 listen_to_my_curves ();
205 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
207 _fade_in (other->_fade_in),
208 _fade_out (other->_fade_out),
209 _envelope (other->_envelope)
211 /* Pure copy constructor */
213 set<boost::shared_ptr<AudioSource> > unique_srcs;
215 for (SourceList::const_iterator i = other->sources.begin(); i != other->sources.end(); ++i) {
216 sources.push_back (*i);
217 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
218 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
220 result = unique_srcs.insert (*i);
223 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
225 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
230 for (SourceList::const_iterator i = other->master_sources.begin(); i != other->master_sources.end(); ++i) {
231 master_sources.push_back (*i);
232 if (unique_srcs.find (*i) == unique_srcs.end()) {
233 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
237 _scale_amplitude = other->_scale_amplitude;
238 _envelope = other->_envelope;
240 _fade_in_disabled = 0;
241 _fade_out_disabled = 0;
243 listen_to_my_curves ();
246 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, const XMLNode& node)
248 _fade_in (0.0, 2.0, 1.0, false),
249 _fade_out (0.0, 2.0, 1.0, false),
250 _envelope (0.0, 2.0, 1.0, false)
252 sources.push_back (src);
253 master_sources.push_back (src);
254 src->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
256 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
258 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
261 set_default_fades ();
263 if (set_state (node)) {
264 throw failed_constructor();
267 listen_to_my_curves ();
270 AudioRegion::AudioRegion (SourceList& srcs, const XMLNode& node)
272 _fade_in (0.0, 2.0, 1.0, false),
273 _fade_out (0.0, 2.0, 1.0, false),
274 _envelope (0.0, 2.0, 1.0, false)
276 set<boost::shared_ptr<AudioSource> > unique_srcs;
278 for (SourceList::iterator i=srcs.begin(); i != srcs.end(); ++i) {
279 sources.push_back (*i);
280 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
281 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
283 result = unique_srcs.insert (*i);
286 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
288 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
293 for (SourceList::iterator i = srcs.begin(); i != srcs.end(); ++i) {
294 master_sources.push_back (*i);
295 if (unique_srcs.find (*i) == unique_srcs.end()) {
296 (*i)->GoingAway.connect (mem_fun (*this, &AudioRegion::source_deleted));
300 set_default_fades ();
301 _scale_amplitude = 1.0;
303 if (set_state (node)) {
304 throw failed_constructor();
307 listen_to_my_curves ();
310 AudioRegion::~AudioRegion ()
312 boost::shared_ptr<Playlist> pl (playlist());
315 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
316 (*i)->remove_playlist (pl);
321 GoingAway (); /* EMIT SIGNAL */
325 AudioRegion::listen_to_my_curves ()
327 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
328 _fade_in.StateChanged.connect (mem_fun (*this, &AudioRegion::fade_in_changed));
329 _fade_out.StateChanged.connect (mem_fun (*this, &AudioRegion::fade_out_changed));
333 AudioRegion::verify_length (nframes_t len)
335 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
337 if (afs && afs->destructive()) {
341 for (uint32_t n=0; n < sources.size(); ++n) {
342 if (_start > sources[n]->length() - len) {
350 AudioRegion::verify_start_and_length (nframes_t new_start, nframes_t new_length)
352 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
354 if (afs && afs->destructive()) {
358 for (uint32_t n=0; n < sources.size(); ++n) {
359 if (new_length > sources[n]->length() - new_start) {
366 AudioRegion::verify_start (nframes_t pos)
368 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
370 if (afs && afs->destructive()) {
374 for (uint32_t n=0; n < sources.size(); ++n) {
375 if (pos > sources[n]->length() - _length) {
383 AudioRegion::verify_start_mutable (nframes_t& new_start)
385 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
387 if (afs && afs->destructive()) {
391 for (uint32_t n=0; n < sources.size(); ++n) {
392 if (new_start > sources[n]->length() - _length) {
393 new_start = sources[n]->length() - _length;
399 AudioRegion::set_envelope_active (bool yn)
401 if (envelope_active() != yn) {
404 snprintf (buf, sizeof (buf), "envelope active");
405 _flags = Flag (_flags|EnvelopeActive);
407 snprintf (buf, sizeof (buf), "envelope off");
408 _flags = Flag (_flags & ~EnvelopeActive);
410 send_change (EnvelopeActiveChanged);
415 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
417 if (chan_n >= sources.size()) {
421 if (sources[chan_n]->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
424 if (_scale_amplitude != 1.0) {
425 for (nframes_t n = 0; n < npeaks; ++n) {
426 buf[n].max *= _scale_amplitude;
427 buf[n].min *= _scale_amplitude;
435 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
437 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
439 return _read_at (sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, read_frames, skip_frames);
443 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
444 nframes_t cnt, uint32_t chan_n) const
446 return _read_at (master_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0);
450 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
451 nframes_t position, nframes_t cnt,
452 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
454 nframes_t internal_offset;
455 nframes_t buf_offset;
459 return 0; /* read nothing */
462 /* precondition: caller has verified that we cover the desired section */
464 if (position < _position) {
466 buf_offset = _position - position;
469 internal_offset = position - _position;
473 if (internal_offset >= _length) {
474 return 0; /* read nothing */
477 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
478 return 0; /* read nothing */
482 /* overwrite whatever is there */
483 mixdown_buffer = buf + buf_offset;
485 mixdown_buffer += buf_offset;
488 _read_data_count = 0;
490 if (chan_n < n_channels()) {
492 if (srcs[chan_n]->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
494 return 0; /* "read nothing" */
497 _read_data_count += srcs[chan_n]->read_data_count();
501 /* track is N-channel, this region has less channels; silence the ones
505 memset (mixdown_buffer, 0, sizeof (Sample) * cnt);
507 /* no fades required */
514 if (_flags & FadeIn) {
516 nframes_t fade_in_length = (nframes_t) _fade_in.back()->when;
518 /* see if this read is within the fade in */
520 if (internal_offset < fade_in_length) {
524 limit = min (to_read, fade_in_length - internal_offset);
526 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
528 for (nframes_t n = 0; n < limit; ++n) {
529 mixdown_buffer[n] *= gain_buffer[n];
536 if (_flags & FadeOut) {
538 /* see if some part of this read is within the fade out */
540 /* ................. >| REGION
546 _length - fade_out_length
549 ^internal_offset + to_read
551 we need the intersection of [internal_offset,internal_offset+to_read] with
552 [_length - fade_out_length, _length]
557 nframes_t fade_out_length = (nframes_t) _fade_out.back()->when;
558 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
559 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
561 if (fade_interval_end > fade_interval_start) {
562 /* (part of the) the fade out is in this buffer */
564 nframes_t limit = fade_interval_end - fade_interval_start;
565 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
566 nframes_t fade_offset = fade_interval_start - internal_offset;
568 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
570 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
571 mixdown_buffer[m] *= gain_buffer[n];
577 /* Regular gain curves */
579 if (envelope_active()) {
580 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
582 if (_scale_amplitude != 1.0f) {
583 for (nframes_t n = 0; n < to_read; ++n) {
584 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
587 for (nframes_t n = 0; n < to_read; ++n) {
588 mixdown_buffer[n] *= gain_buffer[n];
591 } else if (_scale_amplitude != 1.0f) {
592 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
599 /* gack. the things we do for users.
604 for (nframes_t n = 0; n < to_read; ++n) {
605 buf[n] += mixdown_buffer[n];
613 AudioRegion::state (bool full)
615 XMLNode& node (Region::state (full));
619 LocaleGuard lg (X_("POSIX"));
621 node.add_property ("flags", enum_2_string (_flags));
623 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
624 node.add_property ("scale-gain", buf);
626 for (uint32_t n=0; n < sources.size(); ++n) {
627 snprintf (buf2, sizeof(buf2), "source-%d", n);
628 sources[n]->id().print (buf, sizeof (buf));
629 node.add_property (buf2, buf);
632 snprintf (buf, sizeof (buf), "%u", (uint32_t) sources.size());
633 node.add_property ("channels", buf);
637 child = node.add_child (X_("FadeIn"));
639 if ((_flags & DefaultFadeIn)) {
640 child->add_property (X_("default"), X_("yes"));
642 child->add_child_nocopy (_fade_in.get_state ());
645 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
647 child = node.add_child (X_("FadeOut"));
649 if ((_flags & DefaultFadeOut)) {
650 child->add_property (X_("default"), X_("yes"));
652 child->add_child_nocopy (_fade_out.get_state ());
655 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
658 child = node.add_child ("Envelope");
661 bool default_env = false;
663 // If there are only two points, the points are in the start of the region and the end of the region
664 // so, if they are both at 1.0f, that means the default region.
666 if (_envelope.size() == 2 &&
667 _envelope.front()->value == 1.0f &&
668 _envelope.back()->value==1.0f) {
669 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
675 child->add_property ("default", "yes");
677 child->add_child_nocopy (_envelope.get_state ());
681 child->add_property ("default", "yes");
684 for (uint32_t n=0; n < master_sources.size(); ++n) {
685 snprintf (buf2, sizeof(buf2), "master-source-%d", n);
686 master_sources[n]->id().print (buf, sizeof (buf));
687 node.add_property (buf2, buf);
690 if (full && _extra_xml) {
691 node.add_child_copy (*_extra_xml);
698 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
700 const XMLNodeList& nlist = node.children();
701 const XMLProperty *prop;
702 LocaleGuard lg (X_("POSIX"));
704 Region::set_live_state (node, what_changed, false);
706 uint32_t old_flags = _flags;
708 if ((prop = node.property ("flags")) != 0) {
709 _flags = Flag (string_2_enum (prop->value(), _flags));
711 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
713 _flags = Flag (_flags & ~Region::LeftOfSplit);
714 _flags = Flag (_flags & ~Region::RightOfSplit);
717 if ((old_flags ^ _flags) & Muted) {
718 what_changed = Change (what_changed|MuteChanged);
720 if ((old_flags ^ _flags) & Opaque) {
721 what_changed = Change (what_changed|OpacityChanged);
723 if ((old_flags ^ _flags) & Locked) {
724 what_changed = Change (what_changed|LockChanged);
727 if ((prop = node.property ("scale-gain")) != 0) {
728 _scale_amplitude = atof (prop->value().c_str());
729 what_changed = Change (what_changed|ScaleAmplitudeChanged);
731 _scale_amplitude = 1.0;
734 /* Now find envelope description and other misc child items */
736 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
743 if (child->name() == "Envelope") {
747 if ((prop = child->property ("default")) != 0 || _envelope.set_state (*child)) {
748 set_default_envelope ();
751 _envelope.set_max_xval (_length);
752 _envelope.truncate_end (_length);
754 } else if (child->name() == "FadeIn") {
758 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_in.set_state (*child)) {
759 set_default_fade_in ();
762 } else if (child->name() == "FadeOut") {
766 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_out.set_state (*child)) {
767 set_default_fade_out ();
773 send_change (what_changed);
780 AudioRegion::set_state (const XMLNode& node)
782 /* Region::set_state() calls the virtual set_live_state(),
783 which will get us back to AudioRegion::set_live_state()
784 to handle the relevant stuff.
787 return Region::set_state (node);
791 AudioRegion::set_fade_in_shape (FadeShape shape)
793 set_fade_in (shape, (nframes_t) _fade_in.back()->when);
797 AudioRegion::set_fade_out_shape (FadeShape shape)
799 set_fade_out (shape, (nframes_t) _fade_out.back()->when);
803 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
810 _fade_in.fast_simple_add (0.0, 0.0);
811 _fade_in.fast_simple_add (len, 1.0);
815 _fade_in.fast_simple_add (0, 0);
816 _fade_in.fast_simple_add (len * 0.389401, 0.0333333);
817 _fade_in.fast_simple_add (len * 0.629032, 0.0861111);
818 _fade_in.fast_simple_add (len * 0.829493, 0.233333);
819 _fade_in.fast_simple_add (len * 0.9447, 0.483333);
820 _fade_in.fast_simple_add (len * 0.976959, 0.697222);
821 _fade_in.fast_simple_add (len, 1);
825 _fade_in.fast_simple_add (0, 0);
826 _fade_in.fast_simple_add (len * 0.0207373, 0.197222);
827 _fade_in.fast_simple_add (len * 0.0645161, 0.525);
828 _fade_in.fast_simple_add (len * 0.152074, 0.802778);
829 _fade_in.fast_simple_add (len * 0.276498, 0.919444);
830 _fade_in.fast_simple_add (len * 0.481567, 0.980556);
831 _fade_in.fast_simple_add (len * 0.767281, 1);
832 _fade_in.fast_simple_add (len, 1);
836 _fade_in.fast_simple_add (0, 0);
837 _fade_in.fast_simple_add (len * 0.0737327, 0.308333);
838 _fade_in.fast_simple_add (len * 0.246544, 0.658333);
839 _fade_in.fast_simple_add (len * 0.470046, 0.886111);
840 _fade_in.fast_simple_add (len * 0.652074, 0.972222);
841 _fade_in.fast_simple_add (len * 0.771889, 0.988889);
842 _fade_in.fast_simple_add (len, 1);
846 _fade_in.fast_simple_add (0, 0);
847 _fade_in.fast_simple_add (len * 0.304147, 0.0694444);
848 _fade_in.fast_simple_add (len * 0.529954, 0.152778);
849 _fade_in.fast_simple_add (len * 0.725806, 0.333333);
850 _fade_in.fast_simple_add (len * 0.847926, 0.558333);
851 _fade_in.fast_simple_add (len * 0.919355, 0.730556);
852 _fade_in.fast_simple_add (len, 1);
857 _fade_in_shape = shape;
859 send_change (FadeInChanged);
863 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
870 _fade_out.fast_simple_add (len * 0, 1);
871 _fade_out.fast_simple_add (len * 0.023041, 0.697222);
872 _fade_out.fast_simple_add (len * 0.0553, 0.483333);
873 _fade_out.fast_simple_add (len * 0.170507, 0.233333);
874 _fade_out.fast_simple_add (len * 0.370968, 0.0861111);
875 _fade_out.fast_simple_add (len * 0.610599, 0.0333333);
876 _fade_out.fast_simple_add (len * 1, 0);
880 _fade_out.fast_simple_add (len * 0, 1);
881 _fade_out.fast_simple_add (len * 0.228111, 0.988889);
882 _fade_out.fast_simple_add (len * 0.347926, 0.972222);
883 _fade_out.fast_simple_add (len * 0.529954, 0.886111);
884 _fade_out.fast_simple_add (len * 0.753456, 0.658333);
885 _fade_out.fast_simple_add (len * 0.9262673, 0.308333);
886 _fade_out.fast_simple_add (len * 1, 0);
890 _fade_out.fast_simple_add (len * 0, 1);
891 _fade_out.fast_simple_add (len * 0.305556, 1);
892 _fade_out.fast_simple_add (len * 0.548611, 0.991736);
893 _fade_out.fast_simple_add (len * 0.759259, 0.931129);
894 _fade_out.fast_simple_add (len * 0.918981, 0.68595);
895 _fade_out.fast_simple_add (len * 0.976852, 0.22865);
896 _fade_out.fast_simple_add (len * 1, 0);
900 _fade_out.fast_simple_add (len * 0, 1);
901 _fade_out.fast_simple_add (len * 0.080645, 0.730556);
902 _fade_out.fast_simple_add (len * 0.277778, 0.289256);
903 _fade_out.fast_simple_add (len * 0.470046, 0.152778);
904 _fade_out.fast_simple_add (len * 0.695853, 0.0694444);
905 _fade_out.fast_simple_add (len * 1, 0);
909 _fade_out.fast_simple_add (len * 0, 1);
910 _fade_out.fast_simple_add (len * 1, 0);
915 _fade_out_shape = shape;
917 send_change (FadeOutChanged);
921 AudioRegion::set_fade_in_length (nframes_t len)
927 bool changed = _fade_in.extend_to (len);
930 _flags = Flag (_flags & ~DefaultFadeIn);
931 send_change (FadeInChanged);
936 AudioRegion::set_fade_out_length (nframes_t len)
942 bool changed = _fade_out.extend_to (len);
945 _flags = Flag (_flags & ~DefaultFadeOut);
946 send_change (FadeOutChanged);
951 AudioRegion::set_fade_in_active (bool yn)
953 if (yn == (_flags & FadeIn)) {
957 _flags = Flag (_flags|FadeIn);
959 _flags = Flag (_flags & ~FadeIn);
962 send_change (FadeInActiveChanged);
966 AudioRegion::set_fade_out_active (bool yn)
968 if (yn == (_flags & FadeOut)) {
972 _flags = Flag (_flags | FadeOut);
974 _flags = Flag (_flags & ~FadeOut);
977 send_change (FadeOutActiveChanged);
981 AudioRegion::fade_in_is_default () const
983 return _fade_in_shape == Linear && _fade_in.back()->when == 64;
987 AudioRegion::fade_out_is_default () const
989 return _fade_out_shape == Linear && _fade_out.back()->when == 64;
993 AudioRegion::set_default_fade_in ()
995 set_fade_in (Linear, 64);
999 AudioRegion::set_default_fade_out ()
1001 set_fade_out (Linear, 64);
1005 AudioRegion::set_default_fades ()
1007 _fade_in_disabled = 0;
1008 _fade_out_disabled = 0;
1009 set_default_fade_in ();
1010 set_default_fade_out ();
1014 AudioRegion::set_default_envelope ()
1016 _envelope.freeze ();
1018 _envelope.fast_simple_add (0, 1.0f);
1019 _envelope.fast_simple_add (_length, 1.0f);
1024 AudioRegion::recompute_at_end ()
1026 /* our length has changed. recompute a new final point by interpolating
1027 based on the the existing curve.
1030 _envelope.freeze ();
1031 _envelope.truncate_end (_length);
1032 _envelope.set_max_xval (_length);
1035 if (_fade_in.back()->when > _length) {
1036 _fade_in.extend_to (_length);
1037 send_change (FadeInChanged);
1040 if (_fade_out.back()->when > _length) {
1041 _fade_out.extend_to (_length);
1042 send_change (FadeOutChanged);
1047 AudioRegion::recompute_at_start ()
1049 /* as above, but the shift was from the front */
1051 _envelope.truncate_start (_length);
1053 if (_fade_in.back()->when > _length) {
1054 _fade_in.extend_to (_length);
1055 send_change (FadeInChanged);
1058 if (_fade_out.back()->when > _length) {
1059 _fade_out.extend_to (_length);
1060 send_change (FadeOutChanged);
1065 AudioRegion::separate_by_channel (Session& session, vector<boost::shared_ptr<AudioRegion> >& v) const
1071 if (sources.size() < 2) {
1077 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1080 srcs.push_back (*i);
1084 if (sources.size() == 2) {
1092 new_name += ('0' + n + 1);
1095 /* create a copy with just one source. prevent if from being thought of as "whole file" even if
1096 it covers the entire source file(s).
1099 Flag f = Flag (_flags & ~WholeFile);
1101 boost::shared_ptr<Region> r = RegionFactory::create (srcs, _start, _length, new_name, _layer, f);
1102 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
1113 AudioRegion::source_deleted ()
1120 AudioRegion::master_source_names ()
1122 SourceList::iterator i;
1124 vector<string> names;
1125 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1126 names.push_back((*i)->name());
1133 AudioRegion::set_master_sources (SourceList& srcs)
1135 master_sources = srcs;
1139 AudioRegion::source_equivalent (boost::shared_ptr<const Region> o) const
1141 boost::shared_ptr<const AudioRegion> other = boost::dynamic_pointer_cast<const AudioRegion>(o);
1146 SourceList::const_iterator i;
1147 SourceList::const_iterator io;
1149 for (i = sources.begin(), io = other->sources.begin(); i != sources.end() && io != other->sources.end(); ++i, ++io) {
1150 if ((*i)->id() != (*io)->id()) {
1155 for (i = master_sources.begin(), io = other->master_sources.begin(); i != master_sources.end() && io != other->master_sources.end(); ++i, ++io) {
1156 if ((*i)->id() != (*io)->id()) {
1165 AudioRegion::apply (AudioFilter& filter)
1167 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
1168 return filter.run (ar);
1172 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
1174 const nframes_t blocksize = 4096;
1178 spec.channels = sources.size();
1180 if (spec.prepare (blocksize, session.frame_rate())) {
1185 spec.total_frames = _length;
1187 while (spec.pos < _length && !spec.stop) {
1190 /* step 1: interleave */
1192 to_read = min (_length - spec.pos, blocksize);
1194 if (spec.channels == 1) {
1196 if (sources.front()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
1202 Sample buf[blocksize];
1204 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1206 if (sources[chan]->read (buf, _start + spec.pos, to_read) != to_read) {
1210 for (nframes_t x = 0; x < to_read; ++x) {
1211 spec.dataF[chan+(x*spec.channels)] = buf[x];
1216 if (spec.process (to_read)) {
1220 spec.pos += to_read;
1221 spec.progress = (double) spec.pos /_length;
1228 spec.running = false;
1229 spec.status = status;
1235 boost::shared_ptr<Region>
1236 AudioRegion::get_parent() const
1238 boost::shared_ptr<Playlist> pl (playlist());
1241 boost::shared_ptr<AudioRegion> ar;
1242 boost::shared_ptr<AudioRegion const> grrr2 = boost::dynamic_pointer_cast<AudioRegion const> (shared_from_this());
1244 if (grrr2 && (ar = pl->session().find_whole_file_parent (grrr2))) {
1245 return boost::static_pointer_cast<Region> (ar);
1249 return boost::shared_ptr<Region>();
1253 AudioRegion::set_scale_amplitude (gain_t g)
1255 boost::shared_ptr<Playlist> pl (playlist());
1257 _scale_amplitude = g;
1259 /* tell the diskstream we're in */
1265 /* tell everybody else */
1267 send_change (ScaleAmplitudeChanged);
1271 AudioRegion::normalize_to (float target_dB)
1273 const nframes_t blocksize = 64 * 1024;
1274 Sample buf[blocksize];
1279 gain_t target = dB_to_coefficient (target_dB);
1281 if (target == 1.0f) {
1282 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1283 that we may have clipped.
1285 target -= FLT_EPSILON;
1289 fend = _start + _length;
1291 /* first pass: find max amplitude */
1293 while (fpos < fend) {
1297 to_read = min (fend - fpos, blocksize);
1299 for (n = 0; n < n_channels(); ++n) {
1303 if (source (n)->read (buf, fpos, to_read) != to_read) {
1307 maxamp = Session::compute_peak (buf, to_read, maxamp);
1313 if (maxamp == 0.0f) {
1314 /* don't even try */
1318 if (maxamp == target) {
1319 /* we can't do anything useful */
1323 /* compute scale factor */
1325 _scale_amplitude = target/maxamp;
1327 /* tell the diskstream we're in */
1329 boost::shared_ptr<Playlist> pl (playlist());
1335 /* tell everybody else */
1337 send_change (ScaleAmplitudeChanged);
1341 AudioRegion::fade_in_changed ()
1343 send_change (FadeInChanged);
1347 AudioRegion::fade_out_changed ()
1349 send_change (FadeOutChanged);
1353 AudioRegion::envelope_changed ()
1355 send_change (EnvelopeChanged);
1359 AudioRegion::suspend_fade_in ()
1361 if (++_fade_in_disabled == 1) {
1362 if (fade_in_is_default()) {
1363 set_fade_in_active (false);
1369 AudioRegion::resume_fade_in ()
1371 if (--_fade_in_disabled == 0 && _fade_in_disabled) {
1372 set_fade_in_active (true);
1377 AudioRegion::suspend_fade_out ()
1379 if (++_fade_out_disabled == 1) {
1380 if (fade_out_is_default()) {
1381 set_fade_out_active (false);
1387 AudioRegion::resume_fade_out ()
1389 if (--_fade_out_disabled == 0 &&_fade_out_disabled) {
1390 set_fade_out_active (true);
1395 AudioRegion::speed_mismatch (float sr) const
1397 if (sources.empty()) {
1398 /* impossible, but ... */
1402 float fsr = sources.front()->sample_rate();
1408 AudioRegion::source_offset_changed ()
1410 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(sources.front());
1412 if (afs && afs->destructive()) {
1413 // set_start (source()->natural_position(), this);
1414 set_position (source()->natural_position(), this);
1419 AudioRegion::set_playlist (boost::weak_ptr<Playlist> wpl)
1421 boost::shared_ptr<Playlist> old_playlist = (_playlist.lock());
1423 boost::shared_ptr<Playlist> pl (wpl.lock());
1425 if (old_playlist == pl) {
1429 Region::set_playlist (wpl);
1433 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1434 (*i)->remove_playlist (_playlist);
1435 (*i)->add_playlist (pl);
1438 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1439 (*i)->add_playlist (pl);
1444 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1445 (*i)->remove_playlist (old_playlist);
1453 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)
1455 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1458 uint32_t region_length_from_c (void *arg)
1461 return ((AudioRegion *) arg)->length();
1464 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1466 return ( (AudioRegion *) arg)->source()->available_peaks (zoom_factor) ;