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>
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/region_factory.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 listen_to_my_curves ();
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 listen_to_my_curves ();
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 listen_to_my_curves ();
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 listen_to_my_curves ();
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 listen_to_my_curves ();
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 listen_to_my_curves ();
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 listen_to_my_curves ();
307 AudioRegion::~AudioRegion ()
309 boost::shared_ptr<Playlist> pl (playlist());
312 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
313 (*i)->remove_playlist (pl);
318 GoingAway (); /* EMIT SIGNAL */
322 AudioRegion::listen_to_my_curves ()
324 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
325 _fade_in.StateChanged.connect (mem_fun (*this, &AudioRegion::fade_in_changed));
326 _fade_out.StateChanged.connect (mem_fun (*this, &AudioRegion::fade_out_changed));
330 AudioRegion::verify_length (nframes_t len)
332 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
334 if (afs && afs->destructive()) {
338 for (uint32_t n=0; n < sources.size(); ++n) {
339 if (_start > sources[n]->length() - len) {
347 AudioRegion::verify_start_and_length (nframes_t new_start, nframes_t new_length)
349 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
351 if (afs && afs->destructive()) {
355 for (uint32_t n=0; n < sources.size(); ++n) {
356 if (new_length > sources[n]->length() - new_start) {
363 AudioRegion::verify_start (nframes_t pos)
365 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
367 if (afs && afs->destructive()) {
371 for (uint32_t n=0; n < sources.size(); ++n) {
372 if (pos > sources[n]->length() - _length) {
380 AudioRegion::verify_start_mutable (nframes_t& new_start)
382 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
384 if (afs && afs->destructive()) {
388 for (uint32_t n=0; n < sources.size(); ++n) {
389 if (new_start > sources[n]->length() - _length) {
390 new_start = sources[n]->length() - _length;
396 AudioRegion::set_envelope_active (bool yn)
398 if (envelope_active() != yn) {
401 snprintf (buf, sizeof (buf), "envelope active");
402 _flags = Flag (_flags|EnvelopeActive);
404 snprintf (buf, sizeof (buf), "envelope off");
405 _flags = Flag (_flags & ~EnvelopeActive);
407 send_change (EnvelopeActiveChanged);
412 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
414 if (chan_n >= sources.size()) {
418 if (sources[chan_n]->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
421 if (_scale_amplitude != 1.0) {
422 for (nframes_t n = 0; n < npeaks; ++n) {
423 buf[n].max *= _scale_amplitude;
424 buf[n].min *= _scale_amplitude;
432 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
434 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
436 return _read_at (sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, read_frames, skip_frames);
440 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
441 nframes_t cnt, uint32_t chan_n) const
443 return _read_at (master_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0);
447 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
448 nframes_t position, nframes_t cnt,
449 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
451 nframes_t internal_offset;
452 nframes_t buf_offset;
455 /* precondition: caller has verified that we cover the desired section */
457 if (chan_n >= sources.size()) {
458 return 0; /* read nothing */
461 if (position < _position) {
463 buf_offset = _position - position;
466 internal_offset = position - _position;
470 if (internal_offset >= _length) {
471 return 0; /* read nothing */
474 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
475 return 0; /* read nothing */
479 /* overwrite whatever is there */
480 mixdown_buffer = buf + buf_offset;
482 mixdown_buffer += buf_offset;
486 return 0; /* read nothing */
489 _read_data_count = 0;
491 if (srcs[chan_n]->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
492 return 0; /* "read nothing" */
495 _read_data_count += srcs[chan_n]->read_data_count();
499 if (_flags & FadeIn) {
501 nframes_t fade_in_length = (nframes_t) _fade_in.back()->when;
503 /* see if this read is within the fade in */
505 if (internal_offset < fade_in_length) {
509 limit = min (to_read, fade_in_length - internal_offset);
511 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
513 for (nframes_t n = 0; n < limit; ++n) {
514 mixdown_buffer[n] *= gain_buffer[n];
521 if (_flags & FadeOut) {
523 /* see if some part of this read is within the fade out */
525 /* ................. >| REGION
531 _length - fade_out_length
534 ^internal_offset + to_read
536 we need the intersection of [internal_offset,internal_offset+to_read] with
537 [_length - fade_out_length, _length]
542 nframes_t fade_out_length = (nframes_t) _fade_out.back()->when;
543 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
544 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
546 if (fade_interval_end > fade_interval_start) {
547 /* (part of the) the fade out is in this buffer */
549 nframes_t limit = fade_interval_end - fade_interval_start;
550 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
551 nframes_t fade_offset = fade_interval_start - internal_offset;
553 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
555 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
556 mixdown_buffer[m] *= gain_buffer[n];
562 /* Regular gain curves */
564 if (envelope_active()) {
565 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
567 if (_scale_amplitude != 1.0f) {
568 for (nframes_t n = 0; n < to_read; ++n) {
569 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
572 for (nframes_t n = 0; n < to_read; ++n) {
573 mixdown_buffer[n] *= gain_buffer[n];
576 } else if (_scale_amplitude != 1.0f) {
577 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
582 /* gack. the things we do for users.
587 for (nframes_t n = 0; n < to_read; ++n) {
588 buf[n] += mixdown_buffer[n];
596 AudioRegion::state (bool full)
598 XMLNode& node (Region::state (full));
602 LocaleGuard lg (X_("POSIX"));
604 node.add_property ("flags", enum_2_string (_flags));
606 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
607 node.add_property ("scale-gain", buf);
609 for (uint32_t n=0; n < sources.size(); ++n) {
610 snprintf (buf2, sizeof(buf2), "source-%d", n);
611 sources[n]->id().print (buf, sizeof (buf));
612 node.add_property (buf2, buf);
615 snprintf (buf, sizeof (buf), "%u", (uint32_t) sources.size());
616 node.add_property ("channels", buf);
620 child = node.add_child (X_("FadeIn"));
622 if ((_flags & DefaultFadeIn)) {
623 child->add_property (X_("default"), X_("yes"));
625 child->add_child_nocopy (_fade_in.get_state ());
628 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
630 child = node.add_child (X_("FadeOut"));
632 if ((_flags & DefaultFadeOut)) {
633 child->add_property (X_("default"), X_("yes"));
635 child->add_child_nocopy (_fade_out.get_state ());
638 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
641 child = node.add_child ("Envelope");
644 bool default_env = false;
646 // If there are only two points, the points are in the start of the region and the end of the region
647 // so, if they are both at 1.0f, that means the default region.
649 if (_envelope.size() == 2 &&
650 _envelope.front()->value == 1.0f &&
651 _envelope.back()->value==1.0f) {
652 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
658 child->add_property ("default", "yes");
660 child->add_child_nocopy (_envelope.get_state ());
664 child->add_property ("default", "yes");
667 if (full && _extra_xml) {
668 node.add_child_copy (*_extra_xml);
675 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
677 const XMLNodeList& nlist = node.children();
678 const XMLProperty *prop;
679 LocaleGuard lg (X_("POSIX"));
681 Region::set_live_state (node, what_changed, false);
683 uint32_t old_flags = _flags;
685 if ((prop = node.property ("flags")) != 0) {
686 _flags = Flag (string_2_enum (prop->value(), _flags));
688 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
690 _flags = Flag (_flags & ~Region::LeftOfSplit);
691 _flags = Flag (_flags & ~Region::RightOfSplit);
694 if ((old_flags ^ _flags) & Muted) {
695 what_changed = Change (what_changed|MuteChanged);
697 if ((old_flags ^ _flags) & Opaque) {
698 what_changed = Change (what_changed|OpacityChanged);
700 if ((old_flags ^ _flags) & Locked) {
701 what_changed = Change (what_changed|LockChanged);
704 if ((prop = node.property ("scale-gain")) != 0) {
705 _scale_amplitude = atof (prop->value().c_str());
707 _scale_amplitude = 1.0;
710 /* Now find envelope description and other misc child items */
712 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
719 if (child->name() == "Envelope") {
723 if ((prop = child->property ("default")) != 0 || _envelope.set_state (*child)) {
724 set_default_envelope ();
727 _envelope.set_max_xval (_length);
728 _envelope.truncate_end (_length);
730 } else if (child->name() == "FadeIn") {
734 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_in.set_state (*child)) {
735 set_default_fade_in ();
738 } else if (child->name() == "FadeOut") {
742 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_out.set_state (*child)) {
743 set_default_fade_out ();
749 send_change (what_changed);
756 AudioRegion::set_state (const XMLNode& node)
758 /* Region::set_state() calls the virtual set_live_state(),
759 which will get us back to AudioRegion::set_live_state()
760 to handle the relevant stuff.
763 return Region::set_state (node);
767 AudioRegion::set_fade_in_shape (FadeShape shape)
769 set_fade_in (shape, (nframes_t) _fade_in.back()->when);
773 AudioRegion::set_fade_out_shape (FadeShape shape)
775 set_fade_out (shape, (nframes_t) _fade_out.back()->when);
779 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
786 _fade_in.fast_simple_add (0.0, 0.0);
787 _fade_in.fast_simple_add (len, 1.0);
791 _fade_in.fast_simple_add (0, 0);
792 _fade_in.fast_simple_add (len * 0.389401, 0.0333333);
793 _fade_in.fast_simple_add (len * 0.629032, 0.0861111);
794 _fade_in.fast_simple_add (len * 0.829493, 0.233333);
795 _fade_in.fast_simple_add (len * 0.9447, 0.483333);
796 _fade_in.fast_simple_add (len * 0.976959, 0.697222);
797 _fade_in.fast_simple_add (len, 1);
801 _fade_in.fast_simple_add (0, 0);
802 _fade_in.fast_simple_add (len * 0.0207373, 0.197222);
803 _fade_in.fast_simple_add (len * 0.0645161, 0.525);
804 _fade_in.fast_simple_add (len * 0.152074, 0.802778);
805 _fade_in.fast_simple_add (len * 0.276498, 0.919444);
806 _fade_in.fast_simple_add (len * 0.481567, 0.980556);
807 _fade_in.fast_simple_add (len * 0.767281, 1);
808 _fade_in.fast_simple_add (len, 1);
812 _fade_in.fast_simple_add (0, 0);
813 _fade_in.fast_simple_add (len * 0.0737327, 0.308333);
814 _fade_in.fast_simple_add (len * 0.246544, 0.658333);
815 _fade_in.fast_simple_add (len * 0.470046, 0.886111);
816 _fade_in.fast_simple_add (len * 0.652074, 0.972222);
817 _fade_in.fast_simple_add (len * 0.771889, 0.988889);
818 _fade_in.fast_simple_add (len, 1);
822 _fade_in.fast_simple_add (0, 0);
823 _fade_in.fast_simple_add (len * 0.304147, 0.0694444);
824 _fade_in.fast_simple_add (len * 0.529954, 0.152778);
825 _fade_in.fast_simple_add (len * 0.725806, 0.333333);
826 _fade_in.fast_simple_add (len * 0.847926, 0.558333);
827 _fade_in.fast_simple_add (len * 0.919355, 0.730556);
828 _fade_in.fast_simple_add (len, 1);
833 _fade_in_shape = shape;
835 send_change (FadeInChanged);
839 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
846 _fade_out.fast_simple_add (len * 0, 1);
847 _fade_out.fast_simple_add (len * 0.023041, 0.697222);
848 _fade_out.fast_simple_add (len * 0.0553, 0.483333);
849 _fade_out.fast_simple_add (len * 0.170507, 0.233333);
850 _fade_out.fast_simple_add (len * 0.370968, 0.0861111);
851 _fade_out.fast_simple_add (len * 0.610599, 0.0333333);
852 _fade_out.fast_simple_add (len * 1, 0);
856 _fade_out.fast_simple_add (len * 0, 1);
857 _fade_out.fast_simple_add (len * 0.228111, 0.988889);
858 _fade_out.fast_simple_add (len * 0.347926, 0.972222);
859 _fade_out.fast_simple_add (len * 0.529954, 0.886111);
860 _fade_out.fast_simple_add (len * 0.753456, 0.658333);
861 _fade_out.fast_simple_add (len * 0.9262673, 0.308333);
862 _fade_out.fast_simple_add (len * 1, 0);
866 _fade_out.fast_simple_add (len * 0, 1);
867 _fade_out.fast_simple_add (len * 0.305556, 1);
868 _fade_out.fast_simple_add (len * 0.548611, 0.991736);
869 _fade_out.fast_simple_add (len * 0.759259, 0.931129);
870 _fade_out.fast_simple_add (len * 0.918981, 0.68595);
871 _fade_out.fast_simple_add (len * 0.976852, 0.22865);
872 _fade_out.fast_simple_add (len * 1, 0);
876 _fade_out.fast_simple_add (len * 0, 1);
877 _fade_out.fast_simple_add (len * 0.080645, 0.730556);
878 _fade_out.fast_simple_add (len * 0.277778, 0.289256);
879 _fade_out.fast_simple_add (len * 0.470046, 0.152778);
880 _fade_out.fast_simple_add (len * 0.695853, 0.0694444);
881 _fade_out.fast_simple_add (len * 1, 0);
885 _fade_out.fast_simple_add (len * 0, 1);
886 _fade_out.fast_simple_add (len * 1, 0);
891 _fade_out_shape = shape;
893 send_change (FadeOutChanged);
897 AudioRegion::set_fade_in_length (nframes_t len)
899 bool changed = _fade_in.extend_to (len);
902 _flags = Flag (_flags & ~DefaultFadeIn);
903 send_change (FadeInChanged);
908 AudioRegion::set_fade_out_length (nframes_t len)
910 bool changed = _fade_out.extend_to (len);
913 _flags = Flag (_flags & ~DefaultFadeOut);
916 send_change (FadeOutChanged);
920 AudioRegion::set_fade_in_active (bool yn)
922 if (yn == (_flags & FadeIn)) {
926 _flags = Flag (_flags|FadeIn);
928 _flags = Flag (_flags & ~FadeIn);
931 send_change (FadeInActiveChanged);
935 AudioRegion::set_fade_out_active (bool yn)
937 if (yn == (_flags & FadeOut)) {
941 _flags = Flag (_flags | FadeOut);
943 _flags = Flag (_flags & ~FadeOut);
946 send_change (FadeOutActiveChanged);
950 AudioRegion::set_default_fade_in ()
952 set_fade_in (Linear, 64);
956 AudioRegion::set_default_fade_out ()
958 set_fade_out (Linear, 64);
962 AudioRegion::set_default_fades ()
964 _fade_in_disabled = 0;
965 _fade_out_disabled = 0;
966 set_default_fade_in ();
967 set_default_fade_out ();
971 AudioRegion::set_default_envelope ()
975 _envelope.fast_simple_add (0, 1.0f);
976 _envelope.fast_simple_add (_length, 1.0f);
981 AudioRegion::recompute_at_end ()
983 /* our length has changed. recompute a new final point by interpolating
984 based on the the existing curve.
988 _envelope.truncate_end (_length);
989 _envelope.set_max_xval (_length);
992 if (_fade_in.back()->when > _length) {
993 _fade_in.extend_to (_length);
994 send_change (FadeInChanged);
997 if (_fade_out.back()->when > _length) {
998 _fade_out.extend_to (_length);
999 send_change (FadeOutChanged);
1004 AudioRegion::recompute_at_start ()
1006 /* as above, but the shift was from the front */
1008 _envelope.truncate_start (_length);
1010 if (_fade_in.back()->when > _length) {
1011 _fade_in.extend_to (_length);
1012 send_change (FadeInChanged);
1015 if (_fade_out.back()->when > _length) {
1016 _fade_out.extend_to (_length);
1017 send_change (FadeOutChanged);
1022 AudioRegion::separate_by_channel (Session& session, vector<boost::shared_ptr<AudioRegion> >& v) const
1028 if (sources.size() < 2) {
1034 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1037 srcs.push_back (*i);
1041 if (sources.size() == 2) {
1049 new_name += ('0' + n + 1);
1052 /* create a copy with just one source. prevent if from being thought of as "whole file" even if
1053 it covers the entire source file(s).
1056 Flag f = Flag (_flags & ~WholeFile);
1058 boost::shared_ptr<Region> r = RegionFactory::create (srcs, _start, _length, new_name, _layer, f);
1059 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
1070 AudioRegion::source_deleted ()
1077 AudioRegion::master_source_names ()
1079 SourceList::iterator i;
1081 vector<string> names;
1082 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1083 names.push_back((*i)->name());
1090 AudioRegion::source_equivalent (boost::shared_ptr<const Region> o) const
1092 boost::shared_ptr<const AudioRegion> other = boost::dynamic_pointer_cast<const AudioRegion>(o);
1097 SourceList::const_iterator i;
1098 SourceList::const_iterator io;
1100 for (i = sources.begin(), io = other->sources.begin(); i != sources.end() && io != other->sources.end(); ++i, ++io) {
1101 if ((*i)->id() != (*io)->id()) {
1106 for (i = master_sources.begin(), io = other->master_sources.begin(); i != master_sources.end() && io != other->master_sources.end(); ++i, ++io) {
1107 if ((*i)->id() != (*io)->id()) {
1116 AudioRegion::apply (AudioFilter& filter)
1118 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
1119 return filter.run (ar);
1123 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
1125 const nframes_t blocksize = 4096;
1129 spec.channels = sources.size();
1131 if (spec.prepare (blocksize, session.frame_rate())) {
1136 spec.total_frames = _length;
1138 while (spec.pos < _length && !spec.stop) {
1141 /* step 1: interleave */
1143 to_read = min (_length - spec.pos, blocksize);
1145 if (spec.channels == 1) {
1147 if (sources.front()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
1153 Sample buf[blocksize];
1155 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1157 if (sources[chan]->read (buf, _start + spec.pos, to_read) != to_read) {
1161 for (nframes_t x = 0; x < to_read; ++x) {
1162 spec.dataF[chan+(x*spec.channels)] = buf[x];
1167 if (spec.process (to_read)) {
1171 spec.pos += to_read;
1172 spec.progress = (double) spec.pos /_length;
1179 spec.running = false;
1180 spec.status = status;
1186 boost::shared_ptr<Region>
1187 AudioRegion::get_parent() const
1189 boost::shared_ptr<Playlist> pl (playlist());
1192 boost::shared_ptr<AudioRegion> ar;
1193 boost::shared_ptr<AudioRegion const> grrr2 = boost::dynamic_pointer_cast<AudioRegion const> (shared_from_this());
1195 if (grrr2 && (ar = pl->session().find_whole_file_parent (grrr2))) {
1196 return boost::static_pointer_cast<Region> (ar);
1200 return boost::shared_ptr<Region>();
1204 AudioRegion::set_scale_amplitude (gain_t g)
1206 boost::shared_ptr<Playlist> pl (playlist());
1208 _scale_amplitude = g;
1210 /* tell the diskstream we're in */
1216 /* tell everybody else */
1218 send_change (ScaleAmplitudeChanged);
1222 AudioRegion::normalize_to (float target_dB)
1224 const nframes_t blocksize = 64 * 1024;
1225 Sample buf[blocksize];
1230 gain_t target = dB_to_coefficient (target_dB);
1232 if (target == 1.0f) {
1233 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1234 that we may have clipped.
1236 target -= FLT_EPSILON;
1240 fend = _start + _length;
1242 /* first pass: find max amplitude */
1244 while (fpos < fend) {
1248 to_read = min (fend - fpos, blocksize);
1250 for (n = 0; n < n_channels(); ++n) {
1254 if (source (n)->read (buf, fpos, to_read) != to_read) {
1258 maxamp = Session::compute_peak (buf, to_read, maxamp);
1264 if (maxamp == 0.0f) {
1265 /* don't even try */
1269 if (maxamp == target) {
1270 /* we can't do anything useful */
1274 /* compute scale factor */
1276 _scale_amplitude = target/maxamp;
1278 /* tell the diskstream we're in */
1280 boost::shared_ptr<Playlist> pl (playlist());
1286 /* tell everybody else */
1288 send_change (ScaleAmplitudeChanged);
1292 AudioRegion::fade_in_changed ()
1294 send_change (FadeInChanged);
1298 AudioRegion::fade_out_changed ()
1300 send_change (FadeOutChanged);
1304 AudioRegion::envelope_changed ()
1306 send_change (EnvelopeChanged);
1310 AudioRegion::suspend_fade_in ()
1312 if (++_fade_in_disabled == 1) {
1313 set_fade_in_active (false);
1318 AudioRegion::resume_fade_in ()
1320 if (_fade_in_disabled && --_fade_in_disabled == 0) {
1321 set_fade_in_active (true);
1326 AudioRegion::suspend_fade_out ()
1328 if (++_fade_out_disabled == 1) {
1329 set_fade_out_active (false);
1334 AudioRegion::resume_fade_out ()
1336 if (_fade_out_disabled && --_fade_out_disabled == 0) {
1337 set_fade_out_active (true);
1342 AudioRegion::speed_mismatch (float sr) const
1344 if (sources.empty()) {
1345 /* impossible, but ... */
1349 float fsr = sources.front()->sample_rate();
1355 AudioRegion::source_offset_changed ()
1357 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(sources.front());
1359 if (afs && afs->destructive()) {
1360 // set_start (source()->natural_position(), this);
1361 set_position (source()->natural_position(), this);
1366 AudioRegion::set_playlist (boost::weak_ptr<Playlist> wpl)
1368 boost::shared_ptr<Playlist> old_playlist = (_playlist.lock());
1370 boost::shared_ptr<Playlist> pl (wpl.lock());
1372 if (old_playlist == pl) {
1376 Region::set_playlist (wpl);
1380 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1381 (*i)->remove_playlist (_playlist);
1382 (*i)->add_playlist (pl);
1385 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1386 (*i)->add_playlist (pl);
1391 for (SourceList::const_iterator i = sources.begin(); i != sources.end(); ++i) {
1392 (*i)->remove_playlist (old_playlist);
1400 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)
1402 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1405 uint32_t region_length_from_c (void *arg)
1408 return ((AudioRegion *) arg)->length();
1411 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1413 return ( (AudioRegion *) arg)->source()->available_peaks (zoom_factor) ;