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.
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>
44 #include <ardour/runtime_functions.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 Region::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 Region::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 Region::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;
337 return 0; /* read nothing */
340 /* precondition: caller has verified that we cover the desired section */
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;
366 _read_data_count = 0;
368 if (chan_n < n_channels()) {
370 boost::shared_ptr<AudioSource> src = audio_source(chan_n);
371 if (src->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
373 return 0; /* "read nothing" */
376 _read_data_count += src->read_data_count();
380 /* track is N-channel, this region has less channels; silence the ones
384 memset (mixdown_buffer, 0, sizeof (Sample) * cnt);
386 /* no fades required */
393 if (_flags & FadeIn) {
395 nframes_t fade_in_length = (nframes_t) _fade_in.back()->when;
397 /* see if this read is within the fade in */
399 if (internal_offset < fade_in_length) {
403 limit = min (to_read, fade_in_length - internal_offset);
405 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
407 for (nframes_t n = 0; n < limit; ++n) {
408 mixdown_buffer[n] *= gain_buffer[n];
415 if (_flags & FadeOut) {
417 /* see if some part of this read is within the fade out */
419 /* ................. >| REGION
425 _length - fade_out_length
428 ^internal_offset + to_read
430 we need the intersection of [internal_offset,internal_offset+to_read] with
431 [_length - fade_out_length, _length]
436 nframes_t fade_out_length = (nframes_t) _fade_out.back()->when;
437 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
438 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
440 if (fade_interval_end > fade_interval_start) {
441 /* (part of the) the fade out is in this buffer */
443 nframes_t limit = fade_interval_end - fade_interval_start;
444 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
445 nframes_t fade_offset = fade_interval_start - internal_offset;
447 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
449 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
450 mixdown_buffer[m] *= gain_buffer[n];
456 /* Regular gain curves */
458 if (envelope_active()) {
459 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
461 if (_scale_amplitude != 1.0f) {
462 for (nframes_t n = 0; n < to_read; ++n) {
463 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
466 for (nframes_t n = 0; n < to_read; ++n) {
467 mixdown_buffer[n] *= gain_buffer[n];
470 } else if (_scale_amplitude != 1.0f) {
471 apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
478 /* gack. the things we do for users.
483 for (nframes_t n = 0; n < to_read; ++n) {
484 buf[n] += mixdown_buffer[n];
492 AudioRegion::state (bool full)
494 XMLNode& node (Region::state (full));
498 LocaleGuard lg (X_("POSIX"));
500 node.add_property ("flags", enum_2_string (_flags));
502 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
503 node.add_property ("scale-gain", buf);
505 for (uint32_t n=0; n < _sources.size(); ++n) {
506 snprintf (buf2, sizeof(buf2), "source-%d", n);
507 _sources[n]->id().print (buf, sizeof (buf));
508 node.add_property (buf2, buf);
511 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
512 node.add_property ("channels", buf);
516 child = node.add_child (X_("FadeIn"));
518 if ((_flags & DefaultFadeIn)) {
519 child->add_property (X_("default"), X_("yes"));
521 child->add_child_nocopy (_fade_in.get_state ());
524 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
526 child = node.add_child (X_("FadeOut"));
528 if ((_flags & DefaultFadeOut)) {
529 child->add_property (X_("default"), X_("yes"));
531 child->add_child_nocopy (_fade_out.get_state ());
534 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
537 child = node.add_child ("Envelope");
540 bool default_env = false;
542 // If there are only two points, the points are in the start of the region and the end of the region
543 // so, if they are both at 1.0f, that means the default region.
545 if (_envelope.size() == 2 &&
546 _envelope.front()->value == 1.0f &&
547 _envelope.back()->value==1.0f) {
548 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
554 child->add_property ("default", "yes");
556 child->add_child_nocopy (_envelope.get_state ());
560 child->add_property ("default", "yes");
563 if (full && _extra_xml) {
564 node.add_child_copy (*_extra_xml);
571 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
573 const XMLNodeList& nlist = node.children();
574 const XMLProperty *prop;
575 LocaleGuard lg (X_("POSIX"));
577 Region::set_live_state (node, what_changed, false);
579 uint32_t old_flags = _flags;
581 if ((prop = node.property ("flags")) != 0) {
582 _flags = Flag (string_2_enum (prop->value(), _flags));
584 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
586 _flags = Flag (_flags & ~Region::LeftOfSplit);
587 _flags = Flag (_flags & ~Region::RightOfSplit);
590 if ((old_flags ^ _flags) & Muted) {
591 what_changed = Change (what_changed|MuteChanged);
593 if ((old_flags ^ _flags) & Opaque) {
594 what_changed = Change (what_changed|OpacityChanged);
596 if ((old_flags ^ _flags) & Locked) {
597 what_changed = Change (what_changed|LockChanged);
600 if ((prop = node.property ("scale-gain")) != 0) {
601 _scale_amplitude = atof (prop->value().c_str());
603 _scale_amplitude = 1.0;
606 /* Now find envelope description and other misc child items */
608 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
615 if (child->name() == "Envelope") {
619 if ((prop = child->property ("default")) != 0 || _envelope.set_state (*child)) {
620 set_default_envelope ();
623 _envelope.set_max_xval (_length);
624 _envelope.truncate_end (_length);
626 } else if (child->name() == "FadeIn") {
630 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_in.set_state (*child)) {
631 set_default_fade_in ();
634 } else if (child->name() == "FadeOut") {
638 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_out.set_state (*child)) {
639 set_default_fade_out ();
645 send_change (what_changed);
652 AudioRegion::set_state (const XMLNode& node)
654 /* Region::set_state() calls the virtual set_live_state(),
655 which will get us back to AudioRegion::set_live_state()
656 to handle the relevant stuff.
659 return Region::set_state (node);
663 AudioRegion::set_fade_in_shape (FadeShape shape)
665 set_fade_in (shape, (nframes_t) _fade_in.back()->when);
669 AudioRegion::set_fade_out_shape (FadeShape shape)
671 set_fade_out (shape, (nframes_t) _fade_out.back()->when);
675 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
682 _fade_in.fast_simple_add (0.0, 0.0);
683 _fade_in.fast_simple_add (len, 1.0);
687 _fade_in.fast_simple_add (0, 0);
688 _fade_in.fast_simple_add (len * 0.389401, 0.0333333);
689 _fade_in.fast_simple_add (len * 0.629032, 0.0861111);
690 _fade_in.fast_simple_add (len * 0.829493, 0.233333);
691 _fade_in.fast_simple_add (len * 0.9447, 0.483333);
692 _fade_in.fast_simple_add (len * 0.976959, 0.697222);
693 _fade_in.fast_simple_add (len, 1);
697 _fade_in.fast_simple_add (0, 0);
698 _fade_in.fast_simple_add (len * 0.0207373, 0.197222);
699 _fade_in.fast_simple_add (len * 0.0645161, 0.525);
700 _fade_in.fast_simple_add (len * 0.152074, 0.802778);
701 _fade_in.fast_simple_add (len * 0.276498, 0.919444);
702 _fade_in.fast_simple_add (len * 0.481567, 0.980556);
703 _fade_in.fast_simple_add (len * 0.767281, 1);
704 _fade_in.fast_simple_add (len, 1);
708 _fade_in.fast_simple_add (0, 0);
709 _fade_in.fast_simple_add (len * 0.0737327, 0.308333);
710 _fade_in.fast_simple_add (len * 0.246544, 0.658333);
711 _fade_in.fast_simple_add (len * 0.470046, 0.886111);
712 _fade_in.fast_simple_add (len * 0.652074, 0.972222);
713 _fade_in.fast_simple_add (len * 0.771889, 0.988889);
714 _fade_in.fast_simple_add (len, 1);
718 _fade_in.fast_simple_add (0, 0);
719 _fade_in.fast_simple_add (len * 0.304147, 0.0694444);
720 _fade_in.fast_simple_add (len * 0.529954, 0.152778);
721 _fade_in.fast_simple_add (len * 0.725806, 0.333333);
722 _fade_in.fast_simple_add (len * 0.847926, 0.558333);
723 _fade_in.fast_simple_add (len * 0.919355, 0.730556);
724 _fade_in.fast_simple_add (len, 1);
729 _fade_in_shape = shape;
731 send_change (FadeInChanged);
735 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
742 _fade_out.fast_simple_add (len * 0, 1);
743 _fade_out.fast_simple_add (len * 0.023041, 0.697222);
744 _fade_out.fast_simple_add (len * 0.0553, 0.483333);
745 _fade_out.fast_simple_add (len * 0.170507, 0.233333);
746 _fade_out.fast_simple_add (len * 0.370968, 0.0861111);
747 _fade_out.fast_simple_add (len * 0.610599, 0.0333333);
748 _fade_out.fast_simple_add (len * 1, 0);
752 _fade_out.fast_simple_add (len * 0, 1);
753 _fade_out.fast_simple_add (len * 0.228111, 0.988889);
754 _fade_out.fast_simple_add (len * 0.347926, 0.972222);
755 _fade_out.fast_simple_add (len * 0.529954, 0.886111);
756 _fade_out.fast_simple_add (len * 0.753456, 0.658333);
757 _fade_out.fast_simple_add (len * 0.9262673, 0.308333);
758 _fade_out.fast_simple_add (len * 1, 0);
762 _fade_out.fast_simple_add (len * 0, 1);
763 _fade_out.fast_simple_add (len * 0.305556, 1);
764 _fade_out.fast_simple_add (len * 0.548611, 0.991736);
765 _fade_out.fast_simple_add (len * 0.759259, 0.931129);
766 _fade_out.fast_simple_add (len * 0.918981, 0.68595);
767 _fade_out.fast_simple_add (len * 0.976852, 0.22865);
768 _fade_out.fast_simple_add (len * 1, 0);
772 _fade_out.fast_simple_add (len * 0, 1);
773 _fade_out.fast_simple_add (len * 0.080645, 0.730556);
774 _fade_out.fast_simple_add (len * 0.277778, 0.289256);
775 _fade_out.fast_simple_add (len * 0.470046, 0.152778);
776 _fade_out.fast_simple_add (len * 0.695853, 0.0694444);
777 _fade_out.fast_simple_add (len * 1, 0);
781 _fade_out.fast_simple_add (len * 0, 1);
782 _fade_out.fast_simple_add (len * 1, 0);
787 _fade_out_shape = shape;
789 send_change (FadeOutChanged);
793 AudioRegion::set_fade_in_length (nframes_t len)
795 bool changed = _fade_in.extend_to (len);
798 _flags = Flag (_flags & ~DefaultFadeIn);
799 send_change (FadeInChanged);
804 AudioRegion::set_fade_out_length (nframes_t len)
806 bool changed = _fade_out.extend_to (len);
809 _flags = Flag (_flags & ~DefaultFadeOut);
812 send_change (FadeOutChanged);
816 AudioRegion::set_fade_in_active (bool yn)
818 if (yn == (_flags & FadeIn)) {
822 _flags = Flag (_flags|FadeIn);
824 _flags = Flag (_flags & ~FadeIn);
827 send_change (FadeInActiveChanged);
831 AudioRegion::set_fade_out_active (bool yn)
833 if (yn == (_flags & FadeOut)) {
837 _flags = Flag (_flags | FadeOut);
839 _flags = Flag (_flags & ~FadeOut);
842 send_change (FadeOutActiveChanged);
846 AudioRegion::fade_in_is_default () const
848 return _fade_in_shape == Linear && _fade_in.back()->when == 64;
852 AudioRegion::fade_out_is_default () const
854 return _fade_out_shape == Linear && _fade_out.back()->when == 64;
858 AudioRegion::set_default_fade_in ()
860 set_fade_in (Linear, 64);
864 AudioRegion::set_default_fade_out ()
866 set_fade_out (Linear, 64);
870 AudioRegion::set_default_fades ()
872 _fade_in_disabled = 0;
873 _fade_out_disabled = 0;
874 set_default_fade_in ();
875 set_default_fade_out ();
879 AudioRegion::set_default_envelope ()
883 _envelope.fast_simple_add (0, 1.0f);
884 _envelope.fast_simple_add (_length, 1.0f);
889 AudioRegion::recompute_at_end ()
891 /* our length has changed. recompute a new final point by interpolating
892 based on the the existing curve.
896 _envelope.truncate_end (_length);
897 _envelope.set_max_xval (_length);
900 if (_fade_in.back()->when > _length) {
901 _fade_in.extend_to (_length);
902 send_change (FadeInChanged);
905 if (_fade_out.back()->when > _length) {
906 _fade_out.extend_to (_length);
907 send_change (FadeOutChanged);
912 AudioRegion::recompute_at_start ()
914 /* as above, but the shift was from the front */
916 _envelope.truncate_start (_length);
918 if (_fade_in.back()->when > _length) {
919 _fade_in.extend_to (_length);
920 send_change (FadeInChanged);
923 if (_fade_out.back()->when > _length) {
924 _fade_out.extend_to (_length);
925 send_change (FadeOutChanged);
930 AudioRegion::separate_by_channel (Session& session, vector<boost::shared_ptr<AudioRegion> >& v) const
936 if (_sources.size() < 2) {
942 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
949 if (_sources.size() == 2) {
957 new_name += ('0' + n + 1);
960 /* create a copy with just one source. prevent if from being thought of as "whole file" even if
961 it covers the entire source file(s).
964 Flag f = Flag (_flags & ~WholeFile);
966 boost::shared_ptr<Region> r = RegionFactory::create (srcs, _start, _length, new_name, _layer, f);
967 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
978 AudioRegion::apply (AudioFilter& filter)
980 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
981 return filter.run (ar);
985 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
987 const nframes_t blocksize = 4096;
991 spec.channels = _sources.size();
993 if (spec.prepare (blocksize, session.frame_rate())) {
998 spec.total_frames = _length;
1000 while (spec.pos < _length && !spec.stop) {
1003 /* step 1: interleave */
1005 to_read = min (_length - spec.pos, blocksize);
1007 if (spec.channels == 1) {
1009 if (audio_source()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
1015 Sample buf[blocksize];
1017 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1019 if (audio_source(chan)->read (buf, _start + spec.pos, to_read) != to_read) {
1023 for (nframes_t x = 0; x < to_read; ++x) {
1024 spec.dataF[chan+(x*spec.channels)] = buf[x];
1029 if (spec.process (to_read)) {
1033 spec.pos += to_read;
1034 spec.progress = (double) spec.pos /_length;
1041 spec.running = false;
1042 spec.status = status;
1049 AudioRegion::set_scale_amplitude (gain_t g)
1051 boost::shared_ptr<Playlist> pl (playlist());
1053 _scale_amplitude = g;
1055 /* tell the diskstream we're in */
1061 /* tell everybody else */
1063 send_change (ScaleAmplitudeChanged);
1067 AudioRegion::normalize_to (float target_dB)
1069 const nframes_t blocksize = 64 * 1024;
1070 Sample buf[blocksize];
1075 gain_t target = dB_to_coefficient (target_dB);
1077 if (target == 1.0f) {
1078 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1079 that we may have clipped.
1081 target -= FLT_EPSILON;
1085 fend = _start + _length;
1087 /* first pass: find max amplitude */
1089 while (fpos < fend) {
1093 to_read = min (fend - fpos, blocksize);
1095 for (n = 0; n < n_channels(); ++n) {
1099 if (audio_source (n)->read (buf, fpos, to_read) != to_read) {
1103 maxamp = compute_peak (buf, to_read, maxamp);
1109 if (maxamp == 0.0f) {
1110 /* don't even try */
1114 if (maxamp == target) {
1115 /* we can't do anything useful */
1119 /* compute scale factor */
1121 _scale_amplitude = target/maxamp;
1123 /* tell the diskstream we're in */
1125 boost::shared_ptr<Playlist> pl (playlist());
1131 /* tell everybody else */
1133 send_change (ScaleAmplitudeChanged);
1137 AudioRegion::fade_in_changed ()
1139 send_change (FadeInChanged);
1143 AudioRegion::fade_out_changed ()
1145 send_change (FadeOutChanged);
1149 AudioRegion::envelope_changed ()
1151 send_change (EnvelopeChanged);
1155 AudioRegion::suspend_fade_in ()
1157 if (++_fade_in_disabled == 1) {
1158 if (fade_in_is_default()) {
1159 set_fade_in_active (false);
1165 AudioRegion::resume_fade_in ()
1167 if (--_fade_in_disabled == 0 && _fade_in_disabled) {
1168 set_fade_in_active (true);
1173 AudioRegion::suspend_fade_out ()
1175 if (++_fade_out_disabled == 1) {
1176 if (fade_out_is_default()) {
1177 set_fade_out_active (false);
1183 AudioRegion::resume_fade_out ()
1185 if (--_fade_out_disabled == 0 &&_fade_out_disabled) {
1186 set_fade_out_active (true);
1191 AudioRegion::speed_mismatch (float sr) const
1193 if (_sources.empty()) {
1194 /* impossible, but ... */
1198 float fsr = audio_source()->sample_rate();
1204 AudioRegion::source_offset_changed ()
1206 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(_sources.front());
1208 if (afs && afs->destructive()) {
1209 // set_start (source()->natural_position(), this);
1210 set_position (source()->natural_position(), this);
1214 boost::shared_ptr<AudioSource>
1215 AudioRegion::audio_source (uint32_t n) const
1217 // Guaranteed to succeed (use a static cast for speed?)
1218 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1223 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)
1225 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1228 uint32_t region_length_from_c (void *arg)
1231 return ((AudioRegion *) arg)->length();
1234 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1236 return ( (AudioRegion *) arg)->audio_source()->available_peaks (zoom_factor) ;