2 Copyright (C) 2000-2001 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>
35 #include <ardour/audioregion.h>
36 #include <ardour/session.h>
37 #include <ardour/gain.h>
38 #include <ardour/dB.h>
39 #include <ardour/playlist.h>
40 #include <ardour/audiofilter.h>
41 #include <ardour/audiofilesource.h>
42 #include <ardour/destructive_filesource.h>
48 using namespace ARDOUR;
50 /* a Session will reset these to its chosen defaults by calling AudioRegion::set_default_fade() */
52 Change AudioRegion::FadeInChanged = ARDOUR::new_change();
53 Change AudioRegion::FadeOutChanged = ARDOUR::new_change();
54 Change AudioRegion::FadeInActiveChanged = ARDOUR::new_change();
55 Change AudioRegion::FadeOutActiveChanged = ARDOUR::new_change();
56 Change AudioRegion::EnvelopeActiveChanged = ARDOUR::new_change();
57 Change AudioRegion::ScaleAmplitudeChanged = ARDOUR::new_change();
58 Change AudioRegion::EnvelopeChanged = ARDOUR::new_change();
60 AudioRegionState::AudioRegionState (string why)
62 _fade_in (0.0, 2.0, 1.0, false),
63 _fade_out (0.0, 2.0, 1.0, false),
64 _envelope (0.0, 2.0, 1.0, false)
68 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length)
69 : Region (start, length, PBD::basename_nosuffix(src->name()), 0, Region::Flag(Region::DefaultFlags|Region::External)),
70 _fade_in (0.0, 2.0, 1.0, false),
71 _fade_out (0.0, 2.0, 1.0, false),
72 _envelope (0.0, 2.0, 1.0, false)
74 /* basic AudioRegion constructor */
76 sources.push_back (src);
77 master_sources.push_back (src);
78 src->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), src));
80 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
82 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
85 _scale_amplitude = 1.0;
88 set_default_envelope ();
90 save_state ("initial state");
92 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
95 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
96 : Region (start, length, name, layer, flags),
97 _fade_in (0.0, 2.0, 1.0, false),
98 _fade_out (0.0, 2.0, 1.0, false),
99 _envelope (0.0, 2.0, 1.0, false)
101 /* basic AudioRegion constructor */
103 sources.push_back (src);
104 master_sources.push_back (src);
105 src->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), src));
107 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
109 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
112 _scale_amplitude = 1.0;
114 set_default_fades ();
115 set_default_envelope ();
116 save_state ("initial state");
118 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
121 AudioRegion::AudioRegion (SourceList& srcs, nframes_t start, nframes_t length, const string& name, layer_t layer, Flag flags)
122 : Region (start, length, name, layer, flags),
123 _fade_in (0.0, 2.0, 1.0, false),
124 _fade_out (0.0, 2.0, 1.0, false),
125 _envelope (0.0, 2.0, 1.0, false)
127 /* basic AudioRegion constructor */
129 for (SourceList::iterator i=srcs.begin(); i != srcs.end(); ++i) {
130 sources.push_back (*i);
131 master_sources.push_back (*i);
132 (*i)->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), (*i)));
134 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> ((*i));
136 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
140 _scale_amplitude = 1.0;
142 set_default_fades ();
143 set_default_envelope ();
144 save_state ("initial state");
146 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
150 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, nframes_t offset, nframes_t length, const string& name, layer_t layer, Flag flags)
151 : Region (other, offset, length, name, layer, flags),
152 _fade_in (other->_fade_in),
153 _fade_out (other->_fade_out),
154 _envelope (other->_envelope, (double) offset, (double) offset + length)
156 /* create a new AudioRegion, that is part of an existing one */
158 set<boost::shared_ptr<AudioSource> > unique_srcs;
160 for (SourceList::const_iterator i= other->sources.begin(); i != other->sources.end(); ++i) {
161 sources.push_back (*i);
162 (*i)->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), *i));
164 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
166 result = unique_srcs.insert (*i);
169 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
171 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
176 for (SourceList::const_iterator i = other->master_sources.begin(); i != other->master_sources.end(); ++i) {
177 if (unique_srcs.find (*i) == unique_srcs.end()) {
178 (*i)->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), *i));
180 master_sources.push_back (*i);
183 /* return to default fades if the existing ones are too long */
184 _fade_in_disabled = 0;
185 _fade_out_disabled = 0;
188 if (_flags & LeftOfSplit) {
189 if (_fade_in.back()->when >= _length) {
190 set_default_fade_in ();
192 _fade_in_disabled = other->_fade_in_disabled;
194 set_default_fade_out ();
195 _flags = Flag (_flags & ~Region::LeftOfSplit);
198 if (_flags & RightOfSplit) {
199 if (_fade_out.back()->when >= _length) {
200 set_default_fade_out ();
202 _fade_out_disabled = other->_fade_out_disabled;
204 set_default_fade_in ();
205 _flags = Flag (_flags & ~Region::RightOfSplit);
208 _scale_amplitude = other->_scale_amplitude;
210 save_state ("initial state");
212 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
215 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
217 _fade_in (other->_fade_in),
218 _fade_out (other->_fade_out),
219 _envelope (other->_envelope)
221 /* Pure copy constructor */
223 set<boost::shared_ptr<AudioSource> > unique_srcs;
225 for (SourceList::const_iterator i = other->sources.begin(); i != other->sources.end(); ++i) {
226 sources.push_back (*i);
227 (*i)->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), *i));
228 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
230 result = unique_srcs.insert (*i);
233 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
235 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
240 for (SourceList::const_iterator i = other->master_sources.begin(); i != other->master_sources.end(); ++i) {
241 master_sources.push_back (*i);
242 if (unique_srcs.find (*i) == unique_srcs.end()) {
243 (*i)->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), *i));
247 _scale_amplitude = other->_scale_amplitude;
248 _envelope = other->_envelope;
250 _fade_in_disabled = 0;
251 _fade_out_disabled = 0;
253 save_state ("initial state");
255 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
258 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, const XMLNode& node)
260 _fade_in (0.0, 2.0, 1.0, false),
261 _fade_out (0.0, 2.0, 1.0, false),
262 _envelope (0.0, 2.0, 1.0, false)
264 sources.push_back (src);
265 master_sources.push_back (src);
266 src->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), src));
268 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
270 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
273 set_default_fades ();
275 if (set_state (node)) {
276 throw failed_constructor();
279 save_state ("initial state");
281 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
284 AudioRegion::AudioRegion (SourceList& srcs, const XMLNode& node)
286 _fade_in (0.0, 2.0, 1.0, false),
287 _fade_out (0.0, 2.0, 1.0, false),
288 _envelope (0.0, 2.0, 1.0, false)
290 set<boost::shared_ptr<AudioSource> > unique_srcs;
292 for (SourceList::iterator i=srcs.begin(); i != srcs.end(); ++i) {
293 sources.push_back (*i);
294 (*i)->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), *i));
295 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
297 result = unique_srcs.insert (*i);
300 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
302 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
307 for (SourceList::iterator i = srcs.begin(); i != srcs.end(); ++i) {
308 master_sources.push_back (*i);
309 if (unique_srcs.find (*i) == unique_srcs.end()) {
310 (*i)->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), *i));
314 set_default_fades ();
315 _scale_amplitude = 1.0;
317 if (set_state (node)) {
318 throw failed_constructor();
321 save_state ("initial state");
323 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
326 AudioRegion::~AudioRegion ()
328 GoingAway (); /* EMIT SIGNAL */
332 AudioRegion::state_factory (std::string why) const
334 AudioRegionState* state = new AudioRegionState (why);
336 Region::store_state (*state);
338 state->_fade_in = _fade_in;
339 state->_fade_out = _fade_out;
340 state->_envelope = _envelope;
341 state->_scale_amplitude = _scale_amplitude;
342 state->_fade_in_disabled = _fade_in_disabled;
343 state->_fade_out_disabled = _fade_out_disabled;
349 AudioRegion::restore_state (StateManager::State& sstate)
351 AudioRegionState* state = dynamic_cast<AudioRegionState*> (&sstate);
353 Change what_changed = Region::restore_and_return_flags (*state);
355 if (_flags != Flag (state->_flags)) {
357 uint32_t old_flags = _flags;
359 _flags = Flag (state->_flags);
361 if ((old_flags ^ state->_flags) & EnvelopeActive) {
362 what_changed = Change (what_changed|EnvelopeActiveChanged);
366 if (!(_fade_in == state->_fade_in)) {
367 _fade_in = state->_fade_in;
368 what_changed = Change (what_changed|FadeInChanged);
371 if (!(_fade_out == state->_fade_out)) {
372 _fade_out = state->_fade_out;
373 what_changed = Change (what_changed|FadeOutChanged);
376 if (_scale_amplitude != state->_scale_amplitude) {
377 _scale_amplitude = state->_scale_amplitude;
378 what_changed = Change (what_changed|ScaleAmplitudeChanged);
381 if (_fade_in_disabled != state->_fade_in_disabled) {
382 if (_fade_in_disabled == 0 && state->_fade_in_disabled) {
383 set_fade_in_active (false);
384 } else if (_fade_in_disabled && state->_fade_in_disabled == 0) {
385 set_fade_in_active (true);
387 _fade_in_disabled = state->_fade_in_disabled;
390 if (_fade_out_disabled != state->_fade_out_disabled) {
391 if (_fade_out_disabled == 0 && state->_fade_out_disabled) {
392 set_fade_out_active (false);
393 } else if (_fade_out_disabled && state->_fade_out_disabled == 0) {
394 set_fade_out_active (true);
396 _fade_out_disabled = state->_fade_out_disabled;
399 /* XXX need a way to test stored state versus current for envelopes */
401 _envelope = state->_envelope;
402 what_changed = Change (what_changed);
408 AudioRegion::get_memento() const
410 return sigc::bind (mem_fun (*(const_cast<AudioRegion *> (this)), &StateManager::use_state), _current_state_id);
414 AudioRegion::verify_length (nframes_t len)
416 if (boost::dynamic_pointer_cast<DestructiveFileSource>(source())) {
420 for (uint32_t n=0; n < sources.size(); ++n) {
421 if (_start > sources[n]->length() - len) {
429 AudioRegion::verify_start_and_length (nframes_t new_start, nframes_t new_length)
431 if (boost::dynamic_pointer_cast<DestructiveFileSource>(source())) {
435 for (uint32_t n=0; n < sources.size(); ++n) {
436 if (new_length > sources[n]->length() - new_start) {
443 AudioRegion::verify_start (nframes_t pos)
445 if (boost::dynamic_pointer_cast<DestructiveFileSource>(source())) {
449 for (uint32_t n=0; n < sources.size(); ++n) {
450 if (pos > sources[n]->length() - _length) {
458 AudioRegion::verify_start_mutable (nframes_t& new_start)
460 if (boost::dynamic_pointer_cast<DestructiveFileSource>(source())) {
464 for (uint32_t n=0; n < sources.size(); ++n) {
465 if (new_start > sources[n]->length() - _length) {
466 new_start = sources[n]->length() - _length;
472 AudioRegion::set_envelope_active (bool yn)
474 if (envelope_active() != yn) {
477 snprintf (buf, sizeof (buf), "envelope active");
478 _flags = Flag (_flags|EnvelopeActive);
480 snprintf (buf, sizeof (buf), "envelope off");
481 _flags = Flag (_flags & ~EnvelopeActive);
486 send_change (EnvelopeActiveChanged);
492 AudioRegion::read_peaks (PeakData *buf, nframes_t npeaks, nframes_t offset, nframes_t cnt, uint32_t chan_n, double samples_per_unit) const
494 if (chan_n >= sources.size()) {
498 if (sources[chan_n]->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
501 if (_scale_amplitude != 1.0) {
502 for (nframes_t n = 0; n < npeaks; ++n) {
503 buf[n].max *= _scale_amplitude;
504 buf[n].min *= _scale_amplitude;
512 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
514 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
516 return _read_at (sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, read_frames, skip_frames);
520 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, nframes_t position,
521 nframes_t cnt, uint32_t chan_n) const
523 return _read_at (master_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0);
527 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
528 nframes_t position, nframes_t cnt,
529 uint32_t chan_n, nframes_t read_frames, nframes_t skip_frames) const
531 nframes_t internal_offset;
532 nframes_t buf_offset;
535 /* precondition: caller has verified that we cover the desired section */
537 if (chan_n >= sources.size()) {
538 return 0; /* read nothing */
541 if (position < _position) {
543 buf_offset = _position - position;
546 internal_offset = position - _position;
550 if (internal_offset >= _length) {
551 return 0; /* read nothing */
554 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
555 return 0; /* read nothing */
559 /* overwrite whatever is there */
560 mixdown_buffer = buf + buf_offset;
562 mixdown_buffer += buf_offset;
566 return 0; /* read nothing */
569 _read_data_count = 0;
571 if (srcs[chan_n]->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
572 return 0; /* "read nothing" */
575 _read_data_count += srcs[chan_n]->read_data_count();
579 if (_flags & FadeIn) {
581 nframes_t fade_in_length = (nframes_t) _fade_in.back()->when;
583 /* see if this read is within the fade in */
585 if (internal_offset < fade_in_length) {
589 limit = min (to_read, fade_in_length - internal_offset);
591 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
593 for (nframes_t n = 0; n < limit; ++n) {
594 mixdown_buffer[n] *= gain_buffer[n];
601 if (_flags & FadeOut) {
603 /* see if some part of this read is within the fade out */
605 /* ................. >| REGION
611 _length - fade_out_length
614 ^internal_offset + to_read
616 we need the intersection of [internal_offset,internal_offset+to_read] with
617 [_length - fade_out_length, _length]
622 nframes_t fade_out_length = (nframes_t) _fade_out.back()->when;
623 nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
624 nframes_t fade_interval_end = min(internal_offset + to_read, _length);
626 if (fade_interval_end > fade_interval_start) {
627 /* (part of the) the fade out is in this buffer */
629 nframes_t limit = fade_interval_end - fade_interval_start;
630 nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
631 nframes_t fade_offset = fade_interval_start - internal_offset;
633 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
635 for (nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
636 mixdown_buffer[m] *= gain_buffer[n];
642 /* Regular gain curves */
644 if (envelope_active()) {
645 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
647 if (_scale_amplitude != 1.0f) {
648 for (nframes_t n = 0; n < to_read; ++n) {
649 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
652 for (nframes_t n = 0; n < to_read; ++n) {
653 mixdown_buffer[n] *= gain_buffer[n];
656 } else if (_scale_amplitude != 1.0f) {
657 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
662 /* gack. the things we do for users.
667 for (nframes_t n = 0; n < to_read; ++n) {
668 buf[n] += mixdown_buffer[n];
676 AudioRegion::state (bool full)
678 XMLNode& node (Region::state (full));
682 LocaleGuard lg (X_("POSIX"));
684 snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
685 node.add_property ("flags", buf);
686 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
687 node.add_property ("scale-gain", buf);
689 for (uint32_t n=0; n < sources.size(); ++n) {
690 snprintf (buf2, sizeof(buf2), "source-%d", n);
691 sources[n]->id().print (buf);
692 node.add_property (buf2, buf);
695 snprintf (buf, sizeof (buf), "%u", (uint32_t) sources.size());
696 node.add_property ("channels", buf);
700 child = node.add_child (X_("FadeIn"));
702 if ((_flags & DefaultFadeIn)) {
703 child->add_property (X_("default"), X_("yes"));
705 _fade_in.store_state (*child);
708 child = node.add_child (X_("FadeOut"));
710 if ((_flags & DefaultFadeOut)) {
711 child->add_property (X_("default"), X_("yes"));
713 _fade_out.store_state (*child);
717 child = node.add_child ("Envelope");
720 bool default_env = false;
722 // If there are only two points, the points are in the start of the region and the end of the region
723 // so, if they are both at 1.0f, that means the default region.
724 if (_envelope.size() == 2 &&
725 _envelope.front()->value == 1.0f &&
726 _envelope.back()->value==1.0f) {
727 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
733 child->add_property ("default", "yes");
735 _envelope.store_state (*child);
738 child->add_property ("default", "yes");
741 if (full && _extra_xml) {
742 node.add_child_copy (*_extra_xml);
749 AudioRegion::set_state (const XMLNode& node)
751 const XMLNodeList& nlist = node.children();
752 const XMLProperty *prop;
753 LocaleGuard lg (X_("POSIX"));
755 Region::set_state (node);
757 if ((prop = node.property ("flags")) != 0) {
758 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
760 _flags = Flag (_flags & ~Region::LeftOfSplit);
761 _flags = Flag (_flags & ~Region::RightOfSplit);
764 if ((prop = node.property ("scale-gain")) != 0) {
765 _scale_amplitude = atof (prop->value().c_str());
767 _scale_amplitude = 1.0;
770 /* Now find envelope description and other misc child items */
772 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
779 if (child->name() == "Envelope") {
783 if ((prop = child->property ("default")) != 0) {
784 set_default_envelope ();
786 _envelope.load_state (*child);
789 _envelope.set_max_xval (_length);
790 _envelope.truncate_end (_length);
792 } else if (child->name() == "FadeIn") {
796 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
797 set_default_fade_in ();
800 _fade_in.load_state (*child);
803 } else if (child->name() == "FadeOut") {
807 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
808 set_default_fade_out ();
810 _fade_out.load_state (*child);
819 AudioRegion::set_fade_in_shape (FadeShape shape)
821 set_fade_in (shape, (nframes_t) _fade_in.back()->when);
825 AudioRegion::set_fade_out_shape (FadeShape shape)
827 set_fade_out (shape, (nframes_t) _fade_out.back()->when);
831 AudioRegion::set_fade_in (FadeShape shape, nframes_t len)
838 _fade_in.add (0.0, 0.0);
839 _fade_in.add (len, 1.0);
844 _fade_in.add (len * 0.389401, 0.0333333);
845 _fade_in.add (len * 0.629032, 0.0861111);
846 _fade_in.add (len * 0.829493, 0.233333);
847 _fade_in.add (len * 0.9447, 0.483333);
848 _fade_in.add (len * 0.976959, 0.697222);
849 _fade_in.add (len, 1);
854 _fade_in.add (len * 0.0207373, 0.197222);
855 _fade_in.add (len * 0.0645161, 0.525);
856 _fade_in.add (len * 0.152074, 0.802778);
857 _fade_in.add (len * 0.276498, 0.919444);
858 _fade_in.add (len * 0.481567, 0.980556);
859 _fade_in.add (len * 0.767281, 1);
860 _fade_in.add (len, 1);
865 _fade_in.add (len * 0.0737327, 0.308333);
866 _fade_in.add (len * 0.246544, 0.658333);
867 _fade_in.add (len * 0.470046, 0.886111);
868 _fade_in.add (len * 0.652074, 0.972222);
869 _fade_in.add (len * 0.771889, 0.988889);
870 _fade_in.add (len, 1);
875 _fade_in.add (len * 0.304147, 0.0694444);
876 _fade_in.add (len * 0.529954, 0.152778);
877 _fade_in.add (len * 0.725806, 0.333333);
878 _fade_in.add (len * 0.847926, 0.558333);
879 _fade_in.add (len * 0.919355, 0.730556);
880 _fade_in.add (len, 1);
885 _fade_in_shape = shape;
888 save_state (_("fade in change"));
891 send_change (FadeInChanged);
895 AudioRegion::set_fade_out (FadeShape shape, nframes_t len)
902 _fade_out.add (len * 0, 1);
903 _fade_out.add (len * 0.023041, 0.697222);
904 _fade_out.add (len * 0.0553, 0.483333);
905 _fade_out.add (len * 0.170507, 0.233333);
906 _fade_out.add (len * 0.370968, 0.0861111);
907 _fade_out.add (len * 0.610599, 0.0333333);
908 _fade_out.add (len * 1, 0);
912 _fade_out.add (len * 0, 1);
913 _fade_out.add (len * 0.228111, 0.988889);
914 _fade_out.add (len * 0.347926, 0.972222);
915 _fade_out.add (len * 0.529954, 0.886111);
916 _fade_out.add (len * 0.753456, 0.658333);
917 _fade_out.add (len * 0.9262673, 0.308333);
918 _fade_out.add (len * 1, 0);
922 _fade_out.add (len * 0, 1);
923 _fade_out.add (len * 0.305556, 1);
924 _fade_out.add (len * 0.548611, 0.991736);
925 _fade_out.add (len * 0.759259, 0.931129);
926 _fade_out.add (len * 0.918981, 0.68595);
927 _fade_out.add (len * 0.976852, 0.22865);
928 _fade_out.add (len * 1, 0);
932 _fade_out.add (len * 0, 1);
933 _fade_out.add (len * 0.080645, 0.730556);
934 _fade_out.add (len * 0.277778, 0.289256);
935 _fade_out.add (len * 0.470046, 0.152778);
936 _fade_out.add (len * 0.695853, 0.0694444);
937 _fade_out.add (len * 1, 0);
941 _fade_out.add (len * 0, 1);
942 _fade_out.add (len * 1, 0);
947 _fade_out_shape = shape;
950 save_state (_("fade in change"));
953 send_change (FadeOutChanged);
957 AudioRegion::set_fade_in_length (nframes_t len)
959 bool changed = _fade_in.extend_to (len);
962 _flags = Flag (_flags & ~DefaultFadeIn);
966 snprintf (buf, sizeof (buf), "fade in length changed to %u", len);
970 send_change (FadeInChanged);
975 AudioRegion::set_fade_out_length (nframes_t len)
977 bool changed = _fade_out.extend_to (len);
980 _flags = Flag (_flags & ~DefaultFadeOut);
984 snprintf (buf, sizeof (buf), "fade out length changed to %u", len);
989 send_change (FadeOutChanged);
993 AudioRegion::set_fade_in_active (bool yn)
995 if (yn == (_flags & FadeIn)) {
999 _flags = Flag (_flags|FadeIn);
1001 _flags = Flag (_flags & ~FadeIn);
1004 send_change (FadeInActiveChanged);
1008 AudioRegion::set_fade_out_active (bool yn)
1010 if (yn == (_flags & FadeOut)) {
1014 _flags = Flag (_flags | FadeOut);
1016 _flags = Flag (_flags & ~FadeOut);
1019 send_change (FadeOutActiveChanged);
1023 AudioRegion::set_default_fade_in ()
1025 set_fade_in (Linear, 64);
1029 AudioRegion::set_default_fade_out ()
1031 set_fade_out (Linear, 64);
1035 AudioRegion::set_default_fades ()
1037 _fade_in_disabled = 0;
1038 _fade_out_disabled = 0;
1039 set_default_fade_in ();
1040 set_default_fade_out ();
1044 AudioRegion::set_default_envelope ()
1046 _envelope.freeze ();
1048 _envelope.add (0, 1.0f);
1049 _envelope.add (_length, 1.0f);
1054 AudioRegion::recompute_at_end ()
1056 /* our length has changed. recompute a new final point by interpolating
1057 based on the the existing curve.
1060 _envelope.freeze ();
1061 _envelope.truncate_end (_length);
1062 _envelope.set_max_xval (_length);
1065 if (_fade_in.back()->when > _length) {
1066 _fade_in.extend_to (_length);
1067 send_change (FadeInChanged);
1070 if (_fade_out.back()->when > _length) {
1071 _fade_out.extend_to (_length);
1072 send_change (FadeOutChanged);
1077 AudioRegion::recompute_at_start ()
1079 /* as above, but the shift was from the front */
1081 _envelope.truncate_start (_length);
1083 if (_fade_in.back()->when > _length) {
1084 _fade_in.extend_to (_length);
1085 send_change (FadeInChanged);
1088 if (_fade_out.back()->when > _length) {
1089 _fade_out.extend_to (_length);
1090 send_change (FadeOutChanged);
1095 AudioRegion::separate_by_channel (Session& session, vector<AudioRegion*>& v) const
1100 for (SourceList::const_iterator i = master_sources.begin(); i != master_sources.end(); ++i) {
1103 srcs.push_back (*i);
1105 /* generate a new name */
1107 if (session.region_name (new_name, _name)) {
1111 /* create a copy with just one source */
1113 v.push_back (new AudioRegion (srcs, _start, _length, new_name, _layer, _flags));
1120 AudioRegion::source_deleted (boost::shared_ptr<Source> ignored)
1126 AudioRegion::master_source_names ()
1128 SourceList::iterator i;
1130 vector<string> names;
1131 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1132 names.push_back((*i)->name());
1139 AudioRegion::source_equivalent (boost::shared_ptr<const Region> o) const
1141 boost::shared_ptr<const AudioRegion> other = boost::dynamic_pointer_cast<const AudioRegion>(o);
1146 SourceList::const_iterator i;
1147 SourceList::const_iterator io;
1149 for (i = sources.begin(), io = other->sources.begin(); i != sources.end() && io != other->sources.end(); ++i, ++io) {
1150 if ((*i)->id() != (*io)->id()) {
1155 for (i = master_sources.begin(), io = other->master_sources.begin(); i != master_sources.end() && io != other->master_sources.end(); ++i, ++io) {
1156 if ((*i)->id() != (*io)->id()) {
1165 AudioRegion::apply (AudioFilter& filter)
1167 return filter.run (boost::shared_ptr<AudioRegion> (this));
1171 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
1173 const nframes_t blocksize = 4096;
1177 spec.channels = sources.size();
1179 if (spec.prepare (blocksize, session.frame_rate())) {
1184 spec.total_frames = _length;
1186 while (spec.pos < _length && !spec.stop) {
1189 /* step 1: interleave */
1191 to_read = min (_length - spec.pos, blocksize);
1193 if (spec.channels == 1) {
1195 if (sources.front()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
1201 Sample buf[blocksize];
1203 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1205 if (sources[chan]->read (buf, _start + spec.pos, to_read) != to_read) {
1209 for (nframes_t x = 0; x < to_read; ++x) {
1210 spec.dataF[chan+(x*spec.channels)] = buf[x];
1215 if (spec.process (to_read)) {
1219 spec.pos += to_read;
1220 spec.progress = (double) spec.pos /_length;
1227 spec.running = false;
1228 spec.status = status;
1234 boost::shared_ptr<Region>
1235 AudioRegion::get_parent()
1237 boost::shared_ptr<Region> r;
1240 r = _playlist->session().find_whole_file_parent (boost::shared_ptr<AudioRegion>(this));
1247 AudioRegion::set_scale_amplitude (gain_t g)
1249 _scale_amplitude = g;
1251 /* tell the diskstream we're in */
1254 _playlist->Modified();
1257 /* tell everybody else */
1259 send_change (ScaleAmplitudeChanged);
1263 AudioRegion::normalize_to (float target_dB)
1265 const nframes_t blocksize = 64 * 1024;
1266 Sample buf[blocksize];
1271 gain_t target = dB_to_coefficient (target_dB);
1273 if (target == 1.0f) {
1274 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1275 that we may have clipped.
1277 target -= FLT_EPSILON;
1281 fend = _start + _length;
1283 /* first pass: find max amplitude */
1285 while (fpos < fend) {
1289 to_read = min (fend - fpos, blocksize);
1291 for (n = 0; n < n_channels(); ++n) {
1295 if (source (n)->read (buf, fpos, to_read) != to_read) {
1299 maxamp = Session::compute_peak (buf, to_read, maxamp);
1305 if (maxamp == 0.0f) {
1306 /* don't even try */
1310 if (maxamp == target) {
1311 /* we can't do anything useful */
1315 /* compute scale factor */
1317 _scale_amplitude = target/maxamp;
1321 snprintf (buf, sizeof (buf), _("normalized to %.2fdB"), target_dB);
1325 /* tell the diskstream we're in */
1328 _playlist->Modified();
1331 /* tell everybody else */
1333 send_change (ScaleAmplitudeChanged);
1337 AudioRegion::envelope_changed (Change ignored)
1339 save_state (_("envelope change"));
1340 send_change (EnvelopeChanged);
1344 AudioRegion::suspend_fade_in ()
1346 if (++_fade_in_disabled == 1) {
1347 set_fade_in_active (false);
1352 AudioRegion::resume_fade_in ()
1354 if (_fade_in_disabled && --_fade_in_disabled == 0) {
1355 set_fade_in_active (true);
1360 AudioRegion::suspend_fade_out ()
1362 if (++_fade_out_disabled == 1) {
1363 set_fade_out_active (false);
1368 AudioRegion::resume_fade_out ()
1370 if (_fade_out_disabled && --_fade_out_disabled == 0) {
1371 set_fade_out_active (true);
1376 AudioRegion::speed_mismatch (float sr) const
1378 if (sources.empty()) {
1379 /* impossible, but ... */
1383 float fsr = sources.front()->sample_rate();
1389 AudioRegion::source_offset_changed ()
1391 if (boost::dynamic_pointer_cast<DestructiveFileSource>(sources.front())) {
1392 set_start (source()->natural_position(), this);
1393 set_position (source()->natural_position(), this);
1395 set_position (source()->natural_position() + start(), this);
1401 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)
1403 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (nframes_t) npeaks, (nframes_t) start, (nframes_t) cnt, n_chan,samples_per_unit);
1406 uint32_t region_length_from_c (void *arg)
1409 return ((AudioRegion *) arg)->length();
1412 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1414 return ( (AudioRegion *) arg)->source()->available_peaks (zoom_factor) ;