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"
37 using namespace ARDOUR;
40 framecnt_t Crossfade::_short_xfade_length = 0;
42 /* XXX if and when we ever implement parallel processing of the process()
43 callback, these will need to be handled on a per-thread basis.
46 Sample* Crossfade::crossfade_buffer_out = 0;
47 Sample* Crossfade::crossfade_buffer_in = 0;
50 #define CROSSFADE_DEFAULT_PROPERTIES \
51 _active (Properties::active, _session.config.get_xfades_active ()) \
52 , _follow_overlap (Properties::follow_overlap, false)
56 namespace Properties {
57 PropertyDescriptor<bool> follow_overlap;
62 Crossfade::make_property_quarks ()
64 Properties::follow_overlap.property_id = g_quark_from_static_string (X_("follow-overlap"));
65 DEBUG_TRACE (DEBUG::Properties, string_compose ("quark for follow-overlap = %1\n", Properties::follow_overlap.property_id));
69 Crossfade::set_buffer_size (framecnt_t sz)
71 delete [] crossfade_buffer_out;
72 crossfade_buffer_out = 0;
74 delete [] crossfade_buffer_in;
75 crossfade_buffer_in = 0;
78 crossfade_buffer_out = new Sample[sz];
79 crossfade_buffer_in = new Sample[sz];
84 Crossfade::operator== (const Crossfade& other)
86 return (_in == other._in) && (_out == other._out);
89 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out,
93 : AudioRegion (in->session(), position, length, in->name() + string ("<>") + out->name())
94 , CROSSFADE_DEFAULT_PROPERTIES
95 , _fade_in (Evoral::Parameter(FadeInAutomation)) // linear (gain coefficient) => -inf..+6dB
96 , _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
103 _follow_overlap = false;
108 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
109 : AudioRegion (a->session(), 0, 0, a->name() + string ("<>") + b->name())
110 , CROSSFADE_DEFAULT_PROPERTIES
111 , _fade_in (Evoral::Parameter(FadeInAutomation)) // linear (gain coefficient) => -inf..+6dB
112 , _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
116 _follow_overlap = false;
118 if (compute (a, b, model)) {
119 throw failed_constructor();
127 Crossfade::Crossfade (const Playlist& playlist, XMLNode& 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 boost::shared_ptr<Region> r;
136 LocaleGuard lg (X_("POSIX"));
138 /* we have to find the in/out regions before we can do anything else */
140 if ((prop = node.property ("in")) == 0) {
141 error << _("Crossfade: no \"in\" region in state") << endmsg;
142 throw failed_constructor();
145 PBD::ID id (prop->value());
147 if ((r = playlist.find_region (id)) == 0) {
148 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2"), id, playlist.name())
150 throw failed_constructor();
153 if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
154 throw failed_constructor();
157 if ((prop = node.property ("out")) == 0) {
158 error << _("Crossfade: no \"out\" region in state") << endmsg;
159 throw failed_constructor();
162 PBD::ID id2 (prop->value());
164 if ((r = playlist.find_region (id2)) == 0) {
165 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2"), id2, playlist.name())
167 throw failed_constructor();
170 if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
171 throw failed_constructor();
178 if (set_state (node, Stateful::loading_state_version)) {
179 throw failed_constructor();
183 Crossfade::Crossfade (boost::shared_ptr<Crossfade> orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
184 : AudioRegion (boost::dynamic_pointer_cast<const AudioRegion> (orig), 0, true)
185 , CROSSFADE_DEFAULT_PROPERTIES
186 , _fade_in (orig->_fade_in)
187 , _fade_out (orig->_fade_out)
189 _active = orig->_active;
190 _in_update = orig->_in_update;
191 _anchor_point = orig->_anchor_point;
192 _follow_overlap = orig->_follow_overlap;
193 _fixed = orig->_fixed;
198 // copied from Crossfade::initialize()
201 _out->suspend_fade_out ();
202 _in->suspend_fade_in ();
204 overlap_type = _in->coverage (_out->position(), _out->last_frame());
205 layer_relation = (int32_t) (_in->layer() - _out->layer());
207 // Let's make sure the fade isn't too long
208 set_xfade_length(_length);
212 Crossfade::~Crossfade ()
217 Crossfade::initialize ()
219 /* merge source lists from regions */
221 _sources = _in->sources();
222 _sources.insert (_sources.end(), _out->sources().begin(), _out->sources().end());
224 for (SourceList::iterator i = _sources.begin(); i != _sources.end(); ++i) {
225 (*i)->inc_use_count ();
228 _master_sources = _in->master_sources();
229 _master_sources.insert(_master_sources.end(), _out->master_sources().begin(), _out->master_sources().end());
231 for (SourceList::iterator i = _master_sources.begin(); i != _master_sources.end(); ++i) {
232 (*i)->inc_use_count ();
237 _out->suspend_fade_out ();
238 _in->suspend_fade_in ();
243 #define EQUAL_POWER_MINUS_3DB
244 #ifdef EQUAL_POWER_MINUS_3DB
246 _fade_out.add ((_length * 0.000000), 1.000000);
247 _fade_out.add ((_length * 0.166667), 0.948859);
248 _fade_out.add ((_length * 0.333333), 0.851507);
249 _fade_out.add ((_length * 0.500000), 0.707946);
250 _fade_out.add ((_length * 0.666667), 0.518174);
251 _fade_out.add ((_length * 0.833333), 0.282192);
252 _fade_out.add ((_length * 1.000000), 0.000000);
254 #else // EQUAL_POWER_MINUS_6DB
256 _fade_out.add ((_length * 0.000000), 1.000000);
257 _fade_out.add ((_length * 0.166667), 0.833033);
258 _fade_out.add ((_length * 0.333333), 0.666186);
259 _fade_out.add ((_length * 0.500000), 0.499459);
260 _fade_out.add ((_length * 0.666667), 0.332853);
261 _fade_out.add ((_length * 0.833333), 0.166366);
262 _fade_out.add ((_length * 1.000000), 0.000000);
270 #define EQUAL_POWER_MINUS_3DB
271 #ifdef EQUAL_POWER_MINUS_3DB
273 _fade_in.add ((_length * 0.000000), 0.000000);
274 _fade_in.add ((_length * 0.166667), 0.282192);
275 _fade_in.add ((_length * 0.333333), 0.518174);
276 _fade_in.add ((_length * 0.500000), 0.707946);
277 _fade_in.add ((_length * 0.666667), 0.851507);
278 _fade_in.add ((_length * 0.833333), 0.948859);
279 _fade_in.add ((_length * 1.000000), 1.000000);
281 #else // EQUAL_POWER_MINUS_SIX_DB
283 _fade_in.add ((_length * 0.000000), 0.000000);
284 _fade_in.add ((_length * 0.166667), 0.166366);
285 _fade_in.add ((_length * 0.333333), 0.332853);
286 _fade_in.add ((_length * 0.500000), 0.499459);
287 _fade_in.add ((_length * 0.666667), 0.666186);
288 _fade_in.add ((_length * 0.833333), 0.833033);
289 _fade_in.add ((_length * 1.000000), 1.000000);
295 overlap_type = _in->coverage (_out->position(), _out->last_frame());
296 layer_relation = (int32_t) (_in->layer() - _out->layer());
300 Crossfade::read_raw_internal (Sample* buf, framecnt_t start, framecnt_t cnt, int channel) const
302 Sample* mixdown = new Sample[cnt];
303 float* gain = new float[cnt];
306 ret = read_at (buf, mixdown, gain, start, cnt, channel, cnt);
315 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
316 float *gain_buffer, framepos_t start, framecnt_t cnt, uint32_t chan_n,
317 framecnt_t read_frames, framecnt_t skip_frames) const
319 frameoffset_t offset;
326 if (start < _position) {
328 /* handle an initial section of the read area that we do not
332 offset = _position - start;
342 to_write = min (_length.val(), (nframes64_t) cnt);
346 to_write = min ((_length - (start - _position)), cnt);
350 offset = start - _position;
352 /* Prevent data from piling up inthe crossfade buffers when reading a transparent region */
353 if (!(_out->opaque())) {
354 memset (crossfade_buffer_out, 0, sizeof (Sample) * to_write);
355 } else if (!(_in->opaque())) {
356 memset (crossfade_buffer_in, 0, sizeof (Sample) * to_write);
359 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
360 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
362 float* fiv = new float[to_write];
363 float* fov = new float[to_write];
365 _fade_in.curve().get_vector (offset, offset+to_write, fiv, to_write);
366 _fade_out.curve().get_vector (offset, offset+to_write, fov, to_write);
368 /* note: although we have not explicitly taken into account the return values
369 from _out->read_at() or _in->read_at(), the length() function does this
370 implicitly. why? because it computes a value based on the in+out regions'
371 position and length, and so we know precisely how much data they could return.
374 for (framecnt_t n = 0; n < to_write; ++n) {
375 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
385 Crossfade::coverage (framepos_t start, framepos_t end) const
387 framepos_t my_end = _position + _length;
389 if ((start >= _position) && (end <= my_end)) {
390 return OverlapInternal;
392 if ((end >= _position) && (end <= my_end)) {
395 if ((start >= _position) && (start <= my_end)) {
398 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
399 return OverlapExternal;
405 Crossfade::set_active (bool yn)
409 PropertyChanged (PropertyChange (Properties::active));
414 Crossfade::refresh ()
416 /* crossfades must be between non-muted regions */
418 if (_out->muted() || _in->muted()) {
419 Invalidated (shared_from_this ());
423 /* Top layer shouldn't be transparent */
425 if (!((layer_relation > 0 ? _in : _out)->opaque())) {
426 Invalidated (shared_from_this());
430 /* layer ordering cannot change */
432 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
434 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
435 Invalidated (shared_from_this ());
439 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
441 if (ot == OverlapNone) {
442 Invalidated (shared_from_this ());
448 if (ot != overlap_type) {
450 if (_follow_overlap) {
453 compute (_in, _out, _session.config.get_xfade_model());
456 catch (NoCrossfadeHere& err) {
457 Invalidated (shared_from_this ());
464 Invalidated (shared_from_this ());
470 send_signal = update ();
474 PropertyChange bounds;
475 bounds.add (Properties::start);
476 bounds.add (Properties::position);
477 bounds.add (Properties::length);
478 PropertyChanged (bounds); /* EMIT SIGNAL */
491 if (_follow_overlap) {
492 newlen = _out->first_frame() + _out->length() - _in->first_frame();
498 Invalidated (shared_from_this ());
504 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
506 double factor = newlen / (double) _length;
508 _fade_out.x_scale (factor);
509 _fade_in.x_scale (factor);
514 switch (_anchor_point) {
516 _position = _in->first_frame();
520 _position = _in->last_frame() - _length;
524 _position = _out->last_frame() - _length;
531 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
533 boost::shared_ptr<AudioRegion> top;
534 boost::shared_ptr<AudioRegion> bottom;
535 framecnt_t short_xfade_length;
537 short_xfade_length = _short_xfade_length;
539 if (a->layer() < b->layer()) {
547 /* first check for matching ends */
549 if (top->first_frame() == bottom->first_frame()) {
551 /* Both regions start at the same point */
553 if (top->last_frame() < bottom->last_frame()) {
555 /* top ends before bottom, so put an xfade
556 in at the end of top.
559 /* [-------- top ---------- ]
560 * {====== bottom =====================}
566 if (top->last_frame() < short_xfade_length) {
569 _position = top->last_frame() - short_xfade_length;
572 _length = min (short_xfade_length, top->length());
573 _follow_overlap = false;
574 _anchor_point = EndOfIn;
579 /* top ends after (or same time) as bottom - no xfade
582 /* [-------- top ------------------------ ]
583 * {====== bottom =====================}
586 throw NoCrossfadeHere();
589 } else if (top->last_frame() == bottom->last_frame()) {
591 /* Both regions end at the same point */
593 if (top->first_frame() > bottom->first_frame()) {
595 /* top starts after bottom, put an xfade in at the
599 /* [-------- top ---------- ]
600 * {====== bottom =====================}
605 _position = top->first_frame();
606 _length = min (short_xfade_length, top->length());
607 _follow_overlap = false;
608 _anchor_point = StartOfIn;
613 /* top starts before bottom - no xfade
616 /* [-------- top ------------------------ ]
617 * {====== bottom =====================}
620 throw NoCrossfadeHere();
625 /* OK, time to do more regular overlapping */
627 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
631 /* should be NOTREACHED as a precondition of creating
632 a new crossfade, but we need to handle it here.
634 throw NoCrossfadeHere();
637 case OverlapInternal:
638 case OverlapExternal:
639 /* should be NOTREACHED because of tests above */
640 throw NoCrossfadeHere();
643 case OverlapEnd: /* top covers start of bottom but ends within it */
645 /* [---- top ------------------------]
646 * { ==== bottom ============ }
651 _anchor_point = EndOfOut;
653 if (model == FullCrossfade) {
654 _position = bottom->first_frame(); // "{"
655 _length = _out->first_frame() + _out->length() - _in->first_frame();
656 /* leave active alone */
657 _follow_overlap = true;
659 _length = min (short_xfade_length, top->length());
660 _position = top->last_frame() - _length; // "]" - length
662 _follow_overlap = false;
667 case OverlapStart: /* top starts within bottom but covers bottom's end */
669 /* { ==== top ============ }
670 * [---- bottom -------------------]
675 _position = top->first_frame();
676 _anchor_point = StartOfIn;
678 if (model == FullCrossfade) {
679 _length = _out->first_frame() + _out->length() - _in->first_frame();
680 /* leave active alone */
681 _follow_overlap = true;
683 _length = min (short_xfade_length, top->length());
685 _follow_overlap = false;
697 Crossfade::get_state ()
699 XMLNode* node = new XMLNode (X_("Crossfade"));
702 LocaleGuard lg (X_("POSIX"));
704 _out->id().print (buf, sizeof (buf));
705 node->add_property ("out", buf);
706 _in->id().print (buf, sizeof (buf));
707 node->add_property ("in", buf);
708 node->add_property ("active", (_active ? "yes" : "no"));
709 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
710 node->add_property ("fixed", (_fixed ? "yes" : "no"));
711 snprintf (buf, sizeof(buf), "%" PRId64, _length.val());
712 node->add_property ("length", buf);
713 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
714 node->add_property ("anchor-point", buf);
715 snprintf (buf, sizeof(buf), "%" PRId64, _position.val());
716 node->add_property ("position", buf);
718 child = node->add_child ("FadeIn");
720 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
723 pnode = new XMLNode ("point");
725 snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
726 pnode->add_property ("x", buf);
727 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
728 pnode->add_property ("y", buf);
729 child->add_child_nocopy (*pnode);
732 child = node->add_child ("FadeOut");
734 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
737 pnode = new XMLNode ("point");
739 snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
740 pnode->add_property ("x", buf);
741 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
742 pnode->add_property ("y", buf);
743 child->add_child_nocopy (*pnode);
750 Crossfade::set_state (const XMLNode& node, int /*version*/)
752 XMLNodeConstIterator i;
753 XMLNodeList children;
756 const XMLProperty* prop;
757 LocaleGuard lg (X_("POSIX"));
758 PropertyChange what_changed;
761 if ((prop = node.property ("position")) != 0) {
762 sscanf (prop->value().c_str(), "%" PRId64, &val);
763 if (val != _position) {
765 what_changed.add (Properties::position);
768 warning << _("old-style crossfade information - no position information") << endmsg;
769 _position = _in->first_frame();
772 if ((prop = node.property ("active")) != 0) {
773 bool x = string_is_affirmative (prop->value());
776 what_changed.add (Properties::active);
782 if ((prop = node.property ("follow-overlap")) != 0) {
783 _follow_overlap = string_is_affirmative (prop->value());
785 _follow_overlap = false;
788 if ((prop = node.property ("fixed")) != 0) {
789 _fixed = string_is_affirmative (prop->value());
794 if ((prop = node.property ("anchor-point")) != 0) {
795 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
797 _anchor_point = StartOfIn;
800 if ((prop = node.property ("length")) != 0) {
802 sscanf (prop->value().c_str(), "%" PRId64, &val);
803 if (val != _length) {
805 what_changed.add (Properties::length);
810 /* XXX this branch is legacy code from before
811 the point where we stored xfade lengths.
814 if ((_length = overlap_length()) == 0) {
815 throw failed_constructor();
819 if ((fi = find_named_node (node, "FadeIn")) == 0) {
823 if ((fo = find_named_node (node, "FadeOut")) == 0) {
832 children = fi->children();
834 for (i = children.begin(); i != children.end(); ++i) {
835 if ((*i)->name() == "point") {
839 prop = (*i)->property ("x");
840 sscanf (prop->value().c_str(), "%" PRId64, &x);
842 prop = (*i)->property ("y");
843 sscanf (prop->value().c_str(), "%f", &y);
849 _fade_in.front()->value = 0.0;
850 _fade_in.back()->value = 1.0;
859 children = fo->children();
861 for (i = children.begin(); i != children.end(); ++i) {
862 if ((*i)->name() == "point") {
867 prop = (*i)->property ("x");
868 sscanf (prop->value().c_str(), "%" PRId64, &x);
870 prop = (*i)->property ("y");
871 sscanf (prop->value().c_str(), "%f", &y);
873 _fade_out.add (x, y);
877 _fade_out.front()->value = 1.0;
878 _fade_out.back()->value = 0.0;
882 PropertyChanged (what_changed); /* EMIT SIGNAL */
883 FadesChanged (); /* EMIT SIGNAL */
889 Crossfade::can_follow_overlap () const
895 Crossfade::set_follow_overlap (bool yn)
897 if (yn == _follow_overlap || _fixed) {
901 _follow_overlap = yn;
904 set_xfade_length (_short_xfade_length);
906 set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
909 PropertyChanged (PropertyChange (Properties::follow_overlap));
913 Crossfade::set_xfade_length (framecnt_t len)
915 framecnt_t limit = 0;
917 switch (_anchor_point) {
919 limit = _in->length();
923 limit = _in->length();
927 limit = _out->length();
932 len = min (limit, len);
934 double factor = len / (double) _length;
937 _fade_out.x_scale (factor);
938 _fade_in.x_scale (factor);
943 PropertyChanged (PropertyChange (Properties::length));
949 Crossfade::overlap_length () const
954 return _out->first_frame() + _out->length() - _in->first_frame();
958 Crossfade::set_short_xfade_length (framecnt_t n)
960 _short_xfade_length = n;