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 <sigc++/bind.h>
22 #include <pbd/stacktrace.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 nframes_t Crossfade::_short_xfade_length = 0;
41 Change Crossfade::ActiveChanged = new_change();
42 Change Crossfade::FollowOverlapChanged = new_change();
44 /* XXX if and when we ever implement parallel processing of the process()
45 callback, these will need to be handled on a per-thread basis.
48 Sample* Crossfade::crossfade_buffer_out = 0;
49 Sample* Crossfade::crossfade_buffer_in = 0;
52 Crossfade::set_buffer_size (nframes_t sz)
54 delete [] crossfade_buffer_out;
55 crossfade_buffer_out = 0;
57 delete [] crossfade_buffer_in;
58 crossfade_buffer_in = 0;
61 crossfade_buffer_out = new Sample[sz];
62 crossfade_buffer_in = new Sample[sz];
67 Crossfade::operator== (const Crossfade& other)
69 return (_in == other._in) && (_out == other._out);
72 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out,
76 : AudioRegion (in->session(), position, length, "foobar"),
77 _fade_in (Evoral::Parameter(FadeInAutomation)), // linear (gain coefficient) => -inf..+6dB
78 _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
84 _follow_overlap = false;
86 _active = Config->get_xfades_active ();
92 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
93 : AudioRegion (a->session(), 0, 0, "foobar"),
94 _fade_in (Evoral::Parameter(FadeInAutomation)), // linear (gain coefficient) => -inf..+6dB
95 _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
100 if (compute (a, b, model)) {
101 throw failed_constructor();
111 Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
112 : AudioRegion (playlist.session(), 0, 0, "foobar"),
113 _fade_in (Evoral::Parameter(FadeInAutomation)), // linear (gain coefficient) => -inf..+6dB
114 _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
117 boost::shared_ptr<Region> r;
119 LocaleGuard lg (X_("POSIX"));
121 /* we have to find the in/out regions before we can do anything else */
123 if ((prop = node.property ("in")) == 0) {
124 error << _("Crossfade: no \"in\" region in state") << endmsg;
125 throw failed_constructor();
128 PBD::ID id (prop->value());
130 if ((r = playlist.find_region (id)) == 0) {
131 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2"), id, playlist.name())
133 throw failed_constructor();
136 if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
137 throw failed_constructor();
140 if ((prop = node.property ("out")) == 0) {
141 error << _("Crossfade: no \"out\" region in state") << endmsg;
142 throw failed_constructor();
145 PBD::ID id2 (prop->value());
147 if ((r = playlist.find_region (id2)) == 0) {
148 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2"), id2, playlist.name())
150 throw failed_constructor();
153 if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
154 throw failed_constructor();
161 if (set_state (node)) {
162 throw failed_constructor();
166 Crossfade::Crossfade (boost::shared_ptr<Crossfade> orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
167 : AudioRegion (boost::dynamic_pointer_cast<const AudioRegion> (orig)),
168 _fade_in (orig->_fade_in),
169 _fade_out (orig->_fade_out)
171 _active = orig->_active;
172 _in_update = orig->_in_update;
173 _anchor_point = orig->_anchor_point;
174 _follow_overlap = orig->_follow_overlap;
175 _fixed = orig->_fixed;
180 // copied from Crossfade::initialize()
183 _out->suspend_fade_out ();
184 _in->suspend_fade_in ();
186 overlap_type = _in->coverage (_out->position(), _out->last_frame());
187 layer_relation = (int32_t) (_in->layer() - _out->layer());
189 // Let's make sure the fade isn't too long
194 Crossfade::~Crossfade ()
200 Crossfade::initialize ()
202 /* merge source lists from regions */
204 _sources = _in->sources();
205 _sources.insert (_sources.end(), _out->sources().begin(), _out->sources().end());
206 _master_sources = _in->master_sources();
207 _master_sources.insert(_master_sources.end(), _out->master_sources().begin(), _out->master_sources().end());
211 _out->suspend_fade_out ();
212 _in->suspend_fade_in ();
217 #define EQUAL_POWER_MINUS_3DB
218 #ifdef EQUAL_POWER_MINUS_3DB
220 _fade_out.add ((_length * 0.000000), 1.000000);
221 _fade_out.add ((_length * 0.166667), 0.948859);
222 _fade_out.add ((_length * 0.333333), 0.851507);
223 _fade_out.add ((_length * 0.500000), 0.707946);
224 _fade_out.add ((_length * 0.666667), 0.518174);
225 _fade_out.add ((_length * 0.833333), 0.282192);
226 _fade_out.add ((_length * 1.000000), 0.000000);
228 #else // EQUAL_POWER_MINUS_6DB
230 _fade_out.add ((_length * 0.000000), 1.000000);
231 _fade_out.add ((_length * 0.166667), 0.833033);
232 _fade_out.add ((_length * 0.333333), 0.666186);
233 _fade_out.add ((_length * 0.500000), 0.499459);
234 _fade_out.add ((_length * 0.666667), 0.332853);
235 _fade_out.add ((_length * 0.833333), 0.166366);
236 _fade_out.add ((_length * 1.000000), 0.000000);
244 #define EQUAL_POWER_MINUS_3DB
245 #ifdef EQUAL_POWER_MINUS_3DB
247 _fade_in.add ((_length * 0.000000), 0.000000);
248 _fade_in.add ((_length * 0.166667), 0.282192);
249 _fade_in.add ((_length * 0.333333), 0.518174);
250 _fade_in.add ((_length * 0.500000), 0.707946);
251 _fade_in.add ((_length * 0.666667), 0.851507);
252 _fade_in.add ((_length * 0.833333), 0.948859);
253 _fade_in.add ((_length * 1.000000), 1.000000);
255 #else // EQUAL_POWER_MINUS_SIX_DB
257 _fade_in.add ((_length * 0.000000), 0.000000);
258 _fade_in.add ((_length * 0.166667), 0.166366);
259 _fade_in.add ((_length * 0.333333), 0.332853);
260 _fade_in.add ((_length * 0.500000), 0.499459);
261 _fade_in.add ((_length * 0.666667), 0.666186);
262 _fade_in.add ((_length * 0.833333), 0.833033);
263 _fade_in.add ((_length * 1.000000), 1.000000);
269 overlap_type = _in->coverage (_out->position(), _out->last_frame());
270 layer_relation = (int32_t) (_in->layer() - _out->layer());
274 Crossfade::read_raw_internal (Sample* buf, nframes_t start, nframes_t cnt) const
277 Sample* mixdown = new Sample[cnt];
278 float* gain = new float[cnt];
281 ret = read_at (buf, mixdown, gain, start, cnt, chan_n, cnt);
292 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
293 float *gain_buffer, nframes_t start, nframes_t cnt, uint32_t chan_n,
294 nframes_t read_frames, nframes_t skip_frames) const
303 if (start < _position) {
305 /* handle an initial section of the read area that we do not
309 offset = _position - start;
319 to_write = min (_length, cnt);
323 to_write = min (_length - (start - _position), cnt);
327 offset = start - _position;
329 /* Prevent data from piling up inthe crossfade buffers when reading a transparent region */
330 if (!(_out->opaque())) {
331 memset (crossfade_buffer_out, 0, sizeof (Sample) * to_write);
332 } else if (!(_in->opaque())) {
333 memset (crossfade_buffer_in, 0, sizeof (Sample) * to_write);
336 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
337 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
339 float* fiv = new float[to_write];
340 float* fov = new float[to_write];
342 _fade_in.curve().get_vector (offset, offset+to_write, fiv, to_write);
343 _fade_out.curve().get_vector (offset, offset+to_write, fov, to_write);
345 /* note: although we have not explicitly taken into account the return values
346 from _out->read_at() or _in->read_at(), the length() function does this
347 implicitly. why? because it computes a value based on the in+out regions'
348 position and length, and so we know precisely how much data they could return.
351 for (nframes_t n = 0; n < to_write; ++n) {
352 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
362 Crossfade::coverage (nframes_t start, nframes_t end) const
364 nframes_t my_end = _position + _length;
366 if ((start >= _position) && (end <= my_end)) {
367 return OverlapInternal;
369 if ((end >= _position) && (end <= my_end)) {
372 if ((start >= _position) && (start <= my_end)) {
375 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
376 return OverlapExternal;
382 Crossfade::set_active (bool yn)
386 StateChanged (ActiveChanged);
391 Crossfade::refresh ()
393 /* crossfades must be between non-muted regions */
395 if (_out->muted() || _in->muted()) {
396 Invalidated (shared_from_this ());
400 /* Top layer shouldn't be transparent */
402 if (!((layer_relation > 0 ? _in : _out)->opaque())) {
403 Invalidated (shared_from_this());
407 /* layer ordering cannot change */
409 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
411 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
412 Invalidated (shared_from_this ());
416 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
418 if (ot == OverlapNone) {
419 Invalidated (shared_from_this ());
425 if (ot != overlap_type) {
427 if (_follow_overlap) {
430 compute (_in, _out, Config->get_xfade_model());
433 catch (NoCrossfadeHere& err) {
434 Invalidated (shared_from_this ());
442 Invalidated (shared_from_this ());
448 send_signal = update ();
452 StateChanged (BoundsChanged); /* EMIT SIGNAL */
465 if (_follow_overlap) {
466 newlen = _out->first_frame() + _out->length() - _in->first_frame();
472 Invalidated (shared_from_this ());
478 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
480 double factor = newlen / (double) _length;
482 _fade_out.x_scale (factor);
483 _fade_in.x_scale (factor);
488 switch (_anchor_point) {
490 _position = _in->first_frame();
494 _position = _in->last_frame() - _length;
498 _position = _out->last_frame() - _length;
505 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
507 boost::shared_ptr<AudioRegion> top;
508 boost::shared_ptr<AudioRegion> bottom;
509 nframes_t short_xfade_length;
511 short_xfade_length = _short_xfade_length;
513 if (a->layer() < b->layer()) {
521 /* first check for matching ends */
523 if (top->first_frame() == bottom->first_frame()) {
525 /* Both regions start at the same point */
527 if (top->last_frame() < bottom->last_frame()) {
529 /* top ends before bottom, so put an xfade
530 in at the end of top.
533 /* [-------- top ---------- ]
534 * {====== bottom =====================}
540 if (top->last_frame() < short_xfade_length) {
543 _position = top->last_frame() - short_xfade_length;
546 _length = min (short_xfade_length, top->length());
547 _follow_overlap = false;
548 _anchor_point = EndOfIn;
553 /* top ends after (or same time) as bottom - no xfade
556 /* [-------- top ------------------------ ]
557 * {====== bottom =====================}
560 throw NoCrossfadeHere();
563 } else if (top->last_frame() == bottom->last_frame()) {
565 /* Both regions end at the same point */
567 if (top->first_frame() > bottom->first_frame()) {
569 /* top starts after bottom, put an xfade in at the
573 /* [-------- top ---------- ]
574 * {====== bottom =====================}
579 _position = top->first_frame();
580 _length = min (short_xfade_length, top->length());
581 _follow_overlap = false;
582 _anchor_point = StartOfIn;
587 /* top starts before bottom - no xfade
590 /* [-------- top ------------------------ ]
591 * {====== bottom =====================}
594 throw NoCrossfadeHere();
599 /* OK, time to do more regular overlapping */
601 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
605 /* should be NOTREACHED as a precondition of creating
606 a new crossfade, but we need to handle it here.
608 throw NoCrossfadeHere();
611 case OverlapInternal:
612 case OverlapExternal:
613 /* should be NOTREACHED because of tests above */
614 throw NoCrossfadeHere();
617 case OverlapEnd: /* top covers start of bottom but ends within it */
619 /* [---- top ------------------------]
620 * { ==== bottom ============ }
625 _anchor_point = EndOfOut;
627 if (model == FullCrossfade) {
628 _position = bottom->first_frame(); // "{"
629 _length = _out->first_frame() + _out->length() - _in->first_frame();
630 /* leave active alone */
631 _follow_overlap = true;
633 _length = min (short_xfade_length, top->length());
634 _position = top->last_frame() - _length; // "]" - length
636 _follow_overlap = false;
641 case OverlapStart: /* top starts within bottom but covers bottom's end */
643 /* { ==== top ============ }
644 * [---- bottom -------------------]
649 _position = top->first_frame();
650 _anchor_point = StartOfIn;
652 if (model == FullCrossfade) {
653 _length = _out->first_frame() + _out->length() - _in->first_frame();
654 /* leave active alone */
655 _follow_overlap = true;
657 _length = min (short_xfade_length, top->length());
659 _follow_overlap = false;
671 Crossfade::get_state ()
673 XMLNode* node = new XMLNode (X_("Crossfade"));
676 LocaleGuard lg (X_("POSIX"));
678 _out->id().print (buf, sizeof (buf));
679 node->add_property ("out", buf);
680 _in->id().print (buf, sizeof (buf));
681 node->add_property ("in", buf);
682 node->add_property ("active", (_active ? "yes" : "no"));
683 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
684 node->add_property ("fixed", (_fixed ? "yes" : "no"));
685 snprintf (buf, sizeof(buf), "%" PRIu32, _length);
686 node->add_property ("length", buf);
687 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
688 node->add_property ("anchor-point", buf);
689 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
690 node->add_property ("position", buf);
692 child = node->add_child ("FadeIn");
694 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
697 pnode = new XMLNode ("point");
699 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
700 pnode->add_property ("x", buf);
701 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
702 pnode->add_property ("y", buf);
703 child->add_child_nocopy (*pnode);
706 child = node->add_child ("FadeOut");
708 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
711 pnode = new XMLNode ("point");
713 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
714 pnode->add_property ("x", buf);
715 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
716 pnode->add_property ("y", buf);
717 child->add_child_nocopy (*pnode);
724 Crossfade::set_state (const XMLNode& node)
726 XMLNodeConstIterator i;
727 XMLNodeList children;
730 const XMLProperty* prop;
731 LocaleGuard lg (X_("POSIX"));
732 Change what_changed = Change (0);
735 if ((prop = node.property ("position")) != 0) {
736 sscanf (prop->value().c_str(), "%" PRIu32, &val);
737 if (val != _position) {
739 what_changed = Change (what_changed | PositionChanged);
742 warning << _("old-style crossfade information - no position information") << endmsg;
743 _position = _in->first_frame();
746 if ((prop = node.property ("active")) != 0) {
747 bool x = (prop->value() == "yes");
750 what_changed = Change (what_changed | ActiveChanged);
756 if ((prop = node.property ("follow-overlap")) != 0) {
757 _follow_overlap = (prop->value() == "yes");
759 _follow_overlap = false;
762 if ((prop = node.property ("fixed")) != 0) {
763 _fixed = (prop->value() == "yes");
768 if ((prop = node.property ("anchor-point")) != 0) {
769 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
771 _anchor_point = StartOfIn;
774 if ((prop = node.property ("length")) != 0) {
776 sscanf (prop->value().c_str(), "%" PRIu32, &val);
777 if (val != _length) {
778 _length = atol (prop->value().c_str());
779 what_changed = Change (what_changed | LengthChanged);
784 /* XXX this branch is legacy code from before
785 the point where we stored xfade lengths.
788 if ((_length = overlap_length()) == 0) {
789 throw failed_constructor();
793 if ((fi = find_named_node (node, "FadeIn")) == 0) {
797 if ((fo = find_named_node (node, "FadeOut")) == 0) {
806 children = fi->children();
808 for (i = children.begin(); i != children.end(); ++i) {
809 if ((*i)->name() == "point") {
813 prop = (*i)->property ("x");
814 sscanf (prop->value().c_str(), "%" PRIu32, &x);
816 prop = (*i)->property ("y");
817 sscanf (prop->value().c_str(), "%f", &y);
830 children = fo->children();
832 for (i = children.begin(); i != children.end(); ++i) {
833 if ((*i)->name() == "point") {
838 prop = (*i)->property ("x");
839 sscanf (prop->value().c_str(), "%" PRIu32, &x);
841 prop = (*i)->property ("y");
842 sscanf (prop->value().c_str(), "%f", &y);
844 _fade_out.add (x, y);
850 StateChanged (what_changed); /* EMIT SIGNAL */
856 Crossfade::can_follow_overlap () const
862 Crossfade::set_follow_overlap (bool yn)
864 if (yn == _follow_overlap || _fixed) {
868 _follow_overlap = yn;
871 set_length (_short_xfade_length);
873 set_length (_out->first_frame() + _out->length() - _in->first_frame());
876 StateChanged (FollowOverlapChanged);
880 Crossfade::set_length (nframes_t len)
884 switch (_anchor_point) {
886 limit = _in->length();
890 limit = _in->length();
894 limit = _out->length();
899 len = min (limit, len);
901 double factor = len / (double) _length;
904 _fade_out.x_scale (factor);
905 _fade_in.x_scale (factor);
910 StateChanged (LengthChanged);
916 Crossfade::overlap_length () const
921 return _out->first_frame() + _out->length() - _in->first_frame();
925 Crossfade::set_short_xfade_length (nframes_t n)
927 _short_xfade_length = n;
931 Crossfade::invalidate ()
933 Invalidated (shared_from_this ()); /* EMIT SIGNAL */