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.
27 #include <sigc++/bind.h>
28 #include <sigc++/class_slot.h>
30 #include <glibmm/thread.h>
32 #include <pbd/basename.h>
33 #include <pbd/xml++.h>
34 #include <pbd/stacktrace.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/destructive_filesource.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 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
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 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
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 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
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 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
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 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
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 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
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 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
210 assert(_type == DataType::AUDIO);
213 AudioRegion::~AudioRegion ()
215 cerr << "====== " << _name << " DESTRUCTOR @ " << this << endl;
217 GoingAway (); /* EMIT SIGNAL */
221 AudioRegion::set_envelope_active (bool yn)
223 if (envelope_active() != yn) {
226 snprintf (buf, sizeof (buf), "envelope active");
227 _flags = Flag (_flags|EnvelopeActive);
229 snprintf (buf, sizeof (buf), "envelope off");
230 _flags = Flag (_flags & ~EnvelopeActive);
232 send_change (EnvelopeActiveChanged);
237 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
239 if (chan_n >= _sources.size()) {
243 if (audio_source(chan_n)->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
246 if (_scale_amplitude != 1.0) {
247 for (nframes_t n = 0; n < npeaks; ++n) {
248 buf[n].max *= _scale_amplitude;
249 buf[n].min *= _scale_amplitude;
257 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
259 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
261 return _read_at (_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, read_frames, skip_frames);
265 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
266 nframes_t cnt, uint32_t chan_n) const
268 return _read_at (_master_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0);
272 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
273 nframes_t position, nframes_t cnt,
274 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
276 nframes_t internal_offset;
277 nframes_t buf_offset;
280 /* precondition: caller has verified that we cover the desired section */
282 if (chan_n >= _sources.size()) {
283 return 0; /* read nothing */
286 if (position < _position) {
288 buf_offset = _position - position;
291 internal_offset = position - _position;
295 if (internal_offset >= _length) {
296 return 0; /* read nothing */
299 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
300 return 0; /* read nothing */
304 /* overwrite whatever is there */
305 mixdown_buffer = buf + buf_offset;
307 mixdown_buffer += buf_offset;
311 return 0; /* read nothing */
314 _read_data_count = 0;
316 boost::shared_ptr<AudioSource> src = audio_source(chan_n);
317 if (src->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
318 return 0; /* "read nothing" */
321 _read_data_count += src->read_data_count();
325 if (_flags & FadeIn) {
327 nframes_t fade_in_length = (nframes_t) _fade_in.back()->when;
329 /* see if this read is within the fade in */
331 if (internal_offset < fade_in_length) {
335 limit = min (to_read, fade_in_length - internal_offset);
337 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
339 for (nframes_t n = 0; n < limit; ++n) {
340 mixdown_buffer[n] *= gain_buffer[n];
347 if (_flags & FadeOut) {
349 /* see if some part of this read is within the fade out */
351 /* ................. >| REGION
357 _length - fade_out_length
360 ^internal_offset + to_read
362 we need the intersection of [internal_offset,internal_offset+to_read] with
363 [_length - fade_out_length, _length]
368 nframes_t fade_out_length = (nframes_t) _fade_out.back()->when;
369 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
370 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
372 if (fade_interval_end > fade_interval_start) {
373 /* (part of the) the fade out is in this buffer */
375 nframes_t limit = fade_interval_end - fade_interval_start;
376 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
377 nframes_t fade_offset = fade_interval_start - internal_offset;
379 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
381 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
382 mixdown_buffer[m] *= gain_buffer[n];
388 /* Regular gain curves */
390 if (envelope_active()) {
391 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
393 if (_scale_amplitude != 1.0f) {
394 for (nframes_t n = 0; n < to_read; ++n) {
395 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
398 for (nframes_t n = 0; n < to_read; ++n) {
399 mixdown_buffer[n] *= gain_buffer[n];
402 } else if (_scale_amplitude != 1.0f) {
403 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
408 /* gack. the things we do for users.
413 for (nframes_t n = 0; n < to_read; ++n) {
414 buf[n] += mixdown_buffer[n];
422 AudioRegion::state (bool full)
424 XMLNode& node (Region::state (full));
428 LocaleGuard lg (X_("POSIX"));
430 snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
431 node.add_property ("flags", buf);
432 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
433 node.add_property ("scale-gain", buf);
435 for (uint32_t n=0; n < _sources.size(); ++n) {
436 snprintf (buf2, sizeof(buf2), "source-%d", n);
437 _sources[n]->id().print (buf, sizeof (buf));
438 node.add_property (buf2, buf);
441 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
442 node.add_property ("channels", buf);
446 child = node.add_child (X_("FadeIn"));
448 if ((_flags & DefaultFadeIn)) {
449 child->add_property (X_("default"), X_("yes"));
451 _fade_in.store_state (*child);
454 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
456 child = node.add_child (X_("FadeOut"));
458 if ((_flags & DefaultFadeOut)) {
459 child->add_property (X_("default"), X_("yes"));
461 _fade_out.store_state (*child);
464 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
467 child = node.add_child ("Envelope");
470 bool default_env = false;
472 // If there are only two points, the points are in the start of the region and the end of the region
473 // so, if they are both at 1.0f, that means the default region.
474 if (_envelope.size() == 2 &&
475 _envelope.front()->value == 1.0f &&
476 _envelope.back()->value==1.0f) {
477 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
483 child->add_property ("default", "yes");
485 _envelope.store_state (*child);
489 child->add_property ("default", "yes");
492 if (full && _extra_xml) {
493 node.add_child_copy (*_extra_xml);
500 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
502 const XMLNodeList& nlist = node.children();
503 const XMLProperty *prop;
504 LocaleGuard lg (X_("POSIX"));
506 Region::set_live_state (node, what_changed, false);
508 uint32_t old_flags = _flags;
510 if ((prop = node.property ("flags")) != 0) {
511 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
513 _flags = Flag (_flags & ~Region::LeftOfSplit);
514 _flags = Flag (_flags & ~Region::RightOfSplit);
517 if ((old_flags ^ _flags) & Muted) {
518 what_changed = Change (what_changed|MuteChanged);
520 if ((old_flags ^ _flags) & Opaque) {
521 what_changed = Change (what_changed|OpacityChanged);
523 if ((old_flags ^ _flags) & Locked) {
524 what_changed = Change (what_changed|LockChanged);
527 if ((prop = node.property ("scale-gain")) != 0) {
528 _scale_amplitude = atof (prop->value().c_str());
530 _scale_amplitude = 1.0;
533 /* Now find envelope description and other misc child items */
535 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
542 if (child->name() == "Envelope") {
546 if ((prop = child->property ("default")) != 0) {
547 set_default_envelope ();
549 _envelope.load_state (*child);
552 _envelope.set_max_xval (_length);
553 _envelope.truncate_end (_length);
555 } else if (child->name() == "FadeIn") {
559 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
560 set_default_fade_in ();
562 _fade_in.load_state (*child);
565 } else if (child->name() == "FadeOut") {
569 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
570 set_default_fade_out ();
572 _fade_out.load_state (*child);
578 send_change (what_changed);
585 AudioRegion::set_state (const XMLNode& node)
587 /* Region::set_state() calls the virtual set_live_state(),
588 which will get us back to AudioRegion::set_live_state()
589 to handle the relevant stuff.
592 return Region::set_state (node);
596 AudioRegion::set_fade_in_shape (FadeShape shape)
598 set_fade_in (shape, (nframes_t) _fade_in.back()->when);
602 AudioRegion::set_fade_out_shape (FadeShape shape)
604 set_fade_out (shape, (nframes_t) _fade_out.back()->when);
608 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
615 _fade_in.add (0.0, 0.0);
616 _fade_in.add (len, 1.0);
621 _fade_in.add (len * 0.389401, 0.0333333);
622 _fade_in.add (len * 0.629032, 0.0861111);
623 _fade_in.add (len * 0.829493, 0.233333);
624 _fade_in.add (len * 0.9447, 0.483333);
625 _fade_in.add (len * 0.976959, 0.697222);
626 _fade_in.add (len, 1);
631 _fade_in.add (len * 0.0207373, 0.197222);
632 _fade_in.add (len * 0.0645161, 0.525);
633 _fade_in.add (len * 0.152074, 0.802778);
634 _fade_in.add (len * 0.276498, 0.919444);
635 _fade_in.add (len * 0.481567, 0.980556);
636 _fade_in.add (len * 0.767281, 1);
637 _fade_in.add (len, 1);
642 _fade_in.add (len * 0.0737327, 0.308333);
643 _fade_in.add (len * 0.246544, 0.658333);
644 _fade_in.add (len * 0.470046, 0.886111);
645 _fade_in.add (len * 0.652074, 0.972222);
646 _fade_in.add (len * 0.771889, 0.988889);
647 _fade_in.add (len, 1);
652 _fade_in.add (len * 0.304147, 0.0694444);
653 _fade_in.add (len * 0.529954, 0.152778);
654 _fade_in.add (len * 0.725806, 0.333333);
655 _fade_in.add (len * 0.847926, 0.558333);
656 _fade_in.add (len * 0.919355, 0.730556);
657 _fade_in.add (len, 1);
662 _fade_in_shape = shape;
664 send_change (FadeInChanged);
668 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
675 _fade_out.add (len * 0, 1);
676 _fade_out.add (len * 0.023041, 0.697222);
677 _fade_out.add (len * 0.0553, 0.483333);
678 _fade_out.add (len * 0.170507, 0.233333);
679 _fade_out.add (len * 0.370968, 0.0861111);
680 _fade_out.add (len * 0.610599, 0.0333333);
681 _fade_out.add (len * 1, 0);
685 _fade_out.add (len * 0, 1);
686 _fade_out.add (len * 0.228111, 0.988889);
687 _fade_out.add (len * 0.347926, 0.972222);
688 _fade_out.add (len * 0.529954, 0.886111);
689 _fade_out.add (len * 0.753456, 0.658333);
690 _fade_out.add (len * 0.9262673, 0.308333);
691 _fade_out.add (len * 1, 0);
695 _fade_out.add (len * 0, 1);
696 _fade_out.add (len * 0.305556, 1);
697 _fade_out.add (len * 0.548611, 0.991736);
698 _fade_out.add (len * 0.759259, 0.931129);
699 _fade_out.add (len * 0.918981, 0.68595);
700 _fade_out.add (len * 0.976852, 0.22865);
701 _fade_out.add (len * 1, 0);
705 _fade_out.add (len * 0, 1);
706 _fade_out.add (len * 0.080645, 0.730556);
707 _fade_out.add (len * 0.277778, 0.289256);
708 _fade_out.add (len * 0.470046, 0.152778);
709 _fade_out.add (len * 0.695853, 0.0694444);
710 _fade_out.add (len * 1, 0);
714 _fade_out.add (len * 0, 1);
715 _fade_out.add (len * 1, 0);
720 _fade_out_shape = shape;
722 send_change (FadeOutChanged);
726 AudioRegion::set_fade_in_length (nframes_t len)
728 bool changed = _fade_in.extend_to (len);
731 _flags = Flag (_flags & ~DefaultFadeIn);
732 send_change (FadeInChanged);
737 AudioRegion::set_fade_out_length (nframes_t len)
739 bool changed = _fade_out.extend_to (len);
742 _flags = Flag (_flags & ~DefaultFadeOut);
745 send_change (FadeOutChanged);
749 AudioRegion::set_fade_in_active (bool yn)
751 if (yn == (_flags & FadeIn)) {
755 _flags = Flag (_flags|FadeIn);
757 _flags = Flag (_flags & ~FadeIn);
760 send_change (FadeInActiveChanged);
764 AudioRegion::set_fade_out_active (bool yn)
766 if (yn == (_flags & FadeOut)) {
770 _flags = Flag (_flags | FadeOut);
772 _flags = Flag (_flags & ~FadeOut);
775 send_change (FadeOutActiveChanged);
779 AudioRegion::set_default_fade_in ()
781 set_fade_in (Linear, 64);
785 AudioRegion::set_default_fade_out ()
787 set_fade_out (Linear, 64);
791 AudioRegion::set_default_fades ()
793 _fade_in_disabled = 0;
794 _fade_out_disabled = 0;
795 set_default_fade_in ();
796 set_default_fade_out ();
800 AudioRegion::set_default_envelope ()
804 _envelope.add (0, 1.0f);
805 _envelope.add (_length, 1.0f);
810 AudioRegion::recompute_at_end ()
812 /* our length has changed. recompute a new final point by interpolating
813 based on the the existing curve.
817 _envelope.truncate_end (_length);
818 _envelope.set_max_xval (_length);
821 if (_fade_in.back()->when > _length) {
822 _fade_in.extend_to (_length);
823 send_change (FadeInChanged);
826 if (_fade_out.back()->when > _length) {
827 _fade_out.extend_to (_length);
828 send_change (FadeOutChanged);
833 AudioRegion::recompute_at_start ()
835 /* as above, but the shift was from the front */
837 _envelope.truncate_start (_length);
839 if (_fade_in.back()->when > _length) {
840 _fade_in.extend_to (_length);
841 send_change (FadeInChanged);
844 if (_fade_out.back()->when > _length) {
845 _fade_out.extend_to (_length);
846 send_change (FadeOutChanged);
851 AudioRegion::separate_by_channel (Session& session, vector<AudioRegion*>& v) const
856 for (SourceList::const_iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
861 /* generate a new name */
863 if (session.region_name (new_name, _name)) {
867 /* create a copy with just one source */
869 v.push_back (new AudioRegion (srcs, _start, _length, new_name, _layer, _flags));
876 AudioRegion::apply (AudioFilter& filter)
878 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
879 return filter.run (ar);
883 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
885 const nframes_t blocksize = 4096;
889 spec.channels = _sources.size();
891 if (spec.prepare (blocksize, session.frame_rate())) {
896 spec.total_frames = _length;
898 while (spec.pos < _length && !spec.stop) {
901 /* step 1: interleave */
903 to_read = min (_length - spec.pos, blocksize);
905 if (spec.channels == 1) {
907 if (audio_source()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
913 Sample buf[blocksize];
915 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
917 if (audio_source(chan)->read (buf, _start + spec.pos, to_read) != to_read) {
921 for (nframes_t x = 0; x < to_read; ++x) {
922 spec.dataF[chan+(x*spec.channels)] = buf[x];
927 if (spec.process (to_read)) {
932 spec.progress = (double) spec.pos /_length;
939 spec.running = false;
940 spec.status = status;
947 AudioRegion::set_scale_amplitude (gain_t g)
949 _scale_amplitude = g;
951 /* tell the diskstream we're in */
954 _playlist->Modified();
957 /* tell everybody else */
959 send_change (ScaleAmplitudeChanged);
963 AudioRegion::normalize_to (float target_dB)
965 const nframes_t blocksize = 64 * 1024;
966 Sample buf[blocksize];
971 gain_t target = dB_to_coefficient (target_dB);
973 if (target == 1.0f) {
974 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
975 that we may have clipped.
977 target -= FLT_EPSILON;
981 fend = _start + _length;
983 /* first pass: find max amplitude */
985 while (fpos < fend) {
989 to_read = min (fend - fpos, blocksize);
991 for (n = 0; n < n_channels(); ++n) {
995 if (audio_source (n)->read (buf, fpos, to_read) != to_read) {
999 maxamp = Session::compute_peak (buf, to_read, maxamp);
1005 if (maxamp == 0.0f) {
1006 /* don't even try */
1010 if (maxamp == target) {
1011 /* we can't do anything useful */
1015 /* compute scale factor */
1017 _scale_amplitude = target/maxamp;
1019 /* tell the diskstream we're in */
1022 _playlist->Modified();
1025 /* tell everybody else */
1027 send_change (ScaleAmplitudeChanged);
1031 AudioRegion::envelope_changed (Change ignored)
1033 send_change (EnvelopeChanged);
1037 AudioRegion::suspend_fade_in ()
1039 if (++_fade_in_disabled == 1) {
1040 set_fade_in_active (false);
1045 AudioRegion::resume_fade_in ()
1047 if (_fade_in_disabled && --_fade_in_disabled == 0) {
1048 set_fade_in_active (true);
1053 AudioRegion::suspend_fade_out ()
1055 if (++_fade_out_disabled == 1) {
1056 set_fade_out_active (false);
1061 AudioRegion::resume_fade_out ()
1063 if (_fade_out_disabled && --_fade_out_disabled == 0) {
1064 set_fade_out_active (true);
1069 AudioRegion::speed_mismatch (float sr) const
1071 if (_sources.empty()) {
1072 /* impossible, but ... */
1076 float fsr = audio_source()->sample_rate();
1082 AudioRegion::source_offset_changed ()
1084 if (boost::dynamic_pointer_cast<DestructiveFileSource>(_sources.front())) {
1085 set_start (source()->natural_position(), this);
1086 set_position (source()->natural_position(), this);
1090 boost::shared_ptr<AudioSource>
1091 AudioRegion::audio_source (uint32_t n) const
1093 // Guaranteed to succeed (use a static cast?)
1094 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1099 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)
1101 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1104 uint32_t region_length_from_c (void *arg)
1107 return ((AudioRegion *) arg)->length();
1110 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1112 return ( (AudioRegion *) arg)->audio_source()->available_peaks (zoom_factor) ;