PluginInfo::type added to copy constructor. But why is the copy constructor defined...
[ardour.git] / libs / ardour / crossfade.cc
1 /*
2     Copyright (C) 2003-2006 Paul Davis 
3
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.
8
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.
13
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.
17
18 */
19
20 #include <sigc++/bind.h>
21
22 #include <pbd/stacktrace.h>
23
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
32 #include "i18n.h"
33 #include <locale.h>
34
35 using namespace std;
36 using namespace ARDOUR;
37 using namespace PBD;
38
39 nframes_t Crossfade::_short_xfade_length = 0;
40 Change Crossfade::ActiveChanged = new_change();
41 Change Crossfade::FollowOverlapChanged = new_change();
42
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.
45 */
46
47 Sample* Crossfade::crossfade_buffer_out = 0;
48 Sample* Crossfade::crossfade_buffer_in = 0;
49
50 void
51 Crossfade::set_buffer_size (nframes_t sz)
52 {
53         if (crossfade_buffer_out) {
54                 delete [] crossfade_buffer_out;
55                 crossfade_buffer_out = 0;
56         }
57
58         if (crossfade_buffer_in) {
59                 delete [] crossfade_buffer_in;
60                 crossfade_buffer_in = 0;
61         }
62
63         if (sz) {
64                 crossfade_buffer_out = new Sample[sz];
65                 crossfade_buffer_in = new Sample[sz];
66         }
67 }
68
69 bool
70 Crossfade::operator== (const Crossfade& other)
71 {
72         return (_in == other._in) && (_out == other._out);
73 }
74
75 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> in, boost::shared_ptr<AudioRegion> out, 
76                       nframes_t length,
77                       nframes_t position,
78                       AnchorPoint ap)
79         : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
80           _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
81 {
82         _in = in;
83         _out = out;
84
85         _length = length;
86         _position = position;
87         _anchor_point = ap;
88
89         _follow_overlap = false;
90
91         _active = Config->get_xfades_active ();
92         _fixed = true;
93                 
94         initialize ();
95 }
96
97 Crossfade::Crossfade (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model, bool act)
98         : _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
99           _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
100 {
101         _in_update = false;
102         _fixed = false;
103         _follow_overlap = false;
104
105         if (compute (a, b, model)) {
106                 throw failed_constructor();
107         }
108
109         _active = act;
110
111         initialize ();
112 }
113
114 Crossfade::Crossfade (const Playlist& playlist, XMLNode& node)
115         :  _fade_in (0.0, 2.0, 1.0), // linear (gain coefficient) => -inf..+6dB
116            _fade_out (0.0, 2.0, 1.0) // linear (gain coefficient) => -inf..+6dB
117 {
118         boost::shared_ptr<Region> r;
119         XMLProperty* prop;
120         LocaleGuard lg (X_("POSIX"));
121
122         /* we have to find the in/out regions before we can do anything else */
123
124         if ((prop = node.property ("in")) == 0) {
125                 error << _("Crossfade: no \"in\" region in state") << endmsg;
126                 throw failed_constructor();
127         }
128         
129         PBD::ID id (prop->value());
130
131         if ((r = playlist.find_region (id)) == 0) {
132                 error << string_compose (_("Crossfade: no \"in\" region %1 found in playlist %2"), id, playlist.name())
133                       << endmsg;
134                 throw failed_constructor();
135         }
136         
137         if ((_in = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
138                 throw failed_constructor();
139         }
140
141         if ((prop = node.property ("out")) == 0) {
142                 error << _("Crossfade: no \"out\" region in state") << endmsg;
143                 throw failed_constructor();
144         }
145
146         PBD::ID id2 (prop->value());
147
148         if ((r = playlist.find_region (id2)) == 0) {
149                 error << string_compose (_("Crossfade: no \"out\" region %1 found in playlist %2"), id2, playlist.name())
150                       << endmsg;
151                 throw failed_constructor();
152         }
153         
154         if ((_out = boost::dynamic_pointer_cast<AudioRegion> (r)) == 0) {
155                 throw failed_constructor();
156         }
157
158         _length = 0;
159         _active = Config->get_xfades_active();
160
161         initialize();
162         
163         if (set_state (node)) {
164                 throw failed_constructor();
165         }
166
167         /* we should not need to do this here, since all values were set via XML. however
168            some older sessions may have bugs and this allows us to fix them.
169         */
170         
171         update ();
172 }
173
174 Crossfade::Crossfade (const Crossfade &orig, boost::shared_ptr<AudioRegion> newin, boost::shared_ptr<AudioRegion> newout)
175         : _fade_in(orig._fade_in),
176           _fade_out(orig._fade_out)
177 {
178         _active           = orig._active;
179         _in_update        = orig._in_update;
180         _length           = orig._length;
181         _position         = orig._position;
182         _anchor_point     = orig._anchor_point;
183         _follow_overlap   = orig._follow_overlap;
184         _fixed            = orig._fixed;
185         
186         _in = newin;
187         _out = newout;
188
189         // copied from Crossfade::initialize()
190         _in_update = false;
191         
192         _out->suspend_fade_out ();
193         _in->suspend_fade_in ();
194
195         overlap_type = _in->coverage (_out->position(), _out->last_frame());
196         layer_relation = (int32_t) (_in->layer() - _out->layer());
197
198         // Let's make sure the fade isn't too long
199         set_length(_length);
200 }
201
202
203 Crossfade::~Crossfade ()
204 {
205         notify_callbacks ();
206 }
207
208 void
209 Crossfade::initialize ()
210 {
211         _in_update = false;
212         
213         _out->suspend_fade_out ();
214         _in->suspend_fade_in ();
215
216         _fade_out.freeze ();
217         _fade_out.clear ();
218
219 #define EQUAL_POWER_MINUS_3DB
220 #ifdef  EQUAL_POWER_MINUS_3DB
221
222         _fade_out.add ((_length * 0.000000), 1.000000);
223         _fade_out.add ((_length * 0.166667), 0.948859);
224         _fade_out.add ((_length * 0.333333), 0.851507);
225         _fade_out.add ((_length * 0.500000), 0.707946);
226         _fade_out.add ((_length * 0.666667), 0.518174);
227         _fade_out.add ((_length * 0.833333), 0.282192);
228         _fade_out.add ((_length * 1.000000), 0.000000);
229
230 #else // EQUAL_POWER_MINUS_6DB
231
232         _fade_out.add ((_length * 0.000000), 1.000000);
233         _fade_out.add ((_length * 0.166667), 0.833033);
234         _fade_out.add ((_length * 0.333333), 0.666186);
235         _fade_out.add ((_length * 0.500000), 0.499459);
236         _fade_out.add ((_length * 0.666667), 0.332853);
237         _fade_out.add ((_length * 0.833333), 0.166366);
238         _fade_out.add ((_length * 1.000000), 0.000000);
239 #endif
240
241         _fade_out.thaw ();
242         
243         _fade_in.freeze ();
244         _fade_in.clear ();
245
246 #define EQUAL_POWER_MINUS_3DB
247 #ifdef  EQUAL_POWER_MINUS_3DB
248
249         _fade_in.add ((_length * 0.000000), 0.000000);
250         _fade_in.add ((_length * 0.166667), 0.282192);
251         _fade_in.add ((_length * 0.333333), 0.518174);
252         _fade_in.add ((_length * 0.500000), 0.707946);
253         _fade_in.add ((_length * 0.666667), 0.851507);
254         _fade_in.add ((_length * 0.833333), 0.948859);
255         _fade_in.add ((_length * 1.000000), 1.000000);
256
257 #else // EQUAL_POWER_MINUS_SIX_DB
258
259         _fade_in.add ((_length * 0.000000), 0.000000);
260         _fade_in.add ((_length * 0.166667), 0.166366);
261         _fade_in.add ((_length * 0.333333), 0.332853);
262         _fade_in.add ((_length * 0.500000), 0.499459);
263         _fade_in.add ((_length * 0.666667), 0.666186);
264         _fade_in.add ((_length * 0.833333), 0.833033);
265         _fade_in.add ((_length * 1.000000), 1.000000);
266
267 #endif
268
269         _fade_in.thaw ();
270
271         overlap_type = _in->coverage (_out->position(), _out->last_frame());
272         layer_relation = (int32_t) (_in->layer() - _out->layer());
273 }       
274
275 nframes_t 
276 Crossfade::read_at (Sample *buf, Sample *mixdown_buffer, 
277                     float *gain_buffer, nframes_t start, nframes_t cnt, uint32_t chan_n,
278                     nframes_t read_frames, nframes_t skip_frames)
279 {
280         nframes_t offset;
281         nframes_t to_write;
282
283         if (!_active) {
284                 return 0;
285         }
286
287         if (start < _position) {
288
289                 /* handle an initial section of the read area that we do not
290                    cover.
291                 */
292
293                 offset = _position - start;
294
295                 if (offset < cnt) {
296                         cnt -= offset;
297                 } else {
298                         return 0;
299                 }
300                 
301                 start = _position;
302                 buf += offset;
303                 to_write = min (_length, cnt);
304
305         } else {
306                 
307                 to_write = min (_length - (start - _position), cnt);
308                 
309         }
310
311         offset = start - _position;
312
313         /* Prevent data from piling up inthe crossfade buffers when reading a transparent region */
314         if (!(_out->opaque())) {
315                 memset (crossfade_buffer_out, 0, sizeof (Sample) * to_write);
316         } else if (!(_in->opaque())) {
317                 memset (crossfade_buffer_in, 0, sizeof (Sample) * to_write);
318         }
319
320         _out->read_at (crossfade_buffer_out, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
321         _in->read_at (crossfade_buffer_in, mixdown_buffer, gain_buffer, start, to_write, chan_n, read_frames, skip_frames);
322
323         float* fiv = new float[to_write];
324         float* fov = new float[to_write];
325
326         _fade_in.get_vector (offset, offset+to_write, fiv, to_write);
327         _fade_out.get_vector (offset, offset+to_write, fov, to_write);
328
329         /* note: although we have not explicitly taken into account the return values
330            from _out->read_at() or _in->read_at(), the length() function does this
331            implicitly. why? because it computes a value based on the in+out regions'
332            position and length, and so we know precisely how much data they could return. 
333         */
334
335         for (nframes_t n = 0; n < to_write; ++n) {
336                 buf[n] = (crossfade_buffer_out[n] * fov[n]) + (crossfade_buffer_in[n] * fiv[n]);
337         }
338
339         delete [] fov;
340         delete [] fiv;
341
342         return to_write;
343 }       
344
345 OverlapType 
346 Crossfade::coverage (nframes_t start, nframes_t end) const
347 {
348         nframes_t my_end = _position + _length;
349
350         if ((start >= _position) && (end <= my_end)) {
351                 return OverlapInternal;
352         }
353         if ((end >= _position) && (end <= my_end)) {
354                 return OverlapStart;
355         }
356         if ((start >= _position) && (start <= my_end)) {
357                 return OverlapEnd;
358         }
359         if ((_position >= start) && (_position <= end) && (my_end <= end)) {
360                 return OverlapExternal;
361         }
362         return OverlapNone;
363 }
364
365 void
366 Crossfade::set_active (bool yn)
367 {
368         if (_active != yn) {
369                 _active = yn;
370                 StateChanged (ActiveChanged);
371         }
372 }
373
374 bool
375 Crossfade::refresh ()
376 {
377         /* crossfades must be between non-muted regions */
378         
379         if (_out->muted() || _in->muted()) {
380                 Invalidated (shared_from_this());
381                 return false;
382         }
383
384         /* Top layer shouldn't be transparent */
385
386         if (!((layer_relation > 0 ? _in : _out)->opaque())) {
387                 Invalidated (shared_from_this());
388                 return false;
389         }
390
391         /* layer ordering cannot change */
392
393         int32_t new_layer_relation = (int32_t) (_in->layer() - _out->layer());
394
395         if (new_layer_relation * layer_relation < 0) { // different sign, layers rotated 
396                 Invalidated (shared_from_this());
397                 return false;
398         }
399
400         OverlapType ot = _in->coverage (_out->first_frame(), _out->last_frame());
401
402         if (ot == OverlapNone) {
403                 Invalidated (shared_from_this());
404                 return false;
405         } 
406
407         bool send_signal;
408
409         if (ot != overlap_type) {
410
411                 if (_follow_overlap) {
412
413                         try {
414                                 compute (_in, _out, Config->get_xfade_model());
415                         } 
416
417                         catch (NoCrossfadeHere& err) {
418                                 Invalidated (shared_from_this());
419                                 return false;
420                         }
421
422                         send_signal = true;
423
424                 } else {
425
426                         Invalidated (shared_from_this());
427                         return false;
428                 }
429
430         } else {
431
432                 send_signal = update ();
433         }
434
435         if (send_signal) {
436                 StateChanged (BoundsChanged); /* EMIT SIGNAL */
437         }
438
439         _in_update = false;
440
441         return true;
442 }
443
444 bool
445 Crossfade::update ()
446 {
447         nframes_t newlen;
448         
449         if (_follow_overlap) {
450                 newlen = _out->first_frame() + _out->length() - _in->first_frame();
451         } else {
452                 newlen = _length;
453         }
454         
455         if (newlen == 0) {
456                 Invalidated (shared_from_this());
457                 return false;
458         }
459         
460         _in_update = true;
461         
462         if ((_follow_overlap && newlen != _length) || (_length > newlen)) {
463                 
464                 double factor =  newlen / (double) _length;
465                 
466                 _fade_out.x_scale (factor);
467                 _fade_in.x_scale (factor);
468                 
469                 _length = newlen;
470         } 
471                 
472         switch (_anchor_point) {
473         case StartOfIn:
474                 _position = _in->first_frame();
475                 break;
476                 
477         case EndOfIn:
478                 _position = _in->last_frame() - _length + 1;
479                 break;
480                 
481         case EndOfOut:
482                 _position = _out->last_frame() - _length + 1;
483         }
484
485         return true;
486 }
487
488 int
489 Crossfade::compute (boost::shared_ptr<AudioRegion> a, boost::shared_ptr<AudioRegion> b, CrossfadeModel model)
490 {
491         boost::shared_ptr<AudioRegion> top;
492         boost::shared_ptr<AudioRegion> bottom;
493         nframes_t short_xfade_length;
494
495         short_xfade_length = _short_xfade_length; 
496
497         if (a->layer() < b->layer()) {
498                 top = b;
499                 bottom = a;
500         } else {
501                 top = a;
502                 bottom = b;
503         }
504         
505         /* first check for matching ends */
506         
507         if (top->first_frame() == bottom->first_frame()) {
508
509                 /* Both regions start at the same point */
510                 
511                 if (top->last_frame() < bottom->last_frame()) {
512                         
513                         /* top ends before bottom, so put an xfade
514                            in at the end of top.
515                         */
516                         
517                         /* [-------- top ---------- ]
518                          * {====== bottom =====================}
519                          */
520
521                         _in = bottom;
522                         _out = top;
523
524                         if (top->last_frame() < short_xfade_length) {
525                                 _position = 0;
526                         } else {
527                                 _position = top->last_frame() - short_xfade_length;
528                         }
529
530                         _length = min (short_xfade_length, top->length());
531                         _follow_overlap = false;
532                         _anchor_point = EndOfIn;
533                         _active = true;
534                         _fixed = true;
535
536                 } else {
537                         /* top ends after (or same time) as bottom - no xfade
538                          */
539                         
540                         /* [-------- top ------------------------ ]
541                          * {====== bottom =====================}
542                          */
543
544                         throw NoCrossfadeHere();
545                 }
546                 
547         } else if (top->last_frame() == bottom->last_frame()) {
548                 
549                 /* Both regions end at the same point */
550                 
551                 if (top->first_frame() > bottom->first_frame()) {
552                         
553                         /* top starts after bottom, put an xfade in at the
554                            start of top
555                         */
556                         
557                         /*            [-------- top ---------- ]
558                          * {====== bottom =====================}
559                          */
560
561                         _in = top;
562                         _out = bottom;
563                         _position = top->first_frame();
564                         _length = min (short_xfade_length, top->length());
565                         _follow_overlap = false;
566                         _anchor_point = StartOfIn;
567                         _active = true;
568                         _fixed = true;
569                         
570                 } else {
571                         /* top starts before bottom - no xfade
572                          */
573
574                         /* [-------- top ------------------------ ]
575                          *    {====== bottom =====================}
576                          */
577
578                         throw NoCrossfadeHere();
579                 }
580
581         } else {
582         
583                 /* OK, time to do more regular overlapping */
584
585                 OverlapType ot = top->coverage (bottom->first_frame(), bottom->last_frame());
586
587                 switch (ot) {
588                 case OverlapNone:
589                         /* should be NOTREACHED as a precondition of creating
590                            a new crossfade, but we need to handle it here.
591                         */
592                         throw NoCrossfadeHere();
593                         break;
594                         
595                 case OverlapInternal:
596                 case OverlapExternal:
597                         /* should be NOTREACHED because of tests above */
598                         throw NoCrossfadeHere();
599                         break;
600                         
601                 case OverlapEnd: /* top covers start of bottom but ends within it */
602
603                         /* [---- top ------------------------] 
604                          *                { ==== bottom ============ } 
605                          */ 
606
607                         _in = bottom;
608                         _out = top;
609                         _anchor_point = EndOfOut;
610
611                         if (model == FullCrossfade) {
612                                 _position = bottom->first_frame(); // "{"
613                                 _length = _out->first_frame() + _out->length() - _in->first_frame();
614                                 /* leave active alone */
615                                 _follow_overlap = true;
616                         } else {
617                                 _length = min (short_xfade_length, top->length());
618                                 _position = top->last_frame() - _length;  // "]" - length 
619                                 _active = true;
620                                 _follow_overlap = false;
621                                 
622                         }
623                         break;
624                         
625                 case OverlapStart:   /* top starts within bottom but covers bottom's end */
626
627                         /*                   { ==== top ============ } 
628                          *   [---- bottom -------------------] 
629                          */
630
631                         _in = top;
632                         _out = bottom;
633                         _position = top->first_frame();
634                         _anchor_point = StartOfIn;
635
636                         if (model == FullCrossfade) {
637                                 _length = _out->first_frame() + _out->length() - _in->first_frame();
638                                 /* leave active alone */
639                                 _follow_overlap = true;
640                         } else {
641                                 _length = min (short_xfade_length, top->length());
642                                 _active = true;
643                                 _follow_overlap = false;
644                                 
645                         }
646                         
647                         break;
648                 }
649         }
650         
651         return 0;
652 }
653
654 XMLNode&
655 Crossfade::get_state () 
656 {
657         XMLNode* node = new XMLNode (X_("Crossfade"));
658         XMLNode* child;
659         char buf[64];
660         LocaleGuard lg (X_("POSIX"));
661
662         _out->id().print (buf, sizeof (buf));
663         node->add_property ("out", buf);
664         _in->id().print (buf, sizeof (buf));
665         node->add_property ("in", buf);
666         node->add_property ("active", (_active ? "yes" : "no"));
667         node->add_property ("follow-overlap", (_follow_overlap ? "yes" : "no"));
668         node->add_property ("fixed", (_fixed ? "yes" : "no"));
669         snprintf (buf, sizeof(buf), "%" PRIu32, _length);
670         node->add_property ("length", buf);
671         snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _anchor_point);
672         node->add_property ("anchor-point", buf);
673         snprintf (buf, sizeof(buf), "%" PRIu32, (uint32_t) _position);
674         node->add_property ("position", buf);
675
676         child = node->add_child ("FadeIn");
677
678         for (AutomationList::iterator ii = _fade_in.begin(); ii != _fade_in.end(); ++ii) {
679                 XMLNode* pnode;
680
681                 pnode = new XMLNode ("point");
682
683                 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
684                 pnode->add_property ("x", buf);
685                 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
686                 pnode->add_property ("y", buf);
687                 child->add_child_nocopy (*pnode);
688         }
689
690         child = node->add_child ("FadeOut");
691
692         for (AutomationList::iterator ii = _fade_out.begin(); ii != _fade_out.end(); ++ii) {
693                 XMLNode* pnode;
694
695                 pnode = new XMLNode ("point");
696
697                 snprintf (buf, sizeof (buf), "%" PRIu32, (nframes_t) floor ((*ii)->when));
698                 pnode->add_property ("x", buf);
699                 snprintf (buf, sizeof (buf), "%.12g", (*ii)->value);
700                 pnode->add_property ("y", buf);
701                 child->add_child_nocopy (*pnode);
702         }
703
704         return *node;
705 }
706
707 int
708 Crossfade::set_state (const XMLNode& node)
709 {
710         XMLNodeConstIterator i;
711         XMLNodeList children;
712         XMLNode* fi;
713         XMLNode* fo;
714         const XMLProperty* prop;
715         LocaleGuard lg (X_("POSIX"));
716         Change what_changed = Change (0);
717         nframes_t val;
718
719         if ((prop = node.property ("position")) != 0) {
720                 sscanf (prop->value().c_str(), "%" PRIu32, &val);
721                 if (val != _position) {
722                         _position = val;
723                         what_changed = Change (what_changed | PositionChanged);
724                 }
725         } else {
726                 warning << _("old-style crossfade information - no position information") << endmsg;
727                 _position = _in->first_frame();
728         }
729
730         if ((prop = node.property ("active")) != 0) {
731                 bool x = string_is_affirmative (prop->value());
732                 if (x != _active) {
733                         _active = x;
734                         what_changed = Change (what_changed | ActiveChanged);
735                 }
736         } else {
737                 _active = true;
738         }
739
740         if ((prop = node.property ("follow-overlap")) != 0) {
741                 _follow_overlap = string_is_affirmative (prop->value());
742         } else {
743                 _follow_overlap = false;
744         }
745
746         if ((prop = node.property ("fixed")) != 0) {
747                 _fixed = string_is_affirmative (prop->value());
748         } else {
749                 _fixed = false;
750         }
751
752         if ((prop = node.property ("anchor-point")) != 0) {
753                 _anchor_point = AnchorPoint (atoi ((prop->value().c_str())));
754         } else {
755                 _anchor_point = StartOfIn;
756         }
757
758         if ((prop = node.property ("length")) != 0) {
759
760                 sscanf (prop->value().c_str(), "%" PRIu32, &val);
761                 if (val != _length) {
762                         _length = atol (prop->value().c_str());
763                         what_changed = Change (what_changed | LengthChanged);
764                 }
765
766         } else {
767                 
768                 /* XXX this branch is legacy code from before
769                    the point where we stored xfade lengths.
770                 */
771                 
772                 if ((_length = overlap_length()) == 0) {
773                         throw failed_constructor();
774                 }
775         }
776
777         if ((fi = find_named_node (node, "FadeIn")) == 0) {
778                 return -1;
779         }
780         
781         if ((fo = find_named_node (node, "FadeOut")) == 0) {
782                 return -1;
783         }
784
785         /* fade in */
786         
787         _fade_in.freeze ();
788         _fade_in.clear ();
789         
790         children = fi->children();
791         
792         for (i = children.begin(); i != children.end(); ++i) {
793                 if ((*i)->name() == "point") {
794                         nframes_t x;
795                         float y;
796                         
797                         prop = (*i)->property ("x");
798                         sscanf (prop->value().c_str(), "%" PRIu32, &x);
799                         
800                         prop = (*i)->property ("y");
801                         sscanf (prop->value().c_str(), "%f", &y);
802
803                         _fade_in.add (x, y);
804                 }
805         }
806
807         _fade_in.thaw ();
808         
809         /* fade out */
810         
811         _fade_out.freeze ();
812         _fade_out.clear ();
813
814         children = fo->children();
815         
816         for (i = children.begin(); i != children.end(); ++i) {
817                 if ((*i)->name() == "point") {
818                         nframes_t x;
819                         float y;
820                         XMLProperty* prop;
821
822                         prop = (*i)->property ("x");
823                         sscanf (prop->value().c_str(), "%" PRIu32, &x);
824
825                         prop = (*i)->property ("y");
826                         sscanf (prop->value().c_str(), "%f", &y);
827                         
828                         _fade_out.add (x, y);
829                 }
830         }
831
832         _fade_out.thaw ();
833
834         StateChanged (what_changed); /* EMIT SIGNAL */
835
836         return 0;
837 }
838
839 bool
840 Crossfade::can_follow_overlap () const
841 {
842         return !_fixed;
843 }
844
845 void
846 Crossfade::set_follow_overlap (bool yn)
847 {
848         if (yn == _follow_overlap || _fixed) {
849                 return;
850         }
851
852         _follow_overlap = yn;
853
854         if (!yn) {
855                 set_length (_short_xfade_length);
856         } else {
857                 set_length (_out->first_frame() + _out->length() - _in->first_frame());
858         }
859
860         StateChanged (FollowOverlapChanged);
861 }
862
863 nframes_t
864 Crossfade::set_length (nframes_t len)
865 {
866         nframes_t limit = 0;
867
868         switch (_anchor_point) {
869         case StartOfIn:
870                 limit = _in->length();
871                 break;
872
873         case EndOfIn:
874                 limit = _in->length();
875                 break;
876
877         case EndOfOut:
878                 limit = _out->length();
879                 break;
880                 
881         }
882
883         len = min (limit, len);
884
885         double factor = len / (double) _length;
886
887         _in_update = true;
888         _fade_out.x_scale (factor);
889         _fade_in.x_scale (factor);
890         _in_update = false;
891         
892         _length = len;
893
894         StateChanged (LengthChanged);
895
896         return len;
897 }
898
899 nframes_t
900 Crossfade::overlap_length () const
901 {
902         if (_fixed) {
903                 return _length;
904         }
905         return _out->first_frame() + _out->length() - _in->first_frame();
906 }
907
908 void
909 Crossfade::set_short_xfade_length (nframes_t n)
910 {
911         _short_xfade_length = n;
912 }
913
914 void
915 Crossfade::invalidate ()
916 {
917         Invalidated (shared_from_this()); /* EMIT SIGNAL */
918 }