2 Copyright (C) 2003 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;
36 //using namespace sigc;
38 jack_nframes_t Crossfade::_short_xfade_length = 0;
39 Change Crossfade::ActiveChanged = ARDOUR::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 (jack_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 (ARDOUR::AudioRegion& in, ARDOUR::AudioRegion& out,
74 jack_nframes_t length,
75 jack_nframes_t position,
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 (ARDOUR::AudioRegion& a, ARDOUR::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
116 LocaleGuard lg (X_("POSIX"));
118 /* we have to find the in/out regions before we can do anything else */
120 if ((prop = node.property ("in")) == 0) {
121 error << _("Crossfade: no \"in\" region in state") << endmsg;
122 throw failed_constructor();
125 sscanf (prop->value().c_str(), "%" PRIu64, &id);
127 if ((r = playlist.find_region (id)) == 0) {
128 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2"), id, playlist.name())
130 throw failed_constructor();
133 if ((_in = dynamic_cast<AudioRegion*> (r)) == 0) {
134 throw failed_constructor();
137 if ((prop = node.property ("out")) == 0) {
138 error << _("Crossfade: no \"out\" region in state") << endmsg;
139 throw failed_constructor();
142 sscanf (prop->value().c_str(), "%" PRIu64, &id);
144 if ((r = playlist.find_region (id)) == 0) {
145 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2"), id, playlist.name())
147 throw failed_constructor();
150 if ((_out = dynamic_cast<AudioRegion*> (r)) == 0) {
151 throw failed_constructor();
157 if (set_state (node)) {
158 throw failed_constructor();
161 save_state ("initial");
164 Crossfade::~Crossfade ()
166 for (StateMap::iterator i = states.begin(); i != states.end(); ++i) {
172 Crossfade::initialize (bool savestate)
176 _out->suspend_fade_out ();
177 _in->suspend_fade_in ();
181 _fade_out.add (0.0, 1.0);
182 _fade_out.add ((_length * 0.1), 0.99);
183 _fade_out.add ((_length * 0.2), 0.97);
184 _fade_out.add ((_length * 0.8), 0.03);
185 _fade_out.add ((_length * 0.9), 0.01);
186 _fade_out.add (_length, 0.0);
191 _fade_in.add (0.0, 0.0);
192 _fade_in.add ((_length * 0.1), 0.01);
193 _fade_in.add ((_length * 0.2), 0.03);
194 _fade_in.add ((_length * 0.8), 0.97);
195 _fade_in.add ((_length * 0.9), 0.99);
196 _fade_in.add (_length, 1.0);
199 // _in->StateChanged.connect (slot (*this, &Crossfade::member_changed));
200 // _out->StateChanged.connect (slot (*this, &Crossfade::member_changed));
202 overlap_type = _in->coverage (_out->position(), _out->last_frame());
205 save_state ("initial");
210 Crossfade::compute (AudioRegion& a, AudioRegion& b, CrossfadeModel model)
214 jack_nframes_t short_xfade_length;
216 short_xfade_length = _short_xfade_length;
218 if (a.layer() < b.layer()) {
226 /* first check for matching ends */
228 if (top->first_frame() == bottom->first_frame()) {
230 /* Both regions start at the same point */
232 if (top->last_frame() < bottom->last_frame()) {
234 /* top ends before bottom, so put an xfade
235 in at the end of top.
238 /* [-------- top ---------- ]
239 * {====== bottom =====================}
245 if (top->last_frame() < short_xfade_length) {
248 _position = top->last_frame() - short_xfade_length;
251 _length = min (short_xfade_length, top->length());
252 _follow_overlap = false;
253 _anchor_point = EndOfIn;
258 /* top ends after (or same time) as bottom - no xfade
261 /* [-------- top ------------------------ ]
262 * {====== bottom =====================}
265 throw NoCrossfadeHere();
268 } else if (top->last_frame() == bottom->last_frame()) {
270 /* Both regions end at the same point */
272 if (top->first_frame() > bottom->first_frame()) {
274 /* top starts after bottom, put an xfade in at the
278 /* [-------- top ---------- ]
279 * {====== bottom =====================}
284 _position = top->first_frame();
285 _length = min (short_xfade_length, top->length());
286 _follow_overlap = false;
287 _anchor_point = StartOfIn;
292 /* top starts before bottom - no xfade
295 /* [-------- top ------------------------ ]
296 * {====== bottom =====================}
299 throw NoCrossfadeHere();
304 /* OK, time to do more regular overlapping */
306 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
310 /* should be NOTREACHED as a precondition of creating
311 a new crossfade, but we need to handle it here.
313 throw NoCrossfadeHere();
316 case OverlapInternal:
317 case OverlapExternal:
318 /* should be NOTREACHED because of tests above */
319 throw NoCrossfadeHere();
322 case OverlapEnd: /* top covers start of bottom but ends within it */
324 /* [---- top ------------------------]
325 * { ==== bottom ============ }
330 _position = bottom->first_frame();
331 _anchor_point = StartOfIn;
333 if (model == FullCrossfade) {
334 _length = _out->first_frame() + _out->length() - _in->first_frame();
335 /* leave active alone */
336 _follow_overlap = true;
338 _length = min (short_xfade_length, top->length());
340 _follow_overlap = false;
345 case OverlapStart: /* top starts within bottom but covers bottom's end */
347 /* { ==== top ============ }
348 * [---- bottom -------------------]
353 _position = top->first_frame();
354 _anchor_point = StartOfIn;
356 if (model == FullCrossfade) {
357 _length = _out->first_frame() + _out->length() - _in->first_frame();
358 /* leave active alone */
359 _follow_overlap = true;
361 _length = min (short_xfade_length, top->length());
363 _follow_overlap = false;
375 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer,
376 float *gain_buffer, jack_nframes_t start, jack_nframes_t cnt, uint32_t chan_n,
377 jack_nframes_t read_frames, jack_nframes_t skip_frames)
379 jack_nframes_t offset;
380 jack_nframes_t to_write;
386 if (start < _position) {
388 /* handle an initial section of the read area that we do not
392 offset = _position - start;
402 to_write = min (_length, cnt);
406 to_write = min (_length - (start - _position), cnt);
410 offset = start - _position;
412 _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
413 _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
415 float* fiv = new float[to_write];
416 float* fov = new float[to_write];
418 _fade_in.get_vector (offset, offset+to_write, fiv, to_write);
419 _fade_out.get_vector (offset, offset+to_write, fov, to_write);
421 /* note: although we have not explicitly taken into account the return values
422 from _out->read_at() or _in->read_at(), the length() function does this
423 implicitly. why? because it computes a value based on the in+out regions'
424 position and length, and so we know precisely how much data they could return.
427 for (jack_nframes_t n = 0; n < to_write; ++n) {
428 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
438 Crossfade::coverage (jack_nframes_t start, jack_nframes_t end) const
440 jack_nframes_t my_end = _position + _length;
442 if ((start >= _position) && (end <= my_end)) {
443 return OverlapInternal;
445 if ((end >= _position) && (end <= my_end)) {
448 if ((start >= _position) && (start <= my_end)) {
451 if ((_position >= start) && (_position <= end) && (my_end <= end)) {
452 return OverlapExternal;
458 Crossfade::set_active (bool yn)
462 save_state (_("active changed"));
463 send_state_changed (ActiveChanged);
468 Crossfade::refresh ()
470 /* crossfades must be between non-muted regions */
472 if (_out->muted() || _in->muted()) {
477 /* overlap type must be Start, End or External */
481 ot = _in->coverage (_out->first_frame(), _out->last_frame());
485 case OverlapInternal:
493 /* overlap type must not have altered */
495 if (ot != overlap_type) {
502 return update (false);
506 Crossfade::update (bool force)
508 jack_nframes_t newlen;
511 if (_follow_overlap) {
512 newlen = _out->first_frame() + _out->length() - _in->first_frame();
524 if (force || (_follow_overlap && newlen != _length) || (_length > newlen)) {
526 double factor = newlen / (double) _length;
528 _fade_out.x_scale (factor);
529 _fade_in.x_scale (factor);
537 switch (_anchor_point) {
539 if (_position != _in->first_frame()) {
540 _position = _in->first_frame();
546 if (_position != _in->last_frame() - _length) {
547 _position = _in->last_frame() - _length;
553 if (_position != _out->last_frame() - _length) {
554 _position = _out->last_frame() - _length;
560 save_state ("updated");
563 /* UI's may need to know that the overlap changed even
564 though the xfade length did not.
567 send_state_changed (BoundsChanged); /* EMIT SIGNAL */
575 Crossfade::member_changed (Change what_changed)
577 Change what_we_care_about = Change (Region::MuteChanged|
578 Region::LayerChanged|
579 ARDOUR::BoundsChanged);
581 if (what_changed & what_we_care_about) {
587 Crossfade::restore_state (StateManager::State& state)
589 CrossfadeState* xfstate = dynamic_cast<CrossfadeState*> (&state);
590 Change what_changed = Change (0);
594 xfstate->fade_in_memento ();
595 xfstate->fade_out_memento ();
597 if (_length != xfstate->length) {
598 what_changed = Change (what_changed|LengthChanged);
599 _length = xfstate->length;
601 if (_active != xfstate->active) {
602 what_changed = Change (what_changed|ActiveChanged);
603 _active = xfstate->active;
605 if (_position != xfstate->position) {
606 what_changed = Change (what_changed|PositionChanged);
607 _position = xfstate->position;
610 /* XXX what to do about notifications for these? I don't
611 think (G)UI cares about them because they are
612 implicit in the bounds.
615 _follow_overlap = xfstate->follow_overlap;
616 _anchor_point = xfstate->anchor_point;
620 return Change (what_changed);
624 Crossfade::state_factory (std::string why) const
626 CrossfadeState* state = new CrossfadeState (why);
628 state->fade_in_memento = _fade_in.get_memento ();
629 state->fade_out_memento = _fade_out.get_memento ();
630 state->active = _active;
631 state->length = _length;
632 state->position = _position;
633 state->follow_overlap = _follow_overlap;
634 state->anchor_point = _anchor_point;
640 Crossfade::get_memento() const
642 return sigc::bind (mem_fun (*(const_cast<Crossfade *> (this)), &StateManager::use_state), _current_state_id);
646 Crossfade::get_state ()
648 XMLNode* node = new XMLNode (X_("Crossfade"));
651 LocaleGuard lg (X_("POSIX"));
653 snprintf (buf, sizeof(buf), "%" PRIu64, _out->id());
654 node->add_property ("out", buf);
655 snprintf (buf, sizeof(buf), "%" PRIu64, _in->id());
656 node->add_property ("in", buf);
657 node->add_property ("active", (_active ? "yes" : "no"));
658 node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
659 node->add_property ("fixed", (_fixed ? "yes" : "no"));
660 snprintf (buf, sizeof(buf), "%" PRIu32, _length);
661 node->add_property ("length", buf);
662 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
663 node->add_property ("anchor-point", buf);
664 snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
665 node->add_property ("position", buf);
667 child = node->add_child ("FadeIn");
669 for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
672 pnode = new XMLNode ("point");
674 snprintf (buf, sizeof (buf), "%" PRIu32, (jack_nframes_t) floor ((*ii)->when));
675 pnode->add_property ("x", buf);
676 snprintf (buf, sizeof (buf), "%f", (*ii)->value);
677 pnode->add_property ("y", buf);
678 child->add_child_nocopy (*pnode);
681 child = node->add_child ("FadeOut");
683 for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
686 pnode = new XMLNode ("point");
688 snprintf (buf, sizeof (buf), "%" PRIu32, (jack_nframes_t) floor ((*ii)->when));
689 pnode->add_property ("x", buf);
690 snprintf (buf, sizeof (buf), "%f", (*ii)->value);
691 pnode->add_property ("y", buf);
692 child->add_child_nocopy (*pnode);
699 Crossfade::set_state (const XMLNode& node)
701 XMLNodeConstIterator i;
702 XMLNodeList children;
705 const XMLProperty* prop;
706 LocaleGuard lg (X_("POSIX"));
708 if ((prop = node.property ("position")) != 0) {
709 _position = atoi (prop->value().c_str());
711 warning << _("old-style crossfade information - no position information") << endmsg;
712 _position = _in->first_frame();
715 if ((prop = node.property ("active")) != 0) {
716 _active = (prop->value() == "yes");
721 if ((prop = node.property ("follow-overlap")) != 0) {
722 _follow_overlap = (prop->value() == "yes");
724 _follow_overlap = false;
727 if ((prop = node.property ("fixed")) != 0) {
728 _fixed = (prop->value() == "yes");
733 if ((prop = node.property ("anchor-point")) != 0) {
734 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
736 _anchor_point = StartOfIn;
739 if ((prop = node.property ("length")) != 0) {
741 _length = atol (prop->value().c_str());
745 /* XXX this branch is legacy code from before
746 the point where we stored xfade lengths.
749 if ((_length = overlap_length()) == 0) {
750 throw failed_constructor();
754 if ((fi = find_named_node (node, "FadeIn")) == 0) {
758 if ((fo = find_named_node (node, "FadeOut")) == 0) {
766 children = fi->children();
768 for (i = children.begin(); i != children.end(); ++i) {
769 if ((*i)->name() == "point") {
773 prop = (*i)->property ("x");
774 sscanf (prop->value().c_str(), "%" PRIu32, &x);
776 prop = (*i)->property ("y");
777 sscanf (prop->value().c_str(), "%f", &y);
787 children = fo->children();
789 for (i = children.begin(); i != children.end(); ++i) {
790 if ((*i)->name() == "point") {
795 prop = (*i)->property ("x");
796 sscanf (prop->value().c_str(), "%" PRIu32, &x);
798 prop = (*i)->property ("y");
799 sscanf (prop->value().c_str(), "%f", &y);
801 _fade_out.add (x, y);
809 Crossfade::can_follow_overlap () const
815 Crossfade::set_follow_overlap (bool yn)
817 if (yn == _follow_overlap || _fixed) {
821 _follow_overlap = yn;
824 set_length (_short_xfade_length);
826 set_length (_out->first_frame() + _out->length() - _in->first_frame());
831 Crossfade::set_length (jack_nframes_t len)
833 jack_nframes_t limit;
835 switch (_anchor_point) {
837 limit = _in->length();
841 limit = _in->length();
845 limit = _out->length();
850 len = min (limit, len);
852 double factor = len / (double) _length;
855 _fade_out.x_scale (factor);
856 _fade_in.x_scale (factor);
861 save_state ("length changed");
863 send_state_changed (LengthChanged);
869 Crossfade::overlap_length () const
874 return _out->first_frame() + _out->length() - _in->first_frame();
878 Crossfade::set_short_xfade_length (jack_nframes_t n)
880 _short_xfade_length = n;