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>
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 /** Basic AudioRegion constructor (one channel) */
62 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length)
63 : Region (src, start, length, PBD::basename_nosuffix(src->name()), DataType::AUDIO, 0, Region::Flag(Region::DefaultFlags|Region::External)),
64 _fade_in (0.0, 2.0, 1.0, false),
65 _fade_out (0.0, 2.0, 1.0, false),
66 _envelope (0.0, 2.0, 1.0, false)
68 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
70 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
73 _scale_amplitude = 1.0;
76 set_default_envelope ();
78 listen_to_my_curves ();
81 /* Basic AudioRegion constructor (one channel) */
82 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
83 : Region (src, start, length, name, DataType::AUDIO, layer, flags)
84 , _fade_in (0.0, 2.0, 1.0, false)
85 , _fade_out (0.0, 2.0, 1.0, false)
86 , _envelope (0.0, 2.0, 1.0, false)
88 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
90 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
93 _scale_amplitude = 1.0;
96 set_default_envelope ();
98 listen_to_my_curves ();
101 /* Basic AudioRegion constructor (many channels) */
102 AudioRegion::AudioRegion (SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
103 : Region (srcs, start, length, name, DataType::AUDIO, layer, flags)
104 , _fade_in (0.0, 2.0, 1.0, false)
105 , _fade_out (0.0, 2.0, 1.0, false)
106 , _envelope (0.0, 2.0, 1.0, false)
108 _scale_amplitude = 1.0;
110 set_default_fades ();
111 set_default_envelope ();
113 listen_to_my_curves ();
117 /** Create a new AudioRegion, that is part of an existing one */
118 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
119 : Region (other, offset, length, name, layer, flags),
120 _fade_in (other->_fade_in),
121 _fade_out (other->_fade_out),
122 _envelope (other->_envelope, (double) offset, (double) offset + length)
124 /* return to default fades if the existing ones are too long */
125 _fade_in_disabled = 0;
126 _fade_out_disabled = 0;
129 if (_flags & LeftOfSplit) {
130 if (_fade_in.back()->when >= _length) {
131 set_default_fade_in ();
133 _fade_in_disabled = other->_fade_in_disabled;
135 set_default_fade_out ();
136 _flags = Flag (_flags & ~Region::LeftOfSplit);
139 if (_flags & RightOfSplit) {
140 if (_fade_out.back()->when >= _length) {
141 set_default_fade_out ();
143 _fade_out_disabled = other->_fade_out_disabled;
145 set_default_fade_in ();
146 _flags = Flag (_flags & ~Region::RightOfSplit);
149 _scale_amplitude = other->_scale_amplitude;
151 listen_to_my_curves ();
153 assert(_type == DataType::AUDIO);
156 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
158 _fade_in (other->_fade_in),
159 _fade_out (other->_fade_out),
160 _envelope (other->_envelope)
162 _scale_amplitude = other->_scale_amplitude;
163 _envelope = other->_envelope;
165 _fade_in_disabled = 0;
166 _fade_out_disabled = 0;
168 listen_to_my_curves ();
170 assert(_type == DataType::AUDIO);
173 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, const XMLNode& node)
175 , _fade_in (0.0, 2.0, 1.0, false)
176 , _fade_out (0.0, 2.0, 1.0, false)
177 , _envelope (0.0, 2.0, 1.0, false)
179 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
181 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
184 set_default_fades ();
186 if (set_state (node)) {
187 throw failed_constructor();
190 listen_to_my_curves ();
192 assert(_type == DataType::AUDIO);
195 AudioRegion::AudioRegion (SourceList& srcs, const XMLNode& node)
196 : Region (srcs, node),
197 _fade_in (0.0, 2.0, 1.0, false),
198 _fade_out (0.0, 2.0, 1.0, false),
199 _envelope (0.0, 2.0, 1.0, false)
201 set_default_fades ();
202 _scale_amplitude = 1.0;
204 if (set_state (node)) {
205 throw failed_constructor();
208 listen_to_my_curves ();
210 assert(_type == DataType::AUDIO);
213 AudioRegion::~AudioRegion ()
218 AudioRegion::listen_to_my_curves ()
220 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
221 _fade_in.StateChanged.connect (mem_fun (*this, &AudioRegion::fade_in_changed));
222 _fade_out.StateChanged.connect (mem_fun (*this, &AudioRegion::fade_out_changed));
226 AudioRegion::verify_length (nframes_t len)
228 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
230 if (afs && afs->destructive()) {
233 return Region::verify_length(len);
238 AudioRegion::verify_start_and_length (nframes_t new_start, nframes_t new_length)
240 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
242 if (afs && afs->destructive()) {
245 return verify_start_and_length(new_start, new_length);
250 AudioRegion::verify_start (nframes_t pos)
252 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
254 if (afs && afs->destructive()) {
257 return verify_start(pos);
262 AudioRegion::verify_start_mutable (nframes_t& new_start)
264 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
266 if (afs && afs->destructive()) {
269 return verify_start_mutable(new_start);
274 AudioRegion::set_envelope_active (bool yn)
276 if (envelope_active() != yn) {
279 snprintf (buf, sizeof (buf), "envelope active");
280 _flags = Flag (_flags|EnvelopeActive);
282 snprintf (buf, sizeof (buf), "envelope off");
283 _flags = Flag (_flags & ~EnvelopeActive);
285 send_change (EnvelopeActiveChanged);
290 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
292 if (chan_n >= _sources.size()) {
296 if (audio_source(chan_n)->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
299 if (_scale_amplitude != 1.0) {
300 for (nframes_t n = 0; n < npeaks; ++n) {
301 buf[n].max *= _scale_amplitude;
302 buf[n].min *= _scale_amplitude;
310 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
312 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
314 return _read_at (_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, read_frames, skip_frames);
318 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
319 nframes_t cnt, uint32_t chan_n) const
321 return _read_at (_master_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0);
325 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
326 nframes_t position, nframes_t cnt,
327 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
329 // cerr << _name << "._read_at(" << position << ") - " << _position << endl;
331 nframes_t internal_offset;
332 nframes_t buf_offset;
336 return 0; /* read nothing */
339 /* precondition: caller has verified that we cover the desired section */
341 if (position < _position) {
343 buf_offset = _position - position;
346 internal_offset = position - _position;
350 if (internal_offset >= _length) {
351 return 0; /* read nothing */
354 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
355 return 0; /* read nothing */
359 /* overwrite whatever is there */
360 mixdown_buffer = buf + buf_offset;
362 mixdown_buffer += buf_offset;
365 _read_data_count = 0;
367 if (chan_n < n_channels()) {
369 boost::shared_ptr<AudioSource> src = audio_source(chan_n);
370 if (src->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
372 return 0; /* "read nothing" */
375 _read_data_count += src->read_data_count();
379 /* track is N-channel, this region has less channels; silence the ones
383 memset (mixdown_buffer, 0, sizeof (Sample) * cnt);
385 /* no fades required */
392 if (_flags & FadeIn) {
394 nframes_t fade_in_length = (nframes_t) _fade_in.back()->when;
396 /* see if this read is within the fade in */
398 if (internal_offset < fade_in_length) {
402 limit = min (to_read, fade_in_length - internal_offset);
404 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
406 for (nframes_t n = 0; n < limit; ++n) {
407 mixdown_buffer[n] *= gain_buffer[n];
414 if (_flags & FadeOut) {
416 /* see if some part of this read is within the fade out */
418 /* ................. >| REGION
424 _length - fade_out_length
427 ^internal_offset + to_read
429 we need the intersection of [internal_offset,internal_offset+to_read] with
430 [_length - fade_out_length, _length]
435 nframes_t fade_out_length = (nframes_t) _fade_out.back()->when;
436 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
437 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
439 if (fade_interval_end > fade_interval_start) {
440 /* (part of the) the fade out is in this buffer */
442 nframes_t limit = fade_interval_end - fade_interval_start;
443 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
444 nframes_t fade_offset = fade_interval_start - internal_offset;
446 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
448 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
449 mixdown_buffer[m] *= gain_buffer[n];
455 /* Regular gain curves */
457 if (envelope_active()) {
458 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
460 if (_scale_amplitude != 1.0f) {
461 for (nframes_t n = 0; n < to_read; ++n) {
462 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
465 for (nframes_t n = 0; n < to_read; ++n) {
466 mixdown_buffer[n] *= gain_buffer[n];
469 } else if (_scale_amplitude != 1.0f) {
470 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
477 /* gack. the things we do for users.
482 for (nframes_t n = 0; n < to_read; ++n) {
483 buf[n] += mixdown_buffer[n];
491 AudioRegion::state (bool full)
493 XMLNode& node (Region::state (full));
497 LocaleGuard lg (X_("POSIX"));
499 node.add_property ("flags", enum_2_string (_flags));
501 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
502 node.add_property ("scale-gain", buf);
504 for (uint32_t n=0; n < _sources.size(); ++n) {
505 snprintf (buf2, sizeof(buf2), "source-%d", n);
506 _sources[n]->id().print (buf, sizeof (buf));
507 node.add_property (buf2, buf);
510 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
511 node.add_property ("channels", buf);
515 child = node.add_child (X_("FadeIn"));
517 if ((_flags & DefaultFadeIn)) {
518 child->add_property (X_("default"), X_("yes"));
520 child->add_child_nocopy (_fade_in.get_state ());
523 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
525 child = node.add_child (X_("FadeOut"));
527 if ((_flags & DefaultFadeOut)) {
528 child->add_property (X_("default"), X_("yes"));
530 child->add_child_nocopy (_fade_out.get_state ());
533 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
536 child = node.add_child ("Envelope");
539 bool default_env = false;
541 // If there are only two points, the points are in the start of the region and the end of the region
542 // so, if they are both at 1.0f, that means the default region.
544 if (_envelope.size() == 2 &&
545 _envelope.front()->value == 1.0f &&
546 _envelope.back()->value==1.0f) {
547 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
553 child->add_property ("default", "yes");
555 child->add_child_nocopy (_envelope.get_state ());
559 child->add_property ("default", "yes");
562 if (full && _extra_xml) {
563 node.add_child_copy (*_extra_xml);
570 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
572 const XMLNodeList& nlist = node.children();
573 const XMLProperty *prop;
574 LocaleGuard lg (X_("POSIX"));
576 Region::set_live_state (node, what_changed, false);
578 uint32_t old_flags = _flags;
580 if ((prop = node.property ("flags")) != 0) {
581 _flags = Flag (string_2_enum (prop->value(), _flags));
583 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
585 _flags = Flag (_flags & ~Region::LeftOfSplit);
586 _flags = Flag (_flags & ~Region::RightOfSplit);
589 if ((old_flags ^ _flags) & Muted) {
590 what_changed = Change (what_changed|MuteChanged);
592 if ((old_flags ^ _flags) & Opaque) {
593 what_changed = Change (what_changed|OpacityChanged);
595 if ((old_flags ^ _flags) & Locked) {
596 what_changed = Change (what_changed|LockChanged);
599 if ((prop = node.property ("scale-gain")) != 0) {
600 _scale_amplitude = atof (prop->value().c_str());
602 _scale_amplitude = 1.0;
605 /* Now find envelope description and other misc child items */
607 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
614 if (child->name() == "Envelope") {
618 if ((prop = child->property ("default")) != 0 || _envelope.set_state (*child)) {
619 set_default_envelope ();
622 _envelope.set_max_xval (_length);
623 _envelope.truncate_end (_length);
625 } else if (child->name() == "FadeIn") {
629 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_in.set_state (*child)) {
630 set_default_fade_in ();
633 } else if (child->name() == "FadeOut") {
637 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_out.set_state (*child)) {
638 set_default_fade_out ();
644 send_change (what_changed);
651 AudioRegion::set_state (const XMLNode& node)
653 /* Region::set_state() calls the virtual set_live_state(),
654 which will get us back to AudioRegion::set_live_state()
655 to handle the relevant stuff.
658 return Region::set_state (node);
662 AudioRegion::set_fade_in_shape (FadeShape shape)
664 set_fade_in (shape, (nframes_t) _fade_in.back()->when);
668 AudioRegion::set_fade_out_shape (FadeShape shape)
670 set_fade_out (shape, (nframes_t) _fade_out.back()->when);
674 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
681 _fade_in.fast_simple_add (0.0, 0.0);
682 _fade_in.fast_simple_add (len, 1.0);
686 _fade_in.fast_simple_add (0, 0);
687 _fade_in.fast_simple_add (len * 0.389401, 0.0333333);
688 _fade_in.fast_simple_add (len * 0.629032, 0.0861111);
689 _fade_in.fast_simple_add (len * 0.829493, 0.233333);
690 _fade_in.fast_simple_add (len * 0.9447, 0.483333);
691 _fade_in.fast_simple_add (len * 0.976959, 0.697222);
692 _fade_in.fast_simple_add (len, 1);
696 _fade_in.fast_simple_add (0, 0);
697 _fade_in.fast_simple_add (len * 0.0207373, 0.197222);
698 _fade_in.fast_simple_add (len * 0.0645161, 0.525);
699 _fade_in.fast_simple_add (len * 0.152074, 0.802778);
700 _fade_in.fast_simple_add (len * 0.276498, 0.919444);
701 _fade_in.fast_simple_add (len * 0.481567, 0.980556);
702 _fade_in.fast_simple_add (len * 0.767281, 1);
703 _fade_in.fast_simple_add (len, 1);
707 _fade_in.fast_simple_add (0, 0);
708 _fade_in.fast_simple_add (len * 0.0737327, 0.308333);
709 _fade_in.fast_simple_add (len * 0.246544, 0.658333);
710 _fade_in.fast_simple_add (len * 0.470046, 0.886111);
711 _fade_in.fast_simple_add (len * 0.652074, 0.972222);
712 _fade_in.fast_simple_add (len * 0.771889, 0.988889);
713 _fade_in.fast_simple_add (len, 1);
717 _fade_in.fast_simple_add (0, 0);
718 _fade_in.fast_simple_add (len * 0.304147, 0.0694444);
719 _fade_in.fast_simple_add (len * 0.529954, 0.152778);
720 _fade_in.fast_simple_add (len * 0.725806, 0.333333);
721 _fade_in.fast_simple_add (len * 0.847926, 0.558333);
722 _fade_in.fast_simple_add (len * 0.919355, 0.730556);
723 _fade_in.fast_simple_add (len, 1);
728 _fade_in_shape = shape;
730 send_change (FadeInChanged);
734 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
741 _fade_out.fast_simple_add (len * 0, 1);
742 _fade_out.fast_simple_add (len * 0.023041, 0.697222);
743 _fade_out.fast_simple_add (len * 0.0553, 0.483333);
744 _fade_out.fast_simple_add (len * 0.170507, 0.233333);
745 _fade_out.fast_simple_add (len * 0.370968, 0.0861111);
746 _fade_out.fast_simple_add (len * 0.610599, 0.0333333);
747 _fade_out.fast_simple_add (len * 1, 0);
751 _fade_out.fast_simple_add (len * 0, 1);
752 _fade_out.fast_simple_add (len * 0.228111, 0.988889);
753 _fade_out.fast_simple_add (len * 0.347926, 0.972222);
754 _fade_out.fast_simple_add (len * 0.529954, 0.886111);
755 _fade_out.fast_simple_add (len * 0.753456, 0.658333);
756 _fade_out.fast_simple_add (len * 0.9262673, 0.308333);
757 _fade_out.fast_simple_add (len * 1, 0);
761 _fade_out.fast_simple_add (len * 0, 1);
762 _fade_out.fast_simple_add (len * 0.305556, 1);
763 _fade_out.fast_simple_add (len * 0.548611, 0.991736);
764 _fade_out.fast_simple_add (len * 0.759259, 0.931129);
765 _fade_out.fast_simple_add (len * 0.918981, 0.68595);
766 _fade_out.fast_simple_add (len * 0.976852, 0.22865);
767 _fade_out.fast_simple_add (len * 1, 0);
771 _fade_out.fast_simple_add (len * 0, 1);
772 _fade_out.fast_simple_add (len * 0.080645, 0.730556);
773 _fade_out.fast_simple_add (len * 0.277778, 0.289256);
774 _fade_out.fast_simple_add (len * 0.470046, 0.152778);
775 _fade_out.fast_simple_add (len * 0.695853, 0.0694444);
776 _fade_out.fast_simple_add (len * 1, 0);
780 _fade_out.fast_simple_add (len * 0, 1);
781 _fade_out.fast_simple_add (len * 1, 0);
786 _fade_out_shape = shape;
788 send_change (FadeOutChanged);
792 AudioRegion::set_fade_in_length (nframes_t len)
794 bool changed = _fade_in.extend_to (len);
797 _flags = Flag (_flags & ~DefaultFadeIn);
798 send_change (FadeInChanged);
803 AudioRegion::set_fade_out_length (nframes_t len)
805 bool changed = _fade_out.extend_to (len);
808 _flags = Flag (_flags & ~DefaultFadeOut);
811 send_change (FadeOutChanged);
815 AudioRegion::set_fade_in_active (bool yn)
817 if (yn == (_flags & FadeIn)) {
821 _flags = Flag (_flags|FadeIn);
823 _flags = Flag (_flags & ~FadeIn);
826 send_change (FadeInActiveChanged);
830 AudioRegion::set_fade_out_active (bool yn)
832 if (yn == (_flags & FadeOut)) {
836 _flags = Flag (_flags | FadeOut);
838 _flags = Flag (_flags & ~FadeOut);
841 send_change (FadeOutActiveChanged);
845 AudioRegion::fade_in_is_default () const
847 return _fade_in_shape == Linear && _fade_in.back()->when == 64;
851 AudioRegion::fade_out_is_default () const
853 return _fade_out_shape == Linear && _fade_out.back()->when == 64;
857 AudioRegion::set_default_fade_in ()
859 set_fade_in (Linear, 64);
863 AudioRegion::set_default_fade_out ()
865 set_fade_out (Linear, 64);
869 AudioRegion::set_default_fades ()
871 _fade_in_disabled = 0;
872 _fade_out_disabled = 0;
873 set_default_fade_in ();
874 set_default_fade_out ();
878 AudioRegion::set_default_envelope ()
882 _envelope.fast_simple_add (0, 1.0f);
883 _envelope.fast_simple_add (_length, 1.0f);
888 AudioRegion::recompute_at_end ()
890 /* our length has changed. recompute a new final point by interpolating
891 based on the the existing curve.
895 _envelope.truncate_end (_length);
896 _envelope.set_max_xval (_length);
899 if (_fade_in.back()->when > _length) {
900 _fade_in.extend_to (_length);
901 send_change (FadeInChanged);
904 if (_fade_out.back()->when > _length) {
905 _fade_out.extend_to (_length);
906 send_change (FadeOutChanged);
911 AudioRegion::recompute_at_start ()
913 /* as above, but the shift was from the front */
915 _envelope.truncate_start (_length);
917 if (_fade_in.back()->when > _length) {
918 _fade_in.extend_to (_length);
919 send_change (FadeInChanged);
922 if (_fade_out.back()->when > _length) {
923 _fade_out.extend_to (_length);
924 send_change (FadeOutChanged);
929 AudioRegion::separate_by_channel (Session& session, vector<boost::shared_ptr<AudioRegion> >& v) const
935 if (_sources.size() < 2) {
941 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
948 if (_sources.size() == 2) {
956 new_name += ('0' + n + 1);
959 /* create a copy with just one source. prevent if from being thought of as "whole file" even if
960 it covers the entire source file(s).
963 Flag f = Flag (_flags & ~WholeFile);
965 boost::shared_ptr<Region> r = RegionFactory::create (srcs, _start, _length, new_name, _layer, f);
966 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
977 AudioRegion::apply (AudioFilter& filter)
979 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
980 return filter.run (ar);
984 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
986 const nframes_t blocksize = 4096;
990 spec.channels = _sources.size();
992 if (spec.prepare (blocksize, session.frame_rate())) {
997 spec.total_frames = _length;
999 while (spec.pos < _length && !spec.stop) {
1002 /* step 1: interleave */
1004 to_read = min (_length - spec.pos, blocksize);
1006 if (spec.channels == 1) {
1008 if (audio_source()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
1014 Sample buf[blocksize];
1016 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1018 if (audio_source(chan)->read (buf, _start + spec.pos, to_read) != to_read) {
1022 for (nframes_t x = 0; x < to_read; ++x) {
1023 spec.dataF[chan+(x*spec.channels)] = buf[x];
1028 if (spec.process (to_read)) {
1032 spec.pos += to_read;
1033 spec.progress = (double) spec.pos /_length;
1040 spec.running = false;
1041 spec.status = status;
1048 AudioRegion::set_scale_amplitude (gain_t g)
1050 boost::shared_ptr<Playlist> pl (playlist());
1052 _scale_amplitude = g;
1054 /* tell the diskstream we're in */
1060 /* tell everybody else */
1062 send_change (ScaleAmplitudeChanged);
1066 AudioRegion::normalize_to (float target_dB)
1068 const nframes_t blocksize = 64 * 1024;
1069 Sample buf[blocksize];
1074 gain_t target = dB_to_coefficient (target_dB);
1076 if (target == 1.0f) {
1077 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1078 that we may have clipped.
1080 target -= FLT_EPSILON;
1084 fend = _start + _length;
1086 /* first pass: find max amplitude */
1088 while (fpos < fend) {
1092 to_read = min (fend - fpos, blocksize);
1094 for (n = 0; n < n_channels(); ++n) {
1098 if (audio_source (n)->read (buf, fpos, to_read) != to_read) {
1102 maxamp = Session::compute_peak (buf, to_read, maxamp);
1108 if (maxamp == 0.0f) {
1109 /* don't even try */
1113 if (maxamp == target) {
1114 /* we can't do anything useful */
1118 /* compute scale factor */
1120 _scale_amplitude = target/maxamp;
1122 /* tell the diskstream we're in */
1124 boost::shared_ptr<Playlist> pl (playlist());
1130 /* tell everybody else */
1132 send_change (ScaleAmplitudeChanged);
1136 AudioRegion::fade_in_changed ()
1138 send_change (FadeInChanged);
1142 AudioRegion::fade_out_changed ()
1144 send_change (FadeOutChanged);
1148 AudioRegion::envelope_changed ()
1150 send_change (EnvelopeChanged);
1154 AudioRegion::suspend_fade_in ()
1156 if (++_fade_in_disabled == 1) {
1157 if (fade_in_is_default()) {
1158 set_fade_in_active (false);
1164 AudioRegion::resume_fade_in ()
1166 if (--_fade_in_disabled == 0 && _fade_in_disabled) {
1167 set_fade_in_active (true);
1172 AudioRegion::suspend_fade_out ()
1174 if (++_fade_out_disabled == 1) {
1175 if (fade_out_is_default()) {
1176 set_fade_out_active (false);
1182 AudioRegion::resume_fade_out ()
1184 if (--_fade_out_disabled == 0 &&_fade_out_disabled) {
1185 set_fade_out_active (true);
1190 AudioRegion::speed_mismatch (float sr) const
1192 if (_sources.empty()) {
1193 /* impossible, but ... */
1197 float fsr = audio_source()->sample_rate();
1203 AudioRegion::source_offset_changed ()
1205 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(_sources.front());
1207 if (afs && afs->destructive()) {
1208 // set_start (source()->natural_position(), this);
1209 set_position (source()->natural_position(), this);
1213 boost::shared_ptr<AudioSource>
1214 AudioRegion::audio_source (uint32_t n) const
1216 // Guaranteed to succeed (use a static cast for speed?)
1217 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1222 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)
1224 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1227 uint32_t region_length_from_c (void *arg)
1230 return ((AudioRegion *) arg)->length();
1233 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1235 return ( (AudioRegion *) arg)->audio_source()->available_peaks (zoom_factor) ;