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,
90 : AudioRegion (in->session(), position, length, in->name() + string ("<>") + out->name())
91 , CROSSFADE_DEFAULT_PROPERTIES
92 , _fade_in (Evoral::Parameter(FadeInAutomation)) // linear (gain coefficient) => -inf..+6dB
93 , _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
100 _follow_overlap = false;
105 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
106 : AudioRegion (a->session(), 0, 0, a->name() + string ("<>") + b->name())
107 , CROSSFADE_DEFAULT_PROPERTIES
108 , _fade_in (Evoral::Parameter(FadeInAutomation)) // linear (gain coefficient) => -inf..+6dB
109 , _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
113 _follow_overlap = false;
115 if (compute (a, b, model)) {
116 throw failed_constructor();
124 Crossfade::Crossfade (const Playlist& playlist, XMLNode const & node)
125 : AudioRegion (playlist.session(), 0, 0, "unnamed crossfade")
126 , CROSSFADE_DEFAULT_PROPERTIES
127 , _fade_in (Evoral::Parameter(FadeInAutomation)) // linear (gain coefficient) => -inf..+6dB
128 , _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
131 boost::shared_ptr<Region> r;
132 XMLProperty const * prop;
133 LocaleGuard lg (X_("POSIX"));
135 /* we have to find the in/out regions before we can do anything else */
137 if ((prop = node.property ("in")) == 0) {
138 error << _("Crossfade: no \"in\" region in state") << endmsg;
139 throw failed_constructor();
142 PBD::ID id (prop->value());
144 r = playlist.find_region (id);
147 /* the `in' region is not in a playlist, which probably means that this crossfade
148 is in the undo record, so we have to find the region in the global region map.
150 r = RegionFactory::region_by_id (id);
154 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2 nor in region map"), id, playlist.name())
156 throw failed_constructor();
159 if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
160 throw failed_constructor();
163 if ((prop = node.property ("out")) == 0) {
164 error << _("Crossfade: no \"out\" region in state") << endmsg;
165 throw failed_constructor();
168 PBD::ID id2 (prop->value());
170 r = playlist.find_region (id2);
173 r = RegionFactory::region_by_id (id2);
177 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2 nor in region map"), id2, playlist.name())
179 throw failed_constructor();
182 if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
183 throw failed_constructor();
190 if (set_state (node, Stateful::loading_state_version)) {
191 throw failed_constructor();
195 Crossfade::Crossfade (boost::shared_ptr<Crossfade> orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
196 : AudioRegion (boost::dynamic_pointer_cast<const AudioRegion> (orig), 0)
197 , CROSSFADE_DEFAULT_PROPERTIES
198 , _fade_in (orig->_fade_in)
199 , _fade_out (orig->_fade_out)
201 _active = orig->_active;
202 _in_update = orig->_in_update;
203 _anchor_point = orig->_anchor_point;
204 _follow_overlap = orig->_follow_overlap;
205 _fixed = orig->_fixed;
210 // copied from Crossfade::initialize()
213 _out->suspend_fade_out ();
214 _in->suspend_fade_in ();
216 overlap_type = _in->coverage (_out->position(), _out->last_frame());
217 layer_relation = (int32_t) (_in->layer() - _out->layer());
219 // Let's make sure the fade isn't too long
220 set_xfade_length(_length);
224 Crossfade::~Crossfade ()
229 Crossfade::initialize ()
231 /* merge source lists from regions */
233 _sources = _in->sources();
234 _sources.insert (_sources.end(), _out->sources().begin(), _out->sources().end());
236 for (SourceList::iterator i = _sources.begin(); i != _sources.end(); ++i) {
237 (*i)->inc_use_count ();
240 _master_sources = _in->master_sources();
241 _master_sources.insert(_master_sources.end(), _out->master_sources().begin(), _out->master_sources().end());
243 for (SourceList::iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
244 (*i)->inc_use_count ();
249 _out->suspend_fade_out ();
250 _in->suspend_fade_in ();
255 #define EQUAL_POWER_MINUS_3DB
256 #ifdef EQUAL_POWER_MINUS_3DB
258 _fade_out.add ((_length * 0.000000), 1.000000);
259 _fade_out.add ((_length * 0.166667), 0.948859);
260 _fade_out.add ((_length * 0.333333), 0.851507);
261 _fade_out.add ((_length * 0.500000), 0.707946);
262 _fade_out.add ((_length * 0.666667), 0.518174);
263 _fade_out.add ((_length * 0.833333), 0.282192);
264 _fade_out.add ((_length * 1.000000), 0.000000);
266 #else // EQUAL_POWER_MINUS_6DB
268 _fade_out.add ((_length * 0.000000), 1.000000);
269 _fade_out.add ((_length * 0.166667), 0.833033);
270 _fade_out.add ((_length * 0.333333), 0.666186);
271 _fade_out.add ((_length * 0.500000), 0.499459);
272 _fade_out.add ((_length * 0.666667), 0.332853);
273 _fade_out.add ((_length * 0.833333), 0.166366);
274 _fade_out.add ((_length * 1.000000), 0.000000);
282 #define EQUAL_POWER_MINUS_3DB
283 #ifdef EQUAL_POWER_MINUS_3DB
285 _fade_in.add ((_length * 0.000000), 0.000000);
286 _fade_in.add ((_length * 0.166667), 0.282192);
287 _fade_in.add ((_length * 0.333333), 0.518174);
288 _fade_in.add ((_length * 0.500000), 0.707946);
289 _fade_in.add ((_length * 0.666667), 0.851507);
290 _fade_in.add ((_length * 0.833333), 0.948859);
291 _fade_in.add ((_length * 1.000000), 1.000000);
293 #else // EQUAL_POWER_MINUS_SIX_DB
295 _fade_in.add ((_length * 0.000000), 0.000000);
296 _fade_in.add ((_length * 0.166667), 0.166366);
297 _fade_in.add ((_length * 0.333333), 0.332853);
298 _fade_in.add ((_length * 0.500000), 0.499459);
299 _fade_in.add ((_length * 0.666667), 0.666186);
300 _fade_in.add ((_length * 0.833333), 0.833033);
301 _fade_in.add ((_length * 1.000000), 1.000000);
307 overlap_type = _in->coverage (_out->position(), _out->last_frame());
308 layer_relation = (int32_t) (_in->layer() - _out->layer());
312 Crossfade::read_raw_internal (Sample* buf, framecnt_t start, framecnt_t cnt, int channel) const
314 Sample* mixdown = new Sample[cnt];
315 float* gain = new float[cnt];
318 ret = read_at (buf, mixdown, gain, start, cnt, channel, cnt);
327 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
328 float *gain_buffer, framepos_t start, framecnt_t cnt, uint32_t chan_n,
329 framecnt_t read_frames, framecnt_t skip_frames) const
331 frameoffset_t offset;
338 if (start < _position) {
340 /* handle an initial section of the read area that we do not
344 offset = _position - start;
354 to_write = min (_length.val(), cnt);
358 to_write = min ((_length - (start - _position)), cnt);
362 offset = start - _position;
364 /* Prevent data from piling up inthe crossfade buffers when reading a transparent region */
365 if (!(_out->opaque())) {
366 memset (crossfade_buffer_out, 0, sizeof (Sample) * to_write);
367 } else if (!(_in->opaque())) {
368 memset (crossfade_buffer_in, 0, sizeof (Sample) * to_write);
371 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
372 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
374 float* fiv = new float[to_write];
375 float* fov = new float[to_write];
377 _fade_in.curve().get_vector (offset, offset+to_write, fiv, to_write);
378 _fade_out.curve().get_vector (offset, offset+to_write, fov, to_write);
380 /* note: although we have not explicitly taken into account the return values
381 from _out->read_at() or _in->read_at(), the length() function does this
382 implicitly. why? because it computes a value based on the in+out regions'
383 position and length, and so we know precisely how much data they could return.
386 for (framecnt_t n = 0; n < to_write; ++n) {
387 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
397 Crossfade::coverage (framepos_t start, framepos_t end) const
399 framepos_t my_end = _position + _length;
401 if ((start >= _position) && (end <= my_end)) {
402 return OverlapInternal;
404 if ((end >= _position) && (end <= my_end)) {
407 if ((start >= _position) && (start <= my_end)) {
410 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
411 return OverlapExternal;
417 Crossfade::set_active (bool yn)
421 PropertyChanged (PropertyChange (Properties::active));
426 Crossfade::refresh ()
428 /* crossfades must be between non-muted regions */
430 if (_out->muted() || _in->muted()) {
431 Invalidated (shared_from_this ());
435 /* Top layer shouldn't be transparent */
437 if (!((layer_relation > 0 ? _in : _out)->opaque())) {
438 Invalidated (shared_from_this());
442 /* regions must cannot be identically sized and placed */
444 if (_in->position() == _out->position() && _in->length() == _out->length()) {
445 Invalidated (shared_from_this());
449 /* layer ordering cannot change */
451 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
453 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
454 Invalidated (shared_from_this ());
458 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
460 if (ot == OverlapNone) {
461 Invalidated (shared_from_this ());
467 if (ot != overlap_type) {
469 if (_follow_overlap) {
472 compute (_in, _out, _session.config.get_xfade_model());
475 catch (NoCrossfadeHere& err) {
476 Invalidated (shared_from_this ());
483 Invalidated (shared_from_this ());
489 send_signal = update ();
493 PropertyChange bounds;
494 bounds.add (Properties::start);
495 bounds.add (Properties::position);
496 bounds.add (Properties::length);
497 PropertyChanged (bounds); /* EMIT SIGNAL */
510 if (_follow_overlap) {
511 newlen = _out->first_frame() + _out->length() - _in->first_frame();
517 Invalidated (shared_from_this ());
523 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
525 double factor = newlen / (double) _length;
527 _fade_out.x_scale (factor);
528 _fade_in.x_scale (factor);
533 switch (_anchor_point) {
535 _position = _in->first_frame();
539 _position = _in->last_frame() - _length;
543 _position = _out->last_frame() - _length;
550 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
552 boost::shared_ptr<AudioRegion> top;
553 boost::shared_ptr<AudioRegion> bottom;
554 framecnt_t short_xfade_length;
556 short_xfade_length = _short_xfade_length;
558 if (a->layer() < b->layer()) {
566 /* first check for matching ends */
568 if (top->first_frame() == bottom->first_frame()) {
570 /* Both regions start at the same point */
572 if (top->last_frame() < bottom->last_frame()) {
574 /* top ends before bottom, so put an xfade
575 in at the end of top.
578 /* [-------- top ---------- ]
579 * {====== bottom =====================}
585 if (top->last_frame() < short_xfade_length) {
588 _position = top->last_frame() - short_xfade_length;
591 _length = min (short_xfade_length, top->length());
592 _follow_overlap = false;
593 _anchor_point = EndOfIn;
598 /* top ends after (or same time) as bottom - no xfade
601 /* [-------- top ------------------------ ]
602 * {====== bottom =====================}
605 throw NoCrossfadeHere();
608 } else if (top->last_frame() == bottom->last_frame()) {
610 /* Both regions end at the same point */
612 if (top->first_frame() > bottom->first_frame()) {
614 /* top starts after bottom, put an xfade in at the
618 /* [-------- top ---------- ]
619 * {====== bottom =====================}
624 _position = top->first_frame();
625 _length = min (short_xfade_length, top->length());
626 _follow_overlap = false;
627 _anchor_point = StartOfIn;
632 /* top starts before bottom - no xfade
635 /* [-------- top ------------------------ ]
636 * {====== bottom =====================}
639 throw NoCrossfadeHere();
644 /* OK, time to do more regular overlapping */
646 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
650 /* should be NOTREACHED as a precondition of creating
651 a new crossfade, but we need to handle it here.
653 throw NoCrossfadeHere();
656 case OverlapInternal:
657 case OverlapExternal:
658 /* should be NOTREACHED because of tests above */
659 throw NoCrossfadeHere();
662 case OverlapEnd: /* top covers start of bottom but ends within it */
664 /* [---- top ------------------------]
665 * { ==== bottom ============ }
670 _anchor_point = EndOfOut;
672 if (model == FullCrossfade) {
673 _position = bottom->first_frame(); // "{"
674 _length = _out->first_frame() + _out->length() - _in->first_frame();
675 /* leave active alone */
676 _follow_overlap = true;
678 _length = min (short_xfade_length, top->length());
679 _position = top->last_frame() - _length; // "]" - length
681 _follow_overlap = false;
686 case OverlapStart: /* top starts within bottom but covers bottom's end */
688 /* { ==== top ============ }
689 * [---- bottom -------------------]
694 _position = top->first_frame();
695 _anchor_point = StartOfIn;
697 if (model == FullCrossfade) {
698 _length = _out->first_frame() + _out->length() - _in->first_frame();
699 /* leave active alone */
700 _follow_overlap = true;
702 _length = min (short_xfade_length, top->length());
704 _follow_overlap = false;
716 Crossfade::get_state ()
718 XMLNode* node = new XMLNode (X_("Crossfade"));
721 LocaleGuard lg (X_("POSIX"));
723 id().print (buf, sizeof (buf));
724 node->add_property ("id", buf);
725 _out->id().print (buf, sizeof (buf));
726 node->add_property ("out", buf);
727 _in->id().print (buf, sizeof (buf));
728 node->add_property ("in", buf);
729 node->add_property ("active", (_active ? "yes" : "no"));
730 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
731 node->add_property ("fixed", (_fixed ? "yes" : "no"));
732 snprintf (buf, sizeof(buf), "%" PRId64, _length.val());
733 node->add_property ("length", buf);
734 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
735 node->add_property ("anchor-point", buf);
736 snprintf (buf, sizeof(buf), "%" PRId64, _position.val());
737 node->add_property ("position", buf);
739 child = node->add_child ("FadeIn");
741 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
744 pnode = new XMLNode ("point");
746 snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
747 pnode->add_property ("x", buf);
748 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
749 pnode->add_property ("y", buf);
750 child->add_child_nocopy (*pnode);
753 child = node->add_child ("FadeOut");
755 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
758 pnode = new XMLNode ("point");
760 snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
761 pnode->add_property ("x", buf);
762 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
763 pnode->add_property ("y", buf);
764 child->add_child_nocopy (*pnode);
771 Crossfade::set_state (const XMLNode& node, int /*version*/)
773 XMLNodeConstIterator i;
774 XMLNodeList children;
777 const XMLProperty* prop;
778 LocaleGuard lg (X_("POSIX"));
779 PropertyChange what_changed;
782 if ((prop = node.property (X_("id")))) {
786 if ((prop = node.property ("position")) != 0) {
787 sscanf (prop->value().c_str(), "%" PRId64, &val);
788 if (val != _position) {
790 what_changed.add (Properties::position);
793 warning << _("old-style crossfade information - no position information") << endmsg;
794 _position = _in->first_frame();
797 if ((prop = node.property ("active")) != 0) {
798 bool x = string_is_affirmative (prop->value());
801 what_changed.add (Properties::active);
807 if ((prop = node.property ("follow-overlap")) != 0) {
808 _follow_overlap = string_is_affirmative (prop->value());
810 _follow_overlap = false;
813 if ((prop = node.property ("fixed")) != 0) {
814 _fixed = string_is_affirmative (prop->value());
819 if ((prop = node.property ("anchor-point")) != 0) {
820 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
822 _anchor_point = StartOfIn;
825 if ((prop = node.property ("length")) != 0) {
827 sscanf (prop->value().c_str(), "%" PRId64, &val);
828 if (val != _length) {
830 what_changed.add (Properties::length);
835 /* XXX this branch is legacy code from before
836 the point where we stored xfade lengths.
839 if ((_length = overlap_length()) == 0) {
840 throw failed_constructor();
844 if ((fi = find_named_node (node, "FadeIn")) == 0) {
848 if ((fo = find_named_node (node, "FadeOut")) == 0) {
857 children = fi->children();
859 for (i = children.begin(); i != children.end(); ++i) {
860 if ((*i)->name() == "point") {
864 prop = (*i)->property ("x");
865 sscanf (prop->value().c_str(), "%" PRId64, &x);
867 prop = (*i)->property ("y");
868 sscanf (prop->value().c_str(), "%f", &y);
874 if (_fade_in.size() < 2) {
875 /* fade state somehow saved with no points */
879 _fade_in.front()->value = 0.0;
880 _fade_in.back()->value = 1.0;
889 children = fo->children();
891 for (i = children.begin(); i != children.end(); ++i) {
892 if ((*i)->name() == "point") {
897 prop = (*i)->property ("x");
898 sscanf (prop->value().c_str(), "%" PRId64, &x);
900 prop = (*i)->property ("y");
901 sscanf (prop->value().c_str(), "%f", &y);
903 _fade_out.add (x, y);
907 if (_fade_out.size() < 2) {
908 /* fade state somehow saved with no points */
912 _fade_out.front()->value = 1.0;
913 _fade_out.back()->value = 0.0;
917 PropertyChanged (what_changed); /* EMIT SIGNAL */
918 FadesChanged (); /* EMIT SIGNAL */
924 Crossfade::can_follow_overlap () const
930 Crossfade::set_follow_overlap (bool yn)
932 if (yn == _follow_overlap || _fixed) {
936 _follow_overlap = yn;
939 set_xfade_length (_short_xfade_length);
941 set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
944 PropertyChanged (PropertyChange (Properties::follow_overlap));
948 Crossfade::set_xfade_length (framecnt_t len)
950 framecnt_t limit = 0;
952 switch (_anchor_point) {
954 limit = _in->length();
958 limit = _in->length();
962 limit = _out->length();
967 len = min (limit, len);
969 double factor = len / (double) _length;
972 _fade_out.x_scale (factor);
973 _fade_in.x_scale (factor);
978 PropertyChanged (PropertyChange (Properties::length));
984 Crossfade::overlap_length () const
989 return _out->first_frame() + _out->length() - _in->first_frame();
993 Crossfade::set_short_xfade_length (framecnt_t n)
995 _short_xfade_length = n;