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 <sigc++/bind.h>
23 #include <pbd/stacktrace.h>
25 #include <ardour/types.h>
26 #include <ardour/crossfade.h>
27 #include <ardour/crossfade_compare.h>
28 #include <ardour/audioregion.h>
29 #include <ardour/playlist.h>
30 #include <ardour/utils.h>
31 #include <ardour/session.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 if (crossfade_buffer_out) {
55 delete [] crossfade_buffer_out;
56 crossfade_buffer_out = 0;
59 if (crossfade_buffer_in) {
60 delete [] crossfade_buffer_in;
61 crossfade_buffer_in = 0;
65 crossfade_buffer_out = new Sample[sz];
66 crossfade_buffer_in = new Sample[sz];
71 Crossfade::operator== (const Crossfade& other)
73 return (_in == other._in) && (_out == other._out);
76 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out,
80 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
81 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
90 switch (Config->get_xfade_model()) {
92 _follow_overlap = false;
95 _follow_overlap = true;
98 _active = Config->get_xfades_active ();
104 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
105 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
106 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
111 if (compute (a, b, model)) {
112 throw failed_constructor();
121 Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
122 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
123 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
125 boost::shared_ptr<Region> r;
127 LocaleGuard lg (X_("POSIX"));
129 /* we have to find the in/out regions before we can do anything else */
131 if ((prop = node.property ("in")) == 0) {
132 error << _("Crossfade: no \"in\" region in state") << endmsg;
133 throw failed_constructor();
136 PBD::ID id (prop->value());
138 if ((r = playlist.find_region (id)) == 0) {
139 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2"), id, playlist.name())
141 throw failed_constructor();
144 if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
145 throw failed_constructor();
148 if ((prop = node.property ("out")) == 0) {
149 error << _("Crossfade: no \"out\" region in state") << endmsg;
150 throw failed_constructor();
153 PBD::ID id2 (prop->value());
155 if ((r = playlist.find_region (id2)) == 0) {
156 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2"), id2, playlist.name())
158 throw failed_constructor();
161 if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
162 throw failed_constructor();
168 if (set_state (node)) {
169 throw failed_constructor();
173 Crossfade::Crossfade (const Crossfade &orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
174 : _fade_in(orig._fade_in),
175 _fade_out(orig._fade_out)
177 _active = orig._active;
178 _in_update = orig._in_update;
179 _length = orig._length;
180 _position = orig._position;
181 _anchor_point = orig._anchor_point;
182 _follow_overlap = orig._follow_overlap;
183 _fixed = orig._fixed;
188 // copied from Crossfade::initialize()
191 _out->suspend_fade_out ();
192 _in->suspend_fade_in ();
194 overlap_type = _in->coverage (_out->position(), _out->last_frame());
196 // Let's make sure the fade isn't too long
201 Crossfade::~Crossfade ()
203 cerr << "Crossfade deleted\n";
208 Crossfade::initialize ()
212 _out->suspend_fade_out ();
213 _in->suspend_fade_in ();
217 _fade_out.add (0.0, 1.0);
218 _fade_out.add ((_length * 0.1), 0.99);
219 _fade_out.add ((_length * 0.2), 0.97);
220 _fade_out.add ((_length * 0.8), 0.03);
221 _fade_out.add ((_length * 0.9), 0.01);
222 _fade_out.add (_length, 0.0);
227 _fade_in.add (0.0, 0.0);
228 _fade_in.add ((_length * 0.1), 0.01);
229 _fade_in.add ((_length * 0.2), 0.03);
230 _fade_in.add ((_length * 0.8), 0.97);
231 _fade_in.add ((_length * 0.9), 0.99);
232 _fade_in.add (_length, 1.0);
235 _in->StateChanged.connect (sigc::mem_fun (*this, &Crossfade::member_changed));
236 _out->StateChanged.connect (sigc::mem_fun (*this, &Crossfade::member_changed));
238 overlap_type = _in->coverage (_out->position(), _out->last_frame());
242 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
244 boost::shared_ptr<AudioRegion> top;
245 boost::shared_ptr<AudioRegion> bottom;
246 nframes_t short_xfade_length;
248 short_xfade_length = _short_xfade_length;
250 if (a->layer() < b->layer()) {
258 /* first check for matching ends */
260 if (top->first_frame() == bottom->first_frame()) {
262 cerr << "same start\n";
264 /* Both regions start at the same point */
266 if (top->last_frame() < bottom->last_frame()) {
268 /* top ends before bottom, so put an xfade
269 in at the end of top.
272 /* [-------- top ---------- ]
273 * {====== bottom =====================}
279 if (top->last_frame() < short_xfade_length) {
282 _position = top->last_frame() - short_xfade_length;
285 _length = min (short_xfade_length, top->length());
286 _follow_overlap = false;
287 _anchor_point = EndOfIn;
292 /* top ends after (or same time) as bottom - no xfade
295 /* [-------- top ------------------------ ]
296 * {====== bottom =====================}
299 throw NoCrossfadeHere();
302 } else if (top->last_frame() == bottom->last_frame()) {
304 cerr << "same end\n";
306 /* Both regions end at the same point */
308 if (top->first_frame() > bottom->first_frame()) {
310 /* top starts after bottom, put an xfade in at the
314 /* [-------- top ---------- ]
315 * {====== bottom =====================}
320 _position = top->first_frame();
321 _length = min (short_xfade_length, top->length());
322 _follow_overlap = false;
323 _anchor_point = StartOfIn;
328 /* top starts before bottom - no xfade
331 /* [-------- top ------------------------ ]
332 * {====== bottom =====================}
335 throw NoCrossfadeHere();
340 /* OK, time to do more regular overlapping */
342 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
344 cerr << "ot = " << ot << endl;
348 /* should be NOTREACHED as a precondition of creating
349 a new crossfade, but we need to handle it here.
352 throw NoCrossfadeHere();
355 case OverlapInternal:
356 case OverlapExternal:
357 /* should be NOTREACHED because of tests above */
359 throw NoCrossfadeHere();
362 case OverlapEnd: /* top covers start of bottom but ends within it */
364 /* [---- top ------------------------]
365 * { ==== bottom ============ }
370 _anchor_point = StartOfIn;
372 if (model == FullCrossfade) {
373 _position = bottom->first_frame(); // "{"
374 _length = _out->first_frame() + _out->length() - _in->first_frame();
375 /* leave active alone */
376 _follow_overlap = true;
378 _length = min (short_xfade_length, top->length());
379 _position = top->last_frame() - _length; // "]" - length
381 _follow_overlap = false;
386 case OverlapStart: /* top starts within bottom but covers bottom's end */
388 /* { ==== top ============ }
389 * [---- bottom -------------------]
394 _position = top->first_frame();
395 _anchor_point = StartOfIn;
397 if (model == FullCrossfade) {
398 _length = _out->first_frame() + _out->length() - _in->first_frame();
399 /* leave active alone */
400 _follow_overlap = true;
402 _length = min (short_xfade_length, top->length());
404 _follow_overlap = false;
416 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
417 float *gain_buffer, nframes_t start, nframes_t cnt, uint32_t chan_n,
418 nframes_t read_frames, nframes_t skip_frames)
427 if (start < _position) {
429 /* handle an initial section of the read area that we do not
433 offset = _position - start;
443 to_write = min (_length, cnt);
447 to_write = min (_length - (start - _position), cnt);
451 offset = start - _position;
453 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
454 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
456 float* fiv = new float[to_write];
457 float* fov = new float[to_write];
459 _fade_in.get_vector (offset, offset+to_write, fiv, to_write);
460 _fade_out.get_vector (offset, offset+to_write, fov, to_write);
462 /* note: although we have not explicitly taken into account the return values
463 from _out->read_at() or _in->read_at(), the length() function does this
464 implicitly. why? because it computes a value based on the in+out regions'
465 position and length, and so we know precisely how much data they could return.
468 for (nframes_t n = 0; n < to_write; ++n) {
469 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
479 Crossfade::coverage (nframes_t start, nframes_t end) const
481 nframes_t my_end = _position + _length;
483 if ((start >= _position) && (end <= my_end)) {
484 return OverlapInternal;
486 if ((end >= _position) && (end <= my_end)) {
489 if ((start >= _position) && (start <= my_end)) {
492 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
493 return OverlapExternal;
499 Crossfade::set_active (bool yn)
503 StateChanged (ActiveChanged);
508 Crossfade::refresh ()
510 /* crossfades must be between non-muted regions */
512 if (_out->muted() || _in->muted()) {
513 Invalidated (shared_from_this());
517 if (_in->layer() < _out->layer()) {
518 cerr << "layer change, invalidated\n";
519 Invalidated (shared_from_this());
523 /* overlap type must be Start, End or External */
527 ot = _in->coverage (_out->first_frame(), _out->last_frame());
531 case OverlapInternal:
532 Invalidated (shared_from_this());
539 /* overlap type must not have altered */
541 if (ot != overlap_type) {
542 Invalidated (shared_from_this());
548 return update (false);
552 Crossfade::update (bool force)
556 if (_follow_overlap) {
557 newlen = _out->first_frame() + _out->length() - _in->first_frame();
563 Invalidated (shared_from_this());
569 if (force || (_follow_overlap && newlen != _length) || (_length > newlen)) {
571 double factor = newlen / (double) _length;
573 _fade_out.x_scale (factor);
574 _fade_in.x_scale (factor);
580 switch (_anchor_point) {
582 if (_position != _in->first_frame()) {
583 if (_length > _short_xfade_length) {
584 /* assume FullCrossfade */
585 _position = _in->first_frame();
587 /* assume short xfade */
588 _position = _out->last_frame() - _length;
594 if (_position != _in->last_frame() - _length) {
595 _position = _in->last_frame() - _length;
600 if (_position != _out->last_frame() - _length) {
601 _position = _out->last_frame() - _length;
605 /* UI's may need to know that the overlap changed even
606 though the xfade length did not.
609 StateChanged (BoundsChanged); /* EMIT SIGNAL */
617 Crossfade::member_changed (Change what_changed)
619 Change what_we_care_about = Change (Region::MuteChanged|
620 Region::LayerChanged|
623 if (what_changed & what_we_care_about) {
629 Crossfade::get_state ()
631 XMLNode* node = new XMLNode (X_("Crossfade"));
634 LocaleGuard lg (X_("POSIX"));
636 _out->id().print (buf, sizeof (buf));
637 node->add_property ("out", buf);
638 _in->id().print (buf, sizeof (buf));
639 node->add_property ("in", buf);
640 node->add_property ("active", (_active ? "yes" : "no"));
641 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
642 node->add_property ("fixed", (_fixed ? "yes" : "no"));
643 snprintf (buf, sizeof(buf), "%" PRIu32, _length);
644 node->add_property ("length", buf);
645 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
646 node->add_property ("anchor-point", buf);
647 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
648 node->add_property ("position", buf);
650 child = node->add_child ("FadeIn");
652 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
655 pnode = new XMLNode ("point");
657 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
658 pnode->add_property ("x", buf);
659 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
660 pnode->add_property ("y", buf);
661 child->add_child_nocopy (*pnode);
664 child = node->add_child ("FadeOut");
666 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
669 pnode = new XMLNode ("point");
671 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
672 pnode->add_property ("x", buf);
673 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
674 pnode->add_property ("y", buf);
675 child->add_child_nocopy (*pnode);
682 Crossfade::set_state (const XMLNode& node)
684 XMLNodeConstIterator i;
685 XMLNodeList children;
688 const XMLProperty* prop;
689 LocaleGuard lg (X_("POSIX"));
690 Change what_changed = Change (0);
693 if ((prop = node.property ("position")) != 0) {
694 sscanf (prop->value().c_str(), "%" PRIu32, &val);
695 if (val != _position) {
697 what_changed = Change (what_changed | PositionChanged);
700 warning << _("old-style crossfade information - no position information") << endmsg;
701 _position = _in->first_frame();
704 if ((prop = node.property ("active")) != 0) {
705 bool x = (prop->value() == "yes");
708 what_changed = Change (what_changed | ActiveChanged);
714 if ((prop = node.property ("follow-overlap")) != 0) {
715 _follow_overlap = (prop->value() == "yes");
717 _follow_overlap = false;
720 if ((prop = node.property ("fixed")) != 0) {
721 _fixed = (prop->value() == "yes");
726 if ((prop = node.property ("anchor-point")) != 0) {
727 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
729 _anchor_point = StartOfIn;
732 if ((prop = node.property ("length")) != 0) {
734 sscanf (prop->value().c_str(), "%" PRIu32, &val);
735 if (val != _length) {
736 _length = atol (prop->value().c_str());
737 what_changed = Change (what_changed | LengthChanged);
742 /* XXX this branch is legacy code from before
743 the point where we stored xfade lengths.
746 if ((_length = overlap_length()) == 0) {
747 throw failed_constructor();
751 if ((fi = find_named_node (node, "FadeIn")) == 0) {
755 if ((fo = find_named_node (node, "FadeOut")) == 0) {
764 children = fi->children();
766 for (i = children.begin(); i != children.end(); ++i) {
767 if ((*i)->name() == "point") {
771 prop = (*i)->property ("x");
772 sscanf (prop->value().c_str(), "%" PRIu32, &x);
774 prop = (*i)->property ("y");
775 sscanf (prop->value().c_str(), "%f", &y);
788 children = fo->children();
790 for (i = children.begin(); i != children.end(); ++i) {
791 if ((*i)->name() == "point") {
796 prop = (*i)->property ("x");
797 sscanf (prop->value().c_str(), "%" PRIu32, &x);
799 prop = (*i)->property ("y");
800 sscanf (prop->value().c_str(), "%f", &y);
802 _fade_out.add (x, y);
808 StateChanged (what_changed); /* EMIT SIGNAL */
814 Crossfade::can_follow_overlap () const
820 Crossfade::set_follow_overlap (bool yn)
822 if (yn == _follow_overlap || _fixed) {
826 _follow_overlap = yn;
829 set_length (_short_xfade_length);
831 set_length (_out->first_frame() + _out->length() - _in->first_frame());
834 StateChanged (FollowOverlapChanged);
838 Crossfade::set_length (nframes_t len)
842 switch (_anchor_point) {
844 limit = _in->length();
848 limit = _in->length();
852 limit = _out->length();
857 len = min (limit, len);
859 double factor = len / (double) _length;
862 _fade_out.x_scale (factor);
863 _fade_in.x_scale (factor);
868 StateChanged (LengthChanged);
874 Crossfade::overlap_length () const
879 return _out->first_frame() + _out->length() - _in->first_frame();
883 Crossfade::set_short_xfade_length (nframes_t n)
885 _short_xfade_length = n;
889 Crossfade::invalidate ()
891 Invalidated (shared_from_this()); /* EMIT SIGNAL */