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 overlap_type = _in->coverage (_out->position(), _out->last_frame());
229 layer_relation = (int32_t) (_in->layer() - _out->layer());
233 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
234 float *gain_buffer, nframes_t start, nframes_t cnt, uint32_t chan_n,
235 nframes_t read_frames, nframes_t skip_frames)
244 if (start < _position) {
246 /* handle an initial section of the read area that we do not
250 offset = _position - start;
260 to_write = min (_length, cnt);
264 to_write = min (_length - (start - _position), cnt);
268 offset = start - _position;
270 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
271 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
273 float* fiv = new float[to_write];
274 float* fov = new float[to_write];
276 _fade_in.get_vector (offset, offset+to_write, fiv, to_write);
277 _fade_out.get_vector (offset, offset+to_write, fov, to_write);
279 /* note: although we have not explicitly taken into account the return values
280 from _out->read_at() or _in->read_at(), the length() function does this
281 implicitly. why? because it computes a value based on the in+out regions'
282 position and length, and so we know precisely how much data they could return.
285 for (nframes_t n = 0; n < to_write; ++n) {
286 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
296 Crossfade::coverage (nframes_t start, nframes_t end) const
298 nframes_t my_end = _position + _length;
300 if ((start >= _position) && (end <= my_end)) {
301 return OverlapInternal;
303 if ((end >= _position) && (end <= my_end)) {
306 if ((start >= _position) && (start <= my_end)) {
309 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
310 return OverlapExternal;
316 Crossfade::set_active (bool yn)
320 StateChanged (ActiveChanged);
325 Crossfade::refresh ()
327 /* crossfades must be between non-muted regions */
329 if (_out->muted() || _in->muted()) {
330 Invalidated (shared_from_this());
334 /* layer ordering cannot change */
336 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
338 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
339 Invalidated (shared_from_this());
343 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
345 if (ot == OverlapNone) {
346 Invalidated (shared_from_this());
352 if (ot != overlap_type) {
354 if (_follow_overlap) {
357 compute (_in, _out, Config->get_xfade_model());
360 catch (NoCrossfadeHere& err) {
361 Invalidated (shared_from_this());
369 Invalidated (shared_from_this());
375 send_signal = update ();
379 StateChanged (BoundsChanged); /* EMIT SIGNAL */
392 if (_follow_overlap) {
393 newlen = _out->first_frame() + _out->length() - _in->first_frame();
399 Invalidated (shared_from_this());
405 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
407 double factor = newlen / (double) _length;
409 _fade_out.x_scale (factor);
410 _fade_in.x_scale (factor);
415 switch (_anchor_point) {
417 _position = _in->first_frame();
421 _position = _in->last_frame() - _length;
425 _position = _out->last_frame() - _length;
432 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
434 boost::shared_ptr<AudioRegion> top;
435 boost::shared_ptr<AudioRegion> bottom;
436 nframes_t short_xfade_length;
438 short_xfade_length = _short_xfade_length;
440 if (a->layer() < b->layer()) {
448 /* first check for matching ends */
450 if (top->first_frame() == bottom->first_frame()) {
452 /* Both regions start at the same point */
454 if (top->last_frame() < bottom->last_frame()) {
456 /* top ends before bottom, so put an xfade
457 in at the end of top.
460 /* [-------- top ---------- ]
461 * {====== bottom =====================}
467 if (top->last_frame() < short_xfade_length) {
470 _position = top->last_frame() - short_xfade_length;
473 _length = min (short_xfade_length, top->length());
474 _follow_overlap = false;
475 _anchor_point = EndOfIn;
480 /* top ends after (or same time) as bottom - no xfade
483 /* [-------- top ------------------------ ]
484 * {====== bottom =====================}
487 throw NoCrossfadeHere();
490 } else if (top->last_frame() == bottom->last_frame()) {
492 /* Both regions end at the same point */
494 if (top->first_frame() > bottom->first_frame()) {
496 /* top starts after bottom, put an xfade in at the
500 /* [-------- top ---------- ]
501 * {====== bottom =====================}
506 _position = top->first_frame();
507 _length = min (short_xfade_length, top->length());
508 _follow_overlap = false;
509 _anchor_point = StartOfIn;
514 /* top starts before bottom - no xfade
517 /* [-------- top ------------------------ ]
518 * {====== bottom =====================}
521 throw NoCrossfadeHere();
526 /* OK, time to do more regular overlapping */
528 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
532 /* should be NOTREACHED as a precondition of creating
533 a new crossfade, but we need to handle it here.
535 throw NoCrossfadeHere();
538 case OverlapInternal:
539 case OverlapExternal:
540 /* should be NOTREACHED because of tests above */
541 throw NoCrossfadeHere();
544 case OverlapEnd: /* top covers start of bottom but ends within it */
546 /* [---- top ------------------------]
547 * { ==== bottom ============ }
552 _anchor_point = StartOfIn;
554 if (model == FullCrossfade) {
555 _position = bottom->first_frame(); // "{"
556 _length = _out->first_frame() + _out->length() - _in->first_frame();
557 /* leave active alone */
558 _follow_overlap = true;
560 _length = min (short_xfade_length, top->length());
561 _position = top->last_frame() - _length; // "]" - length
563 _follow_overlap = false;
568 case OverlapStart: /* top starts within bottom but covers bottom's end */
570 /* { ==== top ============ }
571 * [---- bottom -------------------]
576 _position = top->first_frame();
577 _anchor_point = StartOfIn;
579 if (model == FullCrossfade) {
580 _length = _out->first_frame() + _out->length() - _in->first_frame();
581 /* leave active alone */
582 _follow_overlap = true;
584 _length = min (short_xfade_length, top->length());
586 _follow_overlap = false;
598 Crossfade::get_state ()
600 XMLNode* node = new XMLNode (X_("Crossfade"));
603 LocaleGuard lg (X_("POSIX"));
605 _out->id().print (buf, sizeof (buf));
606 node->add_property ("out", buf);
607 _in->id().print (buf, sizeof (buf));
608 node->add_property ("in", buf);
609 node->add_property ("active", (_active ? "yes" : "no"));
610 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
611 node->add_property ("fixed", (_fixed ? "yes" : "no"));
612 snprintf (buf, sizeof(buf), "%" PRIu32, _length);
613 node->add_property ("length", buf);
614 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
615 node->add_property ("anchor-point", buf);
616 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
617 node->add_property ("position", buf);
619 child = node->add_child ("FadeIn");
621 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
624 pnode = new XMLNode ("point");
626 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
627 pnode->add_property ("x", buf);
628 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
629 pnode->add_property ("y", buf);
630 child->add_child_nocopy (*pnode);
633 child = node->add_child ("FadeOut");
635 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
638 pnode = new XMLNode ("point");
640 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
641 pnode->add_property ("x", buf);
642 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
643 pnode->add_property ("y", buf);
644 child->add_child_nocopy (*pnode);
651 Crossfade::set_state (const XMLNode& node)
653 XMLNodeConstIterator i;
654 XMLNodeList children;
657 const XMLProperty* prop;
658 LocaleGuard lg (X_("POSIX"));
659 Change what_changed = Change (0);
662 if ((prop = node.property ("position")) != 0) {
663 sscanf (prop->value().c_str(), "%" PRIu32, &val);
664 if (val != _position) {
666 what_changed = Change (what_changed | PositionChanged);
669 warning << _("old-style crossfade information - no position information") << endmsg;
670 _position = _in->first_frame();
673 if ((prop = node.property ("active")) != 0) {
674 bool x = (prop->value() == "yes");
677 what_changed = Change (what_changed | ActiveChanged);
683 if ((prop = node.property ("follow-overlap")) != 0) {
684 _follow_overlap = (prop->value() == "yes");
686 _follow_overlap = false;
689 if ((prop = node.property ("fixed")) != 0) {
690 _fixed = (prop->value() == "yes");
695 if ((prop = node.property ("anchor-point")) != 0) {
696 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
698 _anchor_point = StartOfIn;
701 if ((prop = node.property ("length")) != 0) {
703 sscanf (prop->value().c_str(), "%" PRIu32, &val);
704 if (val != _length) {
705 _length = atol (prop->value().c_str());
706 what_changed = Change (what_changed | LengthChanged);
711 /* XXX this branch is legacy code from before
712 the point where we stored xfade lengths.
715 if ((_length = overlap_length()) == 0) {
716 throw failed_constructor();
720 if ((fi = find_named_node (node, "FadeIn")) == 0) {
724 if ((fo = find_named_node (node, "FadeOut")) == 0) {
733 children = fi->children();
735 for (i = children.begin(); i != children.end(); ++i) {
736 if ((*i)->name() == "point") {
740 prop = (*i)->property ("x");
741 sscanf (prop->value().c_str(), "%" PRIu32, &x);
743 prop = (*i)->property ("y");
744 sscanf (prop->value().c_str(), "%f", &y);
757 children = fo->children();
759 for (i = children.begin(); i != children.end(); ++i) {
760 if ((*i)->name() == "point") {
765 prop = (*i)->property ("x");
766 sscanf (prop->value().c_str(), "%" PRIu32, &x);
768 prop = (*i)->property ("y");
769 sscanf (prop->value().c_str(), "%f", &y);
771 _fade_out.add (x, y);
777 StateChanged (what_changed); /* EMIT SIGNAL */
783 Crossfade::can_follow_overlap () const
789 Crossfade::set_follow_overlap (bool yn)
791 if (yn == _follow_overlap || _fixed) {
795 _follow_overlap = yn;
798 set_length (_short_xfade_length);
800 set_length (_out->first_frame() + _out->length() - _in->first_frame());
803 StateChanged (FollowOverlapChanged);
807 Crossfade::set_length (nframes_t len)
811 switch (_anchor_point) {
813 limit = _in->length();
817 limit = _in->length();
821 limit = _out->length();
826 len = min (limit, len);
828 double factor = len / (double) _length;
831 _fade_out.x_scale (factor);
832 _fade_in.x_scale (factor);
837 StateChanged (LengthChanged);
843 Crossfade::overlap_length () const
848 return _out->first_frame() + _out->length() - _in->first_frame();
852 Crossfade::set_short_xfade_length (nframes_t n)
854 _short_xfade_length = n;
858 Crossfade::invalidate ()
860 Invalidated (shared_from_this()); /* EMIT SIGNAL */