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/types.h"
24 #include "ardour/crossfade.h"
25 #include "ardour/crossfade_compare.h"
26 #include "ardour/audioregion.h"
27 #include "ardour/playlist.h"
28 #include "ardour/utils.h"
29 #include "ardour/session.h"
30 #include "ardour/source.h"
36 using namespace ARDOUR;
39 framecnt_t Crossfade::_short_xfade_length = 0;
40 PropertyChange Crossfade::ActiveChanged = new_change();
41 PropertyChange Crossfade::FollowOverlapChanged = new_change();
43 /* XXX if and when we ever implement parallel processing of the process()
44 callback, these will need to be handled on a per-thread basis.
47 Sample* Crossfade::crossfade_buffer_out = 0;
48 Sample* Crossfade::crossfade_buffer_in = 0;
51 Crossfade::set_buffer_size (framecnt_t sz)
53 delete [] crossfade_buffer_out;
54 crossfade_buffer_out = 0;
56 delete [] crossfade_buffer_in;
57 crossfade_buffer_in = 0;
60 crossfade_buffer_out = new Sample[sz];
61 crossfade_buffer_in = new Sample[sz];
66 Crossfade::operator== (const Crossfade& other)
68 return (_in == other._in) && (_out == other._out);
71 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out,
75 : AudioRegion (in->session(), position, length, in->name() + string ("<>") + out->name()),
76 _fade_in (Evoral::Parameter(FadeInAutomation)), // linear (gain coefficient) => -inf..+6dB
77 _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
83 _follow_overlap = false;
85 _active = _session.config.get_xfades_active ();
91 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
92 : AudioRegion (a->session(), 0, 0, a->name() + string ("<>") + b->name()),
93 _fade_in (Evoral::Parameter(FadeInAutomation)), // linear (gain coefficient) => -inf..+6dB
94 _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
98 _follow_overlap = false;
100 if (compute (a, b, model)) {
101 throw failed_constructor();
109 Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
110 : AudioRegion (playlist.session(), 0, 0, "unnamed crossfade"),
111 _fade_in (Evoral::Parameter(FadeInAutomation)), // linear (gain coefficient) => -inf..+6dB
112 _fade_out (Evoral::Parameter(FadeOutAutomation)) // linear (gain coefficient) => -inf..+6dB
115 boost::shared_ptr<Region> r;
117 LocaleGuard lg (X_("POSIX"));
119 /* we have to find the in/out regions before we can do anything else */
121 if ((prop = node.property ("in")) == 0) {
122 error << _("Crossfade: no \"in\" region in state") << endmsg;
123 throw failed_constructor();
126 PBD::ID id (prop->value());
128 if ((r = playlist.find_region (id)) == 0) {
129 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2"), id, playlist.name())
131 throw failed_constructor();
134 if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
135 throw failed_constructor();
138 if ((prop = node.property ("out")) == 0) {
139 error << _("Crossfade: no \"out\" region in state") << endmsg;
140 throw failed_constructor();
143 PBD::ID id2 (prop->value());
145 if ((r = playlist.find_region (id2)) == 0) {
146 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2"), id2, playlist.name())
148 throw failed_constructor();
151 if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
152 throw failed_constructor();
159 if (set_state (node, Stateful::loading_state_version)) {
160 throw failed_constructor();
164 Crossfade::Crossfade (boost::shared_ptr<Crossfade> orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
165 : AudioRegion (boost::dynamic_pointer_cast<const AudioRegion> (orig), 0, true)
166 , _fade_in (orig->_fade_in)
167 , _fade_out (orig->_fade_out)
169 _active = orig->_active;
170 _in_update = orig->_in_update;
171 _anchor_point = orig->_anchor_point;
172 _follow_overlap = orig->_follow_overlap;
173 _fixed = orig->_fixed;
178 // copied from Crossfade::initialize()
181 _out->suspend_fade_out ();
182 _in->suspend_fade_in ();
184 overlap_type = _in->coverage (_out->position(), _out->last_frame());
185 layer_relation = (int32_t) (_in->layer() - _out->layer());
187 // Let's make sure the fade isn't too long
188 set_xfade_length(_length);
192 Crossfade::~Crossfade ()
197 Crossfade::initialize ()
199 /* merge source lists from regions */
201 _sources = _in->sources();
202 _sources.insert (_sources.end(), _out->sources().begin(), _out->sources().end());
203 _master_sources = _in->master_sources();
204 _master_sources.insert(_master_sources.end(), _out->master_sources().begin(), _out->master_sources().end());
208 _out->suspend_fade_out ();
209 _in->suspend_fade_in ();
214 #define EQUAL_POWER_MINUS_3DB
215 #ifdef EQUAL_POWER_MINUS_3DB
217 _fade_out.add ((_length * 0.000000), 1.000000);
218 _fade_out.add ((_length * 0.166667), 0.948859);
219 _fade_out.add ((_length * 0.333333), 0.851507);
220 _fade_out.add ((_length * 0.500000), 0.707946);
221 _fade_out.add ((_length * 0.666667), 0.518174);
222 _fade_out.add ((_length * 0.833333), 0.282192);
223 _fade_out.add ((_length * 1.000000), 0.000000);
225 #else // EQUAL_POWER_MINUS_6DB
227 _fade_out.add ((_length * 0.000000), 1.000000);
228 _fade_out.add ((_length * 0.166667), 0.833033);
229 _fade_out.add ((_length * 0.333333), 0.666186);
230 _fade_out.add ((_length * 0.500000), 0.499459);
231 _fade_out.add ((_length * 0.666667), 0.332853);
232 _fade_out.add ((_length * 0.833333), 0.166366);
233 _fade_out.add ((_length * 1.000000), 0.000000);
241 #define EQUAL_POWER_MINUS_3DB
242 #ifdef EQUAL_POWER_MINUS_3DB
244 _fade_in.add ((_length * 0.000000), 0.000000);
245 _fade_in.add ((_length * 0.166667), 0.282192);
246 _fade_in.add ((_length * 0.333333), 0.518174);
247 _fade_in.add ((_length * 0.500000), 0.707946);
248 _fade_in.add ((_length * 0.666667), 0.851507);
249 _fade_in.add ((_length * 0.833333), 0.948859);
250 _fade_in.add ((_length * 1.000000), 1.000000);
252 #else // EQUAL_POWER_MINUS_SIX_DB
254 _fade_in.add ((_length * 0.000000), 0.000000);
255 _fade_in.add ((_length * 0.166667), 0.166366);
256 _fade_in.add ((_length * 0.333333), 0.332853);
257 _fade_in.add ((_length * 0.500000), 0.499459);
258 _fade_in.add ((_length * 0.666667), 0.666186);
259 _fade_in.add ((_length * 0.833333), 0.833033);
260 _fade_in.add ((_length * 1.000000), 1.000000);
266 overlap_type = _in->coverage (_out->position(), _out->last_frame());
267 layer_relation = (int32_t) (_in->layer() - _out->layer());
271 Crossfade::read_raw_internal (Sample* buf, framecnt_t start, framecnt_t cnt, int channel) const
273 Sample* mixdown = new Sample[cnt];
274 float* gain = new float[cnt];
277 ret = read_at (buf, mixdown, gain, start, cnt, channel, cnt);
286 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
287 float *gain_buffer, framepos_t start, framecnt_t cnt, uint32_t chan_n,
288 framecnt_t read_frames, framecnt_t skip_frames) const
290 frameoffset_t offset;
297 if (start < _position) {
299 /* handle an initial section of the read area that we do not
303 offset = _position - start;
313 to_write = min (_length.val(), (nframes64_t) cnt);
317 to_write = min ((_length - (start - _position)), cnt);
321 offset = start - _position;
323 /* Prevent data from piling up inthe crossfade buffers when reading a transparent region */
324 if (!(_out->opaque())) {
325 memset (crossfade_buffer_out, 0, sizeof (Sample) * to_write);
326 } else if (!(_in->opaque())) {
327 memset (crossfade_buffer_in, 0, sizeof (Sample) * to_write);
330 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
331 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
333 float* fiv = new float[to_write];
334 float* fov = new float[to_write];
336 _fade_in.curve().get_vector (offset, offset+to_write, fiv, to_write);
337 _fade_out.curve().get_vector (offset, offset+to_write, fov, to_write);
339 /* note: although we have not explicitly taken into account the return values
340 from _out->read_at() or _in->read_at(), the length() function does this
341 implicitly. why? because it computes a value based on the in+out regions'
342 position and length, and so we know precisely how much data they could return.
345 for (framecnt_t n = 0; n < to_write; ++n) {
346 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
356 Crossfade::coverage (framepos_t start, framepos_t end) const
358 framepos_t my_end = _position + _length;
360 if ((start >= _position) && (end <= my_end)) {
361 return OverlapInternal;
363 if ((end >= _position) && (end <= my_end)) {
366 if ((start >= _position) && (start <= my_end)) {
369 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
370 return OverlapExternal;
376 Crossfade::set_active (bool yn)
380 StateChanged (ActiveChanged);
385 Crossfade::refresh ()
387 /* crossfades must be between non-muted regions */
389 if (_out->muted() || _in->muted()) {
390 Invalidated (shared_from_this ());
394 /* Top layer shouldn't be transparent */
396 if (!((layer_relation > 0 ? _in : _out)->opaque())) {
397 Invalidated (shared_from_this());
401 /* layer ordering cannot change */
403 int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
405 if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated
406 Invalidated (shared_from_this ());
410 OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
412 if (ot == OverlapNone) {
413 Invalidated (shared_from_this ());
419 if (ot != overlap_type) {
421 if (_follow_overlap) {
424 compute (_in, _out, _session.config.get_xfade_model());
427 catch (NoCrossfadeHere& err) {
428 Invalidated (shared_from_this ());
435 Invalidated (shared_from_this ());
441 send_signal = update ();
445 StateChanged (BoundsChanged); /* EMIT SIGNAL */
458 if (_follow_overlap) {
459 newlen = _out->first_frame() + _out->length() - _in->first_frame();
465 Invalidated (shared_from_this ());
471 if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
473 double factor = newlen / (double) _length;
475 _fade_out.x_scale (factor);
476 _fade_in.x_scale (factor);
481 switch (_anchor_point) {
483 _position = _in->first_frame();
487 _position = _in->last_frame() - _length;
491 _position = _out->last_frame() - _length;
498 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
500 boost::shared_ptr<AudioRegion> top;
501 boost::shared_ptr<AudioRegion> bottom;
502 framecnt_t short_xfade_length;
504 short_xfade_length = _short_xfade_length;
506 if (a->layer() < b->layer()) {
514 /* first check for matching ends */
516 if (top->first_frame() == bottom->first_frame()) {
518 /* Both regions start at the same point */
520 if (top->last_frame() < bottom->last_frame()) {
522 /* top ends before bottom, so put an xfade
523 in at the end of top.
526 /* [-------- top ---------- ]
527 * {====== bottom =====================}
533 if (top->last_frame() < short_xfade_length) {
536 _position = top->last_frame() - short_xfade_length;
539 _length = min (short_xfade_length, top->length());
540 _follow_overlap = false;
541 _anchor_point = EndOfIn;
546 /* top ends after (or same time) as bottom - no xfade
549 /* [-------- top ------------------------ ]
550 * {====== bottom =====================}
553 throw NoCrossfadeHere();
556 } else if (top->last_frame() == bottom->last_frame()) {
558 /* Both regions end at the same point */
560 if (top->first_frame() > bottom->first_frame()) {
562 /* top starts after bottom, put an xfade in at the
566 /* [-------- top ---------- ]
567 * {====== bottom =====================}
572 _position = top->first_frame();
573 _length = min (short_xfade_length, top->length());
574 _follow_overlap = false;
575 _anchor_point = StartOfIn;
580 /* top starts before bottom - no xfade
583 /* [-------- top ------------------------ ]
584 * {====== bottom =====================}
587 throw NoCrossfadeHere();
592 /* OK, time to do more regular overlapping */
594 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
598 /* should be NOTREACHED as a precondition of creating
599 a new crossfade, but we need to handle it here.
601 throw NoCrossfadeHere();
604 case OverlapInternal:
605 case OverlapExternal:
606 /* should be NOTREACHED because of tests above */
607 throw NoCrossfadeHere();
610 case OverlapEnd: /* top covers start of bottom but ends within it */
612 /* [---- top ------------------------]
613 * { ==== bottom ============ }
618 _anchor_point = EndOfOut;
620 if (model == FullCrossfade) {
621 _position = bottom->first_frame(); // "{"
622 _length = _out->first_frame() + _out->length() - _in->first_frame();
623 /* leave active alone */
624 _follow_overlap = true;
626 _length = min (short_xfade_length, top->length());
627 _position = top->last_frame() - _length; // "]" - length
629 _follow_overlap = false;
634 case OverlapStart: /* top starts within bottom but covers bottom's end */
636 /* { ==== top ============ }
637 * [---- bottom -------------------]
642 _position = top->first_frame();
643 _anchor_point = StartOfIn;
645 if (model == FullCrossfade) {
646 _length = _out->first_frame() + _out->length() - _in->first_frame();
647 /* leave active alone */
648 _follow_overlap = true;
650 _length = min (short_xfade_length, top->length());
652 _follow_overlap = false;
664 Crossfade::get_state ()
666 XMLNode* node = new XMLNode (X_("Crossfade"));
669 LocaleGuard lg (X_("POSIX"));
671 _out->id().print (buf, sizeof (buf));
672 node->add_property ("out", buf);
673 _in->id().print (buf, sizeof (buf));
674 node->add_property ("in", buf);
675 node->add_property ("active", (_active ? "yes" : "no"));
676 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
677 node->add_property ("fixed", (_fixed ? "yes" : "no"));
678 snprintf (buf, sizeof(buf), "%" PRId64, _length.val());
679 node->add_property ("length", buf);
680 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
681 node->add_property ("anchor-point", buf);
682 snprintf (buf, sizeof(buf), "%" PRId64, _position.val());
683 node->add_property ("position", buf);
685 child = node->add_child ("FadeIn");
687 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
690 pnode = new XMLNode ("point");
692 snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
693 pnode->add_property ("x", buf);
694 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
695 pnode->add_property ("y", buf);
696 child->add_child_nocopy (*pnode);
699 child = node->add_child ("FadeOut");
701 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
704 pnode = new XMLNode ("point");
706 snprintf (buf, sizeof (buf), "%" PRId64, (framepos_t) floor ((*ii)->when));
707 pnode->add_property ("x", buf);
708 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
709 pnode->add_property ("y", buf);
710 child->add_child_nocopy (*pnode);
717 Crossfade::set_state (const XMLNode& node, int /*version*/)
719 XMLNodeConstIterator i;
720 XMLNodeList children;
723 const XMLProperty* prop;
724 LocaleGuard lg (X_("POSIX"));
725 PropertyChange what_changed = PropertyChange (0);
728 if ((prop = node.property ("position")) != 0) {
729 sscanf (prop->value().c_str(), "%" PRId64, &val);
730 if (val != _position) {
732 what_changed = PropertyChange (what_changed | PositionChanged);
735 warning << _("old-style crossfade information - no position information") << endmsg;
736 _position = _in->first_frame();
739 if ((prop = node.property ("active")) != 0) {
740 bool x = string_is_affirmative (prop->value());
743 what_changed = PropertyChange (what_changed | ActiveChanged);
749 if ((prop = node.property ("follow-overlap")) != 0) {
750 _follow_overlap = string_is_affirmative (prop->value());
752 _follow_overlap = false;
755 if ((prop = node.property ("fixed")) != 0) {
756 _fixed = string_is_affirmative (prop->value());
761 if ((prop = node.property ("anchor-point")) != 0) {
762 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
764 _anchor_point = StartOfIn;
767 if ((prop = node.property ("length")) != 0) {
769 sscanf (prop->value().c_str(), "%" PRId64, &val);
770 if (val != _length) {
772 what_changed = PropertyChange (what_changed | LengthChanged);
777 /* XXX this branch is legacy code from before
778 the point where we stored xfade lengths.
781 if ((_length = overlap_length()) == 0) {
782 throw failed_constructor();
786 if ((fi = find_named_node (node, "FadeIn")) == 0) {
790 if ((fo = find_named_node (node, "FadeOut")) == 0) {
799 children = fi->children();
801 for (i = children.begin(); i != children.end(); ++i) {
802 if ((*i)->name() == "point") {
806 prop = (*i)->property ("x");
807 sscanf (prop->value().c_str(), "%" PRId64, &x);
809 prop = (*i)->property ("y");
810 sscanf (prop->value().c_str(), "%f", &y);
823 children = fo->children();
825 for (i = children.begin(); i != children.end(); ++i) {
826 if ((*i)->name() == "point") {
831 prop = (*i)->property ("x");
832 sscanf (prop->value().c_str(), "%" PRId64, &x);
834 prop = (*i)->property ("y");
835 sscanf (prop->value().c_str(), "%f", &y);
837 _fade_out.add (x, y);
843 StateChanged (what_changed); /* EMIT SIGNAL */
849 Crossfade::can_follow_overlap () const
855 Crossfade::set_follow_overlap (bool yn)
857 if (yn == _follow_overlap || _fixed) {
861 _follow_overlap = yn;
864 set_xfade_length (_short_xfade_length);
866 set_xfade_length (_out->first_frame() + _out->length() - _in->first_frame());
869 StateChanged (FollowOverlapChanged);
873 Crossfade::set_xfade_length (framecnt_t len)
875 framecnt_t limit = 0;
877 switch (_anchor_point) {
879 limit = _in->length();
883 limit = _in->length();
887 limit = _out->length();
892 len = min (limit, len);
894 double factor = len / (double) _length;
897 _fade_out.x_scale (factor);
898 _fade_in.x_scale (factor);
903 StateChanged (LengthChanged);
909 Crossfade::overlap_length () const
914 return _out->first_frame() + _out->length() - _in->first_frame();
918 Crossfade::set_short_xfade_length (framecnt_t n)
920 _short_xfade_length = n;