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.
20 #include <sigc++/bind.h>
22 #include <pbd/stacktrace.h>
24 #include <ardour/types.h>
25 #include <ardour/crossfade.h>
26 #include <ardour/crossfade_compare.h>
27 #include <ardour/audioregion.h>
28 #include <ardour/playlist.h>
29 #include <ardour/utils.h>
30 #include <ardour/session.h>
31 #include <ardour/source.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 : AudioRegion (in->session(), position, length, "foobar"),
81 _fade_in (Parameter(FadeInAutomation), 0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
82 _fade_out (Parameter(FadeOutAutomation), 0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
89 _follow_overlap = false;
91 _active = Config->get_xfades_active ();
97 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
98 : AudioRegion (a->session(), 0, 0, "foobar"),
99 _fade_in (Parameter(FadeInAutomation), 0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
100 _fade_out (Parameter(FadeOutAutomation), 0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
105 if (compute (a, b, model)) {
106 throw failed_constructor();
116 Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
117 : AudioRegion (playlist.session(), 0, 0, "foobar"),
118 _fade_in (Parameter(FadeInAutomation), 0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
119 _fade_out (Parameter(FadeOutAutomation), 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();
165 if (set_state (node)) {
166 throw failed_constructor();
170 Crossfade::Crossfade (boost::shared_ptr<Crossfade> orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
171 : AudioRegion (boost::dynamic_pointer_cast<const AudioRegion> (orig)),
172 _fade_in (orig->_fade_in),
173 _fade_out (orig->_fade_out)
175 _active = orig->_active;
176 _in_update = orig->_in_update;
177 _anchor_point = orig->_anchor_point;
178 _follow_overlap = orig->_follow_overlap;
179 _fixed = orig->_fixed;
184 // copied from Crossfade::initialize()
187 _out->suspend_fade_out ();
188 _in->suspend_fade_in ();
190 overlap_type = _in->coverage (_out->position(), _out->last_frame());
191 layer_relation = (int32_t) (_in->layer() - _out->layer());
193 // Let's make sure the fade isn't too long
198 Crossfade::~Crossfade ()
204 Crossfade::initialize ()
206 /* merge source lists from regions */
208 _sources = _in->sources();
209 _sources.insert (_sources.end(), _out->sources().begin(), _out->sources().end());
210 _master_sources = _in->master_sources();
211 _master_sources.insert(_master_sources.end(), _out->master_sources().begin(), _out->master_sources().end());
215 _out->suspend_fade_out ();
216 _in->suspend_fade_in ();
220 _fade_out.add (0.0, 1.0);
221 _fade_out.add ((_length * 0.1), 0.99);
222 _fade_out.add ((_length * 0.2), 0.97);
223 _fade_out.add ((_length * 0.8), 0.03);
224 _fade_out.add ((_length * 0.9), 0.01);
225 _fade_out.add (_length, 0.0);
230 _fade_in.add (0.0, 0.0);
231 _fade_in.add ((_length * 0.1), 0.01);
232 _fade_in.add ((_length * 0.2), 0.03);
233 _fade_in.add ((_length * 0.8), 0.97);
234 _fade_in.add ((_length * 0.9), 0.99);
235 _fade_in.add (_length, 1.0);
238 overlap_type = _in->coverage (_out->position(), _out->last_frame());
239 layer_relation = (int32_t) (_in->layer() - _out->layer());
243 Crossfade::read_raw_internal (Sample* buf, nframes_t start, nframes_t cnt) const
246 Sample* mixdown = new Sample[cnt];
247 float* gain = new float[cnt];
250 ret = read_at (buf, mixdown, gain, start, cnt, chan_n, cnt);
261 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
262 float *gain_buffer, nframes_t start, nframes_t cnt, uint32_t chan_n) const
271 if (start < _position) {
273 /* handle an initial section of the read area that we do not
277 offset = _position - start;
287 to_write = min (_length, cnt);
291 to_write = min (_length - (start - _position), cnt);
295 offset = start - _position;
297 /* Prevent data from piling up inthe crossfade buffers when reading a transparent region */
298 if (!(_out->opaque())) {
299 memset (crossfade_buffer_out, 0, sizeof (Sample) * to_write);
300 } else if (!(_in->opaque())) {
301 memset (crossfade_buffer_in, 0, sizeof (Sample) * to_write);
304 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n);
305 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n);
307 float* fiv = new float[to_write];
308 float* fov = new float[to_write];
310 _fade_in.curve().get_vector (offset, offset+to_write, fiv, to_write);
311 _fade_out.curve().get_vector (offset, offset+to_write, fov, to_write);
313 /* note: although we have not explicitly taken into account the return values
314 from _out->read_at() or _in->read_at(), the length() function does this
315 implicitly. why? because it computes a value based on the in+out regions'
316 position and length, and so we know precisely how much data they could return.
319 for (nframes_t n = 0; n < to_write; ++n) {
320 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
330 Crossfade::coverage (nframes_t start, nframes_t end) const
332 nframes_t my_end = _position + _length;
334 if ((start >= _position) && (end <= my_end)) {
335 return OverlapInternal;
337 if ((end >= _position) && (end <= my_end)) {
340 if ((start >= _position) && (start <= my_end)) {
343 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
344 return OverlapExternal;
350 Crossfade::set_active (bool yn)
354 StateChanged (ActiveChanged);
359 Crossfade::refresh ()
361 /* crossfades must be between non-muted regions */
363 if (_out->muted() || _in->muted()) {
364 Invalidated (shared_from_this ());
368 /* Top layer shouldn't be transparent */
370 if (!((layer_relation > 0 ? _in : _out)->opaque())) {
371 Invalidated (shared_from_this());
375 /* layer ordering cannot change */
377 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
379 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
380 Invalidated (shared_from_this ());
384 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
386 if (ot == OverlapNone) {
387 Invalidated (shared_from_this ());
393 if (ot != overlap_type) {
395 if (_follow_overlap) {
398 compute (_in, _out, Config->get_xfade_model());
401 catch (NoCrossfadeHere& err) {
402 Invalidated (shared_from_this ());
410 Invalidated (shared_from_this ());
416 send_signal = update ();
420 StateChanged (BoundsChanged); /* EMIT SIGNAL */
433 if (_follow_overlap) {
434 newlen = _out->first_frame() + _out->length() - _in->first_frame();
440 Invalidated (shared_from_this ());
446 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
448 double factor = newlen / (double) _length;
450 _fade_out.x_scale (factor);
451 _fade_in.x_scale (factor);
456 switch (_anchor_point) {
458 _position = _in->first_frame();
462 _position = _in->last_frame() - _length;
466 _position = _out->last_frame() - _length;
473 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
475 boost::shared_ptr<AudioRegion> top;
476 boost::shared_ptr<AudioRegion> bottom;
477 nframes_t short_xfade_length;
479 short_xfade_length = _short_xfade_length;
481 if (a->layer() < b->layer()) {
489 /* first check for matching ends */
491 if (top->first_frame() == bottom->first_frame()) {
493 /* Both regions start at the same point */
495 if (top->last_frame() < bottom->last_frame()) {
497 /* top ends before bottom, so put an xfade
498 in at the end of top.
501 /* [-------- top ---------- ]
502 * {====== bottom =====================}
508 if (top->last_frame() < short_xfade_length) {
511 _position = top->last_frame() - short_xfade_length;
514 _length = min (short_xfade_length, top->length());
515 _follow_overlap = false;
516 _anchor_point = EndOfIn;
521 /* top ends after (or same time) as bottom - no xfade
524 /* [-------- top ------------------------ ]
525 * {====== bottom =====================}
528 throw NoCrossfadeHere();
531 } else if (top->last_frame() == bottom->last_frame()) {
533 /* Both regions end at the same point */
535 if (top->first_frame() > bottom->first_frame()) {
537 /* top starts after bottom, put an xfade in at the
541 /* [-------- top ---------- ]
542 * {====== bottom =====================}
547 _position = top->first_frame();
548 _length = min (short_xfade_length, top->length());
549 _follow_overlap = false;
550 _anchor_point = StartOfIn;
555 /* top starts before bottom - no xfade
558 /* [-------- top ------------------------ ]
559 * {====== bottom =====================}
562 throw NoCrossfadeHere();
567 /* OK, time to do more regular overlapping */
569 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
573 /* should be NOTREACHED as a precondition of creating
574 a new crossfade, but we need to handle it here.
576 throw NoCrossfadeHere();
579 case OverlapInternal:
580 case OverlapExternal:
581 /* should be NOTREACHED because of tests above */
582 throw NoCrossfadeHere();
585 case OverlapEnd: /* top covers start of bottom but ends within it */
587 /* [---- top ------------------------]
588 * { ==== bottom ============ }
593 _anchor_point = EndOfOut;
595 if (model == FullCrossfade) {
596 _position = bottom->first_frame(); // "{"
597 _length = _out->first_frame() + _out->length() - _in->first_frame();
598 /* leave active alone */
599 _follow_overlap = true;
601 _length = min (short_xfade_length, top->length());
602 _position = top->last_frame() - _length; // "]" - length
604 _follow_overlap = false;
609 case OverlapStart: /* top starts within bottom but covers bottom's end */
611 /* { ==== top ============ }
612 * [---- bottom -------------------]
617 _position = top->first_frame();
618 _anchor_point = StartOfIn;
620 if (model == FullCrossfade) {
621 _length = _out->first_frame() + _out->length() - _in->first_frame();
622 /* leave active alone */
623 _follow_overlap = true;
625 _length = min (short_xfade_length, top->length());
627 _follow_overlap = false;
639 Crossfade::get_state ()
641 XMLNode* node = new XMLNode (X_("Crossfade"));
644 LocaleGuard lg (X_("POSIX"));
646 _out->id().print (buf, sizeof (buf));
647 node->add_property ("out", buf);
648 _in->id().print (buf, sizeof (buf));
649 node->add_property ("in", buf);
650 node->add_property ("active", (_active ? "yes" : "no"));
651 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
652 node->add_property ("fixed", (_fixed ? "yes" : "no"));
653 snprintf (buf, sizeof(buf), "%" PRIu32, _length);
654 node->add_property ("length", buf);
655 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
656 node->add_property ("anchor-point", buf);
657 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
658 node->add_property ("position", buf);
660 child = node->add_child ("FadeIn");
662 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
665 pnode = new XMLNode ("point");
667 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
668 pnode->add_property ("x", buf);
669 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
670 pnode->add_property ("y", buf);
671 child->add_child_nocopy (*pnode);
674 child = node->add_child ("FadeOut");
676 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
679 pnode = new XMLNode ("point");
681 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
682 pnode->add_property ("x", buf);
683 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
684 pnode->add_property ("y", buf);
685 child->add_child_nocopy (*pnode);
692 Crossfade::set_state (const XMLNode& node)
694 XMLNodeConstIterator i;
695 XMLNodeList children;
698 const XMLProperty* prop;
699 LocaleGuard lg (X_("POSIX"));
700 Change what_changed = Change (0);
703 if ((prop = node.property ("position")) != 0) {
704 sscanf (prop->value().c_str(), "%" PRIu32, &val);
705 if (val != _position) {
707 what_changed = Change (what_changed | PositionChanged);
710 warning << _("old-style crossfade information - no position information") << endmsg;
711 _position = _in->first_frame();
714 if ((prop = node.property ("active")) != 0) {
715 bool x = (prop->value() == "yes");
718 what_changed = Change (what_changed | ActiveChanged);
724 if ((prop = node.property ("follow-overlap")) != 0) {
725 _follow_overlap = (prop->value() == "yes");
727 _follow_overlap = false;
730 if ((prop = node.property ("fixed")) != 0) {
731 _fixed = (prop->value() == "yes");
736 if ((prop = node.property ("anchor-point")) != 0) {
737 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
739 _anchor_point = StartOfIn;
742 if ((prop = node.property ("length")) != 0) {
744 sscanf (prop->value().c_str(), "%" PRIu32, &val);
745 if (val != _length) {
746 _length = atol (prop->value().c_str());
747 what_changed = Change (what_changed | LengthChanged);
752 /* XXX this branch is legacy code from before
753 the point where we stored xfade lengths.
756 if ((_length = overlap_length()) == 0) {
757 throw failed_constructor();
761 if ((fi = find_named_node (node, "FadeIn")) == 0) {
765 if ((fo = find_named_node (node, "FadeOut")) == 0) {
774 children = fi->children();
776 for (i = children.begin(); i != children.end(); ++i) {
777 if ((*i)->name() == "point") {
781 prop = (*i)->property ("x");
782 sscanf (prop->value().c_str(), "%" PRIu32, &x);
784 prop = (*i)->property ("y");
785 sscanf (prop->value().c_str(), "%f", &y);
798 children = fo->children();
800 for (i = children.begin(); i != children.end(); ++i) {
801 if ((*i)->name() == "point") {
806 prop = (*i)->property ("x");
807 sscanf (prop->value().c_str(), "%" PRIu32, &x);
809 prop = (*i)->property ("y");
810 sscanf (prop->value().c_str(), "%f", &y);
812 _fade_out.add (x, y);
818 StateChanged (what_changed); /* EMIT SIGNAL */
824 Crossfade::can_follow_overlap () const
830 Crossfade::set_follow_overlap (bool yn)
832 if (yn == _follow_overlap || _fixed) {
836 _follow_overlap = yn;
839 set_length (_short_xfade_length);
841 set_length (_out->first_frame() + _out->length() - _in->first_frame());
844 StateChanged (FollowOverlapChanged);
848 Crossfade::set_length (nframes_t len)
852 switch (_anchor_point) {
854 limit = _in->length();
858 limit = _in->length();
862 limit = _out->length();
867 len = min (limit, len);
869 double factor = len / (double) _length;
872 _fade_out.x_scale (factor);
873 _fade_in.x_scale (factor);
878 StateChanged (LengthChanged);
884 Crossfade::overlap_length () const
889 return _out->first_frame() + _out->length() - _in->first_frame();
893 Crossfade::set_short_xfade_length (nframes_t n)
895 _short_xfade_length = n;
899 Crossfade::invalidate ()
901 Invalidated (shared_from_this ()); /* EMIT SIGNAL */