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, jack_nframes_t start, jack_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, jack_nframes_t start, jack_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, jack_nframes_t start, jack_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, jack_nframes_t offset, jack_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 (jack_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 (jack_nframes_t new_start, jack_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 (jack_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 (jack_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, jack_nframes_t npeaks, jack_nframes_t offset, jack_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 (jack_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, jack_nframes_t position,
514 uint32_t chan_n, jack_nframes_t read_frames, jack_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, jack_nframes_t position,
521 jack_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 jack_nframes_t position, jack_nframes_t cnt,
529 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
531 jack_nframes_t internal_offset;
532 jack_nframes_t buf_offset;
533 jack_nframes_t to_read;
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 */
555 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
556 return 0; /* read nothing */
560 /* overwrite whatever is there */
561 mixdown_buffer = buf + buf_offset;
563 mixdown_buffer += buf_offset;
567 return 0; /* read nothing */
570 _read_data_count = 0;
572 if (srcs[chan_n]->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
573 return 0; /* "read nothing" */
576 _read_data_count += srcs[chan_n]->read_data_count();
580 if (_flags & FadeIn) {
582 jack_nframes_t fade_in_length = (jack_nframes_t) _fade_in.back()->when;
584 /* see if this read is within the fade in */
586 if (internal_offset < fade_in_length) {
588 jack_nframes_t limit;
590 limit = min (to_read, fade_in_length - internal_offset);
592 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
594 for (jack_nframes_t n = 0; n < limit; ++n) {
595 mixdown_buffer[n] *= gain_buffer[n];
602 if (_flags & FadeOut) {
607 /* see if some part of this read is within the fade out */
609 /* ................. >| REGION
615 _length - fade_out_length
618 ^internal_offset + to_read
620 we need the intersection of [internal_offset,internal_offset+to_read] with
621 [_length - fade_out_length, _length]
626 jack_nframes_t fade_out_length = (jack_nframes_t) _fade_out.back()->when;
627 jack_nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
628 jack_nframes_t fade_interval_end = min(internal_offset + to_read, _length);
630 if (fade_interval_end > fade_interval_start) {
631 /* (part of the) the fade out is in this buffer */
633 jack_nframes_t limit = fade_interval_end - fade_interval_start;
634 jack_nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
635 jack_nframes_t fade_offset = fade_interval_start - internal_offset;
637 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
639 for (jack_nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
640 mixdown_buffer[m] *= gain_buffer[n];
646 /* Regular gain curves */
648 if (envelope_active()) {
649 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
651 if (_scale_amplitude != 1.0f) {
652 for (jack_nframes_t n = 0; n < to_read; ++n) {
653 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
656 for (jack_nframes_t n = 0; n < to_read; ++n) {
657 mixdown_buffer[n] *= gain_buffer[n];
660 } else if (_scale_amplitude != 1.0f) {
661 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
666 /* gack. the things we do for users.
671 for (jack_nframes_t n = 0; n < to_read; ++n) {
672 buf[n] += mixdown_buffer[n];
680 AudioRegion::state (bool full)
682 XMLNode& node (Region::state (full));
686 LocaleGuard lg (X_("POSIX"));
688 snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
689 node.add_property ("flags", buf);
690 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
691 node.add_property ("scale-gain", buf);
693 for (uint32_t n=0; n < sources.size(); ++n) {
694 snprintf (buf2, sizeof(buf2), "source-%d", n);
695 sources[n]->id().print (buf);
696 node.add_property (buf2, buf);
699 snprintf (buf, sizeof (buf), "%u", (uint32_t) sources.size());
700 node.add_property ("channels", buf);
704 child = node.add_child (X_("FadeIn"));
706 if ((_flags & DefaultFadeIn)) {
707 child->add_property (X_("default"), X_("yes"));
709 _fade_in.store_state (*child);
712 child = node.add_child (X_("FadeOut"));
714 if ((_flags & DefaultFadeOut)) {
715 child->add_property (X_("default"), X_("yes"));
717 _fade_out.store_state (*child);
721 child = node.add_child ("Envelope");
724 bool default_env = false;
726 // If there are only two points, the points are in the start of the region and the end of the region
727 // so, if they are both at 1.0f, that means the default region.
728 if (_envelope.size() == 2 &&
729 _envelope.front()->value == 1.0f &&
730 _envelope.back()->value==1.0f) {
731 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
737 child->add_property ("default", "yes");
739 _envelope.store_state (*child);
742 child->add_property ("default", "yes");
745 if (full && _extra_xml) {
746 node.add_child_copy (*_extra_xml);
753 AudioRegion::set_state (const XMLNode& node)
755 const XMLNodeList& nlist = node.children();
756 const XMLProperty *prop;
757 LocaleGuard lg (X_("POSIX"));
759 Region::set_state (node);
761 if ((prop = node.property ("flags")) != 0) {
762 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
764 _flags = Flag (_flags & ~Region::LeftOfSplit);
765 _flags = Flag (_flags & ~Region::RightOfSplit);
768 if ((prop = node.property ("scale-gain")) != 0) {
769 _scale_amplitude = atof (prop->value().c_str());
771 _scale_amplitude = 1.0;
774 /* Now find envelope description and other misc child items */
776 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
783 if (child->name() == "Envelope") {
787 if ((prop = child->property ("default")) != 0) {
788 set_default_envelope ();
790 _envelope.load_state (*child);
793 _envelope.set_max_xval (_length);
794 _envelope.truncate_end (_length);
796 } else if (child->name() == "FadeIn") {
800 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
801 set_default_fade_in ();
804 _fade_in.load_state (*child);
807 } else if (child->name() == "FadeOut") {
811 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
812 set_default_fade_out ();
814 _fade_out.load_state (*child);
823 AudioRegion::set_fade_in_shape (FadeShape shape)
825 set_fade_in (shape, (jack_nframes_t) _fade_in.back()->when);
829 AudioRegion::set_fade_out_shape (FadeShape shape)
831 set_fade_out (shape, (jack_nframes_t) _fade_out.back()->when);
835 AudioRegion::set_fade_in (FadeShape shape, jack_nframes_t len)
842 _fade_in.add (0.0, 0.0);
843 _fade_in.add (len, 1.0);
848 _fade_in.add (len * 0.389401, 0.0333333);
849 _fade_in.add (len * 0.629032, 0.0861111);
850 _fade_in.add (len * 0.829493, 0.233333);
851 _fade_in.add (len * 0.9447, 0.483333);
852 _fade_in.add (len * 0.976959, 0.697222);
853 _fade_in.add (len, 1);
858 _fade_in.add (len * 0.0207373, 0.197222);
859 _fade_in.add (len * 0.0645161, 0.525);
860 _fade_in.add (len * 0.152074, 0.802778);
861 _fade_in.add (len * 0.276498, 0.919444);
862 _fade_in.add (len * 0.481567, 0.980556);
863 _fade_in.add (len * 0.767281, 1);
864 _fade_in.add (len, 1);
869 _fade_in.add (len * 0.0737327, 0.308333);
870 _fade_in.add (len * 0.246544, 0.658333);
871 _fade_in.add (len * 0.470046, 0.886111);
872 _fade_in.add (len * 0.652074, 0.972222);
873 _fade_in.add (len * 0.771889, 0.988889);
874 _fade_in.add (len, 1);
879 _fade_in.add (len * 0.304147, 0.0694444);
880 _fade_in.add (len * 0.529954, 0.152778);
881 _fade_in.add (len * 0.725806, 0.333333);
882 _fade_in.add (len * 0.847926, 0.558333);
883 _fade_in.add (len * 0.919355, 0.730556);
884 _fade_in.add (len, 1);
889 _fade_in_shape = shape;
892 save_state (_("fade in change"));
895 send_change (FadeInChanged);
899 AudioRegion::set_fade_out (FadeShape shape, jack_nframes_t len)
906 _fade_out.add (len * 0, 1);
907 _fade_out.add (len * 0.023041, 0.697222);
908 _fade_out.add (len * 0.0553, 0.483333);
909 _fade_out.add (len * 0.170507, 0.233333);
910 _fade_out.add (len * 0.370968, 0.0861111);
911 _fade_out.add (len * 0.610599, 0.0333333);
912 _fade_out.add (len * 1, 0);
916 _fade_out.add (len * 0, 1);
917 _fade_out.add (len * 0.228111, 0.988889);
918 _fade_out.add (len * 0.347926, 0.972222);
919 _fade_out.add (len * 0.529954, 0.886111);
920 _fade_out.add (len * 0.753456, 0.658333);
921 _fade_out.add (len * 0.9262673, 0.308333);
922 _fade_out.add (len * 1, 0);
926 _fade_out.add (len * 0, 1);
927 _fade_out.add (len * 0.305556, 1);
928 _fade_out.add (len * 0.548611, 0.991736);
929 _fade_out.add (len * 0.759259, 0.931129);
930 _fade_out.add (len * 0.918981, 0.68595);
931 _fade_out.add (len * 0.976852, 0.22865);
932 _fade_out.add (len * 1, 0);
936 _fade_out.add (len * 0, 1);
937 _fade_out.add (len * 0.080645, 0.730556);
938 _fade_out.add (len * 0.277778, 0.289256);
939 _fade_out.add (len * 0.470046, 0.152778);
940 _fade_out.add (len * 0.695853, 0.0694444);
941 _fade_out.add (len * 1, 0);
945 _fade_out.add (len * 0, 1);
946 _fade_out.add (len * 1, 0);
951 _fade_out_shape = shape;
954 save_state (_("fade in change"));
957 send_change (FadeOutChanged);
961 AudioRegion::set_fade_in_length (jack_nframes_t len)
963 bool changed = _fade_in.extend_to (len);
966 _flags = Flag (_flags & ~DefaultFadeIn);
970 snprintf (buf, sizeof (buf), "fade in length changed to %u", len);
974 send_change (FadeInChanged);
979 AudioRegion::set_fade_out_length (jack_nframes_t len)
981 bool changed = _fade_out.extend_to (len);
984 _flags = Flag (_flags & ~DefaultFadeOut);
988 snprintf (buf, sizeof (buf), "fade out length changed to %u", len);
993 send_change (FadeOutChanged);
997 AudioRegion::set_fade_in_active (bool yn)
999 if (yn == (_flags & FadeIn)) {
1003 _flags = Flag (_flags|FadeIn);
1005 _flags = Flag (_flags & ~FadeIn);
1008 send_change (FadeInActiveChanged);
1012 AudioRegion::set_fade_out_active (bool yn)
1014 if (yn == (_flags & FadeOut)) {
1018 _flags = Flag (_flags | FadeOut);
1020 _flags = Flag (_flags & ~FadeOut);
1023 send_change (FadeOutActiveChanged);
1027 AudioRegion::set_default_fade_in ()
1029 set_fade_in (Linear, 64);
1033 AudioRegion::set_default_fade_out ()
1035 set_fade_out (Linear, 64);
1039 AudioRegion::set_default_fades ()
1041 _fade_in_disabled = 0;
1042 _fade_out_disabled = 0;
1043 set_default_fade_in ();
1044 set_default_fade_out ();
1048 AudioRegion::set_default_envelope ()
1050 _envelope.freeze ();
1052 _envelope.add (0, 1.0f);
1053 _envelope.add (_length, 1.0f);
1058 AudioRegion::recompute_at_end ()
1060 /* our length has changed. recompute a new final point by interpolating
1061 based on the the existing curve.
1064 _envelope.freeze ();
1065 _envelope.truncate_end (_length);
1066 _envelope.set_max_xval (_length);
1069 if (_fade_in.back()->when > _length) {
1070 _fade_in.extend_to (_length);
1071 send_change (FadeInChanged);
1074 if (_fade_out.back()->when > _length) {
1075 _fade_out.extend_to (_length);
1076 send_change (FadeOutChanged);
1081 AudioRegion::recompute_at_start ()
1083 /* as above, but the shift was from the front */
1085 _envelope.truncate_start (_length);
1087 if (_fade_in.back()->when > _length) {
1088 _fade_in.extend_to (_length);
1089 send_change (FadeInChanged);
1092 if (_fade_out.back()->when > _length) {
1093 _fade_out.extend_to (_length);
1094 send_change (FadeOutChanged);
1099 AudioRegion::separate_by_channel (Session& session, vector<AudioRegion*>& v) const
1104 for (SourceList::const_iterator i = master_sources.begin(); i != master_sources.end(); ++i) {
1107 srcs.push_back (*i);
1109 /* generate a new name */
1111 if (session.region_name (new_name, _name)) {
1115 /* create a copy with just one source */
1117 v.push_back (new AudioRegion (srcs, _start, _length, new_name, _layer, _flags));
1124 AudioRegion::source_deleted (boost::shared_ptr<Source> ignored)
1130 AudioRegion::master_source_names ()
1132 SourceList::iterator i;
1134 vector<string> names;
1135 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1136 names.push_back((*i)->name());
1143 AudioRegion::source_equivalent (boost::shared_ptr<const Region> o) const
1145 boost::shared_ptr<const AudioRegion> other = boost::dynamic_pointer_cast<const AudioRegion>(o);
1150 SourceList::const_iterator i;
1151 SourceList::const_iterator io;
1153 for (i = sources.begin(), io = other->sources.begin(); i != sources.end() && io != other->sources.end(); ++i, ++io) {
1154 if ((*i)->id() != (*io)->id()) {
1159 for (i = master_sources.begin(), io = other->master_sources.begin(); i != master_sources.end() && io != other->master_sources.end(); ++i, ++io) {
1160 if ((*i)->id() != (*io)->id()) {
1169 AudioRegion::apply (AudioFilter& filter)
1171 return filter.run (boost::shared_ptr<AudioRegion> (this));
1175 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
1177 const jack_nframes_t blocksize = 4096;
1178 jack_nframes_t to_read;
1181 spec.channels = sources.size();
1183 if (spec.prepare (blocksize, session.frame_rate())) {
1188 spec.total_frames = _length;
1190 while (spec.pos < _length && !spec.stop) {
1193 /* step 1: interleave */
1195 to_read = min (_length - spec.pos, blocksize);
1197 if (spec.channels == 1) {
1199 if (sources.front()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
1205 Sample buf[blocksize];
1207 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1209 if (sources[chan]->read (buf, _start + spec.pos, to_read) != to_read) {
1213 for (jack_nframes_t x = 0; x < to_read; ++x) {
1214 spec.dataF[chan+(x*spec.channels)] = buf[x];
1219 if (spec.process (to_read)) {
1223 spec.pos += to_read;
1224 spec.progress = (double) spec.pos /_length;
1231 spec.running = false;
1232 spec.status = status;
1238 boost::shared_ptr<Region>
1239 AudioRegion::get_parent()
1241 boost::shared_ptr<Region> r;
1244 r = _playlist->session().find_whole_file_parent (boost::shared_ptr<AudioRegion>(this));
1251 AudioRegion::set_scale_amplitude (gain_t g)
1253 _scale_amplitude = g;
1255 /* tell the diskstream we're in */
1258 _playlist->Modified();
1261 /* tell everybody else */
1263 send_change (ScaleAmplitudeChanged);
1267 AudioRegion::normalize_to (float target_dB)
1269 const jack_nframes_t blocksize = 64 * 1024;
1270 Sample buf[blocksize];
1271 jack_nframes_t fpos;
1272 jack_nframes_t fend;
1273 jack_nframes_t to_read;
1275 gain_t target = dB_to_coefficient (target_dB);
1277 if (target == 1.0f) {
1278 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1279 that we may have clipped.
1281 target -= FLT_EPSILON;
1285 fend = _start + _length;
1287 /* first pass: find max amplitude */
1289 while (fpos < fend) {
1293 to_read = min (fend - fpos, blocksize);
1295 for (n = 0; n < n_channels(); ++n) {
1299 if (source (n)->read (buf, fpos, to_read) != to_read) {
1303 maxamp = Session::compute_peak (buf, to_read, maxamp);
1309 if (maxamp == 0.0f) {
1310 /* don't even try */
1314 if (maxamp == target) {
1315 /* we can't do anything useful */
1319 /* compute scale factor */
1321 _scale_amplitude = target/maxamp;
1325 snprintf (buf, sizeof (buf), _("normalized to %.2fdB"), target_dB);
1329 /* tell the diskstream we're in */
1332 _playlist->Modified();
1335 /* tell everybody else */
1337 send_change (ScaleAmplitudeChanged);
1341 AudioRegion::envelope_changed (Change ignored)
1343 save_state (_("envelope change"));
1344 send_change (EnvelopeChanged);
1348 AudioRegion::suspend_fade_in ()
1350 if (++_fade_in_disabled == 1) {
1351 set_fade_in_active (false);
1356 AudioRegion::resume_fade_in ()
1358 if (_fade_in_disabled && --_fade_in_disabled == 0) {
1359 set_fade_in_active (true);
1364 AudioRegion::suspend_fade_out ()
1366 if (++_fade_out_disabled == 1) {
1367 set_fade_out_active (false);
1372 AudioRegion::resume_fade_out ()
1374 if (_fade_out_disabled && --_fade_out_disabled == 0) {
1375 set_fade_out_active (true);
1380 AudioRegion::speed_mismatch (float sr) const
1382 if (sources.empty()) {
1383 /* impossible, but ... */
1387 float fsr = sources.front()->sample_rate();
1393 AudioRegion::source_offset_changed ()
1395 if (boost::dynamic_pointer_cast<DestructiveFileSource> (source())) {
1396 set_start (source()->natural_position(), this);
1397 set_position (source()->natural_position(), this);
1399 set_position (source()->natural_position() + start(), this);
1405 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)
1407 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (jack_nframes_t) npeaks, (jack_nframes_t) start, (jack_nframes_t) cnt, n_chan,samples_per_unit);
1410 uint32_t region_length_from_c (void *arg)
1413 return ((AudioRegion *) arg)->length();
1416 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1418 return ( (AudioRegion *) arg)->source()->available_peaks (zoom_factor) ;