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/audiofilesource.h>
42 #include <ardour/region_factory.h>
43 #include <ardour/runtime_functions.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();
64 _scale_amplitude = 1.0;
67 set_default_envelope ();
69 listen_to_my_curves ();
72 /* constructor for use by derived types only */
73 AudioRegion::AudioRegion (nframes_t start, nframes_t length, string name)
74 : Region (start, length, name, DataType::AUDIO)
75 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
76 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
77 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
82 /** Basic AudioRegion constructor (one channel) */
83 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length)
84 : Region (src, start, length, PBD::basename_nosuffix(src->name()), DataType::AUDIO, 0, Region::Flag(Region::DefaultFlags|Region::External))
85 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
86 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
87 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
89 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
91 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
97 /* Basic AudioRegion constructor (one channel) */
98 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
99 : Region (src, start, length, name, DataType::AUDIO, layer, flags)
100 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
101 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
102 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
104 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
106 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
112 /* Basic AudioRegion constructor (many channels) */
113 AudioRegion::AudioRegion (SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
114 : Region (srcs, start, length, name, DataType::AUDIO, layer, flags)
115 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
116 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
117 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
123 /** Create a new AudioRegion, that is part of an existing one */
124 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
125 : Region (other, offset, length, name, layer, flags)
126 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
127 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
128 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
130 /* return to default fades if the existing ones are too long */
131 _fade_in_disabled = 0;
132 _fade_out_disabled = 0;
135 if (_flags & LeftOfSplit) {
136 if (_fade_in->back()->when >= _length) {
137 set_default_fade_in ();
139 _fade_in_disabled = other->_fade_in_disabled;
141 set_default_fade_out ();
142 _flags = Flag (_flags & ~Region::LeftOfSplit);
145 if (_flags & RightOfSplit) {
146 if (_fade_out->back()->when >= _length) {
147 set_default_fade_out ();
149 _fade_out_disabled = other->_fade_out_disabled;
151 set_default_fade_in ();
152 _flags = Flag (_flags & ~Region::RightOfSplit);
155 _scale_amplitude = other->_scale_amplitude;
157 listen_to_my_curves ();
159 assert(_type == DataType::AUDIO);
162 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
164 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
165 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
166 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
168 _scale_amplitude = other->_scale_amplitude;
169 _envelope = other->_envelope;
171 _fade_in_disabled = 0;
172 _fade_out_disabled = 0;
174 listen_to_my_curves ();
176 assert(_type == DataType::AUDIO);
179 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, const XMLNode& node)
181 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
182 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
183 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
185 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
187 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
190 set_default_fades ();
192 if (set_state (node)) {
193 throw failed_constructor();
196 listen_to_my_curves ();
198 assert(_type == DataType::AUDIO);
201 AudioRegion::AudioRegion (SourceList& srcs, const XMLNode& node)
202 : Region (srcs, node)
203 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
204 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
205 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
207 set_default_fades ();
208 _scale_amplitude = 1.0;
210 if (set_state (node)) {
211 throw failed_constructor();
214 listen_to_my_curves ();
216 assert(_type == DataType::AUDIO);
219 AudioRegion::~AudioRegion ()
224 AudioRegion::listen_to_my_curves ()
226 _envelope->StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
227 _fade_in->StateChanged.connect (mem_fun (*this, &AudioRegion::fade_in_changed));
228 _fade_out->StateChanged.connect (mem_fun (*this, &AudioRegion::fade_out_changed));
232 AudioRegion::verify_length (nframes_t len)
234 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
236 if (afs && afs->destructive()) {
239 return Region::verify_length(len);
244 AudioRegion::verify_start_and_length (nframes_t new_start, nframes_t new_length)
246 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
248 if (afs && afs->destructive()) {
251 return Region::verify_start_and_length(new_start, new_length);
256 AudioRegion::verify_start (nframes_t pos)
258 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
260 if (afs && afs->destructive()) {
263 return Region::verify_start(pos);
268 AudioRegion::verify_start_mutable (nframes_t& new_start)
270 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
272 if (afs && afs->destructive()) {
275 return Region::verify_start_mutable(new_start);
280 AudioRegion::set_envelope_active (bool yn)
282 if (envelope_active() != yn) {
285 snprintf (buf, sizeof (buf), "envelope active");
286 _flags = Flag (_flags|EnvelopeActive);
288 snprintf (buf, sizeof (buf), "envelope off");
289 _flags = Flag (_flags & ~EnvelopeActive);
291 send_change (EnvelopeActiveChanged);
296 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
298 if (chan_n >= _sources.size()) {
302 if (audio_source(chan_n)->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
305 if (_scale_amplitude != 1.0) {
306 for (nframes_t n = 0; n < npeaks; ++n) {
307 buf[n].max *= _scale_amplitude;
308 buf[n].min *= _scale_amplitude;
316 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position, nframes_t cnt, uint32_t chan_n) const
318 return _read_at (_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n);
322 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
323 nframes_t cnt, uint32_t chan_n) const
325 return _read_at (_master_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n);
329 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
330 nframes_t position, nframes_t cnt, uint32_t chan_n) const
332 // cerr << _name << "._read_at(" << position << ") - " << _position << endl;
334 nframes_t internal_offset;
335 nframes_t buf_offset;
339 return 0; /* read nothing */
342 /* precondition: caller has verified that we cover the desired section */
344 if (position < _position) {
346 buf_offset = _position - position;
349 internal_offset = position - _position;
353 if (internal_offset >= _length) {
354 return 0; /* read nothing */
357 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
358 return 0; /* read nothing */
362 /* overwrite whatever is there */
363 mixdown_buffer = buf + buf_offset;
365 mixdown_buffer += buf_offset;
368 _read_data_count = 0;
370 if (chan_n < n_channels()) {
372 boost::shared_ptr<AudioSource> src = audio_source(chan_n);
373 if (src->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
375 return 0; /* "read nothing" */
378 _read_data_count += src->read_data_count();
382 /* track is N-channel, this region has less channels; silence the ones
386 memset (mixdown_buffer, 0, sizeof (Sample) * cnt);
388 /* no fades required */
395 if (_flags & FadeIn) {
397 nframes_t fade_in_length = (nframes_t) _fade_in->back()->when;
399 /* see if this read is within the fade in */
401 if (internal_offset < fade_in_length) {
405 limit = min (to_read, fade_in_length - internal_offset);
407 _fade_in->curve().get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
409 for (nframes_t n = 0; n < limit; ++n) {
410 mixdown_buffer[n] *= gain_buffer[n];
417 if (_flags & FadeOut) {
419 /* see if some part of this read is within the fade out */
421 /* ................. >| REGION
427 _length - fade_out_length
430 ^internal_offset + to_read
432 we need the intersection of [internal_offset,internal_offset+to_read] with
433 [_length - fade_out_length, _length]
438 nframes_t fade_out_length = (nframes_t) _fade_out->back()->when;
439 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
440 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
442 if (fade_interval_end > fade_interval_start) {
443 /* (part of the) the fade out is in this buffer */
445 nframes_t limit = fade_interval_end - fade_interval_start;
446 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
447 nframes_t fade_offset = fade_interval_start - internal_offset;
449 _fade_out->curve().get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
451 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
452 mixdown_buffer[m] *= gain_buffer[n];
458 /* Regular gain curves */
460 if (envelope_active()) {
461 _envelope->curve().get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
463 if (_scale_amplitude != 1.0f) {
464 for (nframes_t n = 0; n < to_read; ++n) {
465 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
468 for (nframes_t n = 0; n < to_read; ++n) {
469 mixdown_buffer[n] *= gain_buffer[n];
472 } else if (_scale_amplitude != 1.0f) {
473 apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
480 /* gack. the things we do for users.
485 for (nframes_t n = 0; n < to_read; ++n) {
486 buf[n] += mixdown_buffer[n];
494 AudioRegion::state (bool full)
496 XMLNode& node (Region::state (full));
500 LocaleGuard lg (X_("POSIX"));
502 node.add_property ("flags", enum_2_string (_flags));
504 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
505 node.add_property ("scale-gain", buf);
507 for (uint32_t n=0; n < _sources.size(); ++n) {
508 snprintf (buf2, sizeof(buf2), "source-%d", n);
509 _sources[n]->id().print (buf, sizeof (buf));
510 node.add_property (buf2, buf);
513 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
514 node.add_property ("channels", buf);
518 child = node.add_child (X_("FadeIn"));
520 if ((_flags & DefaultFadeIn)) {
521 child->add_property (X_("default"), X_("yes"));
523 child->add_child_nocopy (_fade_in->get_state ());
526 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
528 child = node.add_child (X_("FadeOut"));
530 if ((_flags & DefaultFadeOut)) {
531 child->add_property (X_("default"), X_("yes"));
533 child->add_child_nocopy (_fade_out->get_state ());
536 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
539 child = node.add_child ("Envelope");
542 bool default_env = false;
544 // If there are only two points, the points are in the start of the region and the end of the region
545 // so, if they are both at 1.0f, that means the default region.
547 if (_envelope->size() == 2 &&
548 _envelope->front()->value == 1.0f &&
549 _envelope->back()->value==1.0f) {
550 if (_envelope->front()->when == 0 && _envelope->back()->when == _length) {
556 child->add_property ("default", "yes");
558 child->add_child_nocopy (_envelope->get_state ());
562 child->add_property ("default", "yes");
565 if (full && _extra_xml) {
566 node.add_child_copy (*_extra_xml);
573 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
575 const XMLNodeList& nlist = node.children();
576 const XMLProperty *prop;
577 LocaleGuard lg (X_("POSIX"));
579 Region::set_live_state (node, what_changed, false);
581 uint32_t old_flags = _flags;
583 if ((prop = node.property ("flags")) != 0) {
584 _flags = Flag (string_2_enum (prop->value(), _flags));
586 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
588 _flags = Flag (_flags & ~Region::LeftOfSplit);
589 _flags = Flag (_flags & ~Region::RightOfSplit);
592 if ((old_flags ^ _flags) & Muted) {
593 what_changed = Change (what_changed|MuteChanged);
595 if ((old_flags ^ _flags) & Opaque) {
596 what_changed = Change (what_changed|OpacityChanged);
598 if ((old_flags ^ _flags) & Locked) {
599 what_changed = Change (what_changed|LockChanged);
602 if ((prop = node.property ("scale-gain")) != 0) {
603 _scale_amplitude = atof (prop->value().c_str());
605 _scale_amplitude = 1.0;
608 /* Now find envelope description and other misc child items */
610 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
617 if (child->name() == "Envelope") {
621 if ((prop = child->property ("default")) != 0 || _envelope->set_state (*child)) {
622 set_default_envelope ();
625 _envelope->set_max_xval (_length);
626 _envelope->truncate_end (_length);
628 } else if (child->name() == "FadeIn") {
632 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_in->set_state (*child)) {
633 set_default_fade_in ();
636 } else if (child->name() == "FadeOut") {
640 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_out->set_state (*child)) {
641 set_default_fade_out ();
647 send_change (what_changed);
654 AudioRegion::set_state (const XMLNode& node)
656 /* Region::set_state() calls the virtual set_live_state(),
657 which will get us back to AudioRegion::set_live_state()
658 to handle the relevant stuff.
661 return Region::set_state (node);
665 AudioRegion::set_fade_in_shape (FadeShape shape)
667 set_fade_in (shape, (nframes_t) _fade_in->back()->when);
671 AudioRegion::set_fade_out_shape (FadeShape shape)
673 set_fade_out (shape, (nframes_t) _fade_out->back()->when);
677 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
684 _fade_in->fast_simple_add (0.0, 0.0);
685 _fade_in->fast_simple_add (len, 1.0);
689 _fade_in->fast_simple_add (0, 0);
690 _fade_in->fast_simple_add (len * 0.389401, 0.0333333);
691 _fade_in->fast_simple_add (len * 0.629032, 0.0861111);
692 _fade_in->fast_simple_add (len * 0.829493, 0.233333);
693 _fade_in->fast_simple_add (len * 0.9447, 0.483333);
694 _fade_in->fast_simple_add (len * 0.976959, 0.697222);
695 _fade_in->fast_simple_add (len, 1);
699 _fade_in->fast_simple_add (0, 0);
700 _fade_in->fast_simple_add (len * 0.0207373, 0.197222);
701 _fade_in->fast_simple_add (len * 0.0645161, 0.525);
702 _fade_in->fast_simple_add (len * 0.152074, 0.802778);
703 _fade_in->fast_simple_add (len * 0.276498, 0.919444);
704 _fade_in->fast_simple_add (len * 0.481567, 0.980556);
705 _fade_in->fast_simple_add (len * 0.767281, 1);
706 _fade_in->fast_simple_add (len, 1);
710 _fade_in->fast_simple_add (0, 0);
711 _fade_in->fast_simple_add (len * 0.0737327, 0.308333);
712 _fade_in->fast_simple_add (len * 0.246544, 0.658333);
713 _fade_in->fast_simple_add (len * 0.470046, 0.886111);
714 _fade_in->fast_simple_add (len * 0.652074, 0.972222);
715 _fade_in->fast_simple_add (len * 0.771889, 0.988889);
716 _fade_in->fast_simple_add (len, 1);
720 _fade_in->fast_simple_add (0, 0);
721 _fade_in->fast_simple_add (len * 0.304147, 0.0694444);
722 _fade_in->fast_simple_add (len * 0.529954, 0.152778);
723 _fade_in->fast_simple_add (len * 0.725806, 0.333333);
724 _fade_in->fast_simple_add (len * 0.847926, 0.558333);
725 _fade_in->fast_simple_add (len * 0.919355, 0.730556);
726 _fade_in->fast_simple_add (len, 1);
731 _fade_in_shape = shape;
733 send_change (FadeInChanged);
737 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
739 _fade_out->freeze ();
744 _fade_out->fast_simple_add (len * 0, 1);
745 _fade_out->fast_simple_add (len * 0.023041, 0.697222);
746 _fade_out->fast_simple_add (len * 0.0553, 0.483333);
747 _fade_out->fast_simple_add (len * 0.170507, 0.233333);
748 _fade_out->fast_simple_add (len * 0.370968, 0.0861111);
749 _fade_out->fast_simple_add (len * 0.610599, 0.0333333);
750 _fade_out->fast_simple_add (len * 1, 0);
754 _fade_out->fast_simple_add (len * 0, 1);
755 _fade_out->fast_simple_add (len * 0.228111, 0.988889);
756 _fade_out->fast_simple_add (len * 0.347926, 0.972222);
757 _fade_out->fast_simple_add (len * 0.529954, 0.886111);
758 _fade_out->fast_simple_add (len * 0.753456, 0.658333);
759 _fade_out->fast_simple_add (len * 0.9262673, 0.308333);
760 _fade_out->fast_simple_add (len * 1, 0);
764 _fade_out->fast_simple_add (len * 0, 1);
765 _fade_out->fast_simple_add (len * 0.305556, 1);
766 _fade_out->fast_simple_add (len * 0.548611, 0.991736);
767 _fade_out->fast_simple_add (len * 0.759259, 0.931129);
768 _fade_out->fast_simple_add (len * 0.918981, 0.68595);
769 _fade_out->fast_simple_add (len * 0.976852, 0.22865);
770 _fade_out->fast_simple_add (len * 1, 0);
774 _fade_out->fast_simple_add (len * 0, 1);
775 _fade_out->fast_simple_add (len * 0.080645, 0.730556);
776 _fade_out->fast_simple_add (len * 0.277778, 0.289256);
777 _fade_out->fast_simple_add (len * 0.470046, 0.152778);
778 _fade_out->fast_simple_add (len * 0.695853, 0.0694444);
779 _fade_out->fast_simple_add (len * 1, 0);
783 _fade_out->fast_simple_add (len * 0, 1);
784 _fade_out->fast_simple_add (len * 1, 0);
789 _fade_out_shape = shape;
791 send_change (FadeOutChanged);
795 AudioRegion::set_fade_in_length (nframes_t len)
797 bool changed = _fade_in->extend_to (len);
800 _flags = Flag (_flags & ~DefaultFadeIn);
801 send_change (FadeInChanged);
806 AudioRegion::set_fade_out_length (nframes_t len)
808 bool changed = _fade_out->extend_to (len);
811 _flags = Flag (_flags & ~DefaultFadeOut);
814 send_change (FadeOutChanged);
818 AudioRegion::set_fade_in_active (bool yn)
820 if (yn == (_flags & FadeIn)) {
824 _flags = Flag (_flags|FadeIn);
826 _flags = Flag (_flags & ~FadeIn);
829 send_change (FadeInActiveChanged);
833 AudioRegion::set_fade_out_active (bool yn)
835 if (yn == (_flags & FadeOut)) {
839 _flags = Flag (_flags | FadeOut);
841 _flags = Flag (_flags & ~FadeOut);
844 send_change (FadeOutActiveChanged);
848 AudioRegion::fade_in_is_default () const
850 return _fade_in_shape == Linear && _fade_in->back()->when == 64;
854 AudioRegion::fade_out_is_default () const
856 return _fade_out_shape == Linear && _fade_out->back()->when == 64;
860 AudioRegion::set_default_fade_in ()
862 set_fade_in (Linear, 64);
866 AudioRegion::set_default_fade_out ()
868 set_fade_out (Linear, 64);
872 AudioRegion::set_default_fades ()
874 _fade_in_disabled = 0;
875 _fade_out_disabled = 0;
876 set_default_fade_in ();
877 set_default_fade_out ();
881 AudioRegion::set_default_envelope ()
883 _envelope->freeze ();
885 _envelope->fast_simple_add (0, 1.0f);
886 _envelope->fast_simple_add (_length, 1.0f);
891 AudioRegion::recompute_at_end ()
893 /* our length has changed. recompute a new final point by interpolating
894 based on the the existing curve.
897 _envelope->freeze ();
898 _envelope->truncate_end (_length);
899 _envelope->set_max_xval (_length);
902 if (_fade_in->back()->when > _length) {
903 _fade_in->extend_to (_length);
904 send_change (FadeInChanged);
907 if (_fade_out->back()->when > _length) {
908 _fade_out->extend_to (_length);
909 send_change (FadeOutChanged);
914 AudioRegion::recompute_at_start ()
916 /* as above, but the shift was from the front */
918 _envelope->truncate_start (_length);
920 if (_fade_in->back()->when > _length) {
921 _fade_in->extend_to (_length);
922 send_change (FadeInChanged);
925 if (_fade_out->back()->when > _length) {
926 _fade_out->extend_to (_length);
927 send_change (FadeOutChanged);
932 AudioRegion::separate_by_channel (Session& session, vector<boost::shared_ptr<AudioRegion> >& v) const
938 if (_sources.size() < 2) {
944 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
951 if (_sources.size() == 2) {
959 new_name += ('0' + n + 1);
962 /* create a copy with just one source. prevent if from being thought of as "whole file" even if
963 it covers the entire source file(s).
966 Flag f = Flag (_flags & ~WholeFile);
968 boost::shared_ptr<Region> r = RegionFactory::create (srcs, _start, _length, new_name, _layer, f);
969 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
980 AudioRegion::read_raw_internal (Sample* buf, nframes_t pos, nframes_t cnt) const
982 return audio_source()->read (buf, pos, cnt);
986 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
988 const nframes_t blocksize = 4096;
992 spec.channels = _sources.size();
994 if (spec.prepare (blocksize, session.frame_rate())) {
999 spec.total_frames = _length;
1001 while (spec.pos < _length && !spec.stop) {
1004 /* step 1: interleave */
1006 to_read = min (_length - spec.pos, blocksize);
1008 if (spec.channels == 1) {
1010 if (read_raw_internal (spec.dataF, _start + spec.pos, to_read) != to_read) {
1016 Sample buf[blocksize];
1018 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1020 if (audio_source(chan)->read (buf, _start + spec.pos, to_read) != to_read) {
1024 for (nframes_t x = 0; x < to_read; ++x) {
1025 spec.dataF[chan+(x*spec.channels)] = buf[x];
1030 if (spec.process (to_read)) {
1034 spec.pos += to_read;
1035 spec.progress = (double) spec.pos /_length;
1042 spec.running = false;
1043 spec.status = status;
1050 AudioRegion::set_scale_amplitude (gain_t g)
1052 boost::shared_ptr<Playlist> pl (playlist());
1054 _scale_amplitude = g;
1056 /* tell the diskstream we're in */
1062 /* tell everybody else */
1064 send_change (ScaleAmplitudeChanged);
1068 AudioRegion::normalize_to (float target_dB)
1070 const nframes_t blocksize = 64 * 1024;
1071 Sample buf[blocksize];
1076 gain_t target = dB_to_coefficient (target_dB);
1078 if (target == 1.0f) {
1079 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1080 that we may have clipped.
1082 target -= FLT_EPSILON;
1086 fend = _start + _length;
1088 /* first pass: find max amplitude */
1090 while (fpos < fend) {
1094 to_read = min (fend - fpos, blocksize);
1096 for (n = 0; n < n_channels(); ++n) {
1100 if (read_raw_internal (buf, fpos, to_read) != to_read) {
1104 maxamp = compute_peak (buf, to_read, maxamp);
1110 if (maxamp == 0.0f) {
1111 /* don't even try */
1115 if (maxamp == target) {
1116 /* we can't do anything useful */
1120 /* compute scale factor */
1122 _scale_amplitude = target/maxamp;
1124 /* tell the diskstream we're in */
1126 boost::shared_ptr<Playlist> pl (playlist());
1132 /* tell everybody else */
1134 send_change (ScaleAmplitudeChanged);
1138 AudioRegion::fade_in_changed ()
1140 send_change (FadeInChanged);
1144 AudioRegion::fade_out_changed ()
1146 send_change (FadeOutChanged);
1150 AudioRegion::envelope_changed ()
1152 send_change (EnvelopeChanged);
1156 AudioRegion::suspend_fade_in ()
1158 if (++_fade_in_disabled == 1) {
1159 if (fade_in_is_default()) {
1160 set_fade_in_active (false);
1166 AudioRegion::resume_fade_in ()
1168 if (--_fade_in_disabled == 0 && _fade_in_disabled) {
1169 set_fade_in_active (true);
1174 AudioRegion::suspend_fade_out ()
1176 if (++_fade_out_disabled == 1) {
1177 if (fade_out_is_default()) {
1178 set_fade_out_active (false);
1184 AudioRegion::resume_fade_out ()
1186 if (--_fade_out_disabled == 0 &&_fade_out_disabled) {
1187 set_fade_out_active (true);
1192 AudioRegion::speed_mismatch (float sr) const
1194 if (_sources.empty()) {
1195 /* impossible, but ... */
1199 float fsr = audio_source()->sample_rate();
1205 AudioRegion::source_offset_changed ()
1207 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(_sources.front());
1209 if (afs && afs->destructive()) {
1210 // set_start (source()->natural_position(), this);
1211 set_position (source()->natural_position(), this);
1215 boost::shared_ptr<AudioSource>
1216 AudioRegion::audio_source (uint32_t n) const
1218 // Guaranteed to succeed (use a static cast for speed?)
1219 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1224 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)
1226 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1229 uint32_t region_length_from_c (void *arg)
1232 return ((AudioRegion *) arg)->length();
1235 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1237 return ( (AudioRegion *) arg)->audio_source()->available_peaks (zoom_factor) ;