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 nframes_t maxlen = 0;
343 for (uint32_t n=0; n < sources.size(); ++n) {
344 maxlen = max (maxlen, sources[n]->length() - _start);
347 len = min (len, maxlen);
353 AudioRegion::verify_start_and_length (nframes_t new_start, nframes_t& new_length)
355 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
357 if (afs && afs->destructive()) {
361 nframes_t maxlen = 0;
363 for (uint32_t n=0; n < sources.size(); ++n) {
364 maxlen = max (maxlen, sources[n]->length() - new_start);
367 new_length = min (new_length, maxlen);
372 AudioRegion::verify_start (nframes_t pos)
374 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
376 if (afs && afs->destructive()) {
380 for (uint32_t n=0; n < sources.size(); ++n) {
381 if (pos > sources[n]->length() - _length) {
389 AudioRegion::verify_start_mutable (nframes_t& new_start)
391 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
393 if (afs && afs->destructive()) {
397 for (uint32_t n=0; n < sources.size(); ++n) {
398 if (new_start > sources[n]->length() - _length) {
399 new_start = sources[n]->length() - _length;
405 AudioRegion::set_envelope_active (bool yn)
407 if (envelope_active() != yn) {
410 snprintf (buf, sizeof (buf), "envelope active");
411 _flags = Flag (_flags|EnvelopeActive);
413 snprintf (buf, sizeof (buf), "envelope off");
414 _flags = Flag (_flags & ~EnvelopeActive);
416 send_change (EnvelopeActiveChanged);
421 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
423 if (chan_n >= sources.size()) {
427 if (sources[chan_n]->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
430 if (_scale_amplitude != 1.0) {
431 for (nframes_t n = 0; n < npeaks; ++n) {
432 buf[n].max *= _scale_amplitude;
433 buf[n].min *= _scale_amplitude;
441 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
443 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
445 return _read_at (sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, read_frames, skip_frames);
449 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
450 nframes_t cnt, uint32_t chan_n) const
452 return _read_at (master_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0);
456 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
457 nframes_t position, nframes_t cnt,
458 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
460 nframes_t internal_offset;
461 nframes_t buf_offset;
465 return 0; /* read nothing */
468 /* precondition: caller has verified that we cover the desired section */
470 if (position < _position) {
472 buf_offset = _position - position;
475 internal_offset = position - _position;
479 if (internal_offset >= _length) {
480 return 0; /* read nothing */
483 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
484 return 0; /* read nothing */
488 /* overwrite whatever is there */
489 mixdown_buffer = buf + buf_offset;
491 mixdown_buffer += buf_offset;
494 _read_data_count = 0;
496 if (chan_n < n_channels()) {
498 if (srcs[chan_n]->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
500 return 0; /* "read nothing" */
503 _read_data_count += srcs[chan_n]->read_data_count();
507 /* track is N-channel, this region has less channels; silence the ones
511 memset (mixdown_buffer, 0, sizeof (Sample) * cnt);
513 /* no fades required */
520 if (_flags & FadeIn) {
522 nframes_t fade_in_length = (nframes_t) _fade_in.back()->when;
524 /* see if this read is within the fade in */
526 if (internal_offset < fade_in_length) {
530 limit = min (to_read, fade_in_length - internal_offset);
532 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
534 for (nframes_t n = 0; n < limit; ++n) {
535 mixdown_buffer[n] *= gain_buffer[n];
542 if (_flags & FadeOut) {
544 /* see if some part of this read is within the fade out */
546 /* ................. >| REGION
552 _length - fade_out_length
555 ^internal_offset + to_read
557 we need the intersection of [internal_offset,internal_offset+to_read] with
558 [_length - fade_out_length, _length]
563 nframes_t fade_out_length = (nframes_t) _fade_out.back()->when;
564 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
565 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
567 if (fade_interval_end > fade_interval_start) {
568 /* (part of the) the fade out is in this buffer */
570 nframes_t limit = fade_interval_end - fade_interval_start;
571 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
572 nframes_t fade_offset = fade_interval_start - internal_offset;
574 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
576 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
577 mixdown_buffer[m] *= gain_buffer[n];
583 /* Regular gain curves */
585 if (envelope_active()) {
586 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
588 if (_scale_amplitude != 1.0f) {
589 for (nframes_t n = 0; n < to_read; ++n) {
590 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
593 for (nframes_t n = 0; n < to_read; ++n) {
594 mixdown_buffer[n] *= gain_buffer[n];
597 } else if (_scale_amplitude != 1.0f) {
598 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
605 /* gack. the things we do for users.
610 for (nframes_t n = 0; n < to_read; ++n) {
611 buf[n] += mixdown_buffer[n];
619 AudioRegion::state (bool full)
621 XMLNode& node (Region::state (full));
625 LocaleGuard lg (X_("POSIX"));
627 node.add_property ("flags", enum_2_string (_flags));
629 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
630 node.add_property ("scale-gain", buf);
632 for (uint32_t n=0; n < sources.size(); ++n) {
633 snprintf (buf2, sizeof(buf2), "source-%d", n);
634 sources[n]->id().print (buf, sizeof (buf));
635 node.add_property (buf2, buf);
638 snprintf (buf, sizeof (buf), "%u", (uint32_t) sources.size());
639 node.add_property ("channels", buf);
643 child = node.add_child (X_("FadeIn"));
645 if ((_flags & DefaultFadeIn)) {
646 child->add_property (X_("default"), X_("yes"));
648 child->add_child_nocopy (_fade_in.get_state ());
651 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
653 child = node.add_child (X_("FadeOut"));
655 if ((_flags & DefaultFadeOut)) {
656 child->add_property (X_("default"), X_("yes"));
658 child->add_child_nocopy (_fade_out.get_state ());
661 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
664 child = node.add_child ("Envelope");
667 bool default_env = false;
669 // If there are only two points, the points are in the start of the region and the end of the region
670 // so, if they are both at 1.0f, that means the default region.
672 if (_envelope.size() == 2 &&
673 _envelope.front()->value == 1.0f &&
674 _envelope.back()->value==1.0f) {
675 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
681 child->add_property ("default", "yes");
683 child->add_child_nocopy (_envelope.get_state ());
687 child->add_property ("default", "yes");
690 for (uint32_t n=0; n < master_sources.size(); ++n) {
691 snprintf (buf2, sizeof(buf2), "master-source-%d", n);
692 master_sources[n]->id().print (buf, sizeof (buf));
693 node.add_property (buf2, buf);
696 if (full && _extra_xml) {
697 node.add_child_copy (*_extra_xml);
704 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
706 const XMLNodeList& nlist = node.children();
707 const XMLProperty *prop;
708 LocaleGuard lg (X_("POSIX"));
710 Region::set_live_state (node, what_changed, false);
712 uint32_t old_flags = _flags;
714 if ((prop = node.property ("flags")) != 0) {
715 _flags = Flag (string_2_enum (prop->value(), _flags));
717 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
719 _flags = Flag (_flags & ~Region::LeftOfSplit);
720 _flags = Flag (_flags & ~Region::RightOfSplit);
723 if ((old_flags ^ _flags) & Muted) {
724 what_changed = Change (what_changed|MuteChanged);
726 if ((old_flags ^ _flags) & Opaque) {
727 what_changed = Change (what_changed|OpacityChanged);
729 if ((old_flags ^ _flags) & Locked) {
730 what_changed = Change (what_changed|LockChanged);
733 if ((prop = node.property ("scale-gain")) != 0) {
734 _scale_amplitude = atof (prop->value().c_str());
735 what_changed = Change (what_changed|ScaleAmplitudeChanged);
737 _scale_amplitude = 1.0;
740 /* Now find envelope description and other misc child items */
742 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
749 if (child->name() == "Envelope") {
753 if ((prop = child->property ("default")) != 0 || _envelope.set_state (*child)) {
754 set_default_envelope ();
757 _envelope.set_max_xval (_length);
758 _envelope.truncate_end (_length);
760 } else if (child->name() == "FadeIn") {
764 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
765 set_default_fade_in ();
767 XMLNode* grandchild = child->child ("AutomationList");
769 _fade_in.set_state (*grandchild);
773 if ((prop = child->property ("active")) != 0) {
774 if (prop->value() == "yes") {
775 set_fade_in_active (true);
777 set_fade_in_active (true);
781 } else if (child->name() == "FadeOut") {
785 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
786 set_default_fade_out ();
788 XMLNode* grandchild = child->child ("AutomationList");
790 _fade_out.set_state (*grandchild);
794 if ((prop = child->property ("active")) != 0) {
795 if (prop->value() == "yes") {
796 set_fade_out_active (true);
798 set_fade_out_active (false);
806 send_change (what_changed);
813 AudioRegion::set_state (const XMLNode& node)
815 /* Region::set_state() calls the virtual set_live_state(),
816 which will get us back to AudioRegion::set_live_state()
817 to handle the relevant stuff.
820 return Region::set_state (node);
824 AudioRegion::set_fade_in_shape (FadeShape shape)
826 set_fade_in (shape, (nframes_t) _fade_in.back()->when);
830 AudioRegion::set_fade_out_shape (FadeShape shape)
832 set_fade_out (shape, (nframes_t) _fade_out.back()->when);
836 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
843 _fade_in.fast_simple_add (0.0, 0.0);
844 _fade_in.fast_simple_add (len, 1.0);
848 _fade_in.fast_simple_add (0, 0);
849 _fade_in.fast_simple_add (len * 0.389401, 0.0333333);
850 _fade_in.fast_simple_add (len * 0.629032, 0.0861111);
851 _fade_in.fast_simple_add (len * 0.829493, 0.233333);
852 _fade_in.fast_simple_add (len * 0.9447, 0.483333);
853 _fade_in.fast_simple_add (len * 0.976959, 0.697222);
854 _fade_in.fast_simple_add (len, 1);
858 _fade_in.fast_simple_add (0, 0);
859 _fade_in.fast_simple_add (len * 0.0207373, 0.197222);
860 _fade_in.fast_simple_add (len * 0.0645161, 0.525);
861 _fade_in.fast_simple_add (len * 0.152074, 0.802778);
862 _fade_in.fast_simple_add (len * 0.276498, 0.919444);
863 _fade_in.fast_simple_add (len * 0.481567, 0.980556);
864 _fade_in.fast_simple_add (len * 0.767281, 1);
865 _fade_in.fast_simple_add (len, 1);
869 _fade_in.fast_simple_add (0, 0);
870 _fade_in.fast_simple_add (len * 0.0737327, 0.308333);
871 _fade_in.fast_simple_add (len * 0.246544, 0.658333);
872 _fade_in.fast_simple_add (len * 0.470046, 0.886111);
873 _fade_in.fast_simple_add (len * 0.652074, 0.972222);
874 _fade_in.fast_simple_add (len * 0.771889, 0.988889);
875 _fade_in.fast_simple_add (len, 1);
879 _fade_in.fast_simple_add (0, 0);
880 _fade_in.fast_simple_add (len * 0.304147, 0.0694444);
881 _fade_in.fast_simple_add (len * 0.529954, 0.152778);
882 _fade_in.fast_simple_add (len * 0.725806, 0.333333);
883 _fade_in.fast_simple_add (len * 0.847926, 0.558333);
884 _fade_in.fast_simple_add (len * 0.919355, 0.730556);
885 _fade_in.fast_simple_add (len, 1);
890 _fade_in_shape = shape;
892 send_change (FadeInChanged);
896 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
903 _fade_out.fast_simple_add (len * 0, 1);
904 _fade_out.fast_simple_add (len * 0.023041, 0.697222);
905 _fade_out.fast_simple_add (len * 0.0553, 0.483333);
906 _fade_out.fast_simple_add (len * 0.170507, 0.233333);
907 _fade_out.fast_simple_add (len * 0.370968, 0.0861111);
908 _fade_out.fast_simple_add (len * 0.610599, 0.0333333);
909 _fade_out.fast_simple_add (len * 1, 0);
913 _fade_out.fast_simple_add (len * 0, 1);
914 _fade_out.fast_simple_add (len * 0.228111, 0.988889);
915 _fade_out.fast_simple_add (len * 0.347926, 0.972222);
916 _fade_out.fast_simple_add (len * 0.529954, 0.886111);
917 _fade_out.fast_simple_add (len * 0.753456, 0.658333);
918 _fade_out.fast_simple_add (len * 0.9262673, 0.308333);
919 _fade_out.fast_simple_add (len * 1, 0);
923 _fade_out.fast_simple_add (len * 0, 1);
924 _fade_out.fast_simple_add (len * 0.305556, 1);
925 _fade_out.fast_simple_add (len * 0.548611, 0.991736);
926 _fade_out.fast_simple_add (len * 0.759259, 0.931129);
927 _fade_out.fast_simple_add (len * 0.918981, 0.68595);
928 _fade_out.fast_simple_add (len * 0.976852, 0.22865);
929 _fade_out.fast_simple_add (len * 1, 0);
933 _fade_out.fast_simple_add (len * 0, 1);
934 _fade_out.fast_simple_add (len * 0.080645, 0.730556);
935 _fade_out.fast_simple_add (len * 0.277778, 0.289256);
936 _fade_out.fast_simple_add (len * 0.470046, 0.152778);
937 _fade_out.fast_simple_add (len * 0.695853, 0.0694444);
938 _fade_out.fast_simple_add (len * 1, 0);
942 _fade_out.fast_simple_add (len * 0, 1);
943 _fade_out.fast_simple_add (len * 1, 0);
948 _fade_out_shape = shape;
950 send_change (FadeOutChanged);
954 AudioRegion::set_fade_in_length (nframes_t len)
960 bool changed = _fade_in.extend_to (len);
963 _flags = Flag (_flags & ~DefaultFadeIn);
964 send_change (FadeInChanged);
969 AudioRegion::set_fade_out_length (nframes_t len)
975 bool changed = _fade_out.extend_to (len);
978 _flags = Flag (_flags & ~DefaultFadeOut);
979 send_change (FadeOutChanged);
984 AudioRegion::set_fade_in_active (bool yn)
986 if (yn == (_flags & FadeIn)) {
990 _flags = Flag (_flags|FadeIn);
992 _flags = Flag (_flags & ~FadeIn);
995 send_change (FadeInActiveChanged);
999 AudioRegion::set_fade_out_active (bool yn)
1001 if (yn == (_flags & FadeOut)) {
1005 _flags = Flag (_flags | FadeOut);
1007 _flags = Flag (_flags & ~FadeOut);
1010 send_change (FadeOutActiveChanged);
1014 AudioRegion::fade_in_is_default () const
1016 return _fade_in_shape == Linear && _fade_in.back()->when == 64;
1020 AudioRegion::fade_out_is_default () const
1022 return _fade_out_shape == Linear && _fade_out.back()->when == 64;
1026 AudioRegion::set_default_fade_in ()
1028 set_fade_in (Linear, 64);
1032 AudioRegion::set_default_fade_out ()
1034 set_fade_out (Linear, 64);
1038 AudioRegion::set_default_fades ()
1040 _fade_in_disabled = 0;
1041 _fade_out_disabled = 0;
1042 set_default_fade_in ();
1043 set_default_fade_out ();
1047 AudioRegion::set_default_envelope ()
1049 _envelope.freeze ();
1051 _envelope.fast_simple_add (0, 1.0f);
1052 _envelope.fast_simple_add (_length, 1.0f);
1057 AudioRegion::recompute_at_end ()
1059 /* our length has changed. recompute a new final point by interpolating
1060 based on the the existing curve.
1063 _envelope.freeze ();
1064 _envelope.truncate_end (_length);
1065 _envelope.set_max_xval (_length);
1068 if (_fade_in.back()->when > _length) {
1069 _fade_in.extend_to (_length);
1070 send_change (FadeInChanged);
1073 if (_fade_out.back()->when > _length) {
1074 _fade_out.extend_to (_length);
1075 send_change (FadeOutChanged);
1080 AudioRegion::recompute_at_start ()
1082 /* as above, but the shift was from the front */
1084 _envelope.truncate_start (_length);
1086 if (_fade_in.back()->when > _length) {
1087 _fade_in.extend_to (_length);
1088 send_change (FadeInChanged);
1091 if (_fade_out.back()->when > _length) {
1092 _fade_out.extend_to (_length);
1093 send_change (FadeOutChanged);
1098 AudioRegion::separate_by_channel (Session& session, vector<boost::shared_ptr<AudioRegion> >& v) const
1104 if (sources.size() < 2) {
1110 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1113 srcs.push_back (*i);
1117 if (sources.size() == 2) {
1125 new_name += ('0' + n + 1);
1128 /* create a copy with just one source. prevent if from being thought of as "whole file" even if
1129 it covers the entire source file(s).
1132 Flag f = Flag (_flags & ~WholeFile);
1134 boost::shared_ptr<Region> r = RegionFactory::create (srcs, _start, _length, new_name, _layer, f);
1135 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
1146 AudioRegion::source_deleted ()
1153 AudioRegion::master_source_names ()
1155 SourceList::iterator i;
1157 vector<string> names;
1158 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1159 names.push_back((*i)->name());
1166 AudioRegion::set_master_sources (SourceList& srcs)
1168 master_sources = srcs;
1172 AudioRegion::source_equivalent (boost::shared_ptr<const Region> o) const
1174 boost::shared_ptr<const AudioRegion> other = boost::dynamic_pointer_cast<const AudioRegion>(o);
1179 SourceList::const_iterator i;
1180 SourceList::const_iterator io;
1182 for (i = sources.begin(), io = other->sources.begin(); i != sources.end() && io != other->sources.end(); ++i, ++io) {
1183 if ((*i)->id() != (*io)->id()) {
1188 for (i = master_sources.begin(), io = other->master_sources.begin(); i != master_sources.end() && io != other->master_sources.end(); ++i, ++io) {
1189 if ((*i)->id() != (*io)->id()) {
1198 AudioRegion::apply (AudioFilter& filter)
1200 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
1201 return filter.run (ar);
1205 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
1207 const nframes_t blocksize = 4096;
1211 spec.channels = sources.size();
1213 if (spec.prepare (blocksize, session.frame_rate())) {
1218 spec.total_frames = _length;
1220 while (spec.pos < _length && !spec.stop) {
1223 /* step 1: interleave */
1225 to_read = min (_length - spec.pos, blocksize);
1227 if (spec.channels == 1) {
1229 if (sources.front()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
1235 Sample buf[blocksize];
1237 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1239 if (sources[chan]->read (buf, _start + spec.pos, to_read) != to_read) {
1243 for (nframes_t x = 0; x < to_read; ++x) {
1244 spec.dataF[chan+(x*spec.channels)] = buf[x];
1249 if (spec.process (to_read)) {
1253 spec.pos += to_read;
1254 spec.progress = (double) spec.pos /_length;
1261 spec.running = false;
1262 spec.status = status;
1268 boost::shared_ptr<Region>
1269 AudioRegion::get_parent() const
1271 boost::shared_ptr<Playlist> pl (playlist());
1274 boost::shared_ptr<AudioRegion> ar;
1275 boost::shared_ptr<AudioRegion const> grrr2 = boost::dynamic_pointer_cast<AudioRegion const> (shared_from_this());
1277 if (grrr2 && (ar = pl->session().find_whole_file_parent (grrr2))) {
1278 return boost::static_pointer_cast<Region> (ar);
1282 return boost::shared_ptr<Region>();
1286 AudioRegion::set_scale_amplitude (gain_t g)
1288 boost::shared_ptr<Playlist> pl (playlist());
1290 _scale_amplitude = g;
1292 /* tell the diskstream we're in */
1298 /* tell everybody else */
1300 send_change (ScaleAmplitudeChanged);
1304 AudioRegion::normalize_to (float target_dB)
1306 const nframes_t blocksize = 64 * 1024;
1307 Sample buf[blocksize];
1312 gain_t target = dB_to_coefficient (target_dB);
1314 if (target == 1.0f) {
1315 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1316 that we may have clipped.
1318 target -= FLT_EPSILON;
1322 fend = _start + _length;
1324 /* first pass: find max amplitude */
1326 while (fpos < fend) {
1330 to_read = min (fend - fpos, blocksize);
1332 for (n = 0; n < n_channels(); ++n) {
1336 if (source (n)->read (buf, fpos, to_read) != to_read) {
1340 maxamp = Session::compute_peak (buf, to_read, maxamp);
1346 if (maxamp == 0.0f) {
1347 /* don't even try */
1351 if (maxamp == target) {
1352 /* we can't do anything useful */
1356 /* compute scale factor */
1358 _scale_amplitude = target/maxamp;
1360 /* tell the diskstream we're in */
1362 boost::shared_ptr<Playlist> pl (playlist());
1368 /* tell everybody else */
1370 send_change (ScaleAmplitudeChanged);
1374 AudioRegion::fade_in_changed ()
1376 send_change (FadeInChanged);
1380 AudioRegion::fade_out_changed ()
1382 send_change (FadeOutChanged);
1386 AudioRegion::envelope_changed ()
1388 send_change (EnvelopeChanged);
1392 AudioRegion::suspend_fade_in ()
1394 if (++_fade_in_disabled == 1) {
1395 if (fade_in_is_default()) {
1396 set_fade_in_active (false);
1402 AudioRegion::resume_fade_in ()
1404 if (--_fade_in_disabled == 0 && _fade_in_disabled) {
1405 set_fade_in_active (true);
1410 AudioRegion::suspend_fade_out ()
1412 if (++_fade_out_disabled == 1) {
1413 if (fade_out_is_default()) {
1414 set_fade_out_active (false);
1420 AudioRegion::resume_fade_out ()
1422 if (--_fade_out_disabled == 0 &&_fade_out_disabled) {
1423 set_fade_out_active (true);
1428 AudioRegion::speed_mismatch (float sr) const
1430 if (sources.empty()) {
1431 /* impossible, but ... */
1435 float fsr = sources.front()->sample_rate();
1441 AudioRegion::source_offset_changed ()
1443 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(sources.front());
1445 if (afs && afs->destructive()) {
1446 // set_start (source()->natural_position(), this);
1447 set_position (source()->natural_position(), this);
1452 AudioRegion::set_playlist (boost::weak_ptr<Playlist> wpl)
1454 boost::shared_ptr<Playlist> old_playlist = (_playlist.lock());
1456 boost::shared_ptr<Playlist> pl (wpl.lock());
1458 if (old_playlist == pl) {
1462 Region::set_playlist (wpl);
1466 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1467 (*i)->remove_playlist (_playlist);
1468 (*i)->add_playlist (pl);
1471 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1472 (*i)->add_playlist (pl);
1477 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1478 (*i)->remove_playlist (old_playlist);
1486 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)
1488 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1491 uint32_t region_length_from_c (void *arg)
1494 return ((AudioRegion *) arg)->length();
1497 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1499 return ( (AudioRegion *) arg)->source()->available_peaks (zoom_factor) ;