2 Copyright (C) 2003-2006 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <sigc++/bind.h>
23 #include <pbd/stacktrace.h>
25 #include <ardour/types.h>
26 #include <ardour/crossfade.h>
27 #include <ardour/crossfade_compare.h>
28 #include <ardour/audioregion.h>
29 #include <ardour/playlist.h>
30 #include <ardour/utils.h>
31 #include <ardour/session.h>
37 using namespace ARDOUR;
40 nframes_t Crossfade::_short_xfade_length = 0;
41 Change Crossfade::ActiveChanged = new_change();
42 Change Crossfade::FollowOverlapChanged = new_change();
44 /* XXX if and when we ever implement parallel processing of the process()
45 callback, these will need to be handled on a per-thread basis.
48 Sample* Crossfade::crossfade_buffer_out = 0;
49 Sample* Crossfade::crossfade_buffer_in = 0;
52 Crossfade::set_buffer_size (nframes_t sz)
54 if (crossfade_buffer_out) {
55 delete [] crossfade_buffer_out;
56 crossfade_buffer_out = 0;
59 if (crossfade_buffer_in) {
60 delete [] crossfade_buffer_in;
61 crossfade_buffer_in = 0;
65 crossfade_buffer_out = new Sample[sz];
66 crossfade_buffer_in = new Sample[sz];
71 Crossfade::operator== (const Crossfade& other)
73 return (_in == other._in) && (_out == other._out);
76 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out,
80 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
81 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
90 _follow_overlap = false;
92 cerr << "A: follow overlap = " << _follow_overlap << endl;
94 _active = Config->get_xfades_active ();
100 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
101 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
102 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
107 if (compute (a, b, model)) {
108 throw failed_constructor();
115 cerr << "K: follow overlap = " << _follow_overlap << endl;
118 Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
119 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
120 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
122 boost::shared_ptr<Region> r;
124 LocaleGuard lg (X_("POSIX"));
126 /* we have to find the in/out regions before we can do anything else */
128 if ((prop = node.property ("in")) == 0) {
129 error << _("Crossfade: no \"in\" region in state") << endmsg;
130 throw failed_constructor();
133 PBD::ID id (prop->value());
135 if ((r = playlist.find_region (id)) == 0) {
136 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2"), id, playlist.name())
138 throw failed_constructor();
141 if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
142 throw failed_constructor();
145 if ((prop = node.property ("out")) == 0) {
146 error << _("Crossfade: no \"out\" region in state") << endmsg;
147 throw failed_constructor();
150 PBD::ID id2 (prop->value());
152 if ((r = playlist.find_region (id2)) == 0) {
153 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2"), id2, playlist.name())
155 throw failed_constructor();
158 if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
159 throw failed_constructor();
163 _active = Config->get_xfades_active();
167 if (set_state (node)) {
168 throw failed_constructor();
171 cerr << "D: follow overlap = " << _follow_overlap << endl;
175 Crossfade::Crossfade (const Crossfade &orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
176 : _fade_in(orig._fade_in),
177 _fade_out(orig._fade_out)
179 _active = orig._active;
180 _in_update = orig._in_update;
181 _length = orig._length;
182 _position = orig._position;
183 _anchor_point = orig._anchor_point;
184 _follow_overlap = orig._follow_overlap;
185 _fixed = orig._fixed;
190 // copied from Crossfade::initialize()
193 _out->suspend_fade_out ();
194 _in->suspend_fade_in ();
196 overlap_type = _in->coverage (_out->position(), _out->last_frame());
198 // Let's make sure the fade isn't too long
201 cerr << "B: follow overlap = " << _follow_overlap << endl;
206 Crossfade::~Crossfade ()
208 cerr << this << " Crossfade deleted\n";
213 Crossfade::initialize ()
217 _out->suspend_fade_out ();
218 _in->suspend_fade_in ();
222 _fade_out.add (0.0, 1.0);
223 _fade_out.add ((_length * 0.1), 0.99);
224 _fade_out.add ((_length * 0.2), 0.97);
225 _fade_out.add ((_length * 0.8), 0.03);
226 _fade_out.add ((_length * 0.9), 0.01);
227 _fade_out.add (_length, 0.0);
232 _fade_in.add (0.0, 0.0);
233 _fade_in.add ((_length * 0.1), 0.01);
234 _fade_in.add ((_length * 0.2), 0.03);
235 _fade_in.add ((_length * 0.8), 0.97);
236 _fade_in.add ((_length * 0.9), 0.99);
237 _fade_in.add (_length, 1.0);
240 _in->StateChanged.connect (sigc::mem_fun (*this, &Crossfade::member_changed));
241 _out->StateChanged.connect (sigc::mem_fun (*this, &Crossfade::member_changed));
243 overlap_type = _in->coverage (_out->position(), _out->last_frame());
247 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
249 boost::shared_ptr<AudioRegion> top;
250 boost::shared_ptr<AudioRegion> bottom;
251 nframes_t short_xfade_length;
253 short_xfade_length = _short_xfade_length;
255 if (a->layer() < b->layer()) {
263 /* first check for matching ends */
265 if (top->first_frame() == bottom->first_frame()) {
267 /* Both regions start at the same point */
269 if (top->last_frame() < bottom->last_frame()) {
271 /* top ends before bottom, so put an xfade
272 in at the end of top.
275 /* [-------- top ---------- ]
276 * {====== bottom =====================}
282 if (top->last_frame() < short_xfade_length) {
285 _position = top->last_frame() - short_xfade_length;
288 _length = min (short_xfade_length, top->length());
289 _follow_overlap = false;
290 _anchor_point = EndOfIn;
295 /* top ends after (or same time) as bottom - no xfade
298 /* [-------- top ------------------------ ]
299 * {====== bottom =====================}
302 throw NoCrossfadeHere();
305 } else if (top->last_frame() == bottom->last_frame()) {
307 /* Both regions end at the same point */
309 if (top->first_frame() > bottom->first_frame()) {
311 /* top starts after bottom, put an xfade in at the
315 /* [-------- top ---------- ]
316 * {====== bottom =====================}
321 _position = top->first_frame();
322 _length = min (short_xfade_length, top->length());
323 _follow_overlap = false;
324 _anchor_point = StartOfIn;
329 /* top starts before bottom - no xfade
332 /* [-------- top ------------------------ ]
333 * {====== bottom =====================}
336 throw NoCrossfadeHere();
341 /* OK, time to do more regular overlapping */
343 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
347 /* should be NOTREACHED as a precondition of creating
348 a new crossfade, but we need to handle it here.
350 throw NoCrossfadeHere();
353 case OverlapInternal:
354 case OverlapExternal:
355 /* should be NOTREACHED because of tests above */
356 throw NoCrossfadeHere();
359 case OverlapEnd: /* top covers start of bottom but ends within it */
361 /* [---- top ------------------------]
362 * { ==== bottom ============ }
367 _anchor_point = StartOfIn;
369 if (model == FullCrossfade) {
370 _position = bottom->first_frame(); // "{"
371 _length = _out->first_frame() + _out->length() - _in->first_frame();
372 /* leave active alone */
373 _follow_overlap = true;
375 _length = min (short_xfade_length, top->length());
376 _position = top->last_frame() - _length; // "]" - length
378 _follow_overlap = false;
383 case OverlapStart: /* top starts within bottom but covers bottom's end */
385 /* { ==== top ============ }
386 * [---- bottom -------------------]
391 _position = top->first_frame();
392 _anchor_point = StartOfIn;
394 if (model == FullCrossfade) {
395 _length = _out->first_frame() + _out->length() - _in->first_frame();
396 /* leave active alone */
397 _follow_overlap = true;
399 _length = min (short_xfade_length, top->length());
401 _follow_overlap = false;
413 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
414 float *gain_buffer, nframes_t start, nframes_t cnt, uint32_t chan_n,
415 nframes_t read_frames, nframes_t skip_frames)
424 if (start < _position) {
426 /* handle an initial section of the read area that we do not
430 offset = _position - start;
440 to_write = min (_length, cnt);
444 to_write = min (_length - (start - _position), cnt);
448 offset = start - _position;
450 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
451 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
453 float* fiv = new float[to_write];
454 float* fov = new float[to_write];
456 _fade_in.get_vector (offset, offset+to_write, fiv, to_write);
457 _fade_out.get_vector (offset, offset+to_write, fov, to_write);
459 /* note: although we have not explicitly taken into account the return values
460 from _out->read_at() or _in->read_at(), the length() function does this
461 implicitly. why? because it computes a value based on the in+out regions'
462 position and length, and so we know precisely how much data they could return.
465 for (nframes_t n = 0; n < to_write; ++n) {
466 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
476 Crossfade::coverage (nframes_t start, nframes_t end) const
478 nframes_t my_end = _position + _length;
480 if ((start >= _position) && (end <= my_end)) {
481 return OverlapInternal;
483 if ((end >= _position) && (end <= my_end)) {
486 if ((start >= _position) && (start <= my_end)) {
489 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
490 return OverlapExternal;
496 Crossfade::set_active (bool yn)
500 StateChanged (ActiveChanged);
505 Crossfade::refresh ()
507 /* crossfades must be between non-muted regions */
509 if (_out->muted() || _in->muted()) {
510 Invalidated (shared_from_this());
514 // if (_in->layer() < _out->layer()) {
515 // cerr << this << " layer change, invalidated, in on " << _in->layer() << " out on " << _out->layer() << endl;
516 // Invalidated (shared_from_this());
520 /* overlap type must be Start, End or External */
524 ot = _in->coverage (_out->first_frame(), _out->last_frame());
526 /* overlap type must not have altered */
528 if (ot != overlap_type) {
529 cerr << this << " Invalid B\n";
530 Invalidated (shared_from_this());
536 return update (false);
540 Crossfade::update (bool force)
544 cerr << this << " update, " << _in->name() << " + " << _out->name() << " length = " << _length << endl;
546 if (_follow_overlap) {
547 newlen = _out->first_frame() + _out->length() - _in->first_frame();
548 cerr << "\tmodify length\n";
554 cerr << this << " Invalid C\n";
555 Invalidated (shared_from_this());
561 if (force || (_follow_overlap && newlen != _length) || (_length > newlen)) {
563 double factor = newlen / (double) _length;
565 _fade_out.x_scale (factor);
566 _fade_in.x_scale (factor);
572 cerr << "\tFover = " << _follow_overlap << endl;
574 switch (_anchor_point) {
576 _position = _in->first_frame();
580 _position = _in->last_frame() - _length;
584 _position = _out->last_frame() - _length;
587 /* UI's may need to know that the overlap changed even
588 though the xfade length did not.
591 StateChanged (BoundsChanged); /* EMIT SIGNAL */
599 Crossfade::member_changed (Change what_changed)
601 Change what_we_care_about = Change (Region::MuteChanged|
602 Region::LayerChanged|
605 if (what_changed & what_we_care_about) {
611 Crossfade::get_state ()
613 XMLNode* node = new XMLNode (X_("Crossfade"));
616 LocaleGuard lg (X_("POSIX"));
618 _out->id().print (buf, sizeof (buf));
619 node->add_property ("out", buf);
620 _in->id().print (buf, sizeof (buf));
621 node->add_property ("in", buf);
622 node->add_property ("active", (_active ? "yes" : "no"));
623 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
624 node->add_property ("fixed", (_fixed ? "yes" : "no"));
625 snprintf (buf, sizeof(buf), "%" PRIu32, _length);
626 node->add_property ("length", buf);
627 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
628 node->add_property ("anchor-point", buf);
629 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
630 node->add_property ("position", buf);
632 child = node->add_child ("FadeIn");
634 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
637 pnode = new XMLNode ("point");
639 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
640 pnode->add_property ("x", buf);
641 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
642 pnode->add_property ("y", buf);
643 child->add_child_nocopy (*pnode);
646 child = node->add_child ("FadeOut");
648 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
651 pnode = new XMLNode ("point");
653 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
654 pnode->add_property ("x", buf);
655 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
656 pnode->add_property ("y", buf);
657 child->add_child_nocopy (*pnode);
664 Crossfade::set_state (const XMLNode& node)
666 XMLNodeConstIterator i;
667 XMLNodeList children;
670 const XMLProperty* prop;
671 LocaleGuard lg (X_("POSIX"));
672 Change what_changed = Change (0);
675 if ((prop = node.property ("position")) != 0) {
676 sscanf (prop->value().c_str(), "%" PRIu32, &val);
677 if (val != _position) {
679 what_changed = Change (what_changed | PositionChanged);
682 warning << _("old-style crossfade information - no position information") << endmsg;
683 _position = _in->first_frame();
686 if ((prop = node.property ("active")) != 0) {
687 bool x = (prop->value() == "yes");
690 what_changed = Change (what_changed | ActiveChanged);
696 if ((prop = node.property ("follow-overlap")) != 0) {
697 _follow_overlap = (prop->value() == "yes");
699 _follow_overlap = false;
702 if ((prop = node.property ("fixed")) != 0) {
703 _fixed = (prop->value() == "yes");
708 if ((prop = node.property ("anchor-point")) != 0) {
709 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
711 _anchor_point = StartOfIn;
714 if ((prop = node.property ("length")) != 0) {
716 sscanf (prop->value().c_str(), "%" PRIu32, &val);
717 if (val != _length) {
718 _length = atol (prop->value().c_str());
719 what_changed = Change (what_changed | LengthChanged);
724 /* XXX this branch is legacy code from before
725 the point where we stored xfade lengths.
728 if ((_length = overlap_length()) == 0) {
729 throw failed_constructor();
733 if ((fi = find_named_node (node, "FadeIn")) == 0) {
737 if ((fo = find_named_node (node, "FadeOut")) == 0) {
746 children = fi->children();
748 for (i = children.begin(); i != children.end(); ++i) {
749 if ((*i)->name() == "point") {
753 prop = (*i)->property ("x");
754 sscanf (prop->value().c_str(), "%" PRIu32, &x);
756 prop = (*i)->property ("y");
757 sscanf (prop->value().c_str(), "%f", &y);
770 children = fo->children();
772 for (i = children.begin(); i != children.end(); ++i) {
773 if ((*i)->name() == "point") {
778 prop = (*i)->property ("x");
779 sscanf (prop->value().c_str(), "%" PRIu32, &x);
781 prop = (*i)->property ("y");
782 sscanf (prop->value().c_str(), "%f", &y);
784 _fade_out.add (x, y);
790 StateChanged (what_changed); /* EMIT SIGNAL */
796 Crossfade::can_follow_overlap () const
802 Crossfade::set_follow_overlap (bool yn)
804 if (yn == _follow_overlap || _fixed) {
808 _follow_overlap = yn;
811 set_length (_short_xfade_length);
813 set_length (_out->first_frame() + _out->length() - _in->first_frame());
816 StateChanged (FollowOverlapChanged);
820 Crossfade::set_length (nframes_t len)
824 switch (_anchor_point) {
826 limit = _in->length();
830 limit = _in->length();
834 limit = _out->length();
839 len = min (limit, len);
841 double factor = len / (double) _length;
844 _fade_out.x_scale (factor);
845 _fade_in.x_scale (factor);
850 StateChanged (LengthChanged);
856 Crossfade::overlap_length () const
861 return _out->first_frame() + _out->length() - _in->first_frame();
865 Crossfade::set_short_xfade_length (nframes_t n)
867 _short_xfade_length = n;
871 Crossfade::invalidate ()
873 Invalidated (shared_from_this()); /* EMIT SIGNAL */