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>
36 using namespace ARDOUR;
39 nframes_t Crossfade::_short_xfade_length = 0;
40 Change Crossfade::ActiveChanged = new_change();
41 Change Crossfade::FollowOverlapChanged = new_change();
43 /* XXX if and when we ever implement parallel processing of the process()
44 callback, these will need to be handled on a per-thread basis.
47 Sample* Crossfade::crossfade_buffer_out = 0;
48 Sample* Crossfade::crossfade_buffer_in = 0;
51 Crossfade::set_buffer_size (nframes_t sz)
53 if (crossfade_buffer_out) {
54 delete [] crossfade_buffer_out;
55 crossfade_buffer_out = 0;
58 if (crossfade_buffer_in) {
59 delete [] crossfade_buffer_in;
60 crossfade_buffer_in = 0;
64 crossfade_buffer_out = new Sample[sz];
65 crossfade_buffer_in = new Sample[sz];
70 Crossfade::operator== (const Crossfade& other)
72 return (_in == other._in) && (_out == other._out);
75 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out,
79 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
80 _fade_out (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 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
99 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
104 if (compute (a, b, model)) {
105 throw failed_constructor();
113 Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
114 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
115 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
117 boost::shared_ptr<Region> r;
119 LocaleGuard lg (X_("POSIX"));
121 /* we have to find the in/out regions before we can do anything else */
123 if ((prop = node.property ("in")) == 0) {
124 error << _("Crossfade: no \"in\" region in state") << endmsg;
125 throw failed_constructor();
128 PBD::ID id (prop->value());
130 if ((r = playlist.find_region (id)) == 0) {
131 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2"), id, playlist.name())
133 throw failed_constructor();
136 if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
137 throw failed_constructor();
140 if ((prop = node.property ("out")) == 0) {
141 error << _("Crossfade: no \"out\" region in state") << endmsg;
142 throw failed_constructor();
145 PBD::ID id2 (prop->value());
147 if ((r = playlist.find_region (id2)) == 0) {
148 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2"), id2, playlist.name())
150 throw failed_constructor();
153 if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
154 throw failed_constructor();
158 _active = Config->get_xfades_active();
162 if (set_state (node)) {
163 throw failed_constructor();
167 Crossfade::Crossfade (const Crossfade &orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
168 : _fade_in(orig._fade_in),
169 _fade_out(orig._fade_out)
171 _active = orig._active;
172 _in_update = orig._in_update;
173 _length = orig._length;
174 _position = orig._position;
175 _anchor_point = orig._anchor_point;
176 _follow_overlap = orig._follow_overlap;
177 _fixed = orig._fixed;
182 // copied from Crossfade::initialize()
185 _out->suspend_fade_out ();
186 _in->suspend_fade_in ();
188 overlap_type = _in->coverage (_out->position(), _out->last_frame());
189 layer_relation = (int32_t) (_in->layer() - _out->layer());
191 // Let's make sure the fade isn't too long
196 Crossfade::~Crossfade ()
202 Crossfade::initialize ()
206 _out->suspend_fade_out ();
207 _in->suspend_fade_in ();
211 _fade_out.add (0.0, 1.0);
212 _fade_out.add ((_length * 0.1), 0.99);
213 _fade_out.add ((_length * 0.2), 0.97);
214 _fade_out.add ((_length * 0.8), 0.03);
215 _fade_out.add ((_length * 0.9), 0.01);
216 _fade_out.add (_length, 0.0);
221 _fade_in.add (0.0, 0.0);
222 _fade_in.add ((_length * 0.1), 0.01);
223 _fade_in.add ((_length * 0.2), 0.03);
224 _fade_in.add ((_length * 0.8), 0.97);
225 _fade_in.add ((_length * 0.9), 0.99);
226 _fade_in.add (_length, 1.0);
229 overlap_type = _in->coverage (_out->position(), _out->last_frame());
230 layer_relation = (int32_t) (_in->layer() - _out->layer());
234 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
235 float *gain_buffer, nframes_t start, nframes_t cnt, uint32_t chan_n,
236 nframes_t read_frames, nframes_t skip_frames)
245 if (start < _position) {
247 /* handle an initial section of the read area that we do not
251 offset = _position - start;
261 to_write = min (_length, cnt);
265 to_write = min (_length - (start - _position), cnt);
269 offset = start - _position;
271 /* Prevent data from piling up inthe crossfade buffers when reading a transparent region */
272 if (!(_out->opaque())) {
273 memset (crossfade_buffer_out, 0, sizeof (Sample) * to_write);
274 } else if (!(_in->opaque())) {
275 memset (crossfade_buffer_in, 0, sizeof (Sample) * to_write);
278 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
279 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
281 float* fiv = new float[to_write];
282 float* fov = new float[to_write];
284 _fade_in.get_vector (offset, offset+to_write, fiv, to_write);
285 _fade_out.get_vector (offset, offset+to_write, fov, to_write);
287 /* note: although we have not explicitly taken into account the return values
288 from _out->read_at() or _in->read_at(), the length() function does this
289 implicitly. why? because it computes a value based on the in+out regions'
290 position and length, and so we know precisely how much data they could return.
293 for (nframes_t n = 0; n < to_write; ++n) {
294 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
304 Crossfade::coverage (nframes_t start, nframes_t end) const
306 nframes_t my_end = _position + _length;
308 if ((start >= _position) && (end <= my_end)) {
309 return OverlapInternal;
311 if ((end >= _position) && (end <= my_end)) {
314 if ((start >= _position) && (start <= my_end)) {
317 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
318 return OverlapExternal;
324 Crossfade::set_active (bool yn)
328 StateChanged (ActiveChanged);
333 Crossfade::refresh ()
335 /* crossfades must be between non-muted regions */
337 if (_out->muted() || _in->muted()) {
338 Invalidated (shared_from_this());
342 /* Top layer shouldn't be transparent */
344 if (!((layer_relation > 0 ? _in : _out)->opaque())) {
345 Invalidated (shared_from_this());
349 /* layer ordering cannot change */
351 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
353 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
354 Invalidated (shared_from_this());
358 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
360 if (ot == OverlapNone) {
361 Invalidated (shared_from_this());
367 if (ot != overlap_type) {
369 if (_follow_overlap) {
372 compute (_in, _out, Config->get_xfade_model());
375 catch (NoCrossfadeHere& err) {
376 Invalidated (shared_from_this());
384 Invalidated (shared_from_this());
390 send_signal = update ();
394 StateChanged (BoundsChanged); /* EMIT SIGNAL */
407 if (_follow_overlap) {
408 newlen = _out->first_frame() + _out->length() - _in->first_frame();
414 Invalidated (shared_from_this());
420 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
422 double factor = newlen / (double) _length;
424 _fade_out.x_scale (factor);
425 _fade_in.x_scale (factor);
430 switch (_anchor_point) {
432 _position = _in->first_frame();
436 _position = _in->last_frame() - _length;
440 _position = _out->last_frame() - _length;
447 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
449 boost::shared_ptr<AudioRegion> top;
450 boost::shared_ptr<AudioRegion> bottom;
451 nframes_t short_xfade_length;
453 short_xfade_length = _short_xfade_length;
455 if (a->layer() < b->layer()) {
463 /* first check for matching ends */
465 if (top->first_frame() == bottom->first_frame()) {
467 /* Both regions start at the same point */
469 if (top->last_frame() < bottom->last_frame()) {
471 /* top ends before bottom, so put an xfade
472 in at the end of top.
475 /* [-------- top ---------- ]
476 * {====== bottom =====================}
482 if (top->last_frame() < short_xfade_length) {
485 _position = top->last_frame() - short_xfade_length;
488 _length = min (short_xfade_length, top->length());
489 _follow_overlap = false;
490 _anchor_point = EndOfIn;
495 /* top ends after (or same time) as bottom - no xfade
498 /* [-------- top ------------------------ ]
499 * {====== bottom =====================}
502 throw NoCrossfadeHere();
505 } else if (top->last_frame() == bottom->last_frame()) {
507 /* Both regions end at the same point */
509 if (top->first_frame() > bottom->first_frame()) {
511 /* top starts after bottom, put an xfade in at the
515 /* [-------- top ---------- ]
516 * {====== bottom =====================}
521 _position = top->first_frame();
522 _length = min (short_xfade_length, top->length());
523 _follow_overlap = false;
524 _anchor_point = StartOfIn;
529 /* top starts before bottom - no xfade
532 /* [-------- top ------------------------ ]
533 * {====== bottom =====================}
536 throw NoCrossfadeHere();
541 /* OK, time to do more regular overlapping */
543 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
547 /* should be NOTREACHED as a precondition of creating
548 a new crossfade, but we need to handle it here.
550 throw NoCrossfadeHere();
553 case OverlapInternal:
554 case OverlapExternal:
555 /* should be NOTREACHED because of tests above */
556 throw NoCrossfadeHere();
559 case OverlapEnd: /* top covers start of bottom but ends within it */
561 /* [---- top ------------------------]
562 * { ==== bottom ============ }
567 _anchor_point = EndOfOut;
569 if (model == FullCrossfade) {
570 _position = bottom->first_frame(); // "{"
571 _length = _out->first_frame() + _out->length() - _in->first_frame();
572 /* leave active alone */
573 _follow_overlap = true;
575 _length = min (short_xfade_length, top->length());
576 _position = top->last_frame() - _length; // "]" - length
578 _follow_overlap = false;
583 case OverlapStart: /* top starts within bottom but covers bottom's end */
585 /* { ==== top ============ }
586 * [---- bottom -------------------]
591 _position = top->first_frame();
592 _anchor_point = StartOfIn;
594 if (model == FullCrossfade) {
595 _length = _out->first_frame() + _out->length() - _in->first_frame();
596 /* leave active alone */
597 _follow_overlap = true;
599 _length = min (short_xfade_length, top->length());
601 _follow_overlap = false;
613 Crossfade::get_state ()
615 XMLNode* node = new XMLNode (X_("Crossfade"));
618 LocaleGuard lg (X_("POSIX"));
620 _out->id().print (buf, sizeof (buf));
621 node->add_property ("out", buf);
622 _in->id().print (buf, sizeof (buf));
623 node->add_property ("in", buf);
624 node->add_property ("active", (_active ? "yes" : "no"));
625 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
626 node->add_property ("fixed", (_fixed ? "yes" : "no"));
627 snprintf (buf, sizeof(buf), "%" PRIu32, _length);
628 node->add_property ("length", buf);
629 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
630 node->add_property ("anchor-point", buf);
631 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
632 node->add_property ("position", buf);
634 child = node->add_child ("FadeIn");
636 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
639 pnode = new XMLNode ("point");
641 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
642 pnode->add_property ("x", buf);
643 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
644 pnode->add_property ("y", buf);
645 child->add_child_nocopy (*pnode);
648 child = node->add_child ("FadeOut");
650 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
653 pnode = new XMLNode ("point");
655 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
656 pnode->add_property ("x", buf);
657 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
658 pnode->add_property ("y", buf);
659 child->add_child_nocopy (*pnode);
666 Crossfade::set_state (const XMLNode& node)
668 XMLNodeConstIterator i;
669 XMLNodeList children;
672 const XMLProperty* prop;
673 LocaleGuard lg (X_("POSIX"));
674 Change what_changed = Change (0);
677 if ((prop = node.property ("position")) != 0) {
678 sscanf (prop->value().c_str(), "%" PRIu32, &val);
679 if (val != _position) {
681 what_changed = Change (what_changed | PositionChanged);
684 warning << _("old-style crossfade information - no position information") << endmsg;
685 _position = _in->first_frame();
688 if ((prop = node.property ("active")) != 0) {
689 bool x = (prop->value() == "yes");
692 what_changed = Change (what_changed | ActiveChanged);
698 if ((prop = node.property ("follow-overlap")) != 0) {
699 _follow_overlap = (prop->value() == "yes");
701 _follow_overlap = false;
704 if ((prop = node.property ("fixed")) != 0) {
705 _fixed = (prop->value() == "yes");
710 if ((prop = node.property ("anchor-point")) != 0) {
711 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
713 _anchor_point = StartOfIn;
716 if ((prop = node.property ("length")) != 0) {
718 sscanf (prop->value().c_str(), "%" PRIu32, &val);
719 if (val != _length) {
720 _length = atol (prop->value().c_str());
721 what_changed = Change (what_changed | LengthChanged);
726 /* XXX this branch is legacy code from before
727 the point where we stored xfade lengths.
730 if ((_length = overlap_length()) == 0) {
731 throw failed_constructor();
735 if ((fi = find_named_node (node, "FadeIn")) == 0) {
739 if ((fo = find_named_node (node, "FadeOut")) == 0) {
748 children = fi->children();
750 for (i = children.begin(); i != children.end(); ++i) {
751 if ((*i)->name() == "point") {
755 prop = (*i)->property ("x");
756 sscanf (prop->value().c_str(), "%" PRIu32, &x);
758 prop = (*i)->property ("y");
759 sscanf (prop->value().c_str(), "%f", &y);
772 children = fo->children();
774 for (i = children.begin(); i != children.end(); ++i) {
775 if ((*i)->name() == "point") {
780 prop = (*i)->property ("x");
781 sscanf (prop->value().c_str(), "%" PRIu32, &x);
783 prop = (*i)->property ("y");
784 sscanf (prop->value().c_str(), "%f", &y);
786 _fade_out.add (x, y);
792 StateChanged (what_changed); /* EMIT SIGNAL */
798 Crossfade::can_follow_overlap () const
804 Crossfade::set_follow_overlap (bool yn)
806 if (yn == _follow_overlap || _fixed) {
810 _follow_overlap = yn;
813 set_length (_short_xfade_length);
815 set_length (_out->first_frame() + _out->length() - _in->first_frame());
818 StateChanged (FollowOverlapChanged);
822 Crossfade::set_length (nframes_t len)
826 switch (_anchor_point) {
828 limit = _in->length();
832 limit = _in->length();
836 limit = _out->length();
841 len = min (limit, len);
843 double factor = len / (double) _length;
846 _fade_out.x_scale (factor);
847 _fade_in.x_scale (factor);
852 StateChanged (LengthChanged);
858 Crossfade::overlap_length () const
863 return _out->first_frame() + _out->length() - _in->first_frame();
867 Crossfade::set_short_xfade_length (nframes_t n)
869 _short_xfade_length = n;
873 Crossfade::invalidate ()
875 Invalidated (shared_from_this()); /* EMIT SIGNAL */