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
99 _follow_overlap = false;
104 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
105 : AudioRegion (a->session(), 0, 0, a->name() + string ("<>") + b->name())
106 , CROSSFADE_DEFAULT_PROPERTIES
107 , _fade_in (Evoral::Parameter(FadeInAutomation)) // linear (gain coefficient) => -inf..+6dB
108 , _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
112 _follow_overlap = false;
114 if (compute (a, b, model)) {
115 throw failed_constructor();
123 Crossfade::Crossfade (const Playlist& playlist, XMLNode const & node)
124 : AudioRegion (playlist.session(), 0, 0, "unnamed crossfade")
125 , CROSSFADE_DEFAULT_PROPERTIES
126 , _fade_in (Evoral::Parameter(FadeInAutomation)) // linear (gain coefficient) => -inf..+6dB
127 , _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
130 boost::shared_ptr<Region> r;
131 XMLProperty const * prop;
132 LocaleGuard lg (X_("POSIX"));
134 /* we have to find the in/out regions before we can do anything else */
136 if ((prop = node.property ("in")) == 0) {
137 error << _("Crossfade: no \"in\" region in state") << endmsg;
138 throw failed_constructor();
141 PBD::ID id (prop->value());
143 r = playlist.find_region (id);
146 /* the `in' region is not in a playlist, which probably means that this crossfade
147 is in the undo record, so we have to find the region in the global region map.
149 r = RegionFactory::region_by_id (id);
153 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2 nor in region map"), id, playlist.name())
155 throw failed_constructor();
158 if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
159 throw failed_constructor();
162 if ((prop = node.property ("out")) == 0) {
163 error << _("Crossfade: no \"out\" region in state") << endmsg;
164 throw failed_constructor();
167 PBD::ID id2 (prop->value());
169 r = playlist.find_region (id2);
172 r = RegionFactory::region_by_id (id2);
176 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2 nor in region map"), id2, playlist.name())
178 throw failed_constructor();
181 if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
182 throw failed_constructor();
189 if (set_state (node, Stateful::loading_state_version)) {
190 throw failed_constructor();
194 Crossfade::Crossfade (boost::shared_ptr<Crossfade> orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
195 : AudioRegion (boost::dynamic_pointer_cast<const AudioRegion> (orig), 0)
196 , CROSSFADE_DEFAULT_PROPERTIES
197 , _fade_in (orig->_fade_in)
198 , _fade_out (orig->_fade_out)
200 _active = orig->_active;
201 _in_update = orig->_in_update;
202 _anchor_point = orig->_anchor_point;
203 _follow_overlap = orig->_follow_overlap;
204 _fixed = orig->_fixed;
209 // copied from Crossfade::initialize()
212 _out->suspend_fade_out ();
213 _in->suspend_fade_in ();
215 overlap_type = _in->coverage (_out->position(), _out->last_frame());
216 layer_relation = (int32_t) (_in->layer() - _out->layer());
218 // Let's make sure the fade isn't too long
219 set_xfade_length(_length);
223 Crossfade::~Crossfade ()
228 Crossfade::initialize ()
230 /* merge source lists from regions */
232 _sources = _in->sources();
233 _sources.insert (_sources.end(), _out->sources().begin(), _out->sources().end());
235 for (SourceList::iterator i = _sources.begin(); i != _sources.end(); ++i) {
236 (*i)->inc_use_count ();
239 _master_sources = _in->master_sources();
240 _master_sources.insert(_master_sources.end(), _out->master_sources().begin(), _out->master_sources().end());
242 for (SourceList::iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
243 (*i)->inc_use_count ();
248 _out->suspend_fade_out ();
249 _in->suspend_fade_in ();
254 #define EQUAL_POWER_MINUS_3DB
255 #ifdef EQUAL_POWER_MINUS_3DB
257 _fade_out.add ((_length * 0.000000), 1.000000);
258 _fade_out.add ((_length * 0.166667), 0.948859);
259 _fade_out.add ((_length * 0.333333), 0.851507);
260 _fade_out.add ((_length * 0.500000), 0.707946);
261 _fade_out.add ((_length * 0.666667), 0.518174);
262 _fade_out.add ((_length * 0.833333), 0.282192);
263 _fade_out.add ((_length * 1.000000), 0.000000);
265 #else // EQUAL_POWER_MINUS_6DB
267 _fade_out.add ((_length * 0.000000), 1.000000);
268 _fade_out.add ((_length * 0.166667), 0.833033);
269 _fade_out.add ((_length * 0.333333), 0.666186);
270 _fade_out.add ((_length * 0.500000), 0.499459);
271 _fade_out.add ((_length * 0.666667), 0.332853);
272 _fade_out.add ((_length * 0.833333), 0.166366);
273 _fade_out.add ((_length * 1.000000), 0.000000);
281 #define EQUAL_POWER_MINUS_3DB
282 #ifdef EQUAL_POWER_MINUS_3DB
284 _fade_in.add ((_length * 0.000000), 0.000000);
285 _fade_in.add ((_length * 0.166667), 0.282192);
286 _fade_in.add ((_length * 0.333333), 0.518174);
287 _fade_in.add ((_length * 0.500000), 0.707946);
288 _fade_in.add ((_length * 0.666667), 0.851507);
289 _fade_in.add ((_length * 0.833333), 0.948859);
290 _fade_in.add ((_length * 1.000000), 1.000000);
292 #else // EQUAL_POWER_MINUS_SIX_DB
294 _fade_in.add ((_length * 0.000000), 0.000000);
295 _fade_in.add ((_length * 0.166667), 0.166366);
296 _fade_in.add ((_length * 0.333333), 0.332853);
297 _fade_in.add ((_length * 0.500000), 0.499459);
298 _fade_in.add ((_length * 0.666667), 0.666186);
299 _fade_in.add ((_length * 0.833333), 0.833033);
300 _fade_in.add ((_length * 1.000000), 1.000000);
306 overlap_type = _in->coverage (_out->position(), _out->last_frame());
307 layer_relation = (int32_t) (_in->layer() - _out->layer());
311 Crossfade::read_raw_internal (Sample* buf, framecnt_t start, framecnt_t cnt, int channel) const
313 Sample* mixdown = new Sample[cnt];
314 float* gain = new float[cnt];
317 ret = read_at (buf, mixdown, gain, start, cnt, channel);
326 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
327 float *gain_buffer, framepos_t start, framecnt_t cnt, uint32_t chan_n) const
329 frameoffset_t offset;
336 if (start < _position) {
338 /* handle an initial section of the read area that we do not
342 offset = _position - start;
352 to_write = min (_length.val(), cnt);
356 to_write = min ((_length - (start - _position)), cnt);
360 offset = start - _position;
362 /* Prevent data from piling up inthe crossfade buffers when reading a transparent region */
363 if (!(_out->opaque())) {
364 memset (crossfade_buffer_out, 0, sizeof (Sample) * to_write);
365 } else if (!(_in->opaque())) {
366 memset (crossfade_buffer_in, 0, sizeof (Sample) * to_write);
369 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n);
370 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n);
372 float* fiv = new float[to_write];
373 float* fov = new float[to_write];
375 _fade_in.curve().get_vector (offset, offset+to_write, fiv, to_write);
376 _fade_out.curve().get_vector (offset, offset+to_write, fov, to_write);
378 /* note: although we have not explicitly taken into account the return values
379 from _out->read_at() or _in->read_at(), the length() function does this
380 implicitly. why? because it computes a value based on the in+out regions'
381 position and length, and so we know precisely how much data they could return.
384 for (framecnt_t n = 0; n < to_write; ++n) {
385 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
395 Crossfade::coverage (framepos_t start, framepos_t end) const
397 framepos_t my_end = _position + _length;
399 if ((start >= _position) && (end <= my_end)) {
400 return OverlapInternal;
402 if ((end >= _position) && (end <= my_end)) {
405 if ((start >= _position) && (start <= my_end)) {
408 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
409 return OverlapExternal;
415 Crossfade::set_active (bool yn)
419 PropertyChanged (PropertyChange (Properties::active));
424 Crossfade::refresh ()
426 /* crossfades must be between non-muted regions */
428 if (_out->muted() || _in->muted()) {
429 Invalidated (shared_from_this ());
433 /* Top layer shouldn't be transparent */
435 if (!((layer_relation > 0 ? _in : _out)->opaque())) {
436 Invalidated (shared_from_this());
440 /* regions must cannot be identically sized and placed */
442 if (_in->position() == _out->position() && _in->length() == _out->length()) {
443 Invalidated (shared_from_this());
447 /* layer ordering cannot change */
449 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
451 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
452 Invalidated (shared_from_this ());
456 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
458 if (ot == OverlapNone) {
459 Invalidated (shared_from_this ());
465 if (ot != overlap_type) {
467 if (_follow_overlap) {
470 compute (_in, _out, _session.config.get_xfade_model());
473 catch (NoCrossfadeHere& err) {
474 Invalidated (shared_from_this ());
481 Invalidated (shared_from_this ());
487 send_signal = update ();
491 PropertyChange bounds;
492 bounds.add (Properties::start);
493 bounds.add (Properties::position);
494 bounds.add (Properties::length);
495 PropertyChanged (bounds); /* EMIT SIGNAL */
508 if (_follow_overlap) {
509 newlen = _out->first_frame() + _out->length() - _in->first_frame();
515 Invalidated (shared_from_this ());
521 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
523 double factor = newlen / (double) _length;
525 _fade_out.x_scale (factor);
526 _fade_in.x_scale (factor);
531 switch (_anchor_point) {
533 _position = _in->first_frame();
537 _position = _in->last_frame() - _length;
541 _position = _out->last_frame() - _length;
548 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
550 boost::shared_ptr<AudioRegion> top;
551 boost::shared_ptr<AudioRegion> bottom;
552 framecnt_t short_xfade_length;
554 short_xfade_length = _short_xfade_length;
556 if (a->layer() < b->layer()) {
564 /* first check for matching ends */
566 if (top->first_frame() == bottom->first_frame()) {
568 /* Both regions start at the same point */
570 if (top->last_frame() < bottom->last_frame()) {
572 /* top ends before bottom, so put an xfade
573 in at the end of top.
576 /* [-------- top ---------- ]
577 * {====== bottom =====================}
583 if (top->last_frame() < short_xfade_length) {
586 _position = top->last_frame() - short_xfade_length;
589 set_xfade_length (min (short_xfade_length, top->length()));
590 _follow_overlap = false;
591 _anchor_point = EndOfIn;
596 /* top ends after (or same time) as bottom - no xfade
599 /* [-------- top ------------------------ ]
600 * {====== bottom =====================}
603 throw NoCrossfadeHere();
606 } else if (top->last_frame() == bottom->last_frame()) {
608 /* Both regions end at the same point */
610 if (top->first_frame() > bottom->first_frame()) {
612 /* top starts after bottom, put an xfade in at the
616 /* [-------- top ---------- ]
617 * {====== bottom =====================}
622 _position = top->first_frame();
623 set_xfade_length (min (short_xfade_length, top->length()));
624 _follow_overlap = false;
625 _anchor_point = StartOfIn;
630 /* top starts before bottom - no xfade
633 /* [-------- top ------------------------ ]
634 * {====== bottom =====================}
637 throw NoCrossfadeHere();
642 /* OK, time to do more regular overlapping */
644 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
648 /* should be NOTREACHED as a precondition of creating
649 a new crossfade, but we need to handle it here.
651 throw NoCrossfadeHere();
654 case OverlapInternal:
655 case OverlapExternal:
656 /* should be NOTREACHED because of tests above */
657 throw NoCrossfadeHere();
660 case OverlapEnd: /* top covers start of bottom but ends within it */
662 /* [---- top ------------------------]
663 * { ==== bottom ============ }
668 _anchor_point = EndOfOut;
670 if (model == FullCrossfade) {
671 _position = bottom->first_frame(); // "{"
672 set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
673 /* leave active alone */
674 _follow_overlap = true;
676 set_xfade_length (min (short_xfade_length, top->length()));
677 _position = top->last_frame() - _length; // "]" - length
679 _follow_overlap = false;
684 case OverlapStart: /* top starts within bottom but covers bottom's end */
686 /* { ==== top ============ }
687 * [---- bottom -------------------]
692 _position = top->first_frame();
693 _anchor_point = StartOfIn;
695 if (model == FullCrossfade) {
696 set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
697 /* leave active alone */
698 _follow_overlap = true;
700 set_xfade_length (min (short_xfade_length, top->length()));
702 _follow_overlap = false;
714 Crossfade::get_state ()
716 XMLNode* node = new XMLNode (X_("Crossfade"));
719 LocaleGuard lg (X_("POSIX"));
721 id().print (buf, sizeof (buf));
722 node->add_property ("id", buf);
723 _out->id().print (buf, sizeof (buf));
724 node->add_property ("out", buf);
725 _in->id().print (buf, sizeof (buf));
726 node->add_property ("in", buf);
727 node->add_property ("active", (_active ? "yes" : "no"));
728 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
729 node->add_property ("fixed", (_fixed ? "yes" : "no"));
730 snprintf (buf, sizeof(buf), "%" PRId64, _length.val());
731 node->add_property ("length", buf);
732 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
733 node->add_property ("anchor-point", buf);
734 snprintf (buf, sizeof(buf), "%" PRId64, _position.val());
735 node->add_property ("position", buf);
737 child = node->add_child ("FadeIn");
739 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
742 pnode = new XMLNode ("point");
744 snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
745 pnode->add_property ("x", buf);
746 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
747 pnode->add_property ("y", buf);
748 child->add_child_nocopy (*pnode);
751 child = node->add_child ("FadeOut");
753 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
756 pnode = new XMLNode ("point");
758 snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
759 pnode->add_property ("x", buf);
760 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
761 pnode->add_property ("y", buf);
762 child->add_child_nocopy (*pnode);
769 Crossfade::set_state (const XMLNode& node, int /*version*/)
771 XMLNodeConstIterator i;
772 XMLNodeList children;
775 const XMLProperty* prop;
776 LocaleGuard lg (X_("POSIX"));
777 PropertyChange what_changed;
782 if ((prop = node.property ("position")) != 0) {
783 sscanf (prop->value().c_str(), "%" PRId64, &val);
784 if (val != _position) {
786 what_changed.add (Properties::position);
789 warning << _("old-style crossfade information - no position information") << endmsg;
790 _position = _in->first_frame();
793 if ((prop = node.property ("active")) != 0) {
794 bool x = string_is_affirmative (prop->value());
797 what_changed.add (Properties::active);
803 if ((prop = node.property ("follow-overlap")) != 0) {
804 _follow_overlap = string_is_affirmative (prop->value());
806 _follow_overlap = false;
809 if ((prop = node.property ("fixed")) != 0) {
810 _fixed = string_is_affirmative (prop->value());
815 if ((prop = node.property ("anchor-point")) != 0) {
816 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
818 _anchor_point = StartOfIn;
821 if ((prop = node.property ("length")) != 0) {
823 sscanf (prop->value().c_str(), "%" PRId64, &val);
824 if (val != _length) {
826 what_changed.add (Properties::length);
831 /* XXX this branch is legacy code from before
832 the point where we stored xfade lengths.
835 if ((_length = overlap_length()) == 0) {
836 throw failed_constructor();
840 if ((fi = find_named_node (node, "FadeIn")) == 0) {
844 if ((fo = find_named_node (node, "FadeOut")) == 0) {
853 children = fi->children();
855 for (i = children.begin(); i != children.end(); ++i) {
856 if ((*i)->name() == "point") {
860 prop = (*i)->property ("x");
861 sscanf (prop->value().c_str(), "%" PRId64, &x);
863 prop = (*i)->property ("y");
864 sscanf (prop->value().c_str(), "%f", &y);
870 if (_fade_in.size() < 2) {
871 /* fade state somehow saved with no points */
875 _fade_in.front()->value = 0.0;
876 _fade_in.back()->value = 1.0;
885 children = fo->children();
887 for (i = children.begin(); i != children.end(); ++i) {
888 if ((*i)->name() == "point") {
893 prop = (*i)->property ("x");
894 sscanf (prop->value().c_str(), "%" PRId64, &x);
896 prop = (*i)->property ("y");
897 sscanf (prop->value().c_str(), "%f", &y);
899 _fade_out.add (x, y);
903 if (_fade_out.size() < 2) {
904 /* fade state somehow saved with no points */
908 _fade_out.front()->value = 1.0;
909 _fade_out.back()->value = 0.0;
913 PropertyChanged (what_changed); /* EMIT SIGNAL */
914 FadesChanged (); /* EMIT SIGNAL */
920 Crossfade::can_follow_overlap () const
926 Crossfade::set_follow_overlap (bool yn)
928 if (yn == _follow_overlap || _fixed) {
932 _follow_overlap = yn;
935 set_xfade_length (_short_xfade_length);
937 set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
940 PropertyChanged (PropertyChange (Properties::follow_overlap));
944 Crossfade::set_xfade_length (framecnt_t len)
946 framecnt_t limit = 0;
948 switch (_anchor_point) {
950 limit = _in->length();
954 limit = _in->length();
958 limit = _out->length();
963 len = min (limit, len);
965 double factor = len / (double) _length;
968 _fade_out.x_scale (factor);
969 _fade_in.x_scale (factor);
974 PropertyChanged (PropertyChange (Properties::length));
980 Crossfade::overlap_length () const
985 return _out->first_frame() + _out->length() - _in->first_frame();
989 Crossfade::set_short_xfade_length (framecnt_t n)
991 _short_xfade_length = n;