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 //cerr << _name << "._read_at(" << position << ") - " << _position << endl;
278 nframes_t internal_offset;
279 nframes_t buf_offset;
282 /* precondition: caller has verified that we cover the desired section */
284 if (chan_n >= _sources.size()) {
285 return 0; /* read nothing */
288 if (position < _position) {
290 buf_offset = _position - position;
293 internal_offset = position - _position;
297 if (internal_offset >= _length) {
298 return 0; /* read nothing */
301 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
302 return 0; /* read nothing */
306 /* overwrite whatever is there */
307 mixdown_buffer = buf + buf_offset;
309 mixdown_buffer += buf_offset;
313 return 0; /* read nothing */
316 _read_data_count = 0;
318 boost::shared_ptr<AudioSource> src = audio_source(chan_n);
319 if (src->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
320 return 0; /* "read nothing" */
323 _read_data_count += src->read_data_count();
327 if (_flags & FadeIn) {
329 nframes_t fade_in_length = (nframes_t) _fade_in.back()->when;
331 /* see if this read is within the fade in */
333 if (internal_offset < fade_in_length) {
337 limit = min (to_read, fade_in_length - internal_offset);
339 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
341 for (nframes_t n = 0; n < limit; ++n) {
342 mixdown_buffer[n] *= gain_buffer[n];
349 if (_flags & FadeOut) {
351 /* see if some part of this read is within the fade out */
353 /* ................. >| REGION
359 _length - fade_out_length
362 ^internal_offset + to_read
364 we need the intersection of [internal_offset,internal_offset+to_read] with
365 [_length - fade_out_length, _length]
370 nframes_t fade_out_length = (nframes_t) _fade_out.back()->when;
371 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
372 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
374 if (fade_interval_end > fade_interval_start) {
375 /* (part of the) the fade out is in this buffer */
377 nframes_t limit = fade_interval_end - fade_interval_start;
378 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
379 nframes_t fade_offset = fade_interval_start - internal_offset;
381 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
383 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
384 mixdown_buffer[m] *= gain_buffer[n];
390 /* Regular gain curves */
392 if (envelope_active()) {
393 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
395 if (_scale_amplitude != 1.0f) {
396 for (nframes_t n = 0; n < to_read; ++n) {
397 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
400 for (nframes_t n = 0; n < to_read; ++n) {
401 mixdown_buffer[n] *= gain_buffer[n];
404 } else if (_scale_amplitude != 1.0f) {
405 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
410 /* gack. the things we do for users.
415 for (nframes_t n = 0; n < to_read; ++n) {
416 buf[n] += mixdown_buffer[n];
424 AudioRegion::state (bool full)
426 XMLNode& node (Region::state (full));
430 LocaleGuard lg (X_("POSIX"));
432 snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
433 node.add_property ("flags", buf);
434 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
435 node.add_property ("scale-gain", buf);
437 for (uint32_t n=0; n < _sources.size(); ++n) {
438 snprintf (buf2, sizeof(buf2), "source-%d", n);
439 _sources[n]->id().print (buf, sizeof (buf));
440 node.add_property (buf2, buf);
443 snprintf (buf, sizeof (buf), "%u", (uint32_t) _sources.size());
444 node.add_property ("channels", buf);
448 child = node.add_child (X_("FadeIn"));
450 if ((_flags & DefaultFadeIn)) {
451 child->add_property (X_("default"), X_("yes"));
453 _fade_in.store_state (*child);
456 child->add_property (X_("active"), _fade_in_disabled ? X_("no") : X_("yes"));
458 child = node.add_child (X_("FadeOut"));
460 if ((_flags & DefaultFadeOut)) {
461 child->add_property (X_("default"), X_("yes"));
463 _fade_out.store_state (*child);
466 child->add_property (X_("active"), _fade_out_disabled ? X_("no") : X_("yes"));
469 child = node.add_child ("Envelope");
472 bool default_env = false;
474 // If there are only two points, the points are in the start of the region and the end of the region
475 // so, if they are both at 1.0f, that means the default region.
476 if (_envelope.size() == 2 &&
477 _envelope.front()->value == 1.0f &&
478 _envelope.back()->value==1.0f) {
479 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
485 child->add_property ("default", "yes");
487 _envelope.store_state (*child);
491 child->add_property ("default", "yes");
494 if (full && _extra_xml) {
495 node.add_child_copy (*_extra_xml);
502 AudioRegion::set_live_state (const XMLNode& node, Change& what_changed, bool send)
504 const XMLNodeList& nlist = node.children();
505 const XMLProperty *prop;
506 LocaleGuard lg (X_("POSIX"));
508 Region::set_live_state (node, what_changed, false);
510 uint32_t old_flags = _flags;
512 if ((prop = node.property ("flags")) != 0) {
513 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
515 _flags = Flag (_flags & ~Region::LeftOfSplit);
516 _flags = Flag (_flags & ~Region::RightOfSplit);
519 if ((old_flags ^ _flags) & Muted) {
520 what_changed = Change (what_changed|MuteChanged);
522 if ((old_flags ^ _flags) & Opaque) {
523 what_changed = Change (what_changed|OpacityChanged);
525 if ((old_flags ^ _flags) & Locked) {
526 what_changed = Change (what_changed|LockChanged);
529 if ((prop = node.property ("scale-gain")) != 0) {
530 _scale_amplitude = atof (prop->value().c_str());
532 _scale_amplitude = 1.0;
535 /* Now find envelope description and other misc child items */
537 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
544 if (child->name() == "Envelope") {
548 if ((prop = child->property ("default")) != 0) {
549 set_default_envelope ();
551 _envelope.load_state (*child);
554 _envelope.set_max_xval (_length);
555 _envelope.truncate_end (_length);
557 } else if (child->name() == "FadeIn") {
561 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
562 set_default_fade_in ();
564 _fade_in.load_state (*child);
567 } else if (child->name() == "FadeOut") {
571 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
572 set_default_fade_out ();
574 _fade_out.load_state (*child);
580 send_change (what_changed);
587 AudioRegion::set_state (const XMLNode& node)
589 /* Region::set_state() calls the virtual set_live_state(),
590 which will get us back to AudioRegion::set_live_state()
591 to handle the relevant stuff.
594 return Region::set_state (node);
598 AudioRegion::set_fade_in_shape (FadeShape shape)
600 set_fade_in (shape, (nframes_t) _fade_in.back()->when);
604 AudioRegion::set_fade_out_shape (FadeShape shape)
606 set_fade_out (shape, (nframes_t) _fade_out.back()->when);
610 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
617 _fade_in.add (0.0, 0.0);
618 _fade_in.add (len, 1.0);
623 _fade_in.add (len * 0.389401, 0.0333333);
624 _fade_in.add (len * 0.629032, 0.0861111);
625 _fade_in.add (len * 0.829493, 0.233333);
626 _fade_in.add (len * 0.9447, 0.483333);
627 _fade_in.add (len * 0.976959, 0.697222);
628 _fade_in.add (len, 1);
633 _fade_in.add (len * 0.0207373, 0.197222);
634 _fade_in.add (len * 0.0645161, 0.525);
635 _fade_in.add (len * 0.152074, 0.802778);
636 _fade_in.add (len * 0.276498, 0.919444);
637 _fade_in.add (len * 0.481567, 0.980556);
638 _fade_in.add (len * 0.767281, 1);
639 _fade_in.add (len, 1);
644 _fade_in.add (len * 0.0737327, 0.308333);
645 _fade_in.add (len * 0.246544, 0.658333);
646 _fade_in.add (len * 0.470046, 0.886111);
647 _fade_in.add (len * 0.652074, 0.972222);
648 _fade_in.add (len * 0.771889, 0.988889);
649 _fade_in.add (len, 1);
654 _fade_in.add (len * 0.304147, 0.0694444);
655 _fade_in.add (len * 0.529954, 0.152778);
656 _fade_in.add (len * 0.725806, 0.333333);
657 _fade_in.add (len * 0.847926, 0.558333);
658 _fade_in.add (len * 0.919355, 0.730556);
659 _fade_in.add (len, 1);
664 _fade_in_shape = shape;
666 send_change (FadeInChanged);
670 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
677 _fade_out.add (len * 0, 1);
678 _fade_out.add (len * 0.023041, 0.697222);
679 _fade_out.add (len * 0.0553, 0.483333);
680 _fade_out.add (len * 0.170507, 0.233333);
681 _fade_out.add (len * 0.370968, 0.0861111);
682 _fade_out.add (len * 0.610599, 0.0333333);
683 _fade_out.add (len * 1, 0);
687 _fade_out.add (len * 0, 1);
688 _fade_out.add (len * 0.228111, 0.988889);
689 _fade_out.add (len * 0.347926, 0.972222);
690 _fade_out.add (len * 0.529954, 0.886111);
691 _fade_out.add (len * 0.753456, 0.658333);
692 _fade_out.add (len * 0.9262673, 0.308333);
693 _fade_out.add (len * 1, 0);
697 _fade_out.add (len * 0, 1);
698 _fade_out.add (len * 0.305556, 1);
699 _fade_out.add (len * 0.548611, 0.991736);
700 _fade_out.add (len * 0.759259, 0.931129);
701 _fade_out.add (len * 0.918981, 0.68595);
702 _fade_out.add (len * 0.976852, 0.22865);
703 _fade_out.add (len * 1, 0);
707 _fade_out.add (len * 0, 1);
708 _fade_out.add (len * 0.080645, 0.730556);
709 _fade_out.add (len * 0.277778, 0.289256);
710 _fade_out.add (len * 0.470046, 0.152778);
711 _fade_out.add (len * 0.695853, 0.0694444);
712 _fade_out.add (len * 1, 0);
716 _fade_out.add (len * 0, 1);
717 _fade_out.add (len * 1, 0);
722 _fade_out_shape = shape;
724 send_change (FadeOutChanged);
728 AudioRegion::set_fade_in_length (nframes_t len)
730 bool changed = _fade_in.extend_to (len);
733 _flags = Flag (_flags & ~DefaultFadeIn);
734 send_change (FadeInChanged);
739 AudioRegion::set_fade_out_length (nframes_t len)
741 bool changed = _fade_out.extend_to (len);
744 _flags = Flag (_flags & ~DefaultFadeOut);
747 send_change (FadeOutChanged);
751 AudioRegion::set_fade_in_active (bool yn)
753 if (yn == (_flags & FadeIn)) {
757 _flags = Flag (_flags|FadeIn);
759 _flags = Flag (_flags & ~FadeIn);
762 send_change (FadeInActiveChanged);
766 AudioRegion::set_fade_out_active (bool yn)
768 if (yn == (_flags & FadeOut)) {
772 _flags = Flag (_flags | FadeOut);
774 _flags = Flag (_flags & ~FadeOut);
777 send_change (FadeOutActiveChanged);
781 AudioRegion::set_default_fade_in ()
783 set_fade_in (Linear, 64);
787 AudioRegion::set_default_fade_out ()
789 set_fade_out (Linear, 64);
793 AudioRegion::set_default_fades ()
795 _fade_in_disabled = 0;
796 _fade_out_disabled = 0;
797 set_default_fade_in ();
798 set_default_fade_out ();
802 AudioRegion::set_default_envelope ()
806 _envelope.add (0, 1.0f);
807 _envelope.add (_length, 1.0f);
812 AudioRegion::recompute_at_end ()
814 /* our length has changed. recompute a new final point by interpolating
815 based on the the existing curve.
819 _envelope.truncate_end (_length);
820 _envelope.set_max_xval (_length);
823 if (_fade_in.back()->when > _length) {
824 _fade_in.extend_to (_length);
825 send_change (FadeInChanged);
828 if (_fade_out.back()->when > _length) {
829 _fade_out.extend_to (_length);
830 send_change (FadeOutChanged);
835 AudioRegion::recompute_at_start ()
837 /* as above, but the shift was from the front */
839 _envelope.truncate_start (_length);
841 if (_fade_in.back()->when > _length) {
842 _fade_in.extend_to (_length);
843 send_change (FadeInChanged);
846 if (_fade_out.back()->when > _length) {
847 _fade_out.extend_to (_length);
848 send_change (FadeOutChanged);
853 AudioRegion::separate_by_channel (Session& session, vector<AudioRegion*>& v) const
858 for (SourceList::const_iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
863 /* generate a new name */
865 if (session.region_name (new_name, _name)) {
869 /* create a copy with just one source */
871 v.push_back (new AudioRegion (srcs, _start, _length, new_name, _layer, _flags));
878 AudioRegion::apply (AudioFilter& filter)
880 boost::shared_ptr<AudioRegion> ar = boost::dynamic_pointer_cast<AudioRegion> (shared_from_this());
881 return filter.run (ar);
885 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
887 const nframes_t blocksize = 4096;
891 spec.channels = _sources.size();
893 if (spec.prepare (blocksize, session.frame_rate())) {
898 spec.total_frames = _length;
900 while (spec.pos < _length && !spec.stop) {
903 /* step 1: interleave */
905 to_read = min (_length - spec.pos, blocksize);
907 if (spec.channels == 1) {
909 if (audio_source()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
915 Sample buf[blocksize];
917 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
919 if (audio_source(chan)->read (buf, _start + spec.pos, to_read) != to_read) {
923 for (nframes_t x = 0; x < to_read; ++x) {
924 spec.dataF[chan+(x*spec.channels)] = buf[x];
929 if (spec.process (to_read)) {
934 spec.progress = (double) spec.pos /_length;
941 spec.running = false;
942 spec.status = status;
949 AudioRegion::set_scale_amplitude (gain_t g)
951 _scale_amplitude = g;
953 /* tell the diskstream we're in */
956 _playlist->Modified();
959 /* tell everybody else */
961 send_change (ScaleAmplitudeChanged);
965 AudioRegion::normalize_to (float target_dB)
967 const nframes_t blocksize = 64 * 1024;
968 Sample buf[blocksize];
973 gain_t target = dB_to_coefficient (target_dB);
975 if (target == 1.0f) {
976 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
977 that we may have clipped.
979 target -= FLT_EPSILON;
983 fend = _start + _length;
985 /* first pass: find max amplitude */
987 while (fpos < fend) {
991 to_read = min (fend - fpos, blocksize);
993 for (n = 0; n < n_channels(); ++n) {
997 if (audio_source (n)->read (buf, fpos, to_read) != to_read) {
1001 maxamp = Session::compute_peak (buf, to_read, maxamp);
1007 if (maxamp == 0.0f) {
1008 /* don't even try */
1012 if (maxamp == target) {
1013 /* we can't do anything useful */
1017 /* compute scale factor */
1019 _scale_amplitude = target/maxamp;
1021 /* tell the diskstream we're in */
1024 _playlist->Modified();
1027 /* tell everybody else */
1029 send_change (ScaleAmplitudeChanged);
1033 AudioRegion::envelope_changed (Change ignored)
1035 send_change (EnvelopeChanged);
1039 AudioRegion::suspend_fade_in ()
1041 if (++_fade_in_disabled == 1) {
1042 set_fade_in_active (false);
1047 AudioRegion::resume_fade_in ()
1049 if (_fade_in_disabled && --_fade_in_disabled == 0) {
1050 set_fade_in_active (true);
1055 AudioRegion::suspend_fade_out ()
1057 if (++_fade_out_disabled == 1) {
1058 set_fade_out_active (false);
1063 AudioRegion::resume_fade_out ()
1065 if (_fade_out_disabled && --_fade_out_disabled == 0) {
1066 set_fade_out_active (true);
1071 AudioRegion::speed_mismatch (float sr) const
1073 if (_sources.empty()) {
1074 /* impossible, but ... */
1078 float fsr = audio_source()->sample_rate();
1084 AudioRegion::source_offset_changed ()
1086 if (boost::dynamic_pointer_cast<DestructiveFileSource>(_sources.front())) {
1087 set_start (source()->natural_position(), this);
1088 set_position (source()->natural_position(), this);
1092 boost::shared_ptr<AudioSource>
1093 AudioRegion::audio_source (uint32_t n) const
1095 // Guaranteed to succeed (use a static cast?)
1096 return boost::dynamic_pointer_cast<AudioSource>(source(n));
1101 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)
1103 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1106 uint32_t region_length_from_c (void *arg)
1109 return ((AudioRegion *) arg)->length();
1112 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1114 return ( (AudioRegion *) arg)->audio_source()->available_peaks (zoom_factor) ;