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 <ardour/types.h>
24 #include <ardour/crossfade.h>
25 #include <ardour/crossfade_compare.h>
26 #include <ardour/audioregion.h>
27 #include <ardour/playlist.h>
28 #include <ardour/utils.h>
29 #include <ardour/session.h>
35 using namespace ARDOUR;
38 nframes_t Crossfade::_short_xfade_length = 0;
39 Change Crossfade::ActiveChanged = new_change();
40 Change Crossfade::FollowOverlapChanged = new_change();
42 /* XXX if and when we ever implement parallel processing of the process()
43 callback, these will need to be handled on a per-thread basis.
46 Sample* Crossfade::crossfade_buffer_out = 0;
47 Sample* Crossfade::crossfade_buffer_in = 0;
50 Crossfade::set_buffer_size (nframes_t sz)
52 if (crossfade_buffer_out) {
53 delete [] crossfade_buffer_out;
54 crossfade_buffer_out = 0;
57 if (crossfade_buffer_in) {
58 delete [] crossfade_buffer_in;
59 crossfade_buffer_in = 0;
63 crossfade_buffer_out = new Sample[sz];
64 crossfade_buffer_in = new Sample[sz];
69 Crossfade::operator== (const Crossfade& other)
71 return (_in == other._in) && (_out == other._out);
74 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out,
78 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
79 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
87 switch (Config->get_xfade_model()) {
89 _follow_overlap = false;
92 _follow_overlap = true;
95 _active = Config->get_crossfades_active ();
101 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
102 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
103 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
108 if (compute (a, b, model)) {
109 throw failed_constructor();
118 Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
119 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
120 _fade_out (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 (const Crossfade &orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
171 : _fade_in(orig._fade_in),
172 _fade_out(orig._fade_out)
174 _active = orig._active;
175 _in_update = orig._in_update;
176 _length = orig._length;
177 _position = orig._position;
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());
193 // Let's make sure the fade isn't too long
198 Crossfade::~Crossfade ()
204 Crossfade::initialize ()
208 _out->suspend_fade_out ();
209 _in->suspend_fade_in ();
213 _fade_out.add (0.0, 1.0);
214 _fade_out.add ((_length * 0.1), 0.99);
215 _fade_out.add ((_length * 0.2), 0.97);
216 _fade_out.add ((_length * 0.8), 0.03);
217 _fade_out.add ((_length * 0.9), 0.01);
218 _fade_out.add (_length, 0.0);
223 _fade_in.add (0.0, 0.0);
224 _fade_in.add ((_length * 0.1), 0.01);
225 _fade_in.add ((_length * 0.2), 0.03);
226 _fade_in.add ((_length * 0.8), 0.97);
227 _fade_in.add ((_length * 0.9), 0.99);
228 _fade_in.add (_length, 1.0);
231 _in->StateChanged.connect (sigc::mem_fun (*this, &Crossfade::member_changed));
232 _out->StateChanged.connect (sigc::mem_fun (*this, &Crossfade::member_changed));
234 overlap_type = _in->coverage (_out->position(), _out->last_frame());
238 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
240 boost::shared_ptr<AudioRegion> top;
241 boost::shared_ptr<AudioRegion> bottom;
242 nframes_t short_xfade_length;
244 short_xfade_length = _short_xfade_length;
246 if (a->layer() < b->layer()) {
254 /* first check for matching ends */
256 if (top->first_frame() == bottom->first_frame()) {
258 /* Both regions start at the same point */
260 if (top->last_frame() < bottom->last_frame()) {
262 /* top ends before bottom, so put an xfade
263 in at the end of top.
266 /* [-------- top ---------- ]
267 * {====== bottom =====================}
273 if (top->last_frame() < short_xfade_length) {
276 _position = top->last_frame() - short_xfade_length;
279 _length = min (short_xfade_length, top->length());
280 _follow_overlap = false;
281 _anchor_point = EndOfIn;
286 /* top ends after (or same time) as bottom - no xfade
289 /* [-------- top ------------------------ ]
290 * {====== bottom =====================}
293 throw NoCrossfadeHere();
296 } else if (top->last_frame() == bottom->last_frame()) {
298 /* Both regions end at the same point */
300 if (top->first_frame() > bottom->first_frame()) {
302 /* top starts after bottom, put an xfade in at the
306 /* [-------- top ---------- ]
307 * {====== bottom =====================}
312 _position = top->first_frame();
313 _length = min (short_xfade_length, top->length());
314 _follow_overlap = false;
315 _anchor_point = StartOfIn;
320 /* top starts before bottom - no xfade
323 /* [-------- top ------------------------ ]
324 * {====== bottom =====================}
327 throw NoCrossfadeHere();
332 /* OK, time to do more regular overlapping */
334 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
338 /* should be NOTREACHED as a precondition of creating
339 a new crossfade, but we need to handle it here.
341 throw NoCrossfadeHere();
344 case OverlapInternal:
345 case OverlapExternal:
346 /* should be NOTREACHED because of tests above */
347 throw NoCrossfadeHere();
350 case OverlapEnd: /* top covers start of bottom but ends within it */
352 /* [---- top ------------------------]
353 * { ==== bottom ============ }
358 _position = bottom->first_frame();
359 _anchor_point = StartOfIn;
361 if (model == FullCrossfade) {
362 _length = _out->first_frame() + _out->length() - _in->first_frame();
363 /* leave active alone */
364 _follow_overlap = true;
366 _length = min (short_xfade_length, top->length());
368 _follow_overlap = false;
373 case OverlapStart: /* top starts within bottom but covers bottom's end */
375 /* { ==== top ============ }
376 * [---- bottom -------------------]
381 _position = top->first_frame();
382 _anchor_point = StartOfIn;
384 if (model == FullCrossfade) {
385 _length = _out->first_frame() + _out->length() - _in->first_frame();
386 /* leave active alone */
387 _follow_overlap = true;
389 _length = min (short_xfade_length, top->length());
391 _follow_overlap = false;
403 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
404 float *gain_buffer, nframes_t start, nframes_t cnt, uint32_t chan_n,
405 nframes_t read_frames, nframes_t skip_frames)
414 if (start < _position) {
416 /* handle an initial section of the read area that we do not
420 offset = _position - start;
430 to_write = min (_length, cnt);
434 to_write = min (_length - (start - _position), cnt);
438 offset = start - _position;
440 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
441 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
443 float* fiv = new float[to_write];
444 float* fov = new float[to_write];
446 _fade_in.get_vector (offset, offset+to_write, fiv, to_write);
447 _fade_out.get_vector (offset, offset+to_write, fov, to_write);
449 /* note: although we have not explicitly taken into account the return values
450 from _out->read_at() or _in->read_at(), the length() function does this
451 implicitly. why? because it computes a value based on the in+out regions'
452 position and length, and so we know precisely how much data they could return.
455 for (nframes_t n = 0; n < to_write; ++n) {
456 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
466 Crossfade::coverage (nframes_t start, nframes_t end) const
468 nframes_t my_end = _position + _length;
470 if ((start >= _position) && (end <= my_end)) {
471 return OverlapInternal;
473 if ((end >= _position) && (end <= my_end)) {
476 if ((start >= _position) && (start <= my_end)) {
479 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
480 return OverlapExternal;
486 Crossfade::set_active (bool yn)
490 StateChanged (ActiveChanged);
495 Crossfade::refresh ()
497 /* crossfades must be between non-muted regions */
499 if (_out->muted() || _in->muted()) {
504 /* overlap type must be Start, End or External */
508 ot = _in->coverage (_out->first_frame(), _out->last_frame());
512 case OverlapInternal:
520 /* overlap type must not have altered */
522 if (ot != overlap_type) {
529 return update (false);
533 Crossfade::update (bool force)
537 if (_follow_overlap) {
538 newlen = _out->first_frame() + _out->length() - _in->first_frame();
550 if (force || (_follow_overlap && newlen != _length) || (_length > newlen)) {
552 double factor = newlen / (double) _length;
554 _fade_out.x_scale (factor);
555 _fade_in.x_scale (factor);
561 switch (_anchor_point) {
563 if (_position != _in->first_frame()) {
564 _position = _in->first_frame();
569 if (_position != _in->last_frame() - _length) {
570 _position = _in->last_frame() - _length;
575 if (_position != _out->last_frame() - _length) {
576 _position = _out->last_frame() - _length;
580 /* UI's may need to know that the overlap changed even
581 though the xfade length did not.
584 StateChanged (BoundsChanged); /* EMIT SIGNAL */
592 Crossfade::member_changed (Change what_changed)
594 Change what_we_care_about = Change (Region::MuteChanged|
595 Region::LayerChanged|
598 if (what_changed & what_we_care_about) {
604 Crossfade::get_state ()
606 XMLNode* node = new XMLNode (X_("Crossfade"));
609 LocaleGuard lg (X_("POSIX"));
611 _out->id().print (buf, sizeof (buf));
612 node->add_property ("out", buf);
613 _in->id().print (buf, sizeof (buf));
614 node->add_property ("in", buf);
615 node->add_property ("active", (_active ? "yes" : "no"));
616 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
617 node->add_property ("fixed", (_fixed ? "yes" : "no"));
618 snprintf (buf, sizeof(buf), "%" PRIu32, _length);
619 node->add_property ("length", buf);
620 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
621 node->add_property ("anchor-point", buf);
622 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
623 node->add_property ("position", buf);
625 child = node->add_child ("FadeIn");
627 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
630 pnode = new XMLNode ("point");
632 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
633 pnode->add_property ("x", buf);
634 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
635 pnode->add_property ("y", buf);
636 child->add_child_nocopy (*pnode);
639 child = node->add_child ("FadeOut");
641 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
644 pnode = new XMLNode ("point");
646 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
647 pnode->add_property ("x", buf);
648 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
649 pnode->add_property ("y", buf);
650 child->add_child_nocopy (*pnode);
657 Crossfade::set_state (const XMLNode& node)
659 XMLNodeConstIterator i;
660 XMLNodeList children;
663 const XMLProperty* prop;
664 LocaleGuard lg (X_("POSIX"));
665 Change what_changed = Change (0);
668 if ((prop = node.property ("position")) != 0) {
669 sscanf (prop->value().c_str(), "%" PRIu32, &val);
670 if (val != _position) {
672 what_changed = Change (what_changed | PositionChanged);
675 warning << _("old-style crossfade information - no position information") << endmsg;
676 _position = _in->first_frame();
679 if ((prop = node.property ("active")) != 0) {
680 bool x = (prop->value() == "yes");
683 what_changed = Change (what_changed | ActiveChanged);
689 if ((prop = node.property ("follow-overlap")) != 0) {
690 _follow_overlap = (prop->value() == "yes");
692 _follow_overlap = false;
695 if ((prop = node.property ("fixed")) != 0) {
696 _fixed = (prop->value() == "yes");
701 if ((prop = node.property ("anchor-point")) != 0) {
702 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
704 _anchor_point = StartOfIn;
707 if ((prop = node.property ("length")) != 0) {
709 sscanf (prop->value().c_str(), "%" PRIu32, &val);
710 if (val != _length) {
711 _length = atol (prop->value().c_str());
712 what_changed = Change (what_changed | LengthChanged);
717 /* XXX this branch is legacy code from before
718 the point where we stored xfade lengths.
721 if ((_length = overlap_length()) == 0) {
722 throw failed_constructor();
726 if ((fi = find_named_node (node, "FadeIn")) == 0) {
730 if ((fo = find_named_node (node, "FadeOut")) == 0) {
739 children = fi->children();
741 for (i = children.begin(); i != children.end(); ++i) {
742 if ((*i)->name() == "point") {
746 prop = (*i)->property ("x");
747 sscanf (prop->value().c_str(), "%" PRIu32, &x);
749 prop = (*i)->property ("y");
750 sscanf (prop->value().c_str(), "%f", &y);
763 children = fo->children();
765 for (i = children.begin(); i != children.end(); ++i) {
766 if ((*i)->name() == "point") {
771 prop = (*i)->property ("x");
772 sscanf (prop->value().c_str(), "%" PRIu32, &x);
774 prop = (*i)->property ("y");
775 sscanf (prop->value().c_str(), "%f", &y);
777 _fade_out.add (x, y);
783 StateChanged (what_changed); /* EMIT SIGNAL */
789 Crossfade::can_follow_overlap () const
795 Crossfade::set_follow_overlap (bool yn)
797 if (yn == _follow_overlap || _fixed) {
801 _follow_overlap = yn;
804 set_length (_short_xfade_length);
806 set_length (_out->first_frame() + _out->length() - _in->first_frame());
809 StateChanged (FollowOverlapChanged);
813 Crossfade::set_length (nframes_t len)
817 switch (_anchor_point) {
819 limit = _in->length();
823 limit = _in->length();
827 limit = _out->length();
832 len = min (limit, len);
834 double factor = len / (double) _length;
837 _fade_out.x_scale (factor);
838 _fade_in.x_scale (factor);
843 StateChanged (LengthChanged);
849 Crossfade::overlap_length () const
854 return _out->first_frame() + _out->length() - _in->first_frame();
858 Crossfade::set_short_xfade_length (nframes_t n)
860 _short_xfade_length = n;
864 Crossfade::invalidate ()
866 Invalidated (this); /* EMIT SIGNAL */