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 <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>
35 using namespace ARDOUR;
38 nframes_t Crossfade::_short_xfade_length = 0;
39 Change Crossfade::ActiveChanged = new_change();
41 /* XXX if and when we ever implement parallel processing of the process()
42 callback, these will need to be handled on a per-thread basis.
45 Sample* Crossfade::crossfade_buffer_out = 0;
46 Sample* Crossfade::crossfade_buffer_in = 0;
49 Crossfade::set_buffer_size (nframes_t sz)
51 if (crossfade_buffer_out) {
52 delete [] crossfade_buffer_out;
53 crossfade_buffer_out = 0;
56 if (crossfade_buffer_in) {
57 delete [] crossfade_buffer_in;
58 crossfade_buffer_in = 0;
62 crossfade_buffer_out = new Sample[sz];
63 crossfade_buffer_in = new Sample[sz];
68 Crossfade::operator== (const Crossfade& other)
70 return (_in == other._in) && (_out == other._out);
73 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out,
77 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
78 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
85 _follow_overlap = false;
92 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
93 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
94 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
99 if (compute (a, b, model)) {
100 throw failed_constructor();
109 Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
110 : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
111 _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
113 boost::shared_ptr<Region> r;
115 LocaleGuard lg (X_("POSIX"));
117 /* we have to find the in/out regions before we can do anything else */
119 if ((prop = node.property ("in")) == 0) {
120 error << _("Crossfade: no \"in\" region in state") << endmsg;
121 throw failed_constructor();
124 PBD::ID id (prop->value());
126 if ((r = playlist.find_region (id)) == 0) {
127 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2"), id, playlist.name())
129 throw failed_constructor();
132 if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
133 throw failed_constructor();
136 if ((prop = node.property ("out")) == 0) {
137 error << _("Crossfade: no \"out\" region in state") << endmsg;
138 throw failed_constructor();
141 PBD::ID id2 (prop->value());
143 if ((r = playlist.find_region (id2)) == 0) {
144 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2"), id2, playlist.name())
146 throw failed_constructor();
149 if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
150 throw failed_constructor();
156 if (set_state (node)) {
157 throw failed_constructor();
160 save_state ("initial");
163 Crossfade::Crossfade (const Crossfade &orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
164 : _fade_in(orig._fade_in),
165 _fade_out(orig._fade_out)
167 _active = orig._active;
168 _in_update = orig._in_update;
169 _length = orig._length;
170 _position = orig._position;
171 _anchor_point = orig._anchor_point;
172 _follow_overlap = orig._follow_overlap;
173 _fixed = orig._fixed;
174 _follow_overlap = orig._follow_overlap;
175 _short_xfade_length = orig._short_xfade_length;
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());
188 // Let's make sure the fade isn't too long
193 Crossfade::~Crossfade ()
195 for (StateMap::iterator i = states.begin(); i != states.end(); ++i) {
201 Crossfade::initialize (bool savestate)
205 _out->suspend_fade_out ();
206 _in->suspend_fade_in ();
210 _fade_out.add (0.0, 1.0);
211 _fade_out.add ((_length * 0.1), 0.99);
212 _fade_out.add ((_length * 0.2), 0.97);
213 _fade_out.add ((_length * 0.8), 0.03);
214 _fade_out.add ((_length * 0.9), 0.01);
215 _fade_out.add (_length, 0.0);
220 _fade_in.add (0.0, 0.0);
221 _fade_in.add ((_length * 0.1), 0.01);
222 _fade_in.add ((_length * 0.2), 0.03);
223 _fade_in.add ((_length * 0.8), 0.97);
224 _fade_in.add ((_length * 0.9), 0.99);
225 _fade_in.add (_length, 1.0);
228 // _in->StateChanged.connect (slot (*this, &Crossfade::member_changed));
229 // _out->StateChanged.connect (slot (*this, &Crossfade::member_changed));
231 overlap_type = _in->coverage (_out->position(), _out->last_frame());
234 save_state ("initial");
239 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
241 boost::shared_ptr<AudioRegion> top;
242 boost::shared_ptr<AudioRegion> bottom;
243 nframes_t short_xfade_length;
245 short_xfade_length = _short_xfade_length;
247 if (a->layer() < b->layer()) {
255 /* first check for matching ends */
257 if (top->first_frame() == bottom->first_frame()) {
259 /* Both regions start at the same point */
261 if (top->last_frame() < bottom->last_frame()) {
263 /* top ends before bottom, so put an xfade
264 in at the end of top.
267 /* [-------- top ---------- ]
268 * {====== bottom =====================}
274 if (top->last_frame() < short_xfade_length) {
277 _position = top->last_frame() - short_xfade_length;
280 _length = min (short_xfade_length, top->length());
281 _follow_overlap = false;
282 _anchor_point = EndOfIn;
287 /* top ends after (or same time) as bottom - no xfade
290 /* [-------- top ------------------------ ]
291 * {====== bottom =====================}
294 throw NoCrossfadeHere();
297 } else if (top->last_frame() == bottom->last_frame()) {
299 /* Both regions end at the same point */
301 if (top->first_frame() > bottom->first_frame()) {
303 /* top starts after bottom, put an xfade in at the
307 /* [-------- top ---------- ]
308 * {====== bottom =====================}
313 _position = top->first_frame();
314 _length = min (short_xfade_length, top->length());
315 _follow_overlap = false;
316 _anchor_point = StartOfIn;
321 /* top starts before bottom - no xfade
324 /* [-------- top ------------------------ ]
325 * {====== bottom =====================}
328 throw NoCrossfadeHere();
333 /* OK, time to do more regular overlapping */
335 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
339 /* should be NOTREACHED as a precondition of creating
340 a new crossfade, but we need to handle it here.
342 throw NoCrossfadeHere();
345 case OverlapInternal:
346 case OverlapExternal:
347 /* should be NOTREACHED because of tests above */
348 throw NoCrossfadeHere();
351 case OverlapEnd: /* top covers start of bottom but ends within it */
353 /* [---- top ------------------------]
354 * { ==== bottom ============ }
359 _position = bottom->first_frame();
360 _anchor_point = StartOfIn;
362 if (model == FullCrossfade) {
363 _length = _out->first_frame() + _out->length() - _in->first_frame();
364 /* leave active alone */
365 _follow_overlap = true;
367 _length = min (short_xfade_length, top->length());
369 _follow_overlap = false;
374 case OverlapStart: /* top starts within bottom but covers bottom's end */
376 /* { ==== top ============ }
377 * [---- bottom -------------------]
382 _position = top->first_frame();
383 _anchor_point = StartOfIn;
385 if (model == FullCrossfade) {
386 _length = _out->first_frame() + _out->length() - _in->first_frame();
387 /* leave active alone */
388 _follow_overlap = true;
390 _length = min (short_xfade_length, top->length());
392 _follow_overlap = false;
404 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
405 float *gain_buffer, nframes_t start, nframes_t cnt, uint32_t chan_n,
406 nframes_t read_frames, nframes_t skip_frames)
415 if (start < _position) {
417 /* handle an initial section of the read area that we do not
421 offset = _position - start;
431 to_write = min (_length, cnt);
435 to_write = min (_length - (start - _position), cnt);
439 offset = start - _position;
441 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
442 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
444 float* fiv = new float[to_write];
445 float* fov = new float[to_write];
447 _fade_in.get_vector (offset, offset+to_write, fiv, to_write);
448 _fade_out.get_vector (offset, offset+to_write, fov, to_write);
450 /* note: although we have not explicitly taken into account the return values
451 from _out->read_at() or _in->read_at(), the length() function does this
452 implicitly. why? because it computes a value based on the in+out regions'
453 position and length, and so we know precisely how much data they could return.
456 for (nframes_t n = 0; n < to_write; ++n) {
457 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
467 Crossfade::coverage (nframes_t start, nframes_t end) const
469 nframes_t my_end = _position + _length;
471 if ((start >= _position) && (end <= my_end)) {
472 return OverlapInternal;
474 if ((end >= _position) && (end <= my_end)) {
477 if ((start >= _position) && (start <= my_end)) {
480 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
481 return OverlapExternal;
487 Crossfade::set_active (bool yn)
491 save_state (_("active changed"));
492 send_state_changed (ActiveChanged);
497 Crossfade::refresh ()
499 /* crossfades must be between non-muted regions */
501 if (_out->muted() || _in->muted()) {
506 /* overlap type must be Start, End or External */
510 ot = _in->coverage (_out->first_frame(), _out->last_frame());
514 case OverlapInternal:
522 /* overlap type must not have altered */
524 if (ot != overlap_type) {
531 return update (false);
535 Crossfade::update (bool force)
540 if (_follow_overlap) {
541 newlen = _out->first_frame() + _out->length() - _in->first_frame();
553 if (force || (_follow_overlap && newlen != _length) || (_length > newlen)) {
555 double factor = newlen / (double) _length;
557 _fade_out.x_scale (factor);
558 _fade_in.x_scale (factor);
566 switch (_anchor_point) {
568 if (_position != _in->first_frame()) {
569 _position = _in->first_frame();
575 if (_position != _in->last_frame() - _length) {
576 _position = _in->last_frame() - _length;
582 if (_position != _out->last_frame() - _length) {
583 _position = _out->last_frame() - _length;
589 save_state ("updated");
592 /* UI's may need to know that the overlap changed even
593 though the xfade length did not.
596 send_state_changed (BoundsChanged); /* EMIT SIGNAL */
604 Crossfade::member_changed (Change what_changed)
606 Change what_we_care_about = Change (Region::MuteChanged|
607 Region::LayerChanged|
610 if (what_changed & what_we_care_about) {
616 Crossfade::restore_state (StateManager::State& state)
618 CrossfadeState* xfstate = dynamic_cast<CrossfadeState*> (&state);
619 Change what_changed = Change (0);
623 xfstate->fade_in_memento ();
624 xfstate->fade_out_memento ();
626 if (_length != xfstate->length) {
627 what_changed = Change (what_changed|LengthChanged);
628 _length = xfstate->length;
630 if (_active != xfstate->active) {
631 what_changed = Change (what_changed|ActiveChanged);
632 _active = xfstate->active;
634 if (_position != xfstate->position) {
635 what_changed = Change (what_changed|PositionChanged);
636 _position = xfstate->position;
639 /* XXX what to do about notifications for these? I don't
640 think (G)UI cares about them because they are
641 implicit in the bounds.
644 _follow_overlap = xfstate->follow_overlap;
645 _anchor_point = xfstate->anchor_point;
649 return Change (what_changed);
653 Crossfade::state_factory (std::string why) const
655 CrossfadeState* state = new CrossfadeState (why);
657 state->fade_in_memento = _fade_in.get_memento ();
658 state->fade_out_memento = _fade_out.get_memento ();
659 state->active = _active;
660 state->length = _length;
661 state->position = _position;
662 state->follow_overlap = _follow_overlap;
663 state->anchor_point = _anchor_point;
669 Crossfade::get_memento() const
671 return sigc::bind (mem_fun (*(const_cast<Crossfade *> (this)), &StateManager::use_state), _current_state_id);
675 Crossfade::get_state ()
677 XMLNode* node = new XMLNode (X_("Crossfade"));
680 LocaleGuard lg (X_("POSIX"));
682 _out->id().print (buf);
683 node->add_property ("out", buf);
684 _in->id().print (buf);
685 node->add_property ("in", buf);
686 node->add_property ("active", (_active ? "yes" : "no"));
687 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
688 node->add_property ("fixed", (_fixed ? "yes" : "no"));
689 snprintf (buf, sizeof(buf), "%" PRIu32, _length);
690 node->add_property ("length", buf);
691 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
692 node->add_property ("anchor-point", buf);
693 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
694 node->add_property ("position", buf);
696 child = node->add_child ("FadeIn");
698 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
701 pnode = new XMLNode ("point");
703 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
704 pnode->add_property ("x", buf);
705 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
706 pnode->add_property ("y", buf);
707 child->add_child_nocopy (*pnode);
710 child = node->add_child ("FadeOut");
712 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
715 pnode = new XMLNode ("point");
717 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
718 pnode->add_property ("x", buf);
719 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
720 pnode->add_property ("y", buf);
721 child->add_child_nocopy (*pnode);
728 Crossfade::set_state (const XMLNode& node)
730 XMLNodeConstIterator i;
731 XMLNodeList children;
734 const XMLProperty* prop;
735 LocaleGuard lg (X_("POSIX"));
737 if ((prop = node.property ("position")) != 0) {
738 _position = atoi (prop->value().c_str());
740 warning << _("old-style crossfade information - no position information") << endmsg;
741 _position = _in->first_frame();
744 if ((prop = node.property ("active")) != 0) {
745 _active = (prop->value() == "yes");
750 if ((prop = node.property ("follow-overlap")) != 0) {
751 _follow_overlap = (prop->value() == "yes");
753 _follow_overlap = false;
756 if ((prop = node.property ("fixed")) != 0) {
757 _fixed = (prop->value() == "yes");
762 if ((prop = node.property ("anchor-point")) != 0) {
763 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
765 _anchor_point = StartOfIn;
768 if ((prop = node.property ("length")) != 0) {
770 _length = atol (prop->value().c_str());
774 /* XXX this branch is legacy code from before
775 the point where we stored xfade lengths.
778 if ((_length = overlap_length()) == 0) {
779 throw failed_constructor();
783 if ((fi = find_named_node (node, "FadeIn")) == 0) {
787 if ((fo = find_named_node (node, "FadeOut")) == 0) {
795 children = fi->children();
797 for (i = children.begin(); i != children.end(); ++i) {
798 if ((*i)->name() == "point") {
802 prop = (*i)->property ("x");
803 sscanf (prop->value().c_str(), "%" PRIu32, &x);
805 prop = (*i)->property ("y");
806 sscanf (prop->value().c_str(), "%f", &y);
816 children = fo->children();
818 for (i = children.begin(); i != children.end(); ++i) {
819 if ((*i)->name() == "point") {
824 prop = (*i)->property ("x");
825 sscanf (prop->value().c_str(), "%" PRIu32, &x);
827 prop = (*i)->property ("y");
828 sscanf (prop->value().c_str(), "%f", &y);
830 _fade_out.add (x, y);
838 Crossfade::can_follow_overlap () const
844 Crossfade::set_follow_overlap (bool yn)
846 if (yn == _follow_overlap || _fixed) {
850 _follow_overlap = yn;
853 set_length (_short_xfade_length);
855 set_length (_out->first_frame() + _out->length() - _in->first_frame());
860 Crossfade::set_length (nframes_t len)
864 switch (_anchor_point) {
866 limit = _in->length();
870 limit = _in->length();
874 limit = _out->length();
879 len = min (limit, len);
881 double factor = len / (double) _length;
884 _fade_out.x_scale (factor);
885 _fade_in.x_scale (factor);
890 save_state ("length changed");
892 send_state_changed (LengthChanged);
898 Crossfade::overlap_length () const
903 return _out->first_frame() + _out->length() - _in->first_frame();
907 Crossfade::set_short_xfade_length (nframes_t n)
909 _short_xfade_length = n;