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();
65 _scale_amplitude = 1.0;
68 set_default_envelope ();
70 listen_to_my_curves ();
73 /* constructor for use by derived types only */
74 AudioRegion::AudioRegion (nframes_t start, nframes_t length, string name)
75 : Region (start, length, name, DataType::AUDIO)
76 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
77 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
78 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
83 /** Basic AudioRegion constructor (one channel) */
84 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length)
85 : Region (src, start, length, PBD::basename_nosuffix(src->name()), DataType::AUDIO, 0, Region::Flag(Region::DefaultFlags|Region::External))
86 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
87 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
88 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
90 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
92 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
98 /* Basic AudioRegion constructor (one channel) */
99 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
100 : Region (src, start, length, name, DataType::AUDIO, layer, flags)
101 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
102 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
103 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
105 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
107 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
113 /* Basic AudioRegion constructor (many channels) */
114 AudioRegion::AudioRegion (SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
115 : Region (srcs, start, length, name, DataType::AUDIO, layer, flags)
116 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
117 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
118 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
124 /** Create a new AudioRegion, that is part of an existing one */
125 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
126 : Region (other, offset, length, name, layer, flags)
127 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
128 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
129 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
131 /* return to default fades if the existing ones are too long */
132 _fade_in_disabled = 0;
133 _fade_out_disabled = 0;
136 if (_flags & LeftOfSplit) {
137 if (_fade_in->back()->when >= _length) {
138 set_default_fade_in ();
140 _fade_in_disabled = other->_fade_in_disabled;
142 set_default_fade_out ();
143 _flags = Flag (_flags & ~Region::LeftOfSplit);
146 if (_flags & RightOfSplit) {
147 if (_fade_out->back()->when >= _length) {
148 set_default_fade_out ();
150 _fade_out_disabled = other->_fade_out_disabled;
152 set_default_fade_in ();
153 _flags = Flag (_flags & ~Region::RightOfSplit);
156 _scale_amplitude = other->_scale_amplitude;
158 listen_to_my_curves ();
160 assert(_type == DataType::AUDIO);
163 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
165 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
166 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
167 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
169 _scale_amplitude = other->_scale_amplitude;
170 _envelope = other->_envelope;
172 _fade_in_disabled = 0;
173 _fade_out_disabled = 0;
175 listen_to_my_curves ();
177 assert(_type == DataType::AUDIO);
180 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, const XMLNode& node)
182 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
183 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
184 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
186 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
188 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
191 set_default_fades ();
193 if (set_state (node)) {
194 throw failed_constructor();
197 listen_to_my_curves ();
199 assert(_type == DataType::AUDIO);
202 AudioRegion::AudioRegion (SourceList& srcs, const XMLNode& node)
203 : Region (srcs, node)
204 , _fade_in (new AutomationList(Parameter(FadeInAutomation), 0.0, 2.0, 1.0))
205 , _fade_out (new AutomationList(Parameter(FadeOutAutomation), 0.0, 2.0, 1.0))
206 , _envelope (new AutomationList(Parameter(EnvelopeAutomation), 0.0, 2.0, 1.0))
208 set_default_fades ();
209 _scale_amplitude = 1.0;
211 if (set_state (node)) {
212 throw failed_constructor();
215 listen_to_my_curves ();
217 assert(_type == DataType::AUDIO);
220 AudioRegion::~AudioRegion ()
225 AudioRegion::listen_to_my_curves ()
227 _envelope->StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
228 _fade_in->StateChanged.connect (mem_fun (*this, &AudioRegion::fade_in_changed));
229 _fade_out->StateChanged.connect (mem_fun (*this, &AudioRegion::fade_out_changed));
233 AudioRegion::verify_length (nframes_t len)
235 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
237 if (afs && afs->destructive()) {
240 return Region::verify_length(len);
245 AudioRegion::verify_start_and_length (nframes_t new_start, nframes_t new_length)
247 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
249 if (afs && afs->destructive()) {
252 return Region::verify_start_and_length(new_start, new_length);
257 AudioRegion::verify_start (nframes_t pos)
259 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
261 if (afs && afs->destructive()) {
264 return Region::verify_start(pos);
269 AudioRegion::verify_start_mutable (nframes_t& new_start)
271 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(source());
273 if (afs && afs->destructive()) {
276 return Region::verify_start_mutable(new_start);
281 AudioRegion::set_envelope_active (bool yn)
283 if (envelope_active() != yn) {
286 snprintf (buf, sizeof (buf), "envelope active");
287 _flags = Flag (_flags|EnvelopeActive);
289 snprintf (buf, sizeof (buf), "envelope off");
290 _flags = Flag (_flags & ~EnvelopeActive);
292 send_change (EnvelopeActiveChanged);
297 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
299 if (chan_n >= _sources.size()) {
303 if (audio_source(chan_n)->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
306 if (_scale_amplitude != 1.0) {
307 for (nframes_t n = 0; n < npeaks; ++n) {
308 buf[n].max *= _scale_amplitude;
309 buf[n].min *= _scale_amplitude;
317 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position, nframes_t cnt, uint32_t chan_n) const
319 return _read_at (_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n);
323 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
324 nframes_t cnt, uint32_t chan_n) const
326 return _read_at (_master_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n);
330 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
331 nframes_t position, nframes_t cnt, uint32_t chan_n) const
333 // cerr << _name << "._read_at(" << position << ") - " << _position << endl;
335 nframes_t internal_offset;
336 nframes_t buf_offset;
340 return 0; /* read nothing */
343 /* precondition: caller has verified that we cover the desired section */
345 if (position < _position) {
347 buf_offset = _position - position;
350 internal_offset = position - _position;
354 if (internal_offset >= _length) {
355 return 0; /* read nothing */
358 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
359 return 0; /* read nothing */
363 /* overwrite whatever is there */
364 mixdown_buffer = buf + buf_offset;
366 mixdown_buffer += buf_offset;
369 _read_data_count = 0;
371 if (chan_n < n_channels()) {
373 boost::shared_ptr<AudioSource> src = audio_source(chan_n);
374 if (src->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
376 return 0; /* "read nothing" */
379 _read_data_count += src->read_data_count();
383 /* track is N-channel, this region has less channels; silence the ones
387 memset (mixdown_buffer, 0, sizeof (Sample) * cnt);
389 /* no fades required */
396 if (_flags & FadeIn) {
398 nframes_t fade_in_length = (nframes_t) _fade_in->back()->when;
400 /* see if this read is within the fade in */
402 if (internal_offset < fade_in_length) {
406 limit = min (to_read, fade_in_length - internal_offset);
408 _fade_in->curve().get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
410 for (nframes_t n = 0; n < limit; ++n) {
411 mixdown_buffer[n] *= gain_buffer[n];
418 if (_flags & FadeOut) {
420 /* see if some part of this read is within the fade out */
422 /* ................. >| REGION
428 _length - fade_out_length
431 ^internal_offset + to_read
433 we need the intersection of [internal_offset,internal_offset+to_read] with
434 [_length - fade_out_length, _length]
439 nframes_t fade_out_length = (nframes_t) _fade_out->back()->when;
440 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
441 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
443 if (fade_interval_end > fade_interval_start) {
444 /* (part of the) the fade out is in this buffer */
446 nframes_t limit = fade_interval_end - fade_interval_start;
447 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
448 nframes_t fade_offset = fade_interval_start - internal_offset;
450 _fade_out->curve().get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
452 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
453 mixdown_buffer[m] *= gain_buffer[n];
459 /* Regular gain curves */
461 if (envelope_active()) {
462 _envelope->curve().get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
464 if (_scale_amplitude != 1.0f) {
465 for (nframes_t n = 0; n < to_read; ++n) {
466 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
469 for (nframes_t n = 0; n < to_read; ++n) {
470 mixdown_buffer[n] *= gain_buffer[n];
473 } else if (_scale_amplitude != 1.0f) {
474 apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
481 /* gack. the things we do for users.
486 for (nframes_t n = 0; n < to_read; ++n) {
487 buf[n] += mixdown_buffer[n];
495 AudioRegion::state (bool full)
497 XMLNode& node (Region::state (full));
501 LocaleGuard lg (X_("POSIX"));
503 node.add_property ("flags", enum_2_string (_flags));
505 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
506 node.add_property ("scale-gain", buf);
508 for (uint32_t n=0; n < _sources.size(); ++n) {
509 snprintf (buf2, sizeof(buf2), "source-%d", n);
510 _sources[n]->id().print (buf, sizeof (buf));
511 node.add_property (buf2, buf);
514 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
515 node.add_property ("channels", buf);
519 child = node.add_child (X_("FadeIn"));
521 if ((_flags & DefaultFadeIn)) {
522 child->add_property (X_("default"), X_("yes"));
524 child->add_child_nocopy (_fade_in->get_state ());
527 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
529 child = node.add_child (X_("FadeOut"));
531 if ((_flags & DefaultFadeOut)) {
532 child->add_property (X_("default"), X_("yes"));
534 child->add_child_nocopy (_fade_out->get_state ());
537 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
540 child = node.add_child ("Envelope");
543 bool default_env = false;
545 // If there are only two points, the points are in the start of the region and the end of the region
546 // so, if they are both at 1.0f, that means the default region.
548 if (_envelope->size() == 2 &&
549 _envelope->front()->value == 1.0f &&
550 _envelope->back()->value==1.0f) {
551 if (_envelope->front()->when == 0 && _envelope->back()->when == _length) {
557 child->add_property ("default", "yes");
559 child->add_child_nocopy (_envelope->get_state ());
563 child->add_property ("default", "yes");
566 if (full && _extra_xml) {
567 node.add_child_copy (*_extra_xml);
574 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
576 const XMLNodeList& nlist = node.children();
577 const XMLProperty *prop;
578 LocaleGuard lg (X_("POSIX"));
580 Region::set_live_state (node, what_changed, false);
582 uint32_t old_flags = _flags;
584 if ((prop = node.property ("flags")) != 0) {
585 _flags = Flag (string_2_enum (prop->value(), _flags));
587 //_flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
589 _flags = Flag (_flags & ~Region::LeftOfSplit);
590 _flags = Flag (_flags & ~Region::RightOfSplit);
593 if ((old_flags ^ _flags) & Muted) {
594 what_changed = Change (what_changed|MuteChanged);
596 if ((old_flags ^ _flags) & Opaque) {
597 what_changed = Change (what_changed|OpacityChanged);
599 if ((old_flags ^ _flags) & Locked) {
600 what_changed = Change (what_changed|LockChanged);
603 if ((prop = node.property ("scale-gain")) != 0) {
604 _scale_amplitude = atof (prop->value().c_str());
606 _scale_amplitude = 1.0;
609 /* Now find envelope description and other misc child items */
611 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
618 if (child->name() == "Envelope") {
622 if ((prop = child->property ("default")) != 0 || _envelope->set_state (*child)) {
623 set_default_envelope ();
626 _envelope->set_max_xval (_length);
627 _envelope->truncate_end (_length);
629 } else if (child->name() == "FadeIn") {
633 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_in->set_state (*child)) {
634 set_default_fade_in ();
637 } else if (child->name() == "FadeOut") {
641 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0 || _fade_out->set_state (*child)) {
642 set_default_fade_out ();
648 send_change (what_changed);
655 AudioRegion::set_state (const XMLNode& node)
657 /* Region::set_state() calls the virtual set_live_state(),
658 which will get us back to AudioRegion::set_live_state()
659 to handle the relevant stuff.
662 return Region::set_state (node);
666 AudioRegion::set_fade_in_shape (FadeShape shape)
668 set_fade_in (shape, (nframes_t) _fade_in->back()->when);
672 AudioRegion::set_fade_out_shape (FadeShape shape)
674 set_fade_out (shape, (nframes_t) _fade_out->back()->when);
678 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
685 _fade_in->fast_simple_add (0.0, 0.0);
686 _fade_in->fast_simple_add (len, 1.0);
690 _fade_in->fast_simple_add (0, 0);
691 _fade_in->fast_simple_add (len * 0.389401, 0.0333333);
692 _fade_in->fast_simple_add (len * 0.629032, 0.0861111);
693 _fade_in->fast_simple_add (len * 0.829493, 0.233333);
694 _fade_in->fast_simple_add (len * 0.9447, 0.483333);
695 _fade_in->fast_simple_add (len * 0.976959, 0.697222);
696 _fade_in->fast_simple_add (len, 1);
700 _fade_in->fast_simple_add (0, 0);
701 _fade_in->fast_simple_add (len * 0.0207373, 0.197222);
702 _fade_in->fast_simple_add (len * 0.0645161, 0.525);
703 _fade_in->fast_simple_add (len * 0.152074, 0.802778);
704 _fade_in->fast_simple_add (len * 0.276498, 0.919444);
705 _fade_in->fast_simple_add (len * 0.481567, 0.980556);
706 _fade_in->fast_simple_add (len * 0.767281, 1);
707 _fade_in->fast_simple_add (len, 1);
711 _fade_in->fast_simple_add (0, 0);
712 _fade_in->fast_simple_add (len * 0.0737327, 0.308333);
713 _fade_in->fast_simple_add (len * 0.246544, 0.658333);
714 _fade_in->fast_simple_add (len * 0.470046, 0.886111);
715 _fade_in->fast_simple_add (len * 0.652074, 0.972222);
716 _fade_in->fast_simple_add (len * 0.771889, 0.988889);
717 _fade_in->fast_simple_add (len, 1);
721 _fade_in->fast_simple_add (0, 0);
722 _fade_in->fast_simple_add (len * 0.304147, 0.0694444);
723 _fade_in->fast_simple_add (len * 0.529954, 0.152778);
724 _fade_in->fast_simple_add (len * 0.725806, 0.333333);
725 _fade_in->fast_simple_add (len * 0.847926, 0.558333);
726 _fade_in->fast_simple_add (len * 0.919355, 0.730556);
727 _fade_in->fast_simple_add (len, 1);
732 _fade_in_shape = shape;
734 send_change (FadeInChanged);
738 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
740 _fade_out->freeze ();
745 _fade_out->fast_simple_add (len * 0, 1);
746 _fade_out->fast_simple_add (len * 0.023041, 0.697222);
747 _fade_out->fast_simple_add (len * 0.0553, 0.483333);
748 _fade_out->fast_simple_add (len * 0.170507, 0.233333);
749 _fade_out->fast_simple_add (len * 0.370968, 0.0861111);
750 _fade_out->fast_simple_add (len * 0.610599, 0.0333333);
751 _fade_out->fast_simple_add (len * 1, 0);
755 _fade_out->fast_simple_add (len * 0, 1);
756 _fade_out->fast_simple_add (len * 0.228111, 0.988889);
757 _fade_out->fast_simple_add (len * 0.347926, 0.972222);
758 _fade_out->fast_simple_add (len * 0.529954, 0.886111);
759 _fade_out->fast_simple_add (len * 0.753456, 0.658333);
760 _fade_out->fast_simple_add (len * 0.9262673, 0.308333);
761 _fade_out->fast_simple_add (len * 1, 0);
765 _fade_out->fast_simple_add (len * 0, 1);
766 _fade_out->fast_simple_add (len * 0.305556, 1);
767 _fade_out->fast_simple_add (len * 0.548611, 0.991736);
768 _fade_out->fast_simple_add (len * 0.759259, 0.931129);
769 _fade_out->fast_simple_add (len * 0.918981, 0.68595);
770 _fade_out->fast_simple_add (len * 0.976852, 0.22865);
771 _fade_out->fast_simple_add (len * 1, 0);
775 _fade_out->fast_simple_add (len * 0, 1);
776 _fade_out->fast_simple_add (len * 0.080645, 0.730556);
777 _fade_out->fast_simple_add (len * 0.277778, 0.289256);
778 _fade_out->fast_simple_add (len * 0.470046, 0.152778);
779 _fade_out->fast_simple_add (len * 0.695853, 0.0694444);
780 _fade_out->fast_simple_add (len * 1, 0);
784 _fade_out->fast_simple_add (len * 0, 1);
785 _fade_out->fast_simple_add (len * 1, 0);
790 _fade_out_shape = shape;
792 send_change (FadeOutChanged);
796 AudioRegion::set_fade_in_length (nframes_t len)
798 bool changed = _fade_in->extend_to (len);
801 _flags = Flag (_flags & ~DefaultFadeIn);
802 send_change (FadeInChanged);
807 AudioRegion::set_fade_out_length (nframes_t len)
809 bool changed = _fade_out->extend_to (len);
812 _flags = Flag (_flags & ~DefaultFadeOut);
815 send_change (FadeOutChanged);
819 AudioRegion::set_fade_in_active (bool yn)
821 if (yn == (_flags & FadeIn)) {
825 _flags = Flag (_flags|FadeIn);
827 _flags = Flag (_flags & ~FadeIn);
830 send_change (FadeInActiveChanged);
834 AudioRegion::set_fade_out_active (bool yn)
836 if (yn == (_flags & FadeOut)) {
840 _flags = Flag (_flags | FadeOut);
842 _flags = Flag (_flags & ~FadeOut);
845 send_change (FadeOutActiveChanged);
849 AudioRegion::fade_in_is_default () const
851 return _fade_in_shape == Linear && _fade_in->back()->when == 64;
855 AudioRegion::fade_out_is_default () const
857 return _fade_out_shape == Linear && _fade_out->back()->when == 64;
861 AudioRegion::set_default_fade_in ()
863 set_fade_in (Linear, 64);
867 AudioRegion::set_default_fade_out ()
869 set_fade_out (Linear, 64);
873 AudioRegion::set_default_fades ()
875 _fade_in_disabled = 0;
876 _fade_out_disabled = 0;
877 set_default_fade_in ();
878 set_default_fade_out ();
882 AudioRegion::set_default_envelope ()
884 _envelope->freeze ();
886 _envelope->fast_simple_add (0, 1.0f);
887 _envelope->fast_simple_add (_length, 1.0f);
892 AudioRegion::recompute_at_end ()
894 /* our length has changed. recompute a new final point by interpolating
895 based on the the existing curve.
898 _envelope->freeze ();
899 _envelope->truncate_end (_length);
900 _envelope->set_max_xval (_length);
903 if (_fade_in->back()->when > _length) {
904 _fade_in->extend_to (_length);
905 send_change (FadeInChanged);
908 if (_fade_out->back()->when > _length) {
909 _fade_out->extend_to (_length);
910 send_change (FadeOutChanged);
915 AudioRegion::recompute_at_start ()
917 /* as above, but the shift was from the front */
919 _envelope->truncate_start (_length);
921 if (_fade_in->back()->when > _length) {
922 _fade_in->extend_to (_length);
923 send_change (FadeInChanged);
926 if (_fade_out->back()->when > _length) {
927 _fade_out->extend_to (_length);
928 send_change (FadeOutChanged);
933 AudioRegion::separate_by_channel (Session& session, vector<boost::shared_ptr<AudioRegion> >& v) const
939 if (_sources.size() < 2) {
945 for (SourceList::const_iterator i = _sources.begin(); i != _sources.end(); ++i) {
952 if (_sources.size() == 2) {
960 new_name += ('0' + n + 1);
963 /* create a copy with just one source. prevent if from being thought of as "whole file" even if
964 it covers the entire source file(s).
967 Flag f = Flag (_flags & ~WholeFile);
969 boost::shared_ptr<Region> r = RegionFactory::create (srcs, _start, _length, new_name, _layer, f);
970 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (r);
981 AudioRegion::apply (AudioFilter& filter)
983 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
984 return filter.run (ar);
988 AudioRegion::read_raw_internal (Sample* buf, nframes_t pos, nframes_t cnt) const
990 return audio_source()->read (buf, pos, cnt);
994 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
996 const nframes_t blocksize = 4096;
1000 spec.channels = _sources.size();
1002 if (spec.prepare (blocksize, session.frame_rate())) {
1007 spec.total_frames = _length;
1009 while (spec.pos < _length && !spec.stop) {
1012 /* step 1: interleave */
1014 to_read = min (_length - spec.pos, blocksize);
1016 if (spec.channels == 1) {
1018 if (read_raw_internal (spec.dataF, _start + spec.pos, to_read) != to_read) {
1024 Sample buf[blocksize];
1026 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1028 if (audio_source(chan)->read (buf, _start + spec.pos, to_read) != to_read) {
1032 for (nframes_t x = 0; x < to_read; ++x) {
1033 spec.dataF[chan+(x*spec.channels)] = buf[x];
1038 if (spec.process (to_read)) {
1042 spec.pos += to_read;
1043 spec.progress = (double) spec.pos /_length;
1050 spec.running = false;
1051 spec.status = status;
1058 AudioRegion::set_scale_amplitude (gain_t g)
1060 boost::shared_ptr<Playlist> pl (playlist());
1062 _scale_amplitude = g;
1064 /* tell the diskstream we're in */
1070 /* tell everybody else */
1072 send_change (ScaleAmplitudeChanged);
1076 AudioRegion::normalize_to (float target_dB)
1078 const nframes_t blocksize = 64 * 1024;
1079 Sample buf[blocksize];
1084 gain_t target = dB_to_coefficient (target_dB);
1086 if (target == 1.0f) {
1087 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1088 that we may have clipped.
1090 target -= FLT_EPSILON;
1094 fend = _start + _length;
1096 /* first pass: find max amplitude */
1098 while (fpos < fend) {
1102 to_read = min (fend - fpos, blocksize);
1104 for (n = 0; n < n_channels(); ++n) {
1108 if (read_raw_internal (buf, fpos, to_read) != to_read) {
1112 maxamp = compute_peak (buf, to_read, maxamp);
1118 if (maxamp == 0.0f) {
1119 /* don't even try */
1123 if (maxamp == target) {
1124 /* we can't do anything useful */
1128 /* compute scale factor */
1130 _scale_amplitude = target/maxamp;
1132 /* tell the diskstream we're in */
1134 boost::shared_ptr<Playlist> pl (playlist());
1140 /* tell everybody else */
1142 send_change (ScaleAmplitudeChanged);
1146 AudioRegion::fade_in_changed ()
1148 send_change (FadeInChanged);
1152 AudioRegion::fade_out_changed ()
1154 send_change (FadeOutChanged);
1158 AudioRegion::envelope_changed ()
1160 send_change (EnvelopeChanged);
1164 AudioRegion::suspend_fade_in ()
1166 if (++_fade_in_disabled == 1) {
1167 if (fade_in_is_default()) {
1168 set_fade_in_active (false);
1174 AudioRegion::resume_fade_in ()
1176 if (--_fade_in_disabled == 0 && _fade_in_disabled) {
1177 set_fade_in_active (true);
1182 AudioRegion::suspend_fade_out ()
1184 if (++_fade_out_disabled == 1) {
1185 if (fade_out_is_default()) {
1186 set_fade_out_active (false);
1192 AudioRegion::resume_fade_out ()
1194 if (--_fade_out_disabled == 0 &&_fade_out_disabled) {
1195 set_fade_out_active (true);
1200 AudioRegion::speed_mismatch (float sr) const
1202 if (_sources.empty()) {
1203 /* impossible, but ... */
1207 float fsr = audio_source()->sample_rate();
1213 AudioRegion::source_offset_changed ()
1215 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource>(_sources.front());
1217 if (afs && afs->destructive()) {
1218 // set_start (source()->natural_position(), this);
1219 set_position (source()->natural_position(), this);
1223 boost::shared_ptr<AudioSource>
1224 AudioRegion::audio_source (uint32_t n) const
1226 // Guaranteed to succeed (use a static cast for speed?)
1227 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1232 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)
1234 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1237 uint32_t region_length_from_c (void *arg)
1240 return ((AudioRegion *) arg)->length();
1243 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1245 return ( (AudioRegion *) arg)->audio_source()->available_peaks (zoom_factor) ;