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 "ardour/debug.h"
21 #include "ardour/types.h"
22 #include "ardour/crossfade.h"
23 #include "ardour/audioregion.h"
24 #include "ardour/playlist.h"
25 #include "ardour/utils.h"
26 #include "ardour/session.h"
27 #include "ardour/source.h"
28 #include "ardour/region_factory.h"
34 using namespace ARDOUR;
37 framecnt_t Crossfade::_short_xfade_length = 0;
39 /* XXX if and when we ever implement parallel processing of the process()
40 callback, these will need to be handled on a per-thread basis.
43 Sample* Crossfade::crossfade_buffer_out = 0;
44 Sample* Crossfade::crossfade_buffer_in = 0;
47 #define CROSSFADE_DEFAULT_PROPERTIES \
48 _active (Properties::active, _session.config.get_xfades_active ()) \
49 , _follow_overlap (Properties::follow_overlap, false)
53 namespace Properties {
54 PropertyDescriptor<bool> follow_overlap;
59 Crossfade::make_property_quarks ()
61 Properties::follow_overlap.property_id = g_quark_from_static_string (X_("follow-overlap"));
62 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for follow-overlap = %1\n", Properties::follow_overlap.property_id));
66 Crossfade::set_buffer_size (framecnt_t sz)
68 delete [] crossfade_buffer_out;
69 crossfade_buffer_out = 0;
71 delete [] crossfade_buffer_in;
72 crossfade_buffer_in = 0;
75 crossfade_buffer_out = new Sample[sz];
76 crossfade_buffer_in = new Sample[sz];
81 Crossfade::operator== (const Crossfade& other)
83 return (_in == other._in) && (_out == other._out);
86 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out,
89 : AudioRegion (in->session(), 0, length, in->name() + string ("<>") + out->name())
90 , CROSSFADE_DEFAULT_PROPERTIES
91 , _fade_in (Evoral::Parameter(FadeInAutomation)) // linear (gain coefficient) => -inf..+6dB
92 , _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
95 register_properties ();
101 _follow_overlap = false;
106 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
107 : AudioRegion (a->session(), 0, 0, a->name() + string ("<>") + b->name())
108 , CROSSFADE_DEFAULT_PROPERTIES
109 , _fade_in (Evoral::Parameter(FadeInAutomation)) // linear (gain coefficient) => -inf..+6dB
110 , _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
112 register_properties ();
116 _follow_overlap = false;
118 if (compute (a, b, model)) {
119 throw failed_constructor();
127 Crossfade::Crossfade (const Playlist& playlist, XMLNode const & node)
128 : AudioRegion (playlist.session(), 0, 0, "unnamed crossfade")
129 , CROSSFADE_DEFAULT_PROPERTIES
130 , _fade_in (Evoral::Parameter(FadeInAutomation)) // linear (gain coefficient) => -inf..+6dB
131 , _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
134 register_properties ();
136 boost::shared_ptr<Region> r;
137 XMLProperty const * prop;
138 LocaleGuard lg (X_("POSIX"));
140 /* we have to find the in/out regions before we can do anything else */
142 if ((prop = node.property ("in")) == 0) {
143 error << _("Crossfade: no \"in\" region in state") << endmsg;
144 throw failed_constructor();
147 PBD::ID id (prop->value());
149 r = playlist.find_region (id);
152 /* the `in' region is not in a playlist, which probably means that this crossfade
153 is in the undo record, so we have to find the region in the global region map.
155 r = RegionFactory::region_by_id (id);
159 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2 nor in region map"), id, playlist.name())
161 throw failed_constructor();
164 if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
165 throw failed_constructor();
168 if ((prop = node.property ("out")) == 0) {
169 error << _("Crossfade: no \"out\" region in state") << endmsg;
170 throw failed_constructor();
173 PBD::ID id2 (prop->value());
175 r = playlist.find_region (id2);
178 r = RegionFactory::region_by_id (id2);
182 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2 nor in region map"), id2, playlist.name())
184 throw failed_constructor();
187 if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
188 throw failed_constructor();
195 if (set_state (node, Stateful::loading_state_version)) {
196 throw failed_constructor();
200 Crossfade::Crossfade (boost::shared_ptr<Crossfade> orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
201 : AudioRegion (boost::dynamic_pointer_cast<const AudioRegion> (orig), 0)
202 , CROSSFADE_DEFAULT_PROPERTIES
203 , _fade_in (orig->_fade_in)
204 , _fade_out (orig->_fade_out)
206 register_properties ();
208 _active = orig->_active;
209 _in_update = orig->_in_update;
210 _anchor_point = orig->_anchor_point;
211 _follow_overlap = orig->_follow_overlap;
212 _fixed = orig->_fixed;
217 // copied from Crossfade::initialize()
220 _out->suspend_fade_out ();
221 _in->suspend_fade_in ();
223 overlap_type = _in->coverage (_out->position(), _out->last_frame());
224 layer_relation = (int32_t) (_in->layer() - _out->layer());
226 // Let's make sure the fade isn't too long
227 set_xfade_length(_length);
231 Crossfade::~Crossfade ()
236 Crossfade::register_properties ()
238 add_property (_active);
239 add_property (_follow_overlap);
243 Crossfade::initialize ()
245 /* merge source lists from regions */
247 _sources = _in->sources();
248 _sources.insert (_sources.end(), _out->sources().begin(), _out->sources().end());
250 for (SourceList::iterator i = _sources.begin(); i != _sources.end(); ++i) {
251 (*i)->inc_use_count ();
254 _master_sources = _in->master_sources();
255 _master_sources.insert(_master_sources.end(), _out->master_sources().begin(), _out->master_sources().end());
257 for (SourceList::iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
258 (*i)->inc_use_count ();
263 _out->suspend_fade_out ();
264 _in->suspend_fade_in ();
269 #define EQUAL_POWER_MINUS_3DB
270 #ifdef EQUAL_POWER_MINUS_3DB
272 _fade_out.add ((_length * 0.000000), 1.000000);
273 _fade_out.add ((_length * 0.166667), 0.948859);
274 _fade_out.add ((_length * 0.333333), 0.851507);
275 _fade_out.add ((_length * 0.500000), 0.707946);
276 _fade_out.add ((_length * 0.666667), 0.518174);
277 _fade_out.add ((_length * 0.833333), 0.282192);
278 _fade_out.add ((_length * 1.000000), 0.000000);
280 #else // EQUAL_POWER_MINUS_6DB
282 _fade_out.add ((_length * 0.000000), 1.000000);
283 _fade_out.add ((_length * 0.166667), 0.833033);
284 _fade_out.add ((_length * 0.333333), 0.666186);
285 _fade_out.add ((_length * 0.500000), 0.499459);
286 _fade_out.add ((_length * 0.666667), 0.332853);
287 _fade_out.add ((_length * 0.833333), 0.166366);
288 _fade_out.add ((_length * 1.000000), 0.000000);
296 #define EQUAL_POWER_MINUS_3DB
297 #ifdef EQUAL_POWER_MINUS_3DB
299 _fade_in.add ((_length * 0.000000), 0.000000);
300 _fade_in.add ((_length * 0.166667), 0.282192);
301 _fade_in.add ((_length * 0.333333), 0.518174);
302 _fade_in.add ((_length * 0.500000), 0.707946);
303 _fade_in.add ((_length * 0.666667), 0.851507);
304 _fade_in.add ((_length * 0.833333), 0.948859);
305 _fade_in.add ((_length * 1.000000), 1.000000);
307 #else // EQUAL_POWER_MINUS_SIX_DB
309 _fade_in.add ((_length * 0.000000), 0.000000);
310 _fade_in.add ((_length * 0.166667), 0.166366);
311 _fade_in.add ((_length * 0.333333), 0.332853);
312 _fade_in.add ((_length * 0.500000), 0.499459);
313 _fade_in.add ((_length * 0.666667), 0.666186);
314 _fade_in.add ((_length * 0.833333), 0.833033);
315 _fade_in.add ((_length * 1.000000), 1.000000);
321 overlap_type = _in->coverage (_out->position(), _out->last_frame());
322 layer_relation = (int32_t) (_in->layer() - _out->layer());
326 Crossfade::read_raw_internal (Sample* buf, framecnt_t start, framecnt_t cnt, int channel) const
328 Sample* mixdown = new Sample[cnt];
329 float* gain = new float[cnt];
332 ret = read_at (buf, mixdown, gain, start, cnt, channel);
341 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
342 float *gain_buffer, framepos_t start, framecnt_t cnt, uint32_t chan_n) const
344 frameoffset_t offset;
351 if (start < _position) {
353 /* handle an initial section of the read area that we do not
357 offset = _position - start;
367 to_write = min (_length.val(), cnt);
371 to_write = min ((_length - (start - _position)), cnt);
375 offset = start - _position;
377 /* Prevent data from piling up inthe crossfade buffers when reading a transparent region */
378 if (!(_out->opaque())) {
379 memset (crossfade_buffer_out, 0, sizeof (Sample) * to_write);
380 } else if (!(_in->opaque())) {
381 memset (crossfade_buffer_in, 0, sizeof (Sample) * to_write);
384 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n);
385 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n);
387 float* fiv = new float[to_write];
388 float* fov = new float[to_write];
390 _fade_in.curve().get_vector (offset, offset+to_write, fiv, to_write);
391 _fade_out.curve().get_vector (offset, offset+to_write, fov, to_write);
393 /* note: although we have not explicitly taken into account the return values
394 from _out->read_at() or _in->read_at(), the length() function does this
395 implicitly. why? because it computes a value based on the in+out regions'
396 position and length, and so we know precisely how much data they could return.
399 for (framecnt_t n = 0; n < to_write; ++n) {
400 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
410 Crossfade::coverage (framepos_t start, framepos_t end) const
412 framepos_t my_end = _position + _length;
414 if ((start >= _position) && (end <= my_end)) {
415 return OverlapInternal;
417 if ((end >= _position) && (end <= my_end)) {
420 if ((start >= _position) && (start <= my_end)) {
423 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
424 return OverlapExternal;
430 Crossfade::set_active (bool yn)
434 PropertyChanged (PropertyChange (Properties::active));
439 Crossfade::refresh ()
441 /* crossfades must be between non-muted regions */
443 if (_out->muted() || _in->muted()) {
444 Invalidated (shared_from_this ());
448 /* Top layer shouldn't be transparent */
450 if (!((layer_relation > 0 ? _in : _out)->opaque())) {
451 Invalidated (shared_from_this());
455 /* regions must cannot be identically sized and placed */
457 if (_in->position() == _out->position() && _in->length() == _out->length()) {
458 Invalidated (shared_from_this());
462 /* layer ordering cannot change */
464 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
466 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
467 Invalidated (shared_from_this ());
471 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
473 if (ot == OverlapNone) {
474 Invalidated (shared_from_this ());
480 if (ot != overlap_type) {
482 if (_follow_overlap) {
485 compute (_in, _out, _session.config.get_xfade_model());
488 catch (NoCrossfadeHere& err) {
489 Invalidated (shared_from_this ());
496 Invalidated (shared_from_this ());
502 send_signal = update ();
506 PropertyChange bounds;
507 bounds.add (Properties::start);
508 bounds.add (Properties::position);
509 bounds.add (Properties::length);
510 PropertyChanged (bounds); /* EMIT SIGNAL */
523 if (_follow_overlap) {
524 newlen = _out->first_frame() + _out->length() - _in->first_frame();
530 Invalidated (shared_from_this ());
536 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
538 double factor = newlen / (double) _length;
540 _fade_out.x_scale (factor);
541 _fade_in.x_scale (factor);
546 switch (_anchor_point) {
548 _position = _in->first_frame();
552 _position = _in->last_frame() - _length;
556 _position = _out->last_frame() - _length;
563 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
565 boost::shared_ptr<AudioRegion> top;
566 boost::shared_ptr<AudioRegion> bottom;
567 framecnt_t short_xfade_length;
569 short_xfade_length = _short_xfade_length;
571 if (a->layer() < b->layer()) {
579 /* first check for matching ends */
581 if (top->first_frame() == bottom->first_frame()) {
583 /* Both regions start at the same point */
585 if (top->last_frame() < bottom->last_frame()) {
587 /* top ends before bottom, so put an xfade
588 in at the end of top.
591 /* [-------- top ---------- ]
592 * {====== bottom =====================}
598 if (top->last_frame() < short_xfade_length) {
601 _position = top->last_frame() - short_xfade_length;
604 set_xfade_length (min (short_xfade_length, top->length()));
605 _follow_overlap = false;
606 _anchor_point = EndOfIn;
611 /* top ends after (or same time) as bottom - no xfade
614 /* [-------- top ------------------------ ]
615 * {====== bottom =====================}
618 throw NoCrossfadeHere();
621 } else if (top->last_frame() == bottom->last_frame()) {
623 /* Both regions end at the same point */
625 if (top->first_frame() > bottom->first_frame()) {
627 /* top starts after bottom, put an xfade in at the
631 /* [-------- top ---------- ]
632 * {====== bottom =====================}
637 _position = top->first_frame();
638 set_xfade_length (min (short_xfade_length, top->length()));
639 _follow_overlap = false;
640 _anchor_point = StartOfIn;
645 /* top starts before bottom - no xfade
648 /* [-------- top ------------------------ ]
649 * {====== bottom =====================}
652 throw NoCrossfadeHere();
657 /* OK, time to do more regular overlapping */
659 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
663 /* should be NOTREACHED as a precondition of creating
664 a new crossfade, but we need to handle it here.
666 throw NoCrossfadeHere();
669 case OverlapInternal:
670 case OverlapExternal:
671 /* should be NOTREACHED because of tests above */
672 throw NoCrossfadeHere();
675 case OverlapEnd: /* top covers start of bottom but ends within it */
677 /* [---- top ------------------------]
678 * { ==== bottom ============ }
683 _anchor_point = EndOfOut;
685 if (model == FullCrossfade) {
686 _position = bottom->first_frame(); // "{"
687 set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
688 /* leave active alone */
689 _follow_overlap = true;
691 set_xfade_length (min (short_xfade_length, top->length()));
692 _position = top->last_frame() - _length; // "]" - length
694 _follow_overlap = false;
699 case OverlapStart: /* top starts within bottom but covers bottom's end */
701 /* { ==== top ============ }
702 * [---- bottom -------------------]
707 _position = top->first_frame();
708 _anchor_point = StartOfIn;
710 if (model == FullCrossfade) {
711 set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
712 /* leave active alone */
713 _follow_overlap = true;
715 set_xfade_length (min (short_xfade_length, top->length()));
717 _follow_overlap = false;
729 Crossfade::get_state ()
731 XMLNode* node = new XMLNode (X_("Crossfade"));
734 LocaleGuard lg (X_("POSIX"));
736 id().print (buf, sizeof (buf));
737 node->add_property ("id", buf);
738 _out->id().print (buf, sizeof (buf));
739 node->add_property ("out", buf);
740 _in->id().print (buf, sizeof (buf));
741 node->add_property ("in", buf);
742 node->add_property ("active", (_active ? "yes" : "no"));
743 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
744 node->add_property ("fixed", (_fixed ? "yes" : "no"));
745 snprintf (buf, sizeof(buf), "%" PRId64, _length.val());
746 node->add_property ("length", buf);
747 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
748 node->add_property ("anchor-point", buf);
749 snprintf (buf, sizeof(buf), "%" PRId64, _position.val());
750 node->add_property ("position", buf);
752 child = node->add_child ("FadeIn");
754 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
757 pnode = new XMLNode ("point");
759 snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
760 pnode->add_property ("x", buf);
761 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
762 pnode->add_property ("y", buf);
763 child->add_child_nocopy (*pnode);
766 child = node->add_child ("FadeOut");
768 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
771 pnode = new XMLNode ("point");
773 snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
774 pnode->add_property ("x", buf);
775 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
776 pnode->add_property ("y", buf);
777 child->add_child_nocopy (*pnode);
784 Crossfade::set_state (const XMLNode& node, int /*version*/)
786 XMLNodeConstIterator i;
787 XMLNodeList children;
790 const XMLProperty* prop;
791 LocaleGuard lg (X_("POSIX"));
792 PropertyChange what_changed;
797 if ((prop = node.property ("position")) != 0) {
798 sscanf (prop->value().c_str(), "%" PRId64, &val);
799 if (val != _position) {
801 what_changed.add (Properties::position);
804 warning << _("old-style crossfade information - no position information") << endmsg;
805 _position = _in->first_frame();
808 if ((prop = node.property ("active")) != 0) {
809 bool x = string_is_affirmative (prop->value());
812 what_changed.add (Properties::active);
818 if ((prop = node.property ("follow-overlap")) != 0) {
819 _follow_overlap = string_is_affirmative (prop->value());
821 _follow_overlap = false;
824 if ((prop = node.property ("fixed")) != 0) {
825 _fixed = string_is_affirmative (prop->value());
830 if ((prop = node.property ("anchor-point")) != 0) {
831 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
833 _anchor_point = StartOfIn;
836 if ((prop = node.property ("length")) != 0) {
838 sscanf (prop->value().c_str(), "%" PRId64, &val);
839 if (val != _length) {
841 what_changed.add (Properties::length);
846 /* XXX this branch is legacy code from before
847 the point where we stored xfade lengths.
850 if ((_length = overlap_length()) == 0) {
851 throw failed_constructor();
855 if ((fi = find_named_node (node, "FadeIn")) == 0) {
859 if ((fo = find_named_node (node, "FadeOut")) == 0) {
868 children = fi->children();
870 for (i = children.begin(); i != children.end(); ++i) {
871 if ((*i)->name() == "point") {
875 prop = (*i)->property ("x");
876 sscanf (prop->value().c_str(), "%" PRId64, &x);
878 prop = (*i)->property ("y");
879 sscanf (prop->value().c_str(), "%f", &y);
885 if (_fade_in.size() < 2) {
886 /* fade state somehow saved with no points */
890 _fade_in.front()->value = 0.0;
891 _fade_in.back()->value = 1.0;
900 children = fo->children();
902 for (i = children.begin(); i != children.end(); ++i) {
903 if ((*i)->name() == "point") {
908 prop = (*i)->property ("x");
909 sscanf (prop->value().c_str(), "%" PRId64, &x);
911 prop = (*i)->property ("y");
912 sscanf (prop->value().c_str(), "%f", &y);
914 _fade_out.add (x, y);
918 if (_fade_out.size() < 2) {
919 /* fade state somehow saved with no points */
923 _fade_out.front()->value = 1.0;
924 _fade_out.back()->value = 0.0;
928 PropertyChanged (what_changed); /* EMIT SIGNAL */
929 FadesChanged (); /* EMIT SIGNAL */
935 Crossfade::can_follow_overlap () const
941 Crossfade::set_follow_overlap (bool yn)
943 if (yn == _follow_overlap || _fixed) {
947 _follow_overlap = yn;
950 set_xfade_length (_short_xfade_length);
952 set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
955 PropertyChanged (PropertyChange (Properties::follow_overlap));
959 Crossfade::set_xfade_length (framecnt_t len)
961 framecnt_t limit = 0;
963 switch (_anchor_point) {
965 limit = _in->length();
969 limit = _in->length();
973 limit = _out->length();
978 len = min (limit, len);
980 double factor = len / (double) _length;
983 _fade_out.x_scale (factor);
984 _fade_in.x_scale (factor);
989 PropertyChanged (PropertyChange (Properties::length));
995 Crossfade::overlap_length () const
1000 return _out->first_frame() + _out->length() - _in->first_frame();
1004 Crossfade::set_short_xfade_length (framecnt_t n)
1006 _short_xfade_length = n;