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();
166 if (set_state (node)) {
167 throw failed_constructor();
171 Crossfade::Crossfade (boost::shared_ptr<Crossfade> orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
172 : AudioRegion (boost::dynamic_pointer_cast<const AudioRegion> (orig)),
173 _fade_in (orig->_fade_in),
174 _fade_out (orig->_fade_out)
176 _active = orig->_active;
177 _in_update = orig->_in_update;
178 _anchor_point = orig->_anchor_point;
179 _follow_overlap = orig->_follow_overlap;
180 _fixed = orig->_fixed;
185 // copied from Crossfade::initialize()
188 _out->suspend_fade_out ();
189 _in->suspend_fade_in ();
191 overlap_type = _in->coverage (_out->position(), _out->last_frame());
192 layer_relation = (int32_t) (_in->layer() - _out->layer());
194 // Let's make sure the fade isn't too long
199 Crossfade::~Crossfade ()
205 Crossfade::initialize ()
207 /* merge source lists from regions */
209 _sources = _in->sources();
210 _sources.insert (_sources.end(), _out->sources().begin(), _out->sources().end());
211 _master_sources = _in->master_sources();
212 _master_sources.insert(_master_sources.end(), _out->master_sources().begin(), _out->master_sources().end());
216 _out->suspend_fade_out ();
217 _in->suspend_fade_in ();
221 _fade_out.add (0.0, 1.0);
222 _fade_out.add ((_length * 0.1), 0.99);
223 _fade_out.add ((_length * 0.2), 0.97);
224 _fade_out.add ((_length * 0.8), 0.03);
225 _fade_out.add ((_length * 0.9), 0.01);
226 _fade_out.add (_length, 0.0);
231 _fade_in.add (0.0, 0.0);
232 _fade_in.add ((_length * 0.1), 0.01);
233 _fade_in.add ((_length * 0.2), 0.03);
234 _fade_in.add ((_length * 0.8), 0.97);
235 _fade_in.add ((_length * 0.9), 0.99);
236 _fade_in.add (_length, 1.0);
239 overlap_type = _in->coverage (_out->position(), _out->last_frame());
240 layer_relation = (int32_t) (_in->layer() - _out->layer());
244 Crossfade::read_raw_internal (Sample* buf, nframes_t start, nframes_t cnt) const
247 Sample* mixdown = new Sample[cnt];
248 float* gain = new float[cnt];
251 ret = read_at (buf, mixdown, gain, start, cnt, chan_n, cnt);
262 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
263 float *gain_buffer, nframes_t start, nframes_t cnt, uint32_t chan_n,
264 nframes_t read_frames, nframes_t skip_frames) const
273 if (start < _position) {
275 /* handle an initial section of the read area that we do not
279 offset = _position - start;
289 to_write = min (_length, cnt);
293 to_write = min (_length - (start - _position), cnt);
297 offset = start - _position;
299 /* Prevent data from piling up inthe crossfade buffers when reading a transparent region */
300 if (!(_out->opaque())) {
301 memset (crossfade_buffer_out, 0, sizeof (Sample) * to_write);
302 } else if (!(_in->opaque())) {
303 memset (crossfade_buffer_in, 0, sizeof (Sample) * to_write);
306 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
307 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
309 float* fiv = new float[to_write];
310 float* fov = new float[to_write];
312 _fade_in.curve().get_vector (offset, offset+to_write, fiv, to_write);
313 _fade_out.curve().get_vector (offset, offset+to_write, fov, to_write);
315 /* note: although we have not explicitly taken into account the return values
316 from _out->read_at() or _in->read_at(), the length() function does this
317 implicitly. why? because it computes a value based on the in+out regions'
318 position and length, and so we know precisely how much data they could return.
321 for (nframes_t n = 0; n < to_write; ++n) {
322 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
332 Crossfade::coverage (nframes_t start, nframes_t end) const
334 nframes_t my_end = _position + _length;
336 if ((start >= _position) && (end <= my_end)) {
337 return OverlapInternal;
339 if ((end >= _position) && (end <= my_end)) {
342 if ((start >= _position) && (start <= my_end)) {
345 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
346 return OverlapExternal;
352 Crossfade::set_active (bool yn)
356 StateChanged (ActiveChanged);
361 Crossfade::refresh ()
363 /* crossfades must be between non-muted regions */
365 if (_out->muted() || _in->muted()) {
366 Invalidated (shared_from_this ());
370 /* Top layer shouldn't be transparent */
372 if (!((layer_relation > 0 ? _in : _out)->opaque())) {
373 Invalidated (shared_from_this());
377 /* layer ordering cannot change */
379 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
381 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
382 Invalidated (shared_from_this ());
386 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
388 if (ot == OverlapNone) {
389 Invalidated (shared_from_this ());
395 if (ot != overlap_type) {
397 if (_follow_overlap) {
400 compute (_in, _out, Config->get_xfade_model());
403 catch (NoCrossfadeHere& err) {
404 Invalidated (shared_from_this ());
412 Invalidated (shared_from_this ());
418 send_signal = update ();
422 StateChanged (BoundsChanged); /* EMIT SIGNAL */
435 if (_follow_overlap) {
436 newlen = _out->first_frame() + _out->length() - _in->first_frame();
442 Invalidated (shared_from_this ());
448 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
450 double factor = newlen / (double) _length;
452 _fade_out.x_scale (factor);
453 _fade_in.x_scale (factor);
458 switch (_anchor_point) {
460 _position = _in->first_frame();
464 _position = _in->last_frame() - _length;
468 _position = _out->last_frame() - _length;
475 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
477 boost::shared_ptr<AudioRegion> top;
478 boost::shared_ptr<AudioRegion> bottom;
479 nframes_t short_xfade_length;
481 short_xfade_length = _short_xfade_length;
483 if (a->layer() < b->layer()) {
491 /* first check for matching ends */
493 if (top->first_frame() == bottom->first_frame()) {
495 /* Both regions start at the same point */
497 if (top->last_frame() < bottom->last_frame()) {
499 /* top ends before bottom, so put an xfade
500 in at the end of top.
503 /* [-------- top ---------- ]
504 * {====== bottom =====================}
510 if (top->last_frame() < short_xfade_length) {
513 _position = top->last_frame() - short_xfade_length;
516 _length = min (short_xfade_length, top->length());
517 _follow_overlap = false;
518 _anchor_point = EndOfIn;
523 /* top ends after (or same time) as bottom - no xfade
526 /* [-------- top ------------------------ ]
527 * {====== bottom =====================}
530 throw NoCrossfadeHere();
533 } else if (top->last_frame() == bottom->last_frame()) {
535 /* Both regions end at the same point */
537 if (top->first_frame() > bottom->first_frame()) {
539 /* top starts after bottom, put an xfade in at the
543 /* [-------- top ---------- ]
544 * {====== bottom =====================}
549 _position = top->first_frame();
550 _length = min (short_xfade_length, top->length());
551 _follow_overlap = false;
552 _anchor_point = StartOfIn;
557 /* top starts before bottom - no xfade
560 /* [-------- top ------------------------ ]
561 * {====== bottom =====================}
564 throw NoCrossfadeHere();
569 /* OK, time to do more regular overlapping */
571 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
575 /* should be NOTREACHED as a precondition of creating
576 a new crossfade, but we need to handle it here.
578 throw NoCrossfadeHere();
581 case OverlapInternal:
582 case OverlapExternal:
583 /* should be NOTREACHED because of tests above */
584 throw NoCrossfadeHere();
587 case OverlapEnd: /* top covers start of bottom but ends within it */
589 /* [---- top ------------------------]
590 * { ==== bottom ============ }
595 _anchor_point = EndOfOut;
597 if (model == FullCrossfade) {
598 _position = bottom->first_frame(); // "{"
599 _length = _out->first_frame() + _out->length() - _in->first_frame();
600 /* leave active alone */
601 _follow_overlap = true;
603 _length = min (short_xfade_length, top->length());
604 _position = top->last_frame() - _length; // "]" - length
606 _follow_overlap = false;
611 case OverlapStart: /* top starts within bottom but covers bottom's end */
613 /* { ==== top ============ }
614 * [---- bottom -------------------]
619 _position = top->first_frame();
620 _anchor_point = StartOfIn;
622 if (model == FullCrossfade) {
623 _length = _out->first_frame() + _out->length() - _in->first_frame();
624 /* leave active alone */
625 _follow_overlap = true;
627 _length = min (short_xfade_length, top->length());
629 _follow_overlap = false;
641 Crossfade::get_state ()
643 XMLNode* node = new XMLNode (X_("Crossfade"));
646 LocaleGuard lg (X_("POSIX"));
648 _out->id().print (buf, sizeof (buf));
649 node->add_property ("out", buf);
650 _in->id().print (buf, sizeof (buf));
651 node->add_property ("in", buf);
652 node->add_property ("active", (_active ? "yes" : "no"));
653 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
654 node->add_property ("fixed", (_fixed ? "yes" : "no"));
655 snprintf (buf, sizeof(buf), "%" PRIu32, _length);
656 node->add_property ("length", buf);
657 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
658 node->add_property ("anchor-point", buf);
659 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
660 node->add_property ("position", buf);
662 child = node->add_child ("FadeIn");
664 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
667 pnode = new XMLNode ("point");
669 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
670 pnode->add_property ("x", buf);
671 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
672 pnode->add_property ("y", buf);
673 child->add_child_nocopy (*pnode);
676 child = node->add_child ("FadeOut");
678 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
681 pnode = new XMLNode ("point");
683 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
684 pnode->add_property ("x", buf);
685 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
686 pnode->add_property ("y", buf);
687 child->add_child_nocopy (*pnode);
694 Crossfade::set_state (const XMLNode& node)
696 XMLNodeConstIterator i;
697 XMLNodeList children;
700 const XMLProperty* prop;
701 LocaleGuard lg (X_("POSIX"));
702 Change what_changed = Change (0);
705 if ((prop = node.property ("position")) != 0) {
706 sscanf (prop->value().c_str(), "%" PRIu32, &val);
707 if (val != _position) {
709 what_changed = Change (what_changed | PositionChanged);
712 warning << _("old-style crossfade information - no position information") << endmsg;
713 _position = _in->first_frame();
716 if ((prop = node.property ("active")) != 0) {
717 bool x = (prop->value() == "yes");
720 what_changed = Change (what_changed | ActiveChanged);
726 if ((prop = node.property ("follow-overlap")) != 0) {
727 _follow_overlap = (prop->value() == "yes");
729 _follow_overlap = false;
732 if ((prop = node.property ("fixed")) != 0) {
733 _fixed = (prop->value() == "yes");
738 if ((prop = node.property ("anchor-point")) != 0) {
739 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
741 _anchor_point = StartOfIn;
744 if ((prop = node.property ("length")) != 0) {
746 sscanf (prop->value().c_str(), "%" PRIu32, &val);
747 if (val != _length) {
748 _length = atol (prop->value().c_str());
749 what_changed = Change (what_changed | LengthChanged);
754 /* XXX this branch is legacy code from before
755 the point where we stored xfade lengths.
758 if ((_length = overlap_length()) == 0) {
759 throw failed_constructor();
763 if ((fi = find_named_node (node, "FadeIn")) == 0) {
767 if ((fo = find_named_node (node, "FadeOut")) == 0) {
776 children = fi->children();
778 for (i = children.begin(); i != children.end(); ++i) {
779 if ((*i)->name() == "point") {
783 prop = (*i)->property ("x");
784 sscanf (prop->value().c_str(), "%" PRIu32, &x);
786 prop = (*i)->property ("y");
787 sscanf (prop->value().c_str(), "%f", &y);
800 children = fo->children();
802 for (i = children.begin(); i != children.end(); ++i) {
803 if ((*i)->name() == "point") {
808 prop = (*i)->property ("x");
809 sscanf (prop->value().c_str(), "%" PRIu32, &x);
811 prop = (*i)->property ("y");
812 sscanf (prop->value().c_str(), "%f", &y);
814 _fade_out.add (x, y);
820 StateChanged (what_changed); /* EMIT SIGNAL */
826 Crossfade::can_follow_overlap () const
832 Crossfade::set_follow_overlap (bool yn)
834 if (yn == _follow_overlap || _fixed) {
838 _follow_overlap = yn;
841 set_length (_short_xfade_length);
843 set_length (_out->first_frame() + _out->length() - _in->first_frame());
846 StateChanged (FollowOverlapChanged);
850 Crossfade::set_length (nframes_t len)
854 switch (_anchor_point) {
856 limit = _in->length();
860 limit = _in->length();
864 limit = _out->length();
869 len = min (limit, len);
871 double factor = len / (double) _length;
874 _fade_out.x_scale (factor);
875 _fade_in.x_scale (factor);
880 StateChanged (LengthChanged);
886 Crossfade::overlap_length () const
891 return _out->first_frame() + _out->length() - _in->first_frame();
895 Crossfade::set_short_xfade_length (nframes_t n)
897 _short_xfade_length = n;
901 Crossfade::invalidate ()
903 Invalidated (shared_from_this ()); /* EMIT SIGNAL */