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>
47 using namespace ARDOUR;
49 /* a Session will reset these to its chosen defaults by calling AudioRegion::set_default_fade() */
51 Change AudioRegion::FadeInChanged = ARDOUR::new_change();
52 Change AudioRegion::FadeOutChanged = ARDOUR::new_change();
53 Change AudioRegion::FadeInActiveChanged = ARDOUR::new_change();
54 Change AudioRegion::FadeOutActiveChanged = ARDOUR::new_change();
55 Change AudioRegion::EnvelopeActiveChanged = ARDOUR::new_change();
56 Change AudioRegion::ScaleAmplitudeChanged = ARDOUR::new_change();
57 Change AudioRegion::EnvelopeChanged = ARDOUR::new_change();
59 AudioRegionState::AudioRegionState (string why)
61 _fade_in (0.0, 2.0, 1.0, false),
62 _fade_out (0.0, 2.0, 1.0, false),
63 _envelope (0.0, 2.0, 1.0, false)
67 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, jack_nframes_t start, jack_nframes_t length)
68 : Region (start, length, PBD::basename_nosuffix(src->name()), 0, Region::Flag(Region::DefaultFlags|Region::External)),
69 _fade_in (0.0, 2.0, 1.0, false),
70 _fade_out (0.0, 2.0, 1.0, false),
71 _envelope (0.0, 2.0, 1.0, false)
73 /* basic AudioRegion constructor */
75 sources.push_back (src);
76 master_sources.push_back (src);
77 src->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), src));
79 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
81 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
84 _scale_amplitude = 1.0;
87 set_default_envelope ();
89 save_state ("initial state");
91 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
94 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags)
95 : Region (start, length, name, layer, flags),
96 _fade_in (0.0, 2.0, 1.0, false),
97 _fade_out (0.0, 2.0, 1.0, false),
98 _envelope (0.0, 2.0, 1.0, false)
100 /* basic AudioRegion constructor */
102 sources.push_back (src);
103 master_sources.push_back (src);
104 src->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), src));
106 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
108 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
111 _scale_amplitude = 1.0;
113 set_default_fades ();
114 set_default_envelope ();
115 save_state ("initial state");
117 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
120 AudioRegion::AudioRegion (SourceList& srcs, jack_nframes_t start, jack_nframes_t length, const string& name, layer_t layer, Flag flags)
121 : Region (start, length, name, layer, flags),
122 _fade_in (0.0, 2.0, 1.0, false),
123 _fade_out (0.0, 2.0, 1.0, false),
124 _envelope (0.0, 2.0, 1.0, false)
126 /* basic AudioRegion constructor */
128 for (SourceList::iterator i=srcs.begin(); i != srcs.end(); ++i) {
129 sources.push_back (*i);
130 master_sources.push_back (*i);
131 (*i)->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), (*i)));
133 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> ((*i));
135 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
139 _scale_amplitude = 1.0;
141 set_default_fades ();
142 set_default_envelope ();
143 save_state ("initial state");
145 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
149 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other, jack_nframes_t offset, jack_nframes_t length, const string& name, layer_t layer, Flag flags)
150 : Region (other, offset, length, name, layer, flags),
151 _fade_in (other->_fade_in),
152 _fade_out (other->_fade_out),
153 _envelope (other->_envelope, (double) offset, (double) offset + length)
155 /* create a new AudioRegion, that is part of an existing one */
157 set<boost::shared_ptr<AudioSource> > unique_srcs;
159 for (SourceList::const_iterator i= other->sources.begin(); i != other->sources.end(); ++i) {
160 sources.push_back (*i);
161 (*i)->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), *i));
163 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
165 result = unique_srcs.insert (*i);
168 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
170 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
175 for (SourceList::const_iterator i = other->master_sources.begin(); i != other->master_sources.end(); ++i) {
176 if (unique_srcs.find (*i) == unique_srcs.end()) {
177 (*i)->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), *i));
179 master_sources.push_back (*i);
182 /* return to default fades if the existing ones are too long */
183 _fade_in_disabled = 0;
184 _fade_out_disabled = 0;
187 if (_flags & LeftOfSplit) {
188 if (_fade_in.back()->when >= _length) {
189 set_default_fade_in ();
191 _fade_in_disabled = other->_fade_in_disabled;
193 set_default_fade_out ();
194 _flags = Flag (_flags & ~Region::LeftOfSplit);
197 if (_flags & RightOfSplit) {
198 if (_fade_out.back()->when >= _length) {
199 set_default_fade_out ();
201 _fade_out_disabled = other->_fade_out_disabled;
203 set_default_fade_in ();
204 _flags = Flag (_flags & ~Region::RightOfSplit);
207 _scale_amplitude = other->_scale_amplitude;
209 save_state ("initial state");
211 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
214 AudioRegion::AudioRegion (boost::shared_ptr<const AudioRegion> other)
216 _fade_in (other->_fade_in),
217 _fade_out (other->_fade_out),
218 _envelope (other->_envelope)
220 /* Pure copy constructor */
222 set<boost::shared_ptr<AudioSource> > unique_srcs;
224 for (SourceList::const_iterator i = other->sources.begin(); i != other->sources.end(); ++i) {
225 sources.push_back (*i);
226 (*i)->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), *i));
227 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
229 result = unique_srcs.insert (*i);
232 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
234 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
239 for (SourceList::const_iterator i = other->master_sources.begin(); i != other->master_sources.end(); ++i) {
240 master_sources.push_back (*i);
241 if (unique_srcs.find (*i) == unique_srcs.end()) {
242 (*i)->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), *i));
246 _scale_amplitude = other->_scale_amplitude;
247 _envelope = other->_envelope;
249 _fade_in_disabled = 0;
250 _fade_out_disabled = 0;
252 save_state ("initial state");
254 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
257 AudioRegion::AudioRegion (boost::shared_ptr<AudioSource> src, const XMLNode& node)
259 _fade_in (0.0, 2.0, 1.0, false),
260 _fade_out (0.0, 2.0, 1.0, false),
261 _envelope (0.0, 2.0, 1.0, false)
263 sources.push_back (src);
264 master_sources.push_back (src);
265 src->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), src));
267 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (src);
269 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
272 set_default_fades ();
274 if (set_state (node)) {
275 throw failed_constructor();
278 save_state ("initial state");
280 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
283 AudioRegion::AudioRegion (SourceList& srcs, const XMLNode& node)
285 _fade_in (0.0, 2.0, 1.0, false),
286 _fade_out (0.0, 2.0, 1.0, false),
287 _envelope (0.0, 2.0, 1.0, false)
289 set<boost::shared_ptr<AudioSource> > unique_srcs;
291 for (SourceList::iterator i=srcs.begin(); i != srcs.end(); ++i) {
292 sources.push_back (*i);
293 (*i)->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), *i));
294 pair<set<boost::shared_ptr<AudioSource> >::iterator,bool> result;
296 result = unique_srcs.insert (*i);
299 boost::shared_ptr<AudioFileSource> afs = boost::dynamic_pointer_cast<AudioFileSource> (*i);
301 afs->HeaderPositionOffsetChanged.connect (mem_fun (*this, &AudioRegion::source_offset_changed));
306 for (SourceList::iterator i = srcs.begin(); i != srcs.end(); ++i) {
307 master_sources.push_back (*i);
308 if (unique_srcs.find (*i) == unique_srcs.end()) {
309 (*i)->GoingAway.connect (bind (mem_fun (*this, &AudioRegion::source_deleted), *i));
313 set_default_fades ();
314 _scale_amplitude = 1.0;
316 if (set_state (node)) {
317 throw failed_constructor();
320 save_state ("initial state");
322 _envelope.StateChanged.connect (mem_fun (*this, &AudioRegion::envelope_changed));
325 AudioRegion::~AudioRegion ()
327 GoingAway (); /* EMIT SIGNAL */
331 AudioRegion::state_factory (std::string why) const
333 AudioRegionState* state = new AudioRegionState (why);
335 Region::store_state (*state);
337 state->_fade_in = _fade_in;
338 state->_fade_out = _fade_out;
339 state->_envelope = _envelope;
340 state->_scale_amplitude = _scale_amplitude;
341 state->_fade_in_disabled = _fade_in_disabled;
342 state->_fade_out_disabled = _fade_out_disabled;
348 AudioRegion::restore_state (StateManager::State& sstate)
350 AudioRegionState* state = dynamic_cast<AudioRegionState*> (&sstate);
352 Change what_changed = Region::restore_and_return_flags (*state);
354 if (_flags != Flag (state->_flags)) {
356 uint32_t old_flags = _flags;
358 _flags = Flag (state->_flags);
360 if ((old_flags ^ state->_flags) & EnvelopeActive) {
361 what_changed = Change (what_changed|EnvelopeActiveChanged);
365 if (!(_fade_in == state->_fade_in)) {
366 _fade_in = state->_fade_in;
367 what_changed = Change (what_changed|FadeInChanged);
370 if (!(_fade_out == state->_fade_out)) {
371 _fade_out = state->_fade_out;
372 what_changed = Change (what_changed|FadeOutChanged);
375 if (_scale_amplitude != state->_scale_amplitude) {
376 _scale_amplitude = state->_scale_amplitude;
377 what_changed = Change (what_changed|ScaleAmplitudeChanged);
380 if (_fade_in_disabled != state->_fade_in_disabled) {
381 if (_fade_in_disabled == 0 && state->_fade_in_disabled) {
382 set_fade_in_active (false);
383 } else if (_fade_in_disabled && state->_fade_in_disabled == 0) {
384 set_fade_in_active (true);
386 _fade_in_disabled = state->_fade_in_disabled;
389 if (_fade_out_disabled != state->_fade_out_disabled) {
390 if (_fade_out_disabled == 0 && state->_fade_out_disabled) {
391 set_fade_out_active (false);
392 } else if (_fade_out_disabled && state->_fade_out_disabled == 0) {
393 set_fade_out_active (true);
395 _fade_out_disabled = state->_fade_out_disabled;
398 /* XXX need a way to test stored state versus current for envelopes */
400 _envelope = state->_envelope;
401 what_changed = Change (what_changed);
407 AudioRegion::get_memento() const
409 return sigc::bind (mem_fun (*(const_cast<AudioRegion *> (this)), &StateManager::use_state), _current_state_id);
413 AudioRegion::verify_length (jack_nframes_t len)
415 for (uint32_t n=0; n < sources.size(); ++n) {
416 if (_start > sources[n]->length() - len) {
424 AudioRegion::verify_start_and_length (jack_nframes_t new_start, jack_nframes_t new_length)
426 for (uint32_t n=0; n < sources.size(); ++n) {
427 if (new_length > sources[n]->length() - new_start) {
434 AudioRegion::verify_start (jack_nframes_t pos)
436 for (uint32_t n=0; n < sources.size(); ++n) {
437 if (pos > sources[n]->length() - _length) {
445 AudioRegion::verify_start_mutable (jack_nframes_t& new_start)
447 for (uint32_t n=0; n < sources.size(); ++n) {
448 if (new_start > sources[n]->length() - _length) {
449 new_start = sources[n]->length() - _length;
455 AudioRegion::set_envelope_active (bool yn)
457 if (envelope_active() != yn) {
460 snprintf (buf, sizeof (buf), "envelope active");
461 _flags = Flag (_flags|EnvelopeActive);
463 snprintf (buf, sizeof (buf), "envelope off");
464 _flags = Flag (_flags & ~EnvelopeActive);
469 send_change (EnvelopeActiveChanged);
475 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
477 if (chan_n >= sources.size()) {
481 if (sources[chan_n]->read_peaks (buf, npeaks, offset, cnt, samples_per_unit)) {
484 if (_scale_amplitude != 1.0) {
485 for (jack_nframes_t n = 0; n < npeaks; ++n) {
486 buf[n].max *= _scale_amplitude;
487 buf[n].min *= _scale_amplitude;
495 AudioRegion::read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, jack_nframes_t position,
497 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
499 return _read_at (sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, read_frames, skip_frames);
503 AudioRegion::master_read_at (Sample *buf, Sample *mixdown_buffer, float *gain_buffer, jack_nframes_t position,
504 jack_nframes_t cnt, uint32_t chan_n) const
506 return _read_at (master_sources, buf, mixdown_buffer, gain_buffer, position, cnt, chan_n, 0, 0);
510 AudioRegion::_read_at (const SourceList& srcs, Sample *buf, Sample *mixdown_buffer, float *gain_buffer,
511 jack_nframes_t position, jack_nframes_t cnt,
512 uint32_t chan_n, jack_nframes_t read_frames, jack_nframes_t skip_frames) const
514 jack_nframes_t internal_offset;
515 jack_nframes_t buf_offset;
516 jack_nframes_t to_read;
518 /* precondition: caller has verified that we cover the desired section */
520 if (chan_n >= sources.size()) {
521 return 0; /* read nothing */
524 if (position < _position) {
526 buf_offset = _position - position;
529 internal_offset = position - _position;
533 if (internal_offset >= _length) {
534 return 0; /* read nothing */
538 if ((to_read = min (cnt, _length - internal_offset)) == 0) {
539 return 0; /* read nothing */
543 /* overwrite whatever is there */
544 mixdown_buffer = buf + buf_offset;
546 mixdown_buffer += buf_offset;
550 return 0; /* read nothing */
553 _read_data_count = 0;
555 if (srcs[chan_n]->read (mixdown_buffer, _start + internal_offset, to_read) != to_read) {
556 return 0; /* "read nothing" */
559 _read_data_count += srcs[chan_n]->read_data_count();
563 if (_flags & FadeIn) {
565 jack_nframes_t fade_in_length = (jack_nframes_t) _fade_in.back()->when;
567 /* see if this read is within the fade in */
569 if (internal_offset < fade_in_length) {
571 jack_nframes_t limit;
573 limit = min (to_read, fade_in_length - internal_offset);
575 _fade_in.get_vector (internal_offset, internal_offset+limit, gain_buffer, limit);
577 for (jack_nframes_t n = 0; n < limit; ++n) {
578 mixdown_buffer[n] *= gain_buffer[n];
585 if (_flags & FadeOut) {
590 /* see if some part of this read is within the fade out */
592 /* ................. >| REGION
598 _length - fade_out_length
601 ^internal_offset + to_read
603 we need the intersection of [internal_offset,internal_offset+to_read] with
604 [_length - fade_out_length, _length]
609 jack_nframes_t fade_out_length = (jack_nframes_t) _fade_out.back()->when;
610 jack_nframes_t fade_interval_start = max(internal_offset, _length-fade_out_length);
611 jack_nframes_t fade_interval_end = min(internal_offset + to_read, _length);
613 if (fade_interval_end > fade_interval_start) {
614 /* (part of the) the fade out is in this buffer */
616 jack_nframes_t limit = fade_interval_end - fade_interval_start;
617 jack_nframes_t curve_offset = fade_interval_start - (_length-fade_out_length);
618 jack_nframes_t fade_offset = fade_interval_start - internal_offset;
620 _fade_out.get_vector (curve_offset,curve_offset+limit, gain_buffer, limit);
622 for (jack_nframes_t n = 0, m = fade_offset; n < limit; ++n, ++m) {
623 mixdown_buffer[m] *= gain_buffer[n];
629 /* Regular gain curves */
631 if (envelope_active()) {
632 _envelope.get_vector (internal_offset, internal_offset + to_read, gain_buffer, to_read);
634 if (_scale_amplitude != 1.0f) {
635 for (jack_nframes_t n = 0; n < to_read; ++n) {
636 mixdown_buffer[n] *= gain_buffer[n] * _scale_amplitude;
639 for (jack_nframes_t n = 0; n < to_read; ++n) {
640 mixdown_buffer[n] *= gain_buffer[n];
643 } else if (_scale_amplitude != 1.0f) {
644 Session::apply_gain_to_buffer (mixdown_buffer, to_read, _scale_amplitude);
649 /* gack. the things we do for users.
654 for (jack_nframes_t n = 0; n < to_read; ++n) {
655 buf[n] += mixdown_buffer[n];
663 AudioRegion::state (bool full)
665 XMLNode& node (Region::state (full));
669 LocaleGuard lg (X_("POSIX"));
671 snprintf (buf, sizeof (buf), "0x%x", (int) _flags);
672 node.add_property ("flags", buf);
673 snprintf (buf, sizeof(buf), "%.12g", _scale_amplitude);
674 node.add_property ("scale-gain", buf);
676 for (uint32_t n=0; n < sources.size(); ++n) {
677 snprintf (buf2, sizeof(buf2), "source-%d", n);
678 sources[n]->id().print (buf);
679 node.add_property (buf2, buf);
682 snprintf (buf, sizeof (buf), "%u", (uint32_t) sources.size());
683 node.add_property ("channels", buf);
687 child = node.add_child (X_("FadeIn"));
689 if ((_flags & DefaultFadeIn)) {
690 child->add_property (X_("default"), X_("yes"));
692 _fade_in.store_state (*child);
695 child = node.add_child (X_("FadeOut"));
697 if ((_flags & DefaultFadeOut)) {
698 child->add_property (X_("default"), X_("yes"));
700 _fade_out.store_state (*child);
704 child = node.add_child ("Envelope");
707 bool default_env = false;
709 // If there are only two points, the points are in the start of the region and the end of the region
710 // so, if they are both at 1.0f, that means the default region.
711 if (_envelope.size() == 2 &&
712 _envelope.front()->value == 1.0f &&
713 _envelope.back()->value==1.0f) {
714 if (_envelope.front()->when == 0 && _envelope.back()->when == _length) {
720 child->add_property ("default", "yes");
722 _envelope.store_state (*child);
725 child->add_property ("default", "yes");
728 if (full && _extra_xml) {
729 node.add_child_copy (*_extra_xml);
736 AudioRegion::set_state (const XMLNode& node)
738 const XMLNodeList& nlist = node.children();
739 const XMLProperty *prop;
740 LocaleGuard lg (X_("POSIX"));
742 Region::set_state (node);
744 if ((prop = node.property ("flags")) != 0) {
745 _flags = Flag (strtol (prop->value().c_str(), (char **) 0, 16));
747 _flags = Flag (_flags & ~Region::LeftOfSplit);
748 _flags = Flag (_flags & ~Region::RightOfSplit);
751 if ((prop = node.property ("scale-gain")) != 0) {
752 _scale_amplitude = atof (prop->value().c_str());
754 _scale_amplitude = 1.0;
757 /* Now find envelope description and other misc child items */
759 for (XMLNodeConstIterator niter = nlist.begin(); niter != nlist.end(); ++niter) {
766 if (child->name() == "Envelope") {
770 if ((prop = child->property ("default")) != 0) {
771 set_default_envelope ();
773 _envelope.load_state (*child);
776 _envelope.set_max_xval (_length);
777 _envelope.truncate_end (_length);
779 } else if (child->name() == "FadeIn") {
783 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
784 set_default_fade_in ();
787 _fade_in.load_state (*child);
790 } else if (child->name() == "FadeOut") {
794 if ((prop = child->property ("default")) != 0 || (prop = child->property ("steepness")) != 0) {
795 set_default_fade_out ();
797 _fade_out.load_state (*child);
806 AudioRegion::set_fade_in_shape (FadeShape shape)
808 set_fade_in (shape, (jack_nframes_t) _fade_in.back()->when);
812 AudioRegion::set_fade_out_shape (FadeShape shape)
814 set_fade_out (shape, (jack_nframes_t) _fade_out.back()->when);
818 AudioRegion::set_fade_in (FadeShape shape, jack_nframes_t len)
825 _fade_in.add (0.0, 0.0);
826 _fade_in.add (len, 1.0);
831 _fade_in.add (len * 0.389401, 0.0333333);
832 _fade_in.add (len * 0.629032, 0.0861111);
833 _fade_in.add (len * 0.829493, 0.233333);
834 _fade_in.add (len * 0.9447, 0.483333);
835 _fade_in.add (len * 0.976959, 0.697222);
836 _fade_in.add (len, 1);
841 _fade_in.add (len * 0.0207373, 0.197222);
842 _fade_in.add (len * 0.0645161, 0.525);
843 _fade_in.add (len * 0.152074, 0.802778);
844 _fade_in.add (len * 0.276498, 0.919444);
845 _fade_in.add (len * 0.481567, 0.980556);
846 _fade_in.add (len * 0.767281, 1);
847 _fade_in.add (len, 1);
852 _fade_in.add (len * 0.0737327, 0.308333);
853 _fade_in.add (len * 0.246544, 0.658333);
854 _fade_in.add (len * 0.470046, 0.886111);
855 _fade_in.add (len * 0.652074, 0.972222);
856 _fade_in.add (len * 0.771889, 0.988889);
857 _fade_in.add (len, 1);
862 _fade_in.add (len * 0.304147, 0.0694444);
863 _fade_in.add (len * 0.529954, 0.152778);
864 _fade_in.add (len * 0.725806, 0.333333);
865 _fade_in.add (len * 0.847926, 0.558333);
866 _fade_in.add (len * 0.919355, 0.730556);
867 _fade_in.add (len, 1);
872 _fade_in_shape = shape;
875 save_state (_("fade in change"));
878 send_change (FadeInChanged);
882 AudioRegion::set_fade_out (FadeShape shape, jack_nframes_t len)
889 _fade_out.add (len * 0, 1);
890 _fade_out.add (len * 0.023041, 0.697222);
891 _fade_out.add (len * 0.0553, 0.483333);
892 _fade_out.add (len * 0.170507, 0.233333);
893 _fade_out.add (len * 0.370968, 0.0861111);
894 _fade_out.add (len * 0.610599, 0.0333333);
895 _fade_out.add (len * 1, 0);
899 _fade_out.add (len * 0, 1);
900 _fade_out.add (len * 0.228111, 0.988889);
901 _fade_out.add (len * 0.347926, 0.972222);
902 _fade_out.add (len * 0.529954, 0.886111);
903 _fade_out.add (len * 0.753456, 0.658333);
904 _fade_out.add (len * 0.9262673, 0.308333);
905 _fade_out.add (len * 1, 0);
909 _fade_out.add (len * 0, 1);
910 _fade_out.add (len * 0.305556, 1);
911 _fade_out.add (len * 0.548611, 0.991736);
912 _fade_out.add (len * 0.759259, 0.931129);
913 _fade_out.add (len * 0.918981, 0.68595);
914 _fade_out.add (len * 0.976852, 0.22865);
915 _fade_out.add (len * 1, 0);
919 _fade_out.add (len * 0, 1);
920 _fade_out.add (len * 0.080645, 0.730556);
921 _fade_out.add (len * 0.277778, 0.289256);
922 _fade_out.add (len * 0.470046, 0.152778);
923 _fade_out.add (len * 0.695853, 0.0694444);
924 _fade_out.add (len * 1, 0);
928 _fade_out.add (len * 0, 1);
929 _fade_out.add (len * 1, 0);
934 _fade_out_shape = shape;
937 save_state (_("fade in change"));
940 send_change (FadeOutChanged);
944 AudioRegion::set_fade_in_length (jack_nframes_t len)
946 bool changed = _fade_in.extend_to (len);
949 _flags = Flag (_flags & ~DefaultFadeIn);
953 snprintf (buf, sizeof (buf), "fade in length changed to %u", len);
957 send_change (FadeInChanged);
962 AudioRegion::set_fade_out_length (jack_nframes_t len)
964 bool changed = _fade_out.extend_to (len);
967 _flags = Flag (_flags & ~DefaultFadeOut);
971 snprintf (buf, sizeof (buf), "fade out length changed to %u", len);
976 send_change (FadeOutChanged);
980 AudioRegion::set_fade_in_active (bool yn)
982 if (yn == (_flags & FadeIn)) {
986 _flags = Flag (_flags|FadeIn);
988 _flags = Flag (_flags & ~FadeIn);
991 send_change (FadeInActiveChanged);
995 AudioRegion::set_fade_out_active (bool yn)
997 if (yn == (_flags & FadeOut)) {
1001 _flags = Flag (_flags | FadeOut);
1003 _flags = Flag (_flags & ~FadeOut);
1006 send_change (FadeOutActiveChanged);
1010 AudioRegion::set_default_fade_in ()
1012 set_fade_in (Linear, 64);
1016 AudioRegion::set_default_fade_out ()
1018 set_fade_out (Linear, 64);
1022 AudioRegion::set_default_fades ()
1024 _fade_in_disabled = 0;
1025 _fade_out_disabled = 0;
1026 set_default_fade_in ();
1027 set_default_fade_out ();
1031 AudioRegion::set_default_envelope ()
1033 _envelope.freeze ();
1035 _envelope.add (0, 1.0f);
1036 _envelope.add (_length, 1.0f);
1041 AudioRegion::recompute_at_end ()
1043 /* our length has changed. recompute a new final point by interpolating
1044 based on the the existing curve.
1047 _envelope.freeze ();
1048 _envelope.truncate_end (_length);
1049 _envelope.set_max_xval (_length);
1052 if (_fade_in.back()->when > _length) {
1053 _fade_in.extend_to (_length);
1054 send_change (FadeInChanged);
1057 if (_fade_out.back()->when > _length) {
1058 _fade_out.extend_to (_length);
1059 send_change (FadeOutChanged);
1064 AudioRegion::recompute_at_start ()
1066 /* as above, but the shift was from the front */
1068 _envelope.truncate_start (_length);
1070 if (_fade_in.back()->when > _length) {
1071 _fade_in.extend_to (_length);
1072 send_change (FadeInChanged);
1075 if (_fade_out.back()->when > _length) {
1076 _fade_out.extend_to (_length);
1077 send_change (FadeOutChanged);
1082 AudioRegion::separate_by_channel (Session& session, vector<AudioRegion*>& v) const
1087 for (SourceList::const_iterator i = master_sources.begin(); i != master_sources.end(); ++i) {
1090 srcs.push_back (*i);
1092 /* generate a new name */
1094 if (session.region_name (new_name, _name)) {
1098 /* create a copy with just one source */
1100 v.push_back (new AudioRegion (srcs, _start, _length, new_name, _layer, _flags));
1107 AudioRegion::source_deleted (boost::shared_ptr<Source> ignored)
1113 AudioRegion::master_source_names ()
1115 SourceList::iterator i;
1117 vector<string> names;
1118 for (i = master_sources.begin(); i != master_sources.end(); ++i) {
1119 names.push_back((*i)->name());
1126 AudioRegion::source_equivalent (boost::shared_ptr<const Region> o) const
1128 boost::shared_ptr<const AudioRegion> other = boost::dynamic_pointer_cast<const AudioRegion>(o);
1133 SourceList::const_iterator i;
1134 SourceList::const_iterator io;
1136 for (i = sources.begin(), io = other->sources.begin(); i != sources.end() && io != other->sources.end(); ++i, ++io) {
1137 if ((*i)->id() != (*io)->id()) {
1142 for (i = master_sources.begin(), io = other->master_sources.begin(); i != master_sources.end() && io != other->master_sources.end(); ++i, ++io) {
1143 if ((*i)->id() != (*io)->id()) {
1152 AudioRegion::apply (AudioFilter& filter)
1154 return filter.run (boost::shared_ptr<AudioRegion> (this));
1158 AudioRegion::exportme (Session& session, AudioExportSpecification& spec)
1160 const jack_nframes_t blocksize = 4096;
1161 jack_nframes_t to_read;
1164 spec.channels = sources.size();
1166 if (spec.prepare (blocksize, session.frame_rate())) {
1171 spec.total_frames = _length;
1173 while (spec.pos < _length && !spec.stop) {
1176 /* step 1: interleave */
1178 to_read = min (_length - spec.pos, blocksize);
1180 if (spec.channels == 1) {
1182 if (sources.front()->read (spec.dataF, _start + spec.pos, to_read) != to_read) {
1188 Sample buf[blocksize];
1190 for (uint32_t chan = 0; chan < spec.channels; ++chan) {
1192 if (sources[chan]->read (buf, _start + spec.pos, to_read) != to_read) {
1196 for (jack_nframes_t x = 0; x < to_read; ++x) {
1197 spec.dataF[chan+(x*spec.channels)] = buf[x];
1202 if (spec.process (to_read)) {
1206 spec.pos += to_read;
1207 spec.progress = (double) spec.pos /_length;
1214 spec.running = false;
1215 spec.status = status;
1221 boost::shared_ptr<Region>
1222 AudioRegion::get_parent()
1224 boost::shared_ptr<Region> r;
1227 r = _playlist->session().find_whole_file_parent (boost::shared_ptr<AudioRegion>(this));
1234 AudioRegion::set_scale_amplitude (gain_t g)
1236 _scale_amplitude = g;
1238 /* tell the diskstream we're in */
1241 _playlist->Modified();
1244 /* tell everybody else */
1246 send_change (ScaleAmplitudeChanged);
1250 AudioRegion::normalize_to (float target_dB)
1252 const jack_nframes_t blocksize = 64 * 1024;
1253 Sample buf[blocksize];
1254 jack_nframes_t fpos;
1255 jack_nframes_t fend;
1256 jack_nframes_t to_read;
1258 gain_t target = dB_to_coefficient (target_dB);
1260 if (target == 1.0f) {
1261 /* do not normalize to precisely 1.0 (0 dBFS), to avoid making it appear
1262 that we may have clipped.
1264 target -= FLT_EPSILON;
1268 fend = _start + _length;
1270 /* first pass: find max amplitude */
1272 while (fpos < fend) {
1276 to_read = min (fend - fpos, blocksize);
1278 for (n = 0; n < n_channels(); ++n) {
1282 if (source (n)->read (buf, fpos, to_read) != to_read) {
1286 maxamp = Session::compute_peak (buf, to_read, maxamp);
1292 if (maxamp == 0.0f) {
1293 /* don't even try */
1297 if (maxamp == target) {
1298 /* we can't do anything useful */
1302 /* compute scale factor */
1304 _scale_amplitude = target/maxamp;
1308 snprintf (buf, sizeof (buf), _("normalized to %.2fdB"), target_dB);
1312 /* tell the diskstream we're in */
1315 _playlist->Modified();
1318 /* tell everybody else */
1320 send_change (ScaleAmplitudeChanged);
1324 AudioRegion::envelope_changed (Change ignored)
1326 save_state (_("envelope change"));
1327 send_change (EnvelopeChanged);
1331 AudioRegion::suspend_fade_in ()
1333 if (++_fade_in_disabled == 1) {
1334 set_fade_in_active (false);
1339 AudioRegion::resume_fade_in ()
1341 if (_fade_in_disabled && --_fade_in_disabled == 0) {
1342 set_fade_in_active (true);
1347 AudioRegion::suspend_fade_out ()
1349 if (++_fade_out_disabled == 1) {
1350 set_fade_out_active (false);
1355 AudioRegion::resume_fade_out ()
1357 if (_fade_out_disabled && --_fade_out_disabled == 0) {
1358 set_fade_out_active (true);
1363 AudioRegion::speed_mismatch (float sr) const
1365 if (sources.empty()) {
1366 /* impossible, but ... */
1370 float fsr = sources.front()->sample_rate();
1376 AudioRegion::source_offset_changed ()
1378 set_position (source()->natural_position() + start(), this);
1383 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)
1385 return ((AudioRegion *) arg)->read_peaks ((PeakData *) data, (jack_nframes_t) npeaks, (jack_nframes_t) start, (jack_nframes_t) cnt, n_chan,samples_per_unit);
1388 uint32_t region_length_from_c (void *arg)
1391 return ((AudioRegion *) arg)->length();
1394 uint32_t sourcefile_length_from_c (void *arg, double zoom_factor)
1396 return ( (AudioRegion *) arg)->source()->available_peaks (zoom_factor) ;