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;
213 _position = orig->_position;
218 // copied from Crossfade::initialize()
221 _out->suspend_fade_out ();
222 _in->suspend_fade_in ();
224 overlap_type = _in->coverage (_out->position(), _out->last_frame());
225 layer_relation = (int32_t) (_in->layer() - _out->layer());
227 // Let's make sure the fade isn't too long
228 set_xfade_length(_length);
232 Crossfade::~Crossfade ()
237 Crossfade::register_properties ()
239 add_property (_active);
240 add_property (_follow_overlap);
244 Crossfade::initialize ()
246 /* merge source lists from regions */
248 _sources = _in->sources();
249 _sources.insert (_sources.end(), _out->sources().begin(), _out->sources().end());
251 for (SourceList::iterator i = _sources.begin(); i != _sources.end(); ++i) {
252 (*i)->inc_use_count ();
255 _master_sources = _in->master_sources();
256 _master_sources.insert(_master_sources.end(), _out->master_sources().begin(), _out->master_sources().end());
258 for (SourceList::iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
259 (*i)->inc_use_count ();
264 _out->suspend_fade_out ();
265 _in->suspend_fade_in ();
270 #define EQUAL_POWER_MINUS_3DB
271 #ifdef EQUAL_POWER_MINUS_3DB
273 _fade_out.add ((_length * 0.000000), 1.000000);
274 _fade_out.add ((_length * 0.166667), 0.948859);
275 _fade_out.add ((_length * 0.333333), 0.851507);
276 _fade_out.add ((_length * 0.500000), 0.707946);
277 _fade_out.add ((_length * 0.666667), 0.518174);
278 _fade_out.add ((_length * 0.833333), 0.282192);
279 _fade_out.add ((_length * 1.000000), 0.000000);
281 #else // EQUAL_POWER_MINUS_6DB
283 _fade_out.add ((_length * 0.000000), 1.000000);
284 _fade_out.add ((_length * 0.166667), 0.833033);
285 _fade_out.add ((_length * 0.333333), 0.666186);
286 _fade_out.add ((_length * 0.500000), 0.499459);
287 _fade_out.add ((_length * 0.666667), 0.332853);
288 _fade_out.add ((_length * 0.833333), 0.166366);
289 _fade_out.add ((_length * 1.000000), 0.000000);
297 #define EQUAL_POWER_MINUS_3DB
298 #ifdef EQUAL_POWER_MINUS_3DB
300 _fade_in.add ((_length * 0.000000), 0.000000);
301 _fade_in.add ((_length * 0.166667), 0.282192);
302 _fade_in.add ((_length * 0.333333), 0.518174);
303 _fade_in.add ((_length * 0.500000), 0.707946);
304 _fade_in.add ((_length * 0.666667), 0.851507);
305 _fade_in.add ((_length * 0.833333), 0.948859);
306 _fade_in.add ((_length * 1.000000), 1.000000);
308 #else // EQUAL_POWER_MINUS_SIX_DB
310 _fade_in.add ((_length * 0.000000), 0.000000);
311 _fade_in.add ((_length * 0.166667), 0.166366);
312 _fade_in.add ((_length * 0.333333), 0.332853);
313 _fade_in.add ((_length * 0.500000), 0.499459);
314 _fade_in.add ((_length * 0.666667), 0.666186);
315 _fade_in.add ((_length * 0.833333), 0.833033);
316 _fade_in.add ((_length * 1.000000), 1.000000);
322 overlap_type = _in->coverage (_out->position(), _out->last_frame());
323 layer_relation = (int32_t) (_in->layer() - _out->layer());
327 Crossfade::read_raw_internal (Sample* buf, framecnt_t start, framecnt_t cnt, int channel) const
329 Sample* mixdown = new Sample[cnt];
330 float* gain = new float[cnt];
333 ret = read_at (buf, mixdown, gain, start, cnt, channel);
342 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
343 float *gain_buffer, framepos_t start, framecnt_t cnt, uint32_t chan_n) const
345 frameoffset_t offset;
352 if (start < _position) {
354 /* handle an initial section of the read area that we do not
358 offset = _position - start;
368 to_write = min (_length.val(), cnt);
372 to_write = min ((_length - (start - _position)), cnt);
376 offset = start - _position;
378 /* Prevent data from piling up inthe crossfade buffers when reading a transparent region */
379 if (!(_out->opaque())) {
380 memset (crossfade_buffer_out, 0, sizeof (Sample) * to_write);
381 } else if (!(_in->opaque())) {
382 memset (crossfade_buffer_in, 0, sizeof (Sample) * to_write);
385 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n);
386 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n);
388 float* fiv = new float[to_write];
389 float* fov = new float[to_write];
391 _fade_in.curve().get_vector (offset, offset+to_write, fiv, to_write);
392 _fade_out.curve().get_vector (offset, offset+to_write, fov, to_write);
394 /* note: although we have not explicitly taken into account the return values
395 from _out->read_at() or _in->read_at(), the length() function does this
396 implicitly. why? because it computes a value based on the in+out regions'
397 position and length, and so we know precisely how much data they could return.
400 for (framecnt_t n = 0; n < to_write; ++n) {
401 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
411 Crossfade::coverage (framepos_t start, framepos_t end) const
413 framepos_t my_end = _position + _length;
415 if ((start >= _position) && (end <= my_end)) {
416 return OverlapInternal;
418 if ((end >= _position) && (end <= my_end)) {
421 if ((start >= _position) && (start <= my_end)) {
424 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
425 return OverlapExternal;
431 Crossfade::set_active (bool yn)
435 PropertyChanged (PropertyChange (Properties::active));
440 Crossfade::refresh ()
442 /* crossfades must be between non-muted regions */
444 if (_out->muted() || _in->muted()) {
445 Invalidated (shared_from_this ());
449 /* Top layer shouldn't be transparent */
451 if (!((layer_relation > 0 ? _in : _out)->opaque())) {
452 Invalidated (shared_from_this());
456 /* regions must cannot be identically sized and placed */
458 if (_in->position() == _out->position() && _in->length() == _out->length()) {
459 Invalidated (shared_from_this());
463 /* layer ordering cannot change */
465 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
467 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
468 Invalidated (shared_from_this ());
472 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
474 if (ot == OverlapNone) {
475 Invalidated (shared_from_this ());
481 if (ot != overlap_type) {
483 if (_follow_overlap) {
486 compute (_in, _out, _session.config.get_xfade_model());
489 catch (NoCrossfadeHere& err) {
490 Invalidated (shared_from_this ());
497 Invalidated (shared_from_this ());
503 send_signal = update ();
507 PropertyChange bounds;
508 bounds.add (Properties::start);
509 bounds.add (Properties::position);
510 bounds.add (Properties::length);
511 PropertyChanged (bounds); /* EMIT SIGNAL */
524 if (_follow_overlap) {
525 newlen = _out->first_frame() + _out->length() - _in->first_frame();
531 Invalidated (shared_from_this ());
537 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
539 double factor = newlen / (double) _length;
541 _fade_out.x_scale (factor);
542 _fade_in.x_scale (factor);
547 switch (_anchor_point) {
549 _position = _in->first_frame();
553 _position = _in->last_frame() - _length;
557 _position = _out->last_frame() - _length;
564 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
566 boost::shared_ptr<AudioRegion> top;
567 boost::shared_ptr<AudioRegion> bottom;
568 framecnt_t short_xfade_length;
570 short_xfade_length = _short_xfade_length;
572 if (a->layer() < b->layer()) {
580 /* first check for matching ends */
582 if (top->first_frame() == bottom->first_frame()) {
584 /* Both regions start at the same point */
586 if (top->last_frame() < bottom->last_frame()) {
588 /* top ends before bottom, so put an xfade
589 in at the end of top.
592 /* [-------- top ---------- ]
593 * {====== bottom =====================}
599 if (top->last_frame() < short_xfade_length) {
602 _position = top->last_frame() - short_xfade_length;
605 set_xfade_length (min (short_xfade_length, top->length()));
606 _follow_overlap = false;
607 _anchor_point = EndOfIn;
612 /* top ends after (or same time) as bottom - no xfade
615 /* [-------- top ------------------------ ]
616 * {====== bottom =====================}
619 throw NoCrossfadeHere();
622 } else if (top->last_frame() == bottom->last_frame()) {
624 /* Both regions end at the same point */
626 if (top->first_frame() > bottom->first_frame()) {
628 /* top starts after bottom, put an xfade in at the
632 /* [-------- top ---------- ]
633 * {====== bottom =====================}
638 _position = top->first_frame();
639 set_xfade_length (min (short_xfade_length, top->length()));
640 _follow_overlap = false;
641 _anchor_point = StartOfIn;
646 /* top starts before bottom - no xfade
649 /* [-------- top ------------------------ ]
650 * {====== bottom =====================}
653 throw NoCrossfadeHere();
658 /* OK, time to do more regular overlapping */
660 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
664 /* should be NOTREACHED as a precondition of creating
665 a new crossfade, but we need to handle it here.
667 throw NoCrossfadeHere();
670 case OverlapInternal:
671 case OverlapExternal:
672 /* should be NOTREACHED because of tests above */
673 throw NoCrossfadeHere();
676 case OverlapEnd: /* top covers start of bottom but ends within it */
678 /* [---- top ------------------------]
679 * { ==== bottom ============ }
684 _anchor_point = EndOfOut;
686 if (model == FullCrossfade) {
687 _position = bottom->first_frame(); // "{"
688 set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
689 /* leave active alone */
690 _follow_overlap = true;
692 set_xfade_length (min (short_xfade_length, top->length()));
693 _position = top->last_frame() - _length; // "]" - length
695 _follow_overlap = false;
700 case OverlapStart: /* top starts within bottom but covers bottom's end */
702 /* { ==== top ============ }
703 * [---- bottom -------------------]
708 _position = top->first_frame();
709 _anchor_point = StartOfIn;
711 if (model == FullCrossfade) {
712 set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
713 /* leave active alone */
714 _follow_overlap = true;
716 set_xfade_length (min (short_xfade_length, top->length()));
718 _follow_overlap = false;
730 Crossfade::get_state ()
732 XMLNode* node = new XMLNode (X_("Crossfade"));
735 LocaleGuard lg (X_("POSIX"));
737 id().print (buf, sizeof (buf));
738 node->add_property ("id", buf);
739 _out->id().print (buf, sizeof (buf));
740 node->add_property ("out", buf);
741 _in->id().print (buf, sizeof (buf));
742 node->add_property ("in", buf);
743 node->add_property ("active", (_active ? "yes" : "no"));
744 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
745 node->add_property ("fixed", (_fixed ? "yes" : "no"));
746 snprintf (buf, sizeof(buf), "%" PRId64, _length.val());
747 node->add_property ("length", buf);
748 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
749 node->add_property ("anchor-point", buf);
750 snprintf (buf, sizeof(buf), "%" PRId64, _position.val());
751 node->add_property ("position", buf);
753 child = node->add_child ("FadeIn");
755 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.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);
767 child = node->add_child ("FadeOut");
769 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
772 pnode = new XMLNode ("point");
774 snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
775 pnode->add_property ("x", buf);
776 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
777 pnode->add_property ("y", buf);
778 child->add_child_nocopy (*pnode);
785 Crossfade::set_state (const XMLNode& node, int /*version*/)
787 XMLNodeConstIterator i;
788 XMLNodeList children;
791 const XMLProperty* prop;
792 LocaleGuard lg (X_("POSIX"));
793 PropertyChange what_changed;
798 if ((prop = node.property ("position")) != 0) {
799 sscanf (prop->value().c_str(), "%" PRId64, &val);
800 if (val != _position) {
802 what_changed.add (Properties::position);
805 warning << _("old-style crossfade information - no position information") << endmsg;
806 _position = _in->first_frame();
809 if ((prop = node.property ("active")) != 0) {
810 bool x = string_is_affirmative (prop->value());
813 what_changed.add (Properties::active);
819 if ((prop = node.property ("follow-overlap")) != 0) {
820 _follow_overlap = string_is_affirmative (prop->value());
822 _follow_overlap = false;
825 if ((prop = node.property ("fixed")) != 0) {
826 _fixed = string_is_affirmative (prop->value());
831 if ((prop = node.property ("anchor-point")) != 0) {
832 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
834 _anchor_point = StartOfIn;
837 if ((prop = node.property ("length")) != 0) {
839 sscanf (prop->value().c_str(), "%" PRId64, &val);
840 if (val != _length) {
842 what_changed.add (Properties::length);
847 /* XXX this branch is legacy code from before
848 the point where we stored xfade lengths.
851 if ((_length = overlap_length()) == 0) {
852 throw failed_constructor();
856 if ((fi = find_named_node (node, "FadeIn")) == 0) {
860 if ((fo = find_named_node (node, "FadeOut")) == 0) {
869 children = fi->children();
871 for (i = children.begin(); i != children.end(); ++i) {
872 if ((*i)->name() == "point") {
876 prop = (*i)->property ("x");
877 sscanf (prop->value().c_str(), "%" PRId64, &x);
879 prop = (*i)->property ("y");
880 sscanf (prop->value().c_str(), "%f", &y);
886 if (_fade_in.size() < 2) {
887 /* fade state somehow saved with no points */
891 _fade_in.front()->value = 0.0;
892 _fade_in.back()->value = 1.0;
901 children = fo->children();
903 for (i = children.begin(); i != children.end(); ++i) {
904 if ((*i)->name() == "point") {
909 prop = (*i)->property ("x");
910 sscanf (prop->value().c_str(), "%" PRId64, &x);
912 prop = (*i)->property ("y");
913 sscanf (prop->value().c_str(), "%f", &y);
915 _fade_out.add (x, y);
919 if (_fade_out.size() < 2) {
920 /* fade state somehow saved with no points */
924 _fade_out.front()->value = 1.0;
925 _fade_out.back()->value = 0.0;
929 PropertyChanged (what_changed); /* EMIT SIGNAL */
930 FadesChanged (); /* EMIT SIGNAL */
936 Crossfade::can_follow_overlap () const
942 Crossfade::set_follow_overlap (bool yn)
944 if (yn == _follow_overlap || _fixed) {
948 _follow_overlap = yn;
951 set_xfade_length (_short_xfade_length);
953 set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
956 PropertyChanged (PropertyChange (Properties::follow_overlap));
960 Crossfade::set_xfade_length (framecnt_t len)
962 framecnt_t limit = 0;
964 switch (_anchor_point) {
966 limit = _in->length();
970 limit = _in->length();
974 limit = _out->length();
979 len = min (limit, len);
981 double factor = len / (double) _length;
984 _fade_out.x_scale (factor);
985 _fade_in.x_scale (factor);
990 PropertyChanged (PropertyChange (Properties::length));
996 Crossfade::overlap_length () const
1001 return _out->first_frame() + _out->length() - _in->first_frame();
1005 Crossfade::set_short_xfade_length (framecnt_t n)
1007 _short_xfade_length = n;