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();
159 _active = Config->get_xfades_active();
163 if (set_state (node)) {
164 throw failed_constructor();
168 Crossfade::Crossfade (const Crossfade &orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
169 : _fade_in(orig._fade_in),
170 _fade_out(orig._fade_out)
172 _active = orig._active;
173 _in_update = orig._in_update;
174 _length = orig._length;
175 _position = orig._position;
176 _anchor_point = orig._anchor_point;
177 _follow_overlap = orig._follow_overlap;
178 _fixed = orig._fixed;
183 // copied from Crossfade::initialize()
186 _out->suspend_fade_out ();
187 _in->suspend_fade_in ();
189 overlap_type = _in->coverage (_out->position(), _out->last_frame());
190 layer_relation = (int32_t) (_in->layer() - _out->layer());
192 // Let's make sure the fade isn't too long
197 Crossfade::~Crossfade ()
203 Crossfade::initialize ()
207 _out->suspend_fade_out ();
208 _in->suspend_fade_in ();
212 _fade_out.add (0.0, 1.0);
213 _fade_out.add ((_length * 0.1), 0.99);
214 _fade_out.add ((_length * 0.2), 0.97);
215 _fade_out.add ((_length * 0.8), 0.03);
216 _fade_out.add ((_length * 0.9), 0.01);
217 _fade_out.add (_length, 0.0);
222 _fade_in.add (0.0, 0.0);
223 _fade_in.add ((_length * 0.1), 0.01);
224 _fade_in.add ((_length * 0.2), 0.03);
225 _fade_in.add ((_length * 0.8), 0.97);
226 _fade_in.add ((_length * 0.9), 0.99);
227 _fade_in.add (_length, 1.0);
230 overlap_type = _in->coverage (_out->position(), _out->last_frame());
231 layer_relation = (int32_t) (_in->layer() - _out->layer());
235 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
236 float *gain_buffer, nframes_t start, nframes_t cnt, uint32_t chan_n,
237 nframes_t read_frames, nframes_t skip_frames)
246 if (start < _position) {
248 /* handle an initial section of the read area that we do not
252 offset = _position - start;
262 to_write = min (_length, cnt);
266 to_write = min (_length - (start - _position), cnt);
270 offset = start - _position;
272 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
273 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
275 float* fiv = new float[to_write];
276 float* fov = new float[to_write];
278 _fade_in.get_vector (offset, offset+to_write, fiv, to_write);
279 _fade_out.get_vector (offset, offset+to_write, fov, to_write);
281 /* note: although we have not explicitly taken into account the return values
282 from _out->read_at() or _in->read_at(), the length() function does this
283 implicitly. why? because it computes a value based on the in+out regions'
284 position and length, and so we know precisely how much data they could return.
287 for (nframes_t n = 0; n < to_write; ++n) {
288 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
298 Crossfade::coverage (nframes_t start, nframes_t end) const
300 nframes_t my_end = _position + _length;
302 if ((start >= _position) && (end <= my_end)) {
303 return OverlapInternal;
305 if ((end >= _position) && (end <= my_end)) {
308 if ((start >= _position) && (start <= my_end)) {
311 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
312 return OverlapExternal;
318 Crossfade::set_active (bool yn)
322 StateChanged (ActiveChanged);
327 Crossfade::refresh ()
329 /* crossfades must be between non-muted regions */
331 if (_out->muted() || _in->muted()) {
332 Invalidated (shared_from_this());
336 /* layer ordering cannot change */
338 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
340 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
341 Invalidated (shared_from_this());
345 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
347 if (ot == OverlapNone) {
348 Invalidated (shared_from_this());
354 if (ot != overlap_type) {
356 if (_follow_overlap) {
359 compute (_in, _out, Config->get_xfade_model());
362 catch (NoCrossfadeHere& err) {
363 Invalidated (shared_from_this());
371 Invalidated (shared_from_this());
377 send_signal = update ();
381 StateChanged (BoundsChanged); /* EMIT SIGNAL */
394 if (_follow_overlap) {
395 newlen = _out->first_frame() + _out->length() - _in->first_frame();
401 Invalidated (shared_from_this());
407 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
409 double factor = newlen / (double) _length;
411 _fade_out.x_scale (factor);
412 _fade_in.x_scale (factor);
417 switch (_anchor_point) {
419 _position = _in->first_frame();
423 _position = _in->last_frame() - _length;
427 _position = _out->last_frame() - _length;
434 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
436 boost::shared_ptr<AudioRegion> top;
437 boost::shared_ptr<AudioRegion> bottom;
438 nframes_t short_xfade_length;
440 short_xfade_length = _short_xfade_length;
442 if (a->layer() < b->layer()) {
450 /* first check for matching ends */
452 if (top->first_frame() == bottom->first_frame()) {
454 /* Both regions start at the same point */
456 if (top->last_frame() < bottom->last_frame()) {
458 /* top ends before bottom, so put an xfade
459 in at the end of top.
462 /* [-------- top ---------- ]
463 * {====== bottom =====================}
469 if (top->last_frame() < short_xfade_length) {
472 _position = top->last_frame() - short_xfade_length;
475 _length = min (short_xfade_length, top->length());
476 _follow_overlap = false;
477 _anchor_point = EndOfIn;
482 /* top ends after (or same time) as bottom - no xfade
485 /* [-------- top ------------------------ ]
486 * {====== bottom =====================}
489 throw NoCrossfadeHere();
492 } else if (top->last_frame() == bottom->last_frame()) {
494 /* Both regions end at the same point */
496 if (top->first_frame() > bottom->first_frame()) {
498 /* top starts after bottom, put an xfade in at the
502 /* [-------- top ---------- ]
503 * {====== bottom =====================}
508 _position = top->first_frame();
509 _length = min (short_xfade_length, top->length());
510 _follow_overlap = false;
511 _anchor_point = StartOfIn;
516 /* top starts before bottom - no xfade
519 /* [-------- top ------------------------ ]
520 * {====== bottom =====================}
523 throw NoCrossfadeHere();
528 /* OK, time to do more regular overlapping */
530 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
534 /* should be NOTREACHED as a precondition of creating
535 a new crossfade, but we need to handle it here.
537 throw NoCrossfadeHere();
540 case OverlapInternal:
541 case OverlapExternal:
542 /* should be NOTREACHED because of tests above */
543 throw NoCrossfadeHere();
546 case OverlapEnd: /* top covers start of bottom but ends within it */
548 /* [---- top ------------------------]
549 * { ==== bottom ============ }
554 _anchor_point = StartOfIn;
556 if (model == FullCrossfade) {
557 _position = bottom->first_frame(); // "{"
558 _length = _out->first_frame() + _out->length() - _in->first_frame();
559 /* leave active alone */
560 _follow_overlap = true;
562 _length = min (short_xfade_length, top->length());
563 _position = top->last_frame() - _length; // "]" - length
565 _follow_overlap = false;
570 case OverlapStart: /* top starts within bottom but covers bottom's end */
572 /* { ==== top ============ }
573 * [---- bottom -------------------]
578 _position = top->first_frame();
579 _anchor_point = StartOfIn;
581 if (model == FullCrossfade) {
582 _length = _out->first_frame() + _out->length() - _in->first_frame();
583 /* leave active alone */
584 _follow_overlap = true;
586 _length = min (short_xfade_length, top->length());
588 _follow_overlap = false;
600 Crossfade::get_state ()
602 XMLNode* node = new XMLNode (X_("Crossfade"));
605 LocaleGuard lg (X_("POSIX"));
607 _out->id().print (buf, sizeof (buf));
608 node->add_property ("out", buf);
609 _in->id().print (buf, sizeof (buf));
610 node->add_property ("in", buf);
611 node->add_property ("active", (_active ? "yes" : "no"));
612 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
613 node->add_property ("fixed", (_fixed ? "yes" : "no"));
614 snprintf (buf, sizeof(buf), "%" PRIu32, _length);
615 node->add_property ("length", buf);
616 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
617 node->add_property ("anchor-point", buf);
618 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
619 node->add_property ("position", buf);
621 child = node->add_child ("FadeIn");
623 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
626 pnode = new XMLNode ("point");
628 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
629 pnode->add_property ("x", buf);
630 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
631 pnode->add_property ("y", buf);
632 child->add_child_nocopy (*pnode);
635 child = node->add_child ("FadeOut");
637 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
640 pnode = new XMLNode ("point");
642 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
643 pnode->add_property ("x", buf);
644 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
645 pnode->add_property ("y", buf);
646 child->add_child_nocopy (*pnode);
653 Crossfade::set_state (const XMLNode& node)
655 XMLNodeConstIterator i;
656 XMLNodeList children;
659 const XMLProperty* prop;
660 LocaleGuard lg (X_("POSIX"));
661 Change what_changed = Change (0);
664 if ((prop = node.property ("position")) != 0) {
665 sscanf (prop->value().c_str(), "%" PRIu32, &val);
666 if (val != _position) {
668 what_changed = Change (what_changed | PositionChanged);
671 warning << _("old-style crossfade information - no position information") << endmsg;
672 _position = _in->first_frame();
675 if ((prop = node.property ("active")) != 0) {
676 bool x = (prop->value() == "yes");
679 what_changed = Change (what_changed | ActiveChanged);
685 if ((prop = node.property ("follow-overlap")) != 0) {
686 _follow_overlap = (prop->value() == "yes");
688 _follow_overlap = false;
691 if ((prop = node.property ("fixed")) != 0) {
692 _fixed = (prop->value() == "yes");
697 if ((prop = node.property ("anchor-point")) != 0) {
698 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
700 _anchor_point = StartOfIn;
703 if ((prop = node.property ("length")) != 0) {
705 sscanf (prop->value().c_str(), "%" PRIu32, &val);
706 if (val != _length) {
707 _length = atol (prop->value().c_str());
708 what_changed = Change (what_changed | LengthChanged);
713 /* XXX this branch is legacy code from before
714 the point where we stored xfade lengths.
717 if ((_length = overlap_length()) == 0) {
718 throw failed_constructor();
722 if ((fi = find_named_node (node, "FadeIn")) == 0) {
726 if ((fo = find_named_node (node, "FadeOut")) == 0) {
735 children = fi->children();
737 for (i = children.begin(); i != children.end(); ++i) {
738 if ((*i)->name() == "point") {
742 prop = (*i)->property ("x");
743 sscanf (prop->value().c_str(), "%" PRIu32, &x);
745 prop = (*i)->property ("y");
746 sscanf (prop->value().c_str(), "%f", &y);
759 children = fo->children();
761 for (i = children.begin(); i != children.end(); ++i) {
762 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);
773 _fade_out.add (x, y);
779 StateChanged (what_changed); /* EMIT SIGNAL */
785 Crossfade::can_follow_overlap () const
791 Crossfade::set_follow_overlap (bool yn)
793 if (yn == _follow_overlap || _fixed) {
797 _follow_overlap = yn;
800 set_length (_short_xfade_length);
802 set_length (_out->first_frame() + _out->length() - _in->first_frame());
805 StateChanged (FollowOverlapChanged);
809 Crossfade::set_length (nframes_t len)
813 switch (_anchor_point) {
815 limit = _in->length();
819 limit = _in->length();
823 limit = _out->length();
828 len = min (limit, len);
830 double factor = len / (double) _length;
833 _fade_out.x_scale (factor);
834 _fade_in.x_scale (factor);
839 StateChanged (LengthChanged);
845 Crossfade::overlap_length () const
850 return _out->first_frame() + _out->length() - _in->first_frame();
854 Crossfade::set_short_xfade_length (nframes_t n)
856 _short_xfade_length = n;
860 Crossfade::invalidate ()
862 Invalidated (shared_from_this()); /* EMIT SIGNAL */