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 "pbd/stacktrace.h"
23 #include "ardour/debug.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"
32 #include "ardour/region_factory.h"
38 using namespace ARDOUR;
41 framecnt_t Crossfade::_short_xfade_length = 0;
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 #define CROSSFADE_DEFAULT_PROPERTIES \
52 _active (Properties::active, _session.config.get_xfades_active ()) \
53 , _follow_overlap (Properties::follow_overlap, false)
57 namespace Properties {
58 PropertyDescriptor<bool> follow_overlap;
63 Crossfade::make_property_quarks ()
65 Properties::follow_overlap.property_id = g_quark_from_static_string (X_("follow-overlap"));
66 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for follow-overlap = %1\n", Properties::follow_overlap.property_id));
70 Crossfade::set_buffer_size (framecnt_t sz)
72 delete [] crossfade_buffer_out;
73 crossfade_buffer_out = 0;
75 delete [] crossfade_buffer_in;
76 crossfade_buffer_in = 0;
79 crossfade_buffer_out = new Sample[sz];
80 crossfade_buffer_in = new Sample[sz];
85 Crossfade::operator== (const Crossfade& other)
87 return (_in == other._in) && (_out == other._out);
90 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out,
94 : AudioRegion (in->session(), position, length, in->name() + string ("<>") + out->name())
95 , CROSSFADE_DEFAULT_PROPERTIES
96 , _fade_in (Evoral::Parameter(FadeInAutomation)) // linear (gain coefficient) => -inf..+6dB
97 , _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
104 _follow_overlap = false;
109 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
110 : AudioRegion (a->session(), 0, 0, a->name() + string ("<>") + b->name())
111 , CROSSFADE_DEFAULT_PROPERTIES
112 , _fade_in (Evoral::Parameter(FadeInAutomation)) // linear (gain coefficient) => -inf..+6dB
113 , _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
117 _follow_overlap = false;
119 if (compute (a, b, model)) {
120 throw failed_constructor();
128 Crossfade::Crossfade (const Playlist& playlist, XMLNode const & node)
129 : AudioRegion (playlist.session(), 0, 0, "unnamed crossfade")
130 , CROSSFADE_DEFAULT_PROPERTIES
131 , _fade_in (Evoral::Parameter(FadeInAutomation)) // linear (gain coefficient) => -inf..+6dB
132 , _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
135 boost::shared_ptr<Region> r;
136 XMLProperty const * prop;
137 LocaleGuard lg (X_("POSIX"));
139 /* we have to find the in/out regions before we can do anything else */
141 if ((prop = node.property ("in")) == 0) {
142 error << _("Crossfade: no \"in\" region in state") << endmsg;
143 throw failed_constructor();
146 PBD::ID id (prop->value());
148 r = playlist.find_region (id);
151 /* the `in' region is not in a playlist, which probably means that this crossfade
152 is in the undo record, so we have to find the region in the global region map.
154 r = RegionFactory::region_by_id (id);
158 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2 nor in region map"), id, playlist.name())
160 throw failed_constructor();
163 if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
164 throw failed_constructor();
167 if ((prop = node.property ("out")) == 0) {
168 error << _("Crossfade: no \"out\" region in state") << endmsg;
169 throw failed_constructor();
172 PBD::ID id2 (prop->value());
174 r = playlist.find_region (id2);
177 r = RegionFactory::region_by_id (id2);
181 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2 nor in region map"), id2, playlist.name())
183 throw failed_constructor();
186 if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
187 throw failed_constructor();
194 if (set_state (node, Stateful::loading_state_version)) {
195 throw failed_constructor();
199 Crossfade::Crossfade (boost::shared_ptr<Crossfade> orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
200 : AudioRegion (boost::dynamic_pointer_cast<const AudioRegion> (orig), 0, true)
201 , CROSSFADE_DEFAULT_PROPERTIES
202 , _fade_in (orig->_fade_in)
203 , _fade_out (orig->_fade_out)
205 _active = orig->_active;
206 _in_update = orig->_in_update;
207 _anchor_point = orig->_anchor_point;
208 _follow_overlap = orig->_follow_overlap;
209 _fixed = orig->_fixed;
214 // copied from Crossfade::initialize()
217 _out->suspend_fade_out ();
218 _in->suspend_fade_in ();
220 overlap_type = _in->coverage (_out->position(), _out->last_frame());
221 layer_relation = (int32_t) (_in->layer() - _out->layer());
223 // Let's make sure the fade isn't too long
224 set_xfade_length(_length);
228 Crossfade::~Crossfade ()
233 Crossfade::initialize ()
235 /* merge source lists from regions */
237 _sources = _in->sources();
238 _sources.insert (_sources.end(), _out->sources().begin(), _out->sources().end());
240 for (SourceList::iterator i = _sources.begin(); i != _sources.end(); ++i) {
241 (*i)->inc_use_count ();
244 _master_sources = _in->master_sources();
245 _master_sources.insert(_master_sources.end(), _out->master_sources().begin(), _out->master_sources().end());
247 for (SourceList::iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
248 (*i)->inc_use_count ();
253 _out->suspend_fade_out ();
254 _in->suspend_fade_in ();
259 #define EQUAL_POWER_MINUS_3DB
260 #ifdef EQUAL_POWER_MINUS_3DB
262 _fade_out.add ((_length * 0.000000), 1.000000);
263 _fade_out.add ((_length * 0.166667), 0.948859);
264 _fade_out.add ((_length * 0.333333), 0.851507);
265 _fade_out.add ((_length * 0.500000), 0.707946);
266 _fade_out.add ((_length * 0.666667), 0.518174);
267 _fade_out.add ((_length * 0.833333), 0.282192);
268 _fade_out.add ((_length * 1.000000), 0.000000);
270 #else // EQUAL_POWER_MINUS_6DB
272 _fade_out.add ((_length * 0.000000), 1.000000);
273 _fade_out.add ((_length * 0.166667), 0.833033);
274 _fade_out.add ((_length * 0.333333), 0.666186);
275 _fade_out.add ((_length * 0.500000), 0.499459);
276 _fade_out.add ((_length * 0.666667), 0.332853);
277 _fade_out.add ((_length * 0.833333), 0.166366);
278 _fade_out.add ((_length * 1.000000), 0.000000);
286 #define EQUAL_POWER_MINUS_3DB
287 #ifdef EQUAL_POWER_MINUS_3DB
289 _fade_in.add ((_length * 0.000000), 0.000000);
290 _fade_in.add ((_length * 0.166667), 0.282192);
291 _fade_in.add ((_length * 0.333333), 0.518174);
292 _fade_in.add ((_length * 0.500000), 0.707946);
293 _fade_in.add ((_length * 0.666667), 0.851507);
294 _fade_in.add ((_length * 0.833333), 0.948859);
295 _fade_in.add ((_length * 1.000000), 1.000000);
297 #else // EQUAL_POWER_MINUS_SIX_DB
299 _fade_in.add ((_length * 0.000000), 0.000000);
300 _fade_in.add ((_length * 0.166667), 0.166366);
301 _fade_in.add ((_length * 0.333333), 0.332853);
302 _fade_in.add ((_length * 0.500000), 0.499459);
303 _fade_in.add ((_length * 0.666667), 0.666186);
304 _fade_in.add ((_length * 0.833333), 0.833033);
305 _fade_in.add ((_length * 1.000000), 1.000000);
311 overlap_type = _in->coverage (_out->position(), _out->last_frame());
312 layer_relation = (int32_t) (_in->layer() - _out->layer());
316 Crossfade::read_raw_internal (Sample* buf, framecnt_t start, framecnt_t cnt, int channel) const
318 Sample* mixdown = new Sample[cnt];
319 float* gain = new float[cnt];
322 ret = read_at (buf, mixdown, gain, start, cnt, channel, cnt);
331 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
332 float *gain_buffer, framepos_t start, framecnt_t cnt, uint32_t chan_n,
333 framecnt_t read_frames, framecnt_t skip_frames) const
335 frameoffset_t offset;
342 if (start < _position) {
344 /* handle an initial section of the read area that we do not
348 offset = _position - start;
358 to_write = min (_length.val(), (nframes64_t) cnt);
362 to_write = min ((_length - (start - _position)), cnt);
366 offset = start - _position;
368 /* Prevent data from piling up inthe crossfade buffers when reading a transparent region */
369 if (!(_out->opaque())) {
370 memset (crossfade_buffer_out, 0, sizeof (Sample) * to_write);
371 } else if (!(_in->opaque())) {
372 memset (crossfade_buffer_in, 0, sizeof (Sample) * to_write);
375 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
376 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
378 float* fiv = new float[to_write];
379 float* fov = new float[to_write];
381 _fade_in.curve().get_vector (offset, offset+to_write, fiv, to_write);
382 _fade_out.curve().get_vector (offset, offset+to_write, fov, to_write);
384 /* note: although we have not explicitly taken into account the return values
385 from _out->read_at() or _in->read_at(), the length() function does this
386 implicitly. why? because it computes a value based on the in+out regions'
387 position and length, and so we know precisely how much data they could return.
390 for (framecnt_t n = 0; n < to_write; ++n) {
391 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
401 Crossfade::coverage (framepos_t start, framepos_t end) const
403 framepos_t my_end = _position + _length;
405 if ((start >= _position) && (end <= my_end)) {
406 return OverlapInternal;
408 if ((end >= _position) && (end <= my_end)) {
411 if ((start >= _position) && (start <= my_end)) {
414 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
415 return OverlapExternal;
421 Crossfade::set_active (bool yn)
425 PropertyChanged (PropertyChange (Properties::active));
430 Crossfade::refresh ()
432 /* crossfades must be between non-muted regions */
434 if (_out->muted() || _in->muted()) {
435 Invalidated (shared_from_this ());
439 /* Top layer shouldn't be transparent */
441 if (!((layer_relation > 0 ? _in : _out)->opaque())) {
442 Invalidated (shared_from_this());
446 /* layer ordering cannot change */
448 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
450 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
451 Invalidated (shared_from_this ());
455 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
457 if (ot == OverlapNone) {
458 Invalidated (shared_from_this ());
464 if (ot != overlap_type) {
466 if (_follow_overlap) {
469 compute (_in, _out, _session.config.get_xfade_model());
472 catch (NoCrossfadeHere& err) {
473 Invalidated (shared_from_this ());
480 Invalidated (shared_from_this ());
486 send_signal = update ();
490 PropertyChange bounds;
491 bounds.add (Properties::start);
492 bounds.add (Properties::position);
493 bounds.add (Properties::length);
494 PropertyChanged (bounds); /* EMIT SIGNAL */
507 if (_follow_overlap) {
508 newlen = _out->first_frame() + _out->length() - _in->first_frame();
514 Invalidated (shared_from_this ());
520 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
522 double factor = newlen / (double) _length;
524 _fade_out.x_scale (factor);
525 _fade_in.x_scale (factor);
530 switch (_anchor_point) {
532 _position = _in->first_frame();
536 _position = _in->last_frame() - _length;
540 _position = _out->last_frame() - _length;
547 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
549 boost::shared_ptr<AudioRegion> top;
550 boost::shared_ptr<AudioRegion> bottom;
551 framecnt_t short_xfade_length;
553 short_xfade_length = _short_xfade_length;
555 if (a->layer() < b->layer()) {
563 /* first check for matching ends */
565 if (top->first_frame() == bottom->first_frame()) {
567 /* Both regions start at the same point */
569 if (top->last_frame() < bottom->last_frame()) {
571 /* top ends before bottom, so put an xfade
572 in at the end of top.
575 /* [-------- top ---------- ]
576 * {====== bottom =====================}
582 if (top->last_frame() < short_xfade_length) {
585 _position = top->last_frame() - short_xfade_length;
588 _length = min (short_xfade_length, top->length());
589 _follow_overlap = false;
590 _anchor_point = EndOfIn;
595 /* top ends after (or same time) as bottom - no xfade
598 /* [-------- top ------------------------ ]
599 * {====== bottom =====================}
602 throw NoCrossfadeHere();
605 } else if (top->last_frame() == bottom->last_frame()) {
607 /* Both regions end at the same point */
609 if (top->first_frame() > bottom->first_frame()) {
611 /* top starts after bottom, put an xfade in at the
615 /* [-------- top ---------- ]
616 * {====== bottom =====================}
621 _position = top->first_frame();
622 _length = min (short_xfade_length, top->length());
623 _follow_overlap = false;
624 _anchor_point = StartOfIn;
629 /* top starts before bottom - no xfade
632 /* [-------- top ------------------------ ]
633 * {====== bottom =====================}
636 throw NoCrossfadeHere();
641 /* OK, time to do more regular overlapping */
643 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
647 /* should be NOTREACHED as a precondition of creating
648 a new crossfade, but we need to handle it here.
650 throw NoCrossfadeHere();
653 case OverlapInternal:
654 case OverlapExternal:
655 /* should be NOTREACHED because of tests above */
656 throw NoCrossfadeHere();
659 case OverlapEnd: /* top covers start of bottom but ends within it */
661 /* [---- top ------------------------]
662 * { ==== bottom ============ }
667 _anchor_point = EndOfOut;
669 if (model == FullCrossfade) {
670 _position = bottom->first_frame(); // "{"
671 _length = _out->first_frame() + _out->length() - _in->first_frame();
672 /* leave active alone */
673 _follow_overlap = true;
675 _length = min (short_xfade_length, top->length());
676 _position = top->last_frame() - _length; // "]" - length
678 _follow_overlap = false;
683 case OverlapStart: /* top starts within bottom but covers bottom's end */
685 /* { ==== top ============ }
686 * [---- bottom -------------------]
691 _position = top->first_frame();
692 _anchor_point = StartOfIn;
694 if (model == FullCrossfade) {
695 _length = _out->first_frame() + _out->length() - _in->first_frame();
696 /* leave active alone */
697 _follow_overlap = true;
699 _length = min (short_xfade_length, top->length());
701 _follow_overlap = false;
713 Crossfade::get_state ()
715 XMLNode* node = new XMLNode (X_("Crossfade"));
718 LocaleGuard lg (X_("POSIX"));
720 _out->id().print (buf, sizeof (buf));
721 node->add_property ("out", buf);
722 _in->id().print (buf, sizeof (buf));
723 node->add_property ("in", buf);
724 node->add_property ("active", (_active ? "yes" : "no"));
725 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
726 node->add_property ("fixed", (_fixed ? "yes" : "no"));
727 snprintf (buf, sizeof(buf), "%" PRId64, _length.val());
728 node->add_property ("length", buf);
729 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
730 node->add_property ("anchor-point", buf);
731 snprintf (buf, sizeof(buf), "%" PRId64, _position.val());
732 node->add_property ("position", buf);
734 child = node->add_child ("FadeIn");
736 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
739 pnode = new XMLNode ("point");
741 snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
742 pnode->add_property ("x", buf);
743 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
744 pnode->add_property ("y", buf);
745 child->add_child_nocopy (*pnode);
748 child = node->add_child ("FadeOut");
750 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
753 pnode = new XMLNode ("point");
755 snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
756 pnode->add_property ("x", buf);
757 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
758 pnode->add_property ("y", buf);
759 child->add_child_nocopy (*pnode);
766 Crossfade::set_state (const XMLNode& node, int /*version*/)
768 XMLNodeConstIterator i;
769 XMLNodeList children;
772 const XMLProperty* prop;
773 LocaleGuard lg (X_("POSIX"));
774 PropertyChange what_changed;
777 if ((prop = node.property ("position")) != 0) {
778 sscanf (prop->value().c_str(), "%" PRId64, &val);
779 if (val != _position) {
781 what_changed.add (Properties::position);
784 warning << _("old-style crossfade information - no position information") << endmsg;
785 _position = _in->first_frame();
788 if ((prop = node.property ("active")) != 0) {
789 bool x = string_is_affirmative (prop->value());
792 what_changed.add (Properties::active);
798 if ((prop = node.property ("follow-overlap")) != 0) {
799 _follow_overlap = string_is_affirmative (prop->value());
801 _follow_overlap = false;
804 if ((prop = node.property ("fixed")) != 0) {
805 _fixed = string_is_affirmative (prop->value());
810 if ((prop = node.property ("anchor-point")) != 0) {
811 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
813 _anchor_point = StartOfIn;
816 if ((prop = node.property ("length")) != 0) {
818 sscanf (prop->value().c_str(), "%" PRId64, &val);
819 if (val != _length) {
821 what_changed.add (Properties::length);
826 /* XXX this branch is legacy code from before
827 the point where we stored xfade lengths.
830 if ((_length = overlap_length()) == 0) {
831 throw failed_constructor();
835 if ((fi = find_named_node (node, "FadeIn")) == 0) {
839 if ((fo = find_named_node (node, "FadeOut")) == 0) {
848 children = fi->children();
850 for (i = children.begin(); i != children.end(); ++i) {
851 if ((*i)->name() == "point") {
855 prop = (*i)->property ("x");
856 sscanf (prop->value().c_str(), "%" PRId64, &x);
858 prop = (*i)->property ("y");
859 sscanf (prop->value().c_str(), "%f", &y);
865 _fade_in.front()->value = 0.0;
866 _fade_in.back()->value = 1.0;
875 children = fo->children();
877 for (i = children.begin(); i != children.end(); ++i) {
878 if ((*i)->name() == "point") {
883 prop = (*i)->property ("x");
884 sscanf (prop->value().c_str(), "%" PRId64, &x);
886 prop = (*i)->property ("y");
887 sscanf (prop->value().c_str(), "%f", &y);
889 _fade_out.add (x, y);
893 _fade_out.front()->value = 1.0;
894 _fade_out.back()->value = 0.0;
898 PropertyChanged (what_changed); /* EMIT SIGNAL */
899 FadesChanged (); /* EMIT SIGNAL */
905 Crossfade::can_follow_overlap () const
911 Crossfade::set_follow_overlap (bool yn)
913 if (yn == _follow_overlap || _fixed) {
917 _follow_overlap = yn;
920 set_xfade_length (_short_xfade_length);
922 set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
925 PropertyChanged (PropertyChange (Properties::follow_overlap));
929 Crossfade::set_xfade_length (framecnt_t len)
931 framecnt_t limit = 0;
933 switch (_anchor_point) {
935 limit = _in->length();
939 limit = _in->length();
943 limit = _out->length();
948 len = min (limit, len);
950 double factor = len / (double) _length;
953 _fade_out.x_scale (factor);
954 _fade_in.x_scale (factor);
959 PropertyChanged (PropertyChange (Properties::length));
965 Crossfade::overlap_length () const
970 return _out->first_frame() + _out->length() - _in->first_frame();
974 Crossfade::set_short_xfade_length (framecnt_t n)
976 _short_xfade_length = n;