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 _active = Config->get_xfades_active ();
98 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
99 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
100 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
105 if (compute (a, b, model)) {
106 throw failed_constructor();
114 Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
115 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
116 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
118 boost::shared_ptr<Region> r;
120 LocaleGuard lg (X_("POSIX"));
122 /* we have to find the in/out regions before we can do anything else */
124 if ((prop = node.property ("in")) == 0) {
125 error << _("Crossfade: no \"in\" region in state") << endmsg;
126 throw failed_constructor();
129 PBD::ID id (prop->value());
131 if ((r = playlist.find_region (id)) == 0) {
132 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2"), id, playlist.name())
134 throw failed_constructor();
137 if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
138 throw failed_constructor();
141 if ((prop = node.property ("out")) == 0) {
142 error << _("Crossfade: no \"out\" region in state") << endmsg;
143 throw failed_constructor();
146 PBD::ID id2 (prop->value());
148 if ((r = playlist.find_region (id2)) == 0) {
149 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2"), id2, playlist.name())
151 throw failed_constructor();
154 if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
155 throw failed_constructor();
161 if (set_state (node)) {
162 throw failed_constructor();
166 Crossfade::Crossfade (const Crossfade &orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
167 : _fade_in(orig._fade_in),
168 _fade_out(orig._fade_out)
170 _active = orig._active;
171 _in_update = orig._in_update;
172 _length = orig._length;
173 _position = orig._position;
174 _anchor_point = orig._anchor_point;
175 _follow_overlap = orig._follow_overlap;
176 _fixed = orig._fixed;
181 // copied from Crossfade::initialize()
184 _out->suspend_fade_out ();
185 _in->suspend_fade_in ();
187 overlap_type = _in->coverage (_out->position(), _out->last_frame());
188 layer_relation = (int32_t) (_in->layer() - _out->layer());
190 // Let's make sure the fade isn't too long
195 Crossfade::~Crossfade ()
201 Crossfade::initialize ()
205 _out->suspend_fade_out ();
206 _in->suspend_fade_in ();
210 _fade_out.add (0.0, 1.0);
211 _fade_out.add ((_length * 0.1), 0.99);
212 _fade_out.add ((_length * 0.2), 0.97);
213 _fade_out.add ((_length * 0.8), 0.03);
214 _fade_out.add ((_length * 0.9), 0.01);
215 _fade_out.add (_length, 0.0);
220 _fade_in.add (0.0, 0.0);
221 _fade_in.add ((_length * 0.1), 0.01);
222 _fade_in.add ((_length * 0.2), 0.03);
223 _fade_in.add ((_length * 0.8), 0.97);
224 _fade_in.add ((_length * 0.9), 0.99);
225 _fade_in.add (_length, 1.0);
228 // _in->StateChanged.connect (sigc::mem_fun (*this, &Crossfade::member_changed));
229 // _out->StateChanged.connect (sigc::mem_fun (*this, &Crossfade::member_changed));
231 overlap_type = _in->coverage (_out->position(), _out->last_frame());
232 layer_relation = (int32_t) (_in->layer() - _out->layer());
236 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
237 float *gain_buffer, nframes_t start, nframes_t cnt, uint32_t chan_n,
238 nframes_t read_frames, nframes_t skip_frames)
247 if (start < _position) {
249 /* handle an initial section of the read area that we do not
253 offset = _position - start;
263 to_write = min (_length, cnt);
267 to_write = min (_length - (start - _position), cnt);
271 offset = start - _position;
273 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
274 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
276 float* fiv = new float[to_write];
277 float* fov = new float[to_write];
279 _fade_in.get_vector (offset, offset+to_write, fiv, to_write);
280 _fade_out.get_vector (offset, offset+to_write, fov, to_write);
282 /* note: although we have not explicitly taken into account the return values
283 from _out->read_at() or _in->read_at(), the length() function does this
284 implicitly. why? because it computes a value based on the in+out regions'
285 position and length, and so we know precisely how much data they could return.
288 for (nframes_t n = 0; n < to_write; ++n) {
289 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
299 Crossfade::coverage (nframes_t start, nframes_t end) const
301 nframes_t my_end = _position + _length;
303 if ((start >= _position) && (end <= my_end)) {
304 return OverlapInternal;
306 if ((end >= _position) && (end <= my_end)) {
309 if ((start >= _position) && (start <= my_end)) {
312 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
313 return OverlapExternal;
319 Crossfade::set_active (bool yn)
323 StateChanged (ActiveChanged);
328 Crossfade::refresh ()
330 /* crossfades must be between non-muted regions */
332 if (_out->muted() || _in->muted()) {
333 Invalidated (shared_from_this());
337 /* layer ordering cannot change */
339 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
341 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
342 Invalidated (shared_from_this());
346 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
348 if (ot == OverlapNone) {
349 Invalidated (shared_from_this());
355 if (ot != overlap_type) {
357 if (_follow_overlap) {
360 compute (_in, _out, Config->get_xfade_model());
363 catch (NoCrossfadeHere& err) {
364 Invalidated (shared_from_this());
372 Invalidated (shared_from_this());
378 send_signal = update ();
382 StateChanged (BoundsChanged); /* EMIT SIGNAL */
395 if (_follow_overlap) {
396 newlen = _out->first_frame() + _out->length() - _in->first_frame();
402 Invalidated (shared_from_this());
408 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
410 double factor = newlen / (double) _length;
412 _fade_out.x_scale (factor);
413 _fade_in.x_scale (factor);
418 switch (_anchor_point) {
420 _position = _in->first_frame();
424 _position = _in->last_frame() - _length;
428 _position = _out->last_frame() - _length;
435 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
437 boost::shared_ptr<AudioRegion> top;
438 boost::shared_ptr<AudioRegion> bottom;
439 nframes_t short_xfade_length;
441 short_xfade_length = _short_xfade_length;
443 if (a->layer() < b->layer()) {
451 /* first check for matching ends */
453 if (top->first_frame() == bottom->first_frame()) {
455 /* Both regions start at the same point */
457 if (top->last_frame() < bottom->last_frame()) {
459 /* top ends before bottom, so put an xfade
460 in at the end of top.
463 /* [-------- top ---------- ]
464 * {====== bottom =====================}
470 if (top->last_frame() < short_xfade_length) {
473 _position = top->last_frame() - short_xfade_length;
476 _length = min (short_xfade_length, top->length());
477 _follow_overlap = false;
478 _anchor_point = EndOfIn;
483 /* top ends after (or same time) as bottom - no xfade
486 /* [-------- top ------------------------ ]
487 * {====== bottom =====================}
490 throw NoCrossfadeHere();
493 } else if (top->last_frame() == bottom->last_frame()) {
495 /* Both regions end at the same point */
497 if (top->first_frame() > bottom->first_frame()) {
499 /* top starts after bottom, put an xfade in at the
503 /* [-------- top ---------- ]
504 * {====== bottom =====================}
509 _position = top->first_frame();
510 _length = min (short_xfade_length, top->length());
511 _follow_overlap = false;
512 _anchor_point = StartOfIn;
517 /* top starts before bottom - no xfade
520 /* [-------- top ------------------------ ]
521 * {====== bottom =====================}
524 throw NoCrossfadeHere();
529 /* OK, time to do more regular overlapping */
531 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
535 /* should be NOTREACHED as a precondition of creating
536 a new crossfade, but we need to handle it here.
538 throw NoCrossfadeHere();
541 case OverlapInternal:
542 case OverlapExternal:
543 /* should be NOTREACHED because of tests above */
544 throw NoCrossfadeHere();
547 case OverlapEnd: /* top covers start of bottom but ends within it */
549 /* [---- top ------------------------]
550 * { ==== bottom ============ }
555 _anchor_point = StartOfIn;
557 if (model == FullCrossfade) {
558 _position = bottom->first_frame(); // "{"
559 _length = _out->first_frame() + _out->length() - _in->first_frame();
560 /* leave active alone */
561 _follow_overlap = true;
563 _length = min (short_xfade_length, top->length());
564 _position = top->last_frame() - _length; // "]" - length
566 _follow_overlap = false;
571 case OverlapStart: /* top starts within bottom but covers bottom's end */
573 /* { ==== top ============ }
574 * [---- bottom -------------------]
579 _position = top->first_frame();
580 _anchor_point = StartOfIn;
582 if (model == FullCrossfade) {
583 _length = _out->first_frame() + _out->length() - _in->first_frame();
584 /* leave active alone */
585 _follow_overlap = true;
587 _length = min (short_xfade_length, top->length());
589 _follow_overlap = false;
601 Crossfade::member_changed (Change what_changed)
603 Change what_we_care_about = Change (Region::MuteChanged|
604 Region::LayerChanged|
607 if (what_changed & what_we_care_about) {
609 if (what_changed & what_we_care_about) {
614 catch (NoCrossfadeHere& err) {
615 // relax, Invalidated inside refresh()
621 Crossfade::get_state ()
623 XMLNode* node = new XMLNode (X_("Crossfade"));
626 LocaleGuard lg (X_("POSIX"));
628 _out->id().print (buf, sizeof (buf));
629 node->add_property ("out", buf);
630 _in->id().print (buf, sizeof (buf));
631 node->add_property ("in", buf);
632 node->add_property ("active", (_active ? "yes" : "no"));
633 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
634 node->add_property ("fixed", (_fixed ? "yes" : "no"));
635 snprintf (buf, sizeof(buf), "%" PRIu32, _length);
636 node->add_property ("length", buf);
637 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
638 node->add_property ("anchor-point", buf);
639 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
640 node->add_property ("position", buf);
642 child = node->add_child ("FadeIn");
644 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
647 pnode = new XMLNode ("point");
649 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
650 pnode->add_property ("x", buf);
651 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
652 pnode->add_property ("y", buf);
653 child->add_child_nocopy (*pnode);
656 child = node->add_child ("FadeOut");
658 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
661 pnode = new XMLNode ("point");
663 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
664 pnode->add_property ("x", buf);
665 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
666 pnode->add_property ("y", buf);
667 child->add_child_nocopy (*pnode);
674 Crossfade::set_state (const XMLNode& node)
676 XMLNodeConstIterator i;
677 XMLNodeList children;
680 const XMLProperty* prop;
681 LocaleGuard lg (X_("POSIX"));
682 Change what_changed = Change (0);
685 if ((prop = node.property ("position")) != 0) {
686 sscanf (prop->value().c_str(), "%" PRIu32, &val);
687 if (val != _position) {
689 what_changed = Change (what_changed | PositionChanged);
692 warning << _("old-style crossfade information - no position information") << endmsg;
693 _position = _in->first_frame();
696 if ((prop = node.property ("active")) != 0) {
697 bool x = (prop->value() == "yes");
700 what_changed = Change (what_changed | ActiveChanged);
706 if ((prop = node.property ("follow-overlap")) != 0) {
707 _follow_overlap = (prop->value() == "yes");
709 _follow_overlap = false;
712 if ((prop = node.property ("fixed")) != 0) {
713 _fixed = (prop->value() == "yes");
718 if ((prop = node.property ("anchor-point")) != 0) {
719 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
721 _anchor_point = StartOfIn;
724 if ((prop = node.property ("length")) != 0) {
726 sscanf (prop->value().c_str(), "%" PRIu32, &val);
727 if (val != _length) {
728 _length = atol (prop->value().c_str());
729 what_changed = Change (what_changed | LengthChanged);
734 /* XXX this branch is legacy code from before
735 the point where we stored xfade lengths.
738 if ((_length = overlap_length()) == 0) {
739 throw failed_constructor();
743 if ((fi = find_named_node (node, "FadeIn")) == 0) {
747 if ((fo = find_named_node (node, "FadeOut")) == 0) {
756 children = fi->children();
758 for (i = children.begin(); i != children.end(); ++i) {
759 if ((*i)->name() == "point") {
763 prop = (*i)->property ("x");
764 sscanf (prop->value().c_str(), "%" PRIu32, &x);
766 prop = (*i)->property ("y");
767 sscanf (prop->value().c_str(), "%f", &y);
780 children = fo->children();
782 for (i = children.begin(); i != children.end(); ++i) {
783 if ((*i)->name() == "point") {
788 prop = (*i)->property ("x");
789 sscanf (prop->value().c_str(), "%" PRIu32, &x);
791 prop = (*i)->property ("y");
792 sscanf (prop->value().c_str(), "%f", &y);
794 _fade_out.add (x, y);
800 StateChanged (what_changed); /* EMIT SIGNAL */
806 Crossfade::can_follow_overlap () const
812 Crossfade::set_follow_overlap (bool yn)
814 if (yn == _follow_overlap || _fixed) {
818 _follow_overlap = yn;
821 set_length (_short_xfade_length);
823 set_length (_out->first_frame() + _out->length() - _in->first_frame());
826 StateChanged (FollowOverlapChanged);
830 Crossfade::set_length (nframes_t len)
834 switch (_anchor_point) {
836 limit = _in->length();
840 limit = _in->length();
844 limit = _out->length();
849 len = min (limit, len);
851 double factor = len / (double) _length;
854 _fade_out.x_scale (factor);
855 _fade_in.x_scale (factor);
860 StateChanged (LengthChanged);
866 Crossfade::overlap_length () const
871 return _out->first_frame() + _out->length() - _in->first_frame();
875 Crossfade::set_short_xfade_length (nframes_t n)
877 _short_xfade_length = n;
881 Crossfade::invalidate ()
883 Invalidated (shared_from_this()); /* EMIT SIGNAL */