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;
335 /* precondition: caller has verified that we cover the desired section */
337 if (chan_n >= _sources.size()) {
338 return 0; /* read nothing */
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;
366 return 0; /* read nothing */
369 _read_data_count = 0;
371 boost::shared_ptr<AudioSource> src = audio_source(chan_n);
372 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 if (_flags & FadeIn) {
382 nframes_t fade_in_length = (nframes_t) _fade_in.back()->when;
384 /* see if this read is within the fade in */
386 if (internal_offset < fade_in_length) {
390 limit = min (to_read, fade_in_length - internal_offset);
392 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
394 for (nframes_t n = 0; n < limit; ++n) {
395 mixdown_buffer[n] *= gain_buffer[n];
402 if (_flags & FadeOut) {
404 /* see if some part of this read is within the fade out */
406 /* ................. >| REGION
412 _length - fade_out_length
415 ^internal_offset + to_read
417 we need the intersection of [internal_offset,internal_offset+to_read] with
418 [_length - fade_out_length, _length]
423 nframes_t fade_out_length = (nframes_t) _fade_out.back()->when;
424 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
425 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
427 if (fade_interval_end > fade_interval_start) {
428 /* (part of the) the fade out is in this buffer */
430 nframes_t limit = fade_interval_end - fade_interval_start;
431 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
432 nframes_t fade_offset = fade_interval_start - internal_offset;
434 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
436 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
437 mixdown_buffer[m] *= gain_buffer[n];
443 /* Regular gain curves */
445 if (envelope_active()) {
446 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
448 if (_scale_amplitude != 1.0f) {
449 for (nframes_t n = 0; n < to_read; ++n) {
450 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
453 for (nframes_t n = 0; n < to_read; ++n) {
454 mixdown_buffer[n] *= gain_buffer[n];
457 } else if (_scale_amplitude != 1.0f) {
458 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
463 /* gack. the things we do for users.
468 for (nframes_t n = 0; n < to_read; ++n) {
469 buf[n] += mixdown_buffer[n];
477 AudioRegion::state (bool full)
479 XMLNode& node (Region::state (full));
483 LocaleGuard lg (X_("POSIX"));
485 node.add_property ("flags", enum_2_string (_flags));
487 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
488 node.add_property ("scale-gain", buf);
490 for (uint32_t n=0; n < _sources.size(); ++n) {
491 snprintf (buf2, sizeof(buf2), "source-%d", n);
492 _sources[n]->id().print (buf, sizeof (buf));
493 node.add_property (buf2, buf);
496 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
497 node.add_property ("channels", buf);
501 child = node.add_child (X_("FadeIn"));
503 if ((_flags & DefaultFadeIn)) {
504 child->add_property (X_("default"), X_("yes"));
506 child->add_child_nocopy (_fade_in.get_state ());
509 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
511 child = node.add_child (X_("FadeOut"));
513 if ((_flags & DefaultFadeOut)) {
514 child->add_property (X_("default"), X_("yes"));
516 child->add_child_nocopy (_fade_out.get_state ());
519 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
522 child = node.add_child ("Envelope");
525 bool default_env = false;
527 // If there are only two points, the points are in the start of the region and the end of the region
528 // so, if they are both at 1.0f, that means the default region.
530 if (_envelope.size() == 2 &&
531 _envelope.front()->value == 1.0f &&
532 _envelope.back()->value==1.0f) {
533 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
539 child->add_property ("default", "yes");
541 child->add_child_nocopy (_envelope.get_state ());
545 child->add_property ("default", "yes");
548 if (full && _extra_xml) {
549 node.add_child_copy (*_extra_xml);
556 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
558 const XMLNodeList& nlist = node.children();
559 const XMLProperty *prop;
560 LocaleGuard lg (X_("POSIX"));
562 Region::set_live_state (node, what_changed, false);
564 uint32_t old_flags = _flags;
566 if ((prop = node.property ("flags")) != 0) {
567 _flags = Flag (string_2_enum (prop->value(), _flags));
569 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
571 _flags = Flag (_flags & ~Region::LeftOfSplit);
572 _flags = Flag (_flags & ~Region::RightOfSplit);
575 if ((old_flags ^ _flags) & Muted) {
576 what_changed = Change (what_changed|MuteChanged);
578 if ((old_flags ^ _flags) & Opaque) {
579 what_changed = Change (what_changed|OpacityChanged);
581 if ((old_flags ^ _flags) & Locked) {
582 what_changed = Change (what_changed|LockChanged);
585 if ((prop = node.property ("scale-gain")) != 0) {
586 _scale_amplitude = atof (prop->value().c_str());
588 _scale_amplitude = 1.0;
591 /* Now find envelope description and other misc child items */
593 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
600 if (child->name() == "Envelope") {
604 if ((prop = child->property ("default")) != 0 || _envelope.set_state (*child)) {
605 set_default_envelope ();
608 _envelope.set_max_xval (_length);
609 _envelope.truncate_end (_length);
611 } else if (child->name() == "FadeIn") {
615 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_in.set_state (*child)) {
616 set_default_fade_in ();
619 } else if (child->name() == "FadeOut") {
623 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_out.set_state (*child)) {
624 set_default_fade_out ();
630 send_change (what_changed);
637 AudioRegion::set_state (const XMLNode& node)
639 /* Region::set_state() calls the virtual set_live_state(),
640 which will get us back to AudioRegion::set_live_state()
641 to handle the relevant stuff.
644 return Region::set_state (node);
648 AudioRegion::set_fade_in_shape (FadeShape shape)
650 set_fade_in (shape, (nframes_t) _fade_in.back()->when);
654 AudioRegion::set_fade_out_shape (FadeShape shape)
656 set_fade_out (shape, (nframes_t) _fade_out.back()->when);
660 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
667 _fade_in.fast_simple_add (0.0, 0.0);
668 _fade_in.fast_simple_add (len, 1.0);
672 _fade_in.fast_simple_add (0, 0);
673 _fade_in.fast_simple_add (len * 0.389401, 0.0333333);
674 _fade_in.fast_simple_add (len * 0.629032, 0.0861111);
675 _fade_in.fast_simple_add (len * 0.829493, 0.233333);
676 _fade_in.fast_simple_add (len * 0.9447, 0.483333);
677 _fade_in.fast_simple_add (len * 0.976959, 0.697222);
678 _fade_in.fast_simple_add (len, 1);
682 _fade_in.fast_simple_add (0, 0);
683 _fade_in.fast_simple_add (len * 0.0207373, 0.197222);
684 _fade_in.fast_simple_add (len * 0.0645161, 0.525);
685 _fade_in.fast_simple_add (len * 0.152074, 0.802778);
686 _fade_in.fast_simple_add (len * 0.276498, 0.919444);
687 _fade_in.fast_simple_add (len * 0.481567, 0.980556);
688 _fade_in.fast_simple_add (len * 0.767281, 1);
689 _fade_in.fast_simple_add (len, 1);
693 _fade_in.fast_simple_add (0, 0);
694 _fade_in.fast_simple_add (len * 0.0737327, 0.308333);
695 _fade_in.fast_simple_add (len * 0.246544, 0.658333);
696 _fade_in.fast_simple_add (len * 0.470046, 0.886111);
697 _fade_in.fast_simple_add (len * 0.652074, 0.972222);
698 _fade_in.fast_simple_add (len * 0.771889, 0.988889);
699 _fade_in.fast_simple_add (len, 1);
703 _fade_in.fast_simple_add (0, 0);
704 _fade_in.fast_simple_add (len * 0.304147, 0.0694444);
705 _fade_in.fast_simple_add (len * 0.529954, 0.152778);
706 _fade_in.fast_simple_add (len * 0.725806, 0.333333);
707 _fade_in.fast_simple_add (len * 0.847926, 0.558333);
708 _fade_in.fast_simple_add (len * 0.919355, 0.730556);
709 _fade_in.fast_simple_add (len, 1);
714 _fade_in_shape = shape;
716 send_change (FadeInChanged);
720 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
727 _fade_out.fast_simple_add (len * 0, 1);
728 _fade_out.fast_simple_add (len * 0.023041, 0.697222);
729 _fade_out.fast_simple_add (len * 0.0553, 0.483333);
730 _fade_out.fast_simple_add (len * 0.170507, 0.233333);
731 _fade_out.fast_simple_add (len * 0.370968, 0.0861111);
732 _fade_out.fast_simple_add (len * 0.610599, 0.0333333);
733 _fade_out.fast_simple_add (len * 1, 0);
737 _fade_out.fast_simple_add (len * 0, 1);
738 _fade_out.fast_simple_add (len * 0.228111, 0.988889);
739 _fade_out.fast_simple_add (len * 0.347926, 0.972222);
740 _fade_out.fast_simple_add (len * 0.529954, 0.886111);
741 _fade_out.fast_simple_add (len * 0.753456, 0.658333);
742 _fade_out.fast_simple_add (len * 0.9262673, 0.308333);
743 _fade_out.fast_simple_add (len * 1, 0);
747 _fade_out.fast_simple_add (len * 0, 1);
748 _fade_out.fast_simple_add (len * 0.305556, 1);
749 _fade_out.fast_simple_add (len * 0.548611, 0.991736);
750 _fade_out.fast_simple_add (len * 0.759259, 0.931129);
751 _fade_out.fast_simple_add (len * 0.918981, 0.68595);
752 _fade_out.fast_simple_add (len * 0.976852, 0.22865);
753 _fade_out.fast_simple_add (len * 1, 0);
757 _fade_out.fast_simple_add (len * 0, 1);
758 _fade_out.fast_simple_add (len * 0.080645, 0.730556);
759 _fade_out.fast_simple_add (len * 0.277778, 0.289256);
760 _fade_out.fast_simple_add (len * 0.470046, 0.152778);
761 _fade_out.fast_simple_add (len * 0.695853, 0.0694444);
762 _fade_out.fast_simple_add (len * 1, 0);
766 _fade_out.fast_simple_add (len * 0, 1);
767 _fade_out.fast_simple_add (len * 1, 0);
772 _fade_out_shape = shape;
774 send_change (FadeOutChanged);
778 AudioRegion::set_fade_in_length (nframes_t len)
780 bool changed = _fade_in.extend_to (len);
783 _flags = Flag (_flags & ~DefaultFadeIn);
784 send_change (FadeInChanged);
789 AudioRegion::set_fade_out_length (nframes_t len)
791 bool changed = _fade_out.extend_to (len);
794 _flags = Flag (_flags & ~DefaultFadeOut);
797 send_change (FadeOutChanged);
801 AudioRegion::set_fade_in_active (bool yn)
803 if (yn == (_flags & FadeIn)) {
807 _flags = Flag (_flags|FadeIn);
809 _flags = Flag (_flags & ~FadeIn);
812 send_change (FadeInActiveChanged);
816 AudioRegion::set_fade_out_active (bool yn)
818 if (yn == (_flags & FadeOut)) {
822 _flags = Flag (_flags | FadeOut);
824 _flags = Flag (_flags & ~FadeOut);
827 send_change (FadeOutActiveChanged);
831 AudioRegion::set_default_fade_in ()
833 set_fade_in (Linear, 64);
837 AudioRegion::set_default_fade_out ()
839 set_fade_out (Linear, 64);
843 AudioRegion::set_default_fades ()
845 _fade_in_disabled = 0;
846 _fade_out_disabled = 0;
847 set_default_fade_in ();
848 set_default_fade_out ();
852 AudioRegion::set_default_envelope ()
856 _envelope.fast_simple_add (0, 1.0f);
857 _envelope.fast_simple_add (_length, 1.0f);
862 AudioRegion::recompute_at_end ()
864 /* our length has changed. recompute a new final point by interpolating
865 based on the the existing curve.
869 _envelope.truncate_end (_length);
870 _envelope.set_max_xval (_length);
873 if (_fade_in.back()->when > _length) {
874 _fade_in.extend_to (_length);
875 send_change (FadeInChanged);
878 if (_fade_out.back()->when > _length) {
879 _fade_out.extend_to (_length);
880 send_change (FadeOutChanged);
885 AudioRegion::recompute_at_start ()
887 /* as above, but the shift was from the front */
889 _envelope.truncate_start (_length);
891 if (_fade_in.back()->when > _length) {
892 _fade_in.extend_to (_length);
893 send_change (FadeInChanged);
896 if (_fade_out.back()->when > _length) {
897 _fade_out.extend_to (_length);
898 send_change (FadeOutChanged);
903 AudioRegion::separate_by_channel (Session& session, vector<boost::shared_ptr<AudioRegion> >& v) const
909 if (_sources.size() < 2) {
915 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
922 if (_sources.size() == 2) {
930 new_name += ('0' + n + 1);
933 /* create a copy with just one source. prevent if from being thought of as "whole file" even if
934 it covers the entire source file(s).
937 Flag f = Flag (_flags & ~WholeFile);
939 boost::shared_ptr<Region> r = RegionFactory::create (srcs, _start, _length, new_name, _layer, f);
940 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
941 cerr << "new region name is " << ar->name() << endl;
952 AudioRegion::apply (AudioFilter& filter)
954 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
955 return filter.run (ar);
959 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
961 const nframes_t blocksize = 4096;
965 spec.channels = _sources.size();
967 if (spec.prepare (blocksize, session.frame_rate())) {
972 spec.total_frames = _length;
974 while (spec.pos < _length && !spec.stop) {
977 /* step 1: interleave */
979 to_read = min (_length - spec.pos, blocksize);
981 if (spec.channels == 1) {
983 if (audio_source()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
989 Sample buf[blocksize];
991 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
993 if (audio_source(chan)->read (buf, _start + spec.pos, to_read) != to_read) {
997 for (nframes_t x = 0; x < to_read; ++x) {
998 spec.dataF[chan+(x*spec.channels)] = buf[x];
1003 if (spec.process (to_read)) {
1007 spec.pos += to_read;
1008 spec.progress = (double) spec.pos /_length;
1015 spec.running = false;
1016 spec.status = status;
1023 AudioRegion::set_scale_amplitude (gain_t g)
1025 boost::shared_ptr<Playlist> pl (playlist());
1027 _scale_amplitude = g;
1029 /* tell the diskstream we're in */
1035 /* tell everybody else */
1037 send_change (ScaleAmplitudeChanged);
1041 AudioRegion::normalize_to (float target_dB)
1043 const nframes_t blocksize = 64 * 1024;
1044 Sample buf[blocksize];
1049 gain_t target = dB_to_coefficient (target_dB);
1051 if (target == 1.0f) {
1052 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1053 that we may have clipped.
1055 target -= FLT_EPSILON;
1059 fend = _start + _length;
1061 /* first pass: find max amplitude */
1063 while (fpos < fend) {
1067 to_read = min (fend - fpos, blocksize);
1069 for (n = 0; n < n_channels(); ++n) {
1073 if (audio_source (n)->read (buf, fpos, to_read) != to_read) {
1077 maxamp = Session::compute_peak (buf, to_read, maxamp);
1083 if (maxamp == 0.0f) {
1084 /* don't even try */
1088 if (maxamp == target) {
1089 /* we can't do anything useful */
1093 /* compute scale factor */
1095 _scale_amplitude = target/maxamp;
1097 /* tell the diskstream we're in */
1099 boost::shared_ptr<Playlist> pl (playlist());
1102 cerr << "Send modified\n";
1106 /* tell everybody else */
1108 send_change (ScaleAmplitudeChanged);
1112 AudioRegion::fade_in_changed ()
1114 send_change (FadeInChanged);
1118 AudioRegion::fade_out_changed ()
1120 send_change (FadeOutChanged);
1124 AudioRegion::envelope_changed ()
1126 send_change (EnvelopeChanged);
1130 AudioRegion::suspend_fade_in ()
1132 if (++_fade_in_disabled == 1) {
1133 set_fade_in_active (false);
1138 AudioRegion::resume_fade_in ()
1140 if (_fade_in_disabled && --_fade_in_disabled == 0) {
1141 set_fade_in_active (true);
1146 AudioRegion::suspend_fade_out ()
1148 if (++_fade_out_disabled == 1) {
1149 set_fade_out_active (false);
1154 AudioRegion::resume_fade_out ()
1156 if (_fade_out_disabled && --_fade_out_disabled == 0) {
1157 set_fade_out_active (true);
1162 AudioRegion::speed_mismatch (float sr) const
1164 if (_sources.empty()) {
1165 /* impossible, but ... */
1169 float fsr = audio_source()->sample_rate();
1175 AudioRegion::source_offset_changed ()
1177 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(_sources.front());
1179 if (afs && afs->destructive()) {
1180 // set_start (source()->natural_position(), this);
1181 set_position (source()->natural_position(), this);
1185 boost::shared_ptr<AudioSource>
1186 AudioRegion::audio_source (uint32_t n) const
1188 // Guaranteed to succeed (use a static cast?)
1189 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1194 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)
1196 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1199 uint32_t region_length_from_c (void *arg)
1202 return ((AudioRegion *) arg)->length();
1205 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1207 return ( (AudioRegion *) arg)->audio_source()->available_peaks (zoom_factor) ;