2 Copyright (C) 2000-2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #include <sigc++/bind.h>
28 #include <sigc++/class_slot.h>
30 #include <glibmm/thread.h>
32 #include <pbd/basename.h>
33 #include <pbd/xml++.h>
34 #include <pbd/stacktrace.h>
35 #include <pbd/enumwriter.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/destructive_filesource.h>
50 using namespace ARDOUR;
52 /* a Session will reset these to its chosen defaults by calling AudioRegion::set_default_fade() */
54 Change AudioRegion::FadeInChanged = ARDOUR::new_change();
55 Change AudioRegion::FadeOutChanged = ARDOUR::new_change();
56 Change AudioRegion::FadeInActiveChanged = ARDOUR::new_change();
57 Change AudioRegion::FadeOutActiveChanged = ARDOUR::new_change();
58 Change AudioRegion::EnvelopeActiveChanged = ARDOUR::new_change();
59 Change AudioRegion::ScaleAmplitudeChanged = ARDOUR::new_change();
60 Change AudioRegion::EnvelopeChanged = ARDOUR::new_change();
62 /** Basic AudioRegion constructor (one channel) */
63 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length)
64 : Region (src, start, length, PBD::basename_nosuffix(src->name()), DataType::AUDIO, 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 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
71 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
74 _scale_amplitude = 1.0;
77 set_default_envelope ();
79 listen_to_my_curves ();
82 /* Basic AudioRegion constructor (one channel) */
83 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
84 : Region (src, start, length, name, DataType::AUDIO, layer, flags)
85 , _fade_in (0.0, 2.0, 1.0, false)
86 , _fade_out (0.0, 2.0, 1.0, false)
87 , _envelope (0.0, 2.0, 1.0, false)
89 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
91 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
94 _scale_amplitude = 1.0;
97 set_default_envelope ();
99 listen_to_my_curves ();
102 /* Basic AudioRegion constructor (many channels) */
103 AudioRegion::AudioRegion (SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
104 : Region (srcs, start, length, name, DataType::AUDIO, layer, flags)
105 , _fade_in (0.0, 2.0, 1.0, false)
106 , _fade_out (0.0, 2.0, 1.0, false)
107 , _envelope (0.0, 2.0, 1.0, false)
109 _scale_amplitude = 1.0;
111 set_default_fades ();
112 set_default_envelope ();
114 listen_to_my_curves ();
118 /** Create a new AudioRegion, that is part of an existing one */
119 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
120 : Region (other, offset, length, name, layer, flags),
121 _fade_in (other->_fade_in),
122 _fade_out (other->_fade_out),
123 _envelope (other->_envelope, (double) offset, (double) offset + length)
125 /* return to default fades if the existing ones are too long */
126 _fade_in_disabled = 0;
127 _fade_out_disabled = 0;
130 if (_flags & LeftOfSplit) {
131 if (_fade_in.back()->when >= _length) {
132 set_default_fade_in ();
134 _fade_in_disabled = other->_fade_in_disabled;
136 set_default_fade_out ();
137 _flags = Flag (_flags & ~Region::LeftOfSplit);
140 if (_flags & RightOfSplit) {
141 if (_fade_out.back()->when >= _length) {
142 set_default_fade_out ();
144 _fade_out_disabled = other->_fade_out_disabled;
146 set_default_fade_in ();
147 _flags = Flag (_flags & ~Region::RightOfSplit);
150 _scale_amplitude = other->_scale_amplitude;
152 listen_to_my_curves ();
154 assert(_type == DataType::AUDIO);
157 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
159 _fade_in (other->_fade_in),
160 _fade_out (other->_fade_out),
161 _envelope (other->_envelope)
163 _scale_amplitude = other->_scale_amplitude;
164 _envelope = other->_envelope;
166 _fade_in_disabled = 0;
167 _fade_out_disabled = 0;
169 listen_to_my_curves ();
171 assert(_type == DataType::AUDIO);
174 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, const XMLNode& node)
176 , _fade_in (0.0, 2.0, 1.0, false)
177 , _fade_out (0.0, 2.0, 1.0, false)
178 , _envelope (0.0, 2.0, 1.0, false)
180 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
182 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
185 set_default_fades ();
187 if (set_state (node)) {
188 throw failed_constructor();
191 listen_to_my_curves ();
193 assert(_type == DataType::AUDIO);
196 AudioRegion::AudioRegion (SourceList& srcs, const XMLNode& node)
197 : Region (srcs, node),
198 _fade_in (0.0, 2.0, 1.0, false),
199 _fade_out (0.0, 2.0, 1.0, false),
200 _envelope (0.0, 2.0, 1.0, false)
202 set_default_fades ();
203 _scale_amplitude = 1.0;
205 if (set_state (node)) {
206 throw failed_constructor();
209 listen_to_my_curves ();
211 assert(_type == DataType::AUDIO);
214 AudioRegion::~AudioRegion ()
219 AudioRegion::listen_to_my_curves ()
221 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
222 _fade_in.StateChanged.connect (mem_fun (*this, &AudioRegion::fade_in_changed));
223 _fade_out.StateChanged.connect (mem_fun (*this, &AudioRegion::fade_out_changed));
227 AudioRegion::verify_length (nframes_t len)
229 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
231 if (afs && afs->destructive()) {
234 return Region::verify_length(len);
239 AudioRegion::verify_start_and_length (nframes_t new_start, nframes_t new_length)
241 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
243 if (afs && afs->destructive()) {
246 return verify_start_and_length(new_start, new_length);
251 AudioRegion::verify_start (nframes_t pos)
253 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
255 if (afs && afs->destructive()) {
258 return verify_start(pos);
263 AudioRegion::verify_start_mutable (nframes_t& new_start)
265 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
267 if (afs && afs->destructive()) {
270 return verify_start_mutable(new_start);
275 AudioRegion::set_envelope_active (bool yn)
277 if (envelope_active() != yn) {
280 snprintf (buf, sizeof (buf), "envelope active");
281 _flags = Flag (_flags|EnvelopeActive);
283 snprintf (buf, sizeof (buf), "envelope off");
284 _flags = Flag (_flags & ~EnvelopeActive);
286 send_change (EnvelopeActiveChanged);
291 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
293 if (chan_n >= _sources.size()) {
297 if (audio_source(chan_n)->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
300 if (_scale_amplitude != 1.0) {
301 for (nframes_t n = 0; n < npeaks; ++n) {
302 buf[n].max *= _scale_amplitude;
303 buf[n].min *= _scale_amplitude;
311 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
313 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
315 return _read_at (_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, read_frames, skip_frames);
319 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
320 nframes_t cnt, uint32_t chan_n) const
322 return _read_at (_master_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0);
326 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
327 nframes_t position, nframes_t cnt,
328 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
330 //cerr << _name << "._read_at(" << position << ") - " << _position << endl;
332 nframes_t internal_offset;
333 nframes_t buf_offset;
336 /* precondition: caller has verified that we cover the desired section */
338 if (chan_n >= _sources.size()) {
339 return 0; /* read nothing */
342 if (position < _position) {
344 buf_offset = _position - position;
347 internal_offset = position - _position;
351 if (internal_offset >= _length) {
352 return 0; /* read nothing */
355 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
356 return 0; /* read nothing */
360 /* overwrite whatever is there */
361 mixdown_buffer = buf + buf_offset;
363 mixdown_buffer += buf_offset;
367 return 0; /* read nothing */
370 _read_data_count = 0;
372 boost::shared_ptr<AudioSource> src = audio_source(chan_n);
373 if (src->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
374 return 0; /* "read nothing" */
377 _read_data_count += src->read_data_count();
381 if (_flags & FadeIn) {
383 nframes_t fade_in_length = (nframes_t) _fade_in.back()->when;
385 /* see if this read is within the fade in */
387 if (internal_offset < fade_in_length) {
391 limit = min (to_read, fade_in_length - internal_offset);
393 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
395 for (nframes_t n = 0; n < limit; ++n) {
396 mixdown_buffer[n] *= gain_buffer[n];
403 if (_flags & FadeOut) {
405 /* see if some part of this read is within the fade out */
407 /* ................. >| REGION
413 _length - fade_out_length
416 ^internal_offset + to_read
418 we need the intersection of [internal_offset,internal_offset+to_read] with
419 [_length - fade_out_length, _length]
424 nframes_t fade_out_length = (nframes_t) _fade_out.back()->when;
425 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
426 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
428 if (fade_interval_end > fade_interval_start) {
429 /* (part of the) the fade out is in this buffer */
431 nframes_t limit = fade_interval_end - fade_interval_start;
432 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
433 nframes_t fade_offset = fade_interval_start - internal_offset;
435 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
437 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
438 mixdown_buffer[m] *= gain_buffer[n];
444 /* Regular gain curves */
446 if (envelope_active()) {
447 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
449 if (_scale_amplitude != 1.0f) {
450 for (nframes_t n = 0; n < to_read; ++n) {
451 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
454 for (nframes_t n = 0; n < to_read; ++n) {
455 mixdown_buffer[n] *= gain_buffer[n];
458 } else if (_scale_amplitude != 1.0f) {
459 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
464 /* gack. the things we do for users.
469 for (nframes_t n = 0; n < to_read; ++n) {
470 buf[n] += mixdown_buffer[n];
478 AudioRegion::state (bool full)
480 XMLNode& node (Region::state (full));
484 LocaleGuard lg (X_("POSIX"));
486 node.add_property ("flags", enum_2_string (_flags));
488 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
489 node.add_property ("scale-gain", buf);
491 for (uint32_t n=0; n < _sources.size(); ++n) {
492 snprintf (buf2, sizeof(buf2), "source-%d", n);
493 _sources[n]->id().print (buf, sizeof (buf));
494 node.add_property (buf2, buf);
497 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
498 node.add_property ("channels", buf);
502 child = node.add_child (X_("FadeIn"));
504 if ((_flags & DefaultFadeIn)) {
505 child->add_property (X_("default"), X_("yes"));
507 child->add_child_nocopy (_fade_in.get_state ());
510 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
512 child = node.add_child (X_("FadeOut"));
514 if ((_flags & DefaultFadeOut)) {
515 child->add_property (X_("default"), X_("yes"));
517 child->add_child_nocopy (_fade_out.get_state ());
520 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
523 child = node.add_child ("Envelope");
526 bool default_env = false;
528 // If there are only two points, the points are in the start of the region and the end of the region
529 // so, if they are both at 1.0f, that means the default region.
531 if (_envelope.size() == 2 &&
532 _envelope.front()->value == 1.0f &&
533 _envelope.back()->value==1.0f) {
534 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
540 child->add_property ("default", "yes");
542 child->add_child_nocopy (_envelope.get_state ());
546 child->add_property ("default", "yes");
549 if (full && _extra_xml) {
550 node.add_child_copy (*_extra_xml);
557 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
559 const XMLNodeList& nlist = node.children();
560 const XMLProperty *prop;
561 LocaleGuard lg (X_("POSIX"));
563 Region::set_live_state (node, what_changed, false);
565 uint32_t old_flags = _flags;
567 if ((prop = node.property ("flags")) != 0) {
568 _flags = Flag (string_2_enum (prop->value(), _flags));
570 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
572 _flags = Flag (_flags & ~Region::LeftOfSplit);
573 _flags = Flag (_flags & ~Region::RightOfSplit);
576 if ((old_flags ^ _flags) & Muted) {
577 what_changed = Change (what_changed|MuteChanged);
579 if ((old_flags ^ _flags) & Opaque) {
580 what_changed = Change (what_changed|OpacityChanged);
582 if ((old_flags ^ _flags) & Locked) {
583 what_changed = Change (what_changed|LockChanged);
586 if ((prop = node.property ("scale-gain")) != 0) {
587 _scale_amplitude = atof (prop->value().c_str());
589 _scale_amplitude = 1.0;
592 /* Now find envelope description and other misc child items */
594 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
601 if (child->name() == "Envelope") {
605 if ((prop = child->property ("default")) != 0 || _envelope.set_state (*child)) {
606 set_default_envelope ();
609 _envelope.set_max_xval (_length);
610 _envelope.truncate_end (_length);
612 } else if (child->name() == "FadeIn") {
616 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_in.set_state (*child)) {
617 set_default_fade_in ();
620 } else if (child->name() == "FadeOut") {
624 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_out.set_state (*child)) {
625 set_default_fade_out ();
631 send_change (what_changed);
638 AudioRegion::set_state (const XMLNode& node)
640 /* Region::set_state() calls the virtual set_live_state(),
641 which will get us back to AudioRegion::set_live_state()
642 to handle the relevant stuff.
645 return Region::set_state (node);
649 AudioRegion::set_fade_in_shape (FadeShape shape)
651 set_fade_in (shape, (nframes_t) _fade_in.back()->when);
655 AudioRegion::set_fade_out_shape (FadeShape shape)
657 set_fade_out (shape, (nframes_t) _fade_out.back()->when);
661 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
668 _fade_in.fast_simple_add (0.0, 0.0);
669 _fade_in.fast_simple_add (len, 1.0);
673 _fade_in.fast_simple_add (0, 0);
674 _fade_in.fast_simple_add (len * 0.389401, 0.0333333);
675 _fade_in.fast_simple_add (len * 0.629032, 0.0861111);
676 _fade_in.fast_simple_add (len * 0.829493, 0.233333);
677 _fade_in.fast_simple_add (len * 0.9447, 0.483333);
678 _fade_in.fast_simple_add (len * 0.976959, 0.697222);
679 _fade_in.fast_simple_add (len, 1);
683 _fade_in.fast_simple_add (0, 0);
684 _fade_in.fast_simple_add (len * 0.0207373, 0.197222);
685 _fade_in.fast_simple_add (len * 0.0645161, 0.525);
686 _fade_in.fast_simple_add (len * 0.152074, 0.802778);
687 _fade_in.fast_simple_add (len * 0.276498, 0.919444);
688 _fade_in.fast_simple_add (len * 0.481567, 0.980556);
689 _fade_in.fast_simple_add (len * 0.767281, 1);
690 _fade_in.fast_simple_add (len, 1);
694 _fade_in.fast_simple_add (0, 0);
695 _fade_in.fast_simple_add (len * 0.0737327, 0.308333);
696 _fade_in.fast_simple_add (len * 0.246544, 0.658333);
697 _fade_in.fast_simple_add (len * 0.470046, 0.886111);
698 _fade_in.fast_simple_add (len * 0.652074, 0.972222);
699 _fade_in.fast_simple_add (len * 0.771889, 0.988889);
700 _fade_in.fast_simple_add (len, 1);
704 _fade_in.fast_simple_add (0, 0);
705 _fade_in.fast_simple_add (len * 0.304147, 0.0694444);
706 _fade_in.fast_simple_add (len * 0.529954, 0.152778);
707 _fade_in.fast_simple_add (len * 0.725806, 0.333333);
708 _fade_in.fast_simple_add (len * 0.847926, 0.558333);
709 _fade_in.fast_simple_add (len * 0.919355, 0.730556);
710 _fade_in.fast_simple_add (len, 1);
715 _fade_in_shape = shape;
717 send_change (FadeInChanged);
721 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
728 _fade_out.fast_simple_add (len * 0, 1);
729 _fade_out.fast_simple_add (len * 0.023041, 0.697222);
730 _fade_out.fast_simple_add (len * 0.0553, 0.483333);
731 _fade_out.fast_simple_add (len * 0.170507, 0.233333);
732 _fade_out.fast_simple_add (len * 0.370968, 0.0861111);
733 _fade_out.fast_simple_add (len * 0.610599, 0.0333333);
734 _fade_out.fast_simple_add (len * 1, 0);
738 _fade_out.fast_simple_add (len * 0, 1);
739 _fade_out.fast_simple_add (len * 0.228111, 0.988889);
740 _fade_out.fast_simple_add (len * 0.347926, 0.972222);
741 _fade_out.fast_simple_add (len * 0.529954, 0.886111);
742 _fade_out.fast_simple_add (len * 0.753456, 0.658333);
743 _fade_out.fast_simple_add (len * 0.9262673, 0.308333);
744 _fade_out.fast_simple_add (len * 1, 0);
748 _fade_out.fast_simple_add (len * 0, 1);
749 _fade_out.fast_simple_add (len * 0.305556, 1);
750 _fade_out.fast_simple_add (len * 0.548611, 0.991736);
751 _fade_out.fast_simple_add (len * 0.759259, 0.931129);
752 _fade_out.fast_simple_add (len * 0.918981, 0.68595);
753 _fade_out.fast_simple_add (len * 0.976852, 0.22865);
754 _fade_out.fast_simple_add (len * 1, 0);
758 _fade_out.fast_simple_add (len * 0, 1);
759 _fade_out.fast_simple_add (len * 0.080645, 0.730556);
760 _fade_out.fast_simple_add (len * 0.277778, 0.289256);
761 _fade_out.fast_simple_add (len * 0.470046, 0.152778);
762 _fade_out.fast_simple_add (len * 0.695853, 0.0694444);
763 _fade_out.fast_simple_add (len * 1, 0);
767 _fade_out.fast_simple_add (len * 0, 1);
768 _fade_out.fast_simple_add (len * 1, 0);
773 _fade_out_shape = shape;
775 send_change (FadeOutChanged);
779 AudioRegion::set_fade_in_length (nframes_t len)
781 bool changed = _fade_in.extend_to (len);
784 _flags = Flag (_flags & ~DefaultFadeIn);
785 send_change (FadeInChanged);
790 AudioRegion::set_fade_out_length (nframes_t len)
792 bool changed = _fade_out.extend_to (len);
795 _flags = Flag (_flags & ~DefaultFadeOut);
798 send_change (FadeOutChanged);
802 AudioRegion::set_fade_in_active (bool yn)
804 if (yn == (_flags & FadeIn)) {
808 _flags = Flag (_flags|FadeIn);
810 _flags = Flag (_flags & ~FadeIn);
813 send_change (FadeInActiveChanged);
817 AudioRegion::set_fade_out_active (bool yn)
819 if (yn == (_flags & FadeOut)) {
823 _flags = Flag (_flags | FadeOut);
825 _flags = Flag (_flags & ~FadeOut);
828 send_change (FadeOutActiveChanged);
832 AudioRegion::set_default_fade_in ()
834 set_fade_in (Linear, 64);
838 AudioRegion::set_default_fade_out ()
840 set_fade_out (Linear, 64);
844 AudioRegion::set_default_fades ()
846 _fade_in_disabled = 0;
847 _fade_out_disabled = 0;
848 set_default_fade_in ();
849 set_default_fade_out ();
853 AudioRegion::set_default_envelope ()
857 _envelope.fast_simple_add (0, 1.0f);
858 _envelope.fast_simple_add (_length, 1.0f);
863 AudioRegion::recompute_at_end ()
865 /* our length has changed. recompute a new final point by interpolating
866 based on the the existing curve.
870 _envelope.truncate_end (_length);
871 _envelope.set_max_xval (_length);
874 if (_fade_in.back()->when > _length) {
875 _fade_in.extend_to (_length);
876 send_change (FadeInChanged);
879 if (_fade_out.back()->when > _length) {
880 _fade_out.extend_to (_length);
881 send_change (FadeOutChanged);
886 AudioRegion::recompute_at_start ()
888 /* as above, but the shift was from the front */
890 _envelope.truncate_start (_length);
892 if (_fade_in.back()->when > _length) {
893 _fade_in.extend_to (_length);
894 send_change (FadeInChanged);
897 if (_fade_out.back()->when > _length) {
898 _fade_out.extend_to (_length);
899 send_change (FadeOutChanged);
904 AudioRegion::separate_by_channel (Session& session, vector<AudioRegion*>& v) const
909 for (SourceList::const_iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
914 /* generate a new name */
916 if (session.region_name (new_name, _name)) {
920 /* create a copy with just one source */
922 v.push_back (new AudioRegion (srcs, _start, _length, new_name, _layer, _flags));
929 AudioRegion::apply (AudioFilter& filter)
931 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
932 return filter.run (ar);
936 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
938 const nframes_t blocksize = 4096;
942 spec.channels = _sources.size();
944 if (spec.prepare (blocksize, session.frame_rate())) {
949 spec.total_frames = _length;
951 while (spec.pos < _length && !spec.stop) {
954 /* step 1: interleave */
956 to_read = min (_length - spec.pos, blocksize);
958 if (spec.channels == 1) {
960 if (audio_source()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
966 Sample buf[blocksize];
968 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
970 if (audio_source(chan)->read (buf, _start + spec.pos, to_read) != to_read) {
974 for (nframes_t x = 0; x < to_read; ++x) {
975 spec.dataF[chan+(x*spec.channels)] = buf[x];
980 if (spec.process (to_read)) {
985 spec.progress = (double) spec.pos /_length;
992 spec.running = false;
993 spec.status = status;
1000 AudioRegion::set_scale_amplitude (gain_t g)
1002 boost::shared_ptr<Playlist> pl (playlist());
1004 _scale_amplitude = g;
1006 /* tell the diskstream we're in */
1012 /* tell everybody else */
1014 send_change (ScaleAmplitudeChanged);
1018 AudioRegion::normalize_to (float target_dB)
1020 const nframes_t blocksize = 64 * 1024;
1021 Sample buf[blocksize];
1026 gain_t target = dB_to_coefficient (target_dB);
1028 if (target == 1.0f) {
1029 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1030 that we may have clipped.
1032 target -= FLT_EPSILON;
1036 fend = _start + _length;
1038 /* first pass: find max amplitude */
1040 while (fpos < fend) {
1044 to_read = min (fend - fpos, blocksize);
1046 for (n = 0; n < n_channels(); ++n) {
1050 if (audio_source (n)->read (buf, fpos, to_read) != to_read) {
1054 maxamp = Session::compute_peak (buf, to_read, maxamp);
1060 if (maxamp == 0.0f) {
1061 /* don't even try */
1065 if (maxamp == target) {
1066 /* we can't do anything useful */
1070 /* compute scale factor */
1072 _scale_amplitude = target/maxamp;
1074 /* tell the diskstream we're in */
1076 boost::shared_ptr<Playlist> pl (playlist());
1082 /* tell everybody else */
1084 send_change (ScaleAmplitudeChanged);
1088 AudioRegion::fade_in_changed ()
1090 send_change (FadeInChanged);
1094 AudioRegion::fade_out_changed ()
1096 send_change (FadeOutChanged);
1100 AudioRegion::envelope_changed ()
1102 send_change (EnvelopeChanged);
1106 AudioRegion::suspend_fade_in ()
1108 if (++_fade_in_disabled == 1) {
1109 set_fade_in_active (false);
1114 AudioRegion::resume_fade_in ()
1116 if (_fade_in_disabled && --_fade_in_disabled == 0) {
1117 set_fade_in_active (true);
1122 AudioRegion::suspend_fade_out ()
1124 if (++_fade_out_disabled == 1) {
1125 set_fade_out_active (false);
1130 AudioRegion::resume_fade_out ()
1132 if (_fade_out_disabled && --_fade_out_disabled == 0) {
1133 set_fade_out_active (true);
1138 AudioRegion::speed_mismatch (float sr) const
1140 if (_sources.empty()) {
1141 /* impossible, but ... */
1145 float fsr = audio_source()->sample_rate();
1151 AudioRegion::source_offset_changed ()
1153 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(_sources.front());
1155 if (afs && afs->destructive()) {
1156 // set_start (source()->natural_position(), this);
1157 set_position (source()->natural_position(), this);
1161 boost::shared_ptr<AudioSource>
1162 AudioRegion::audio_source (uint32_t n) const
1164 // Guaranteed to succeed (use a static cast?)
1165 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1170 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)
1172 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1175 uint32_t region_length_from_c (void *arg)
1178 return ((AudioRegion *) arg)->length();
1181 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1183 return ( (AudioRegion *) arg)->audio_source()->available_peaks (zoom_factor) ;