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 (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 (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 (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 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n);
298 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n);
300 float* fiv = new float[to_write];
301 float* fov = new float[to_write];
303 _fade_in.curve().get_vector (offset, offset+to_write, fiv, to_write);
304 _fade_out.curve().get_vector (offset, offset+to_write, fov, to_write);
306 /* note: although we have not explicitly taken into account the return values
307 from _out->read_at() or _in->read_at(), the length() function does this
308 implicitly. why? because it computes a value based on the in+out regions'
309 position and length, and so we know precisely how much data they could return.
312 for (nframes_t n = 0; n < to_write; ++n) {
313 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
323 Crossfade::coverage (nframes_t start, nframes_t end) const
325 nframes_t my_end = _position + _length;
327 if ((start >= _position) && (end <= my_end)) {
328 return OverlapInternal;
330 if ((end >= _position) && (end <= my_end)) {
333 if ((start >= _position) && (start <= my_end)) {
336 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
337 return OverlapExternal;
343 Crossfade::set_active (bool yn)
347 StateChanged (ActiveChanged);
352 Crossfade::refresh ()
354 /* crossfades must be between non-muted regions */
356 if (_out->muted() || _in->muted()) {
357 Invalidated (shared_from_this ());
361 /* layer ordering cannot change */
363 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
365 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
366 Invalidated (shared_from_this ());
370 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
372 if (ot == OverlapNone) {
373 Invalidated (shared_from_this ());
379 if (ot != overlap_type) {
381 if (_follow_overlap) {
384 compute (_in, _out, Config->get_xfade_model());
387 catch (NoCrossfadeHere& err) {
388 Invalidated (shared_from_this ());
396 Invalidated (shared_from_this ());
402 send_signal = update ();
406 StateChanged (BoundsChanged); /* EMIT SIGNAL */
419 if (_follow_overlap) {
420 newlen = _out->first_frame() + _out->length() - _in->first_frame();
426 Invalidated (shared_from_this ());
432 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
434 double factor = newlen / (double) _length;
436 _fade_out.x_scale (factor);
437 _fade_in.x_scale (factor);
442 switch (_anchor_point) {
444 _position = _in->first_frame();
448 _position = _in->last_frame() - _length;
452 _position = _out->last_frame() - _length;
459 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
461 boost::shared_ptr<AudioRegion> top;
462 boost::shared_ptr<AudioRegion> bottom;
463 nframes_t short_xfade_length;
465 short_xfade_length = _short_xfade_length;
467 if (a->layer() < b->layer()) {
475 /* first check for matching ends */
477 if (top->first_frame() == bottom->first_frame()) {
479 /* Both regions start at the same point */
481 if (top->last_frame() < bottom->last_frame()) {
483 /* top ends before bottom, so put an xfade
484 in at the end of top.
487 /* [-------- top ---------- ]
488 * {====== bottom =====================}
494 if (top->last_frame() < short_xfade_length) {
497 _position = top->last_frame() - short_xfade_length;
500 _length = min (short_xfade_length, top->length());
501 _follow_overlap = false;
502 _anchor_point = EndOfIn;
507 /* top ends after (or same time) as bottom - no xfade
510 /* [-------- top ------------------------ ]
511 * {====== bottom =====================}
514 throw NoCrossfadeHere();
517 } else if (top->last_frame() == bottom->last_frame()) {
519 /* Both regions end at the same point */
521 if (top->first_frame() > bottom->first_frame()) {
523 /* top starts after bottom, put an xfade in at the
527 /* [-------- top ---------- ]
528 * {====== bottom =====================}
533 _position = top->first_frame();
534 _length = min (short_xfade_length, top->length());
535 _follow_overlap = false;
536 _anchor_point = StartOfIn;
541 /* top starts before bottom - no xfade
544 /* [-------- top ------------------------ ]
545 * {====== bottom =====================}
548 throw NoCrossfadeHere();
553 /* OK, time to do more regular overlapping */
555 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
559 /* should be NOTREACHED as a precondition of creating
560 a new crossfade, but we need to handle it here.
562 throw NoCrossfadeHere();
565 case OverlapInternal:
566 case OverlapExternal:
567 /* should be NOTREACHED because of tests above */
568 throw NoCrossfadeHere();
571 case OverlapEnd: /* top covers start of bottom but ends within it */
573 /* [---- top ------------------------]
574 * { ==== bottom ============ }
579 _anchor_point = EndOfOut;
581 if (model == FullCrossfade) {
582 _position = bottom->first_frame(); // "{"
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());
588 _position = top->last_frame() - _length; // "]" - length
590 _follow_overlap = false;
595 case OverlapStart: /* top starts within bottom but covers bottom's end */
597 /* { ==== top ============ }
598 * [---- bottom -------------------]
603 _position = top->first_frame();
604 _anchor_point = StartOfIn;
606 if (model == FullCrossfade) {
607 _length = _out->first_frame() + _out->length() - _in->first_frame();
608 /* leave active alone */
609 _follow_overlap = true;
611 _length = min (short_xfade_length, top->length());
613 _follow_overlap = false;
625 Crossfade::get_state ()
627 XMLNode* node = new XMLNode (X_("Crossfade"));
630 LocaleGuard lg (X_("POSIX"));
632 _out->id().print (buf, sizeof (buf));
633 node->add_property ("out", buf);
634 _in->id().print (buf, sizeof (buf));
635 node->add_property ("in", buf);
636 node->add_property ("active", (_active ? "yes" : "no"));
637 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
638 node->add_property ("fixed", (_fixed ? "yes" : "no"));
639 snprintf (buf, sizeof(buf), "%" PRIu32, _length);
640 node->add_property ("length", buf);
641 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
642 node->add_property ("anchor-point", buf);
643 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
644 node->add_property ("position", buf);
646 child = node->add_child ("FadeIn");
648 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.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);
660 child = node->add_child ("FadeOut");
662 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.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);
678 Crossfade::set_state (const XMLNode& node)
680 XMLNodeConstIterator i;
681 XMLNodeList children;
684 const XMLProperty* prop;
685 LocaleGuard lg (X_("POSIX"));
686 Change what_changed = Change (0);
689 if ((prop = node.property ("position")) != 0) {
690 sscanf (prop->value().c_str(), "%" PRIu32, &val);
691 if (val != _position) {
693 what_changed = Change (what_changed | PositionChanged);
696 warning << _("old-style crossfade information - no position information") << endmsg;
697 _position = _in->first_frame();
700 if ((prop = node.property ("active")) != 0) {
701 bool x = (prop->value() == "yes");
704 what_changed = Change (what_changed | ActiveChanged);
710 if ((prop = node.property ("follow-overlap")) != 0) {
711 _follow_overlap = (prop->value() == "yes");
713 _follow_overlap = false;
716 if ((prop = node.property ("fixed")) != 0) {
717 _fixed = (prop->value() == "yes");
722 if ((prop = node.property ("anchor-point")) != 0) {
723 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
725 _anchor_point = StartOfIn;
728 if ((prop = node.property ("length")) != 0) {
730 sscanf (prop->value().c_str(), "%" PRIu32, &val);
731 if (val != _length) {
732 _length = atol (prop->value().c_str());
733 what_changed = Change (what_changed | LengthChanged);
738 /* XXX this branch is legacy code from before
739 the point where we stored xfade lengths.
742 if ((_length = overlap_length()) == 0) {
743 throw failed_constructor();
747 if ((fi = find_named_node (node, "FadeIn")) == 0) {
751 if ((fo = find_named_node (node, "FadeOut")) == 0) {
760 children = fi->children();
762 for (i = children.begin(); i != children.end(); ++i) {
763 if ((*i)->name() == "point") {
767 prop = (*i)->property ("x");
768 sscanf (prop->value().c_str(), "%" PRIu32, &x);
770 prop = (*i)->property ("y");
771 sscanf (prop->value().c_str(), "%f", &y);
784 children = fo->children();
786 for (i = children.begin(); i != children.end(); ++i) {
787 if ((*i)->name() == "point") {
792 prop = (*i)->property ("x");
793 sscanf (prop->value().c_str(), "%" PRIu32, &x);
795 prop = (*i)->property ("y");
796 sscanf (prop->value().c_str(), "%f", &y);
798 _fade_out.add (x, y);
804 StateChanged (what_changed); /* EMIT SIGNAL */
810 Crossfade::can_follow_overlap () const
816 Crossfade::set_follow_overlap (bool yn)
818 if (yn == _follow_overlap || _fixed) {
822 _follow_overlap = yn;
825 set_length (_short_xfade_length);
827 set_length (_out->first_frame() + _out->length() - _in->first_frame());
830 StateChanged (FollowOverlapChanged);
834 Crossfade::set_length (nframes_t len)
838 switch (_anchor_point) {
840 limit = _in->length();
844 limit = _in->length();
848 limit = _out->length();
853 len = min (limit, len);
855 double factor = len / (double) _length;
858 _fade_out.x_scale (factor);
859 _fade_in.x_scale (factor);
864 StateChanged (LengthChanged);
870 Crossfade::overlap_length () const
875 return _out->first_frame() + _out->length() - _in->first_frame();
879 Crossfade::set_short_xfade_length (nframes_t n)
881 _short_xfade_length = n;
885 Crossfade::invalidate ()
887 Invalidated (shared_from_this ()); /* EMIT SIGNAL */