fix crash at session close/exit if a midi-control-surface is used
[ardour.git] / libs / ardour / audioregion.cc
index 97e63b5e8cf4e3916b3fbb74f858c9670c5f6c40..630819d7c11cd10f5faad768036d6f1db65c5f81 100644 (file)
@@ -957,6 +957,34 @@ AudioRegion::set_state (const XMLNode& node, int version)
        return _set_state (node, version, what_changed, true);
 }
 
+void
+AudioRegion::fade_range (framepos_t start, framepos_t end)
+{
+       framepos_t s, e;
+
+       switch (coverage (start, end)) {
+       case Evoral::OverlapStart:
+               s = _position;
+               e = end;
+               set_fade_in (FadeConstantPower, e - s);
+               break;
+       case Evoral::OverlapEnd:
+               s = start;
+               e = _position + _length;
+               set_fade_out (FadeConstantPower, e - s);
+               break;
+       case Evoral::OverlapInternal:
+               /* needs addressing, perhaps. Difficult to do if we can't
+                * control one edge of the fade relative to the relevant edge
+                * of the region, which we cannot - fades are currently assumed
+                * to start/end at the start/end of the region
+                */
+               break;
+       default:
+               return;
+       }
+}
+
 void
 AudioRegion::set_fade_in_shape (FadeShape shape)
 {
@@ -991,7 +1019,7 @@ AudioRegion::set_fade_in (FadeShape shape, framecnt_t len)
        _fade_in->clear ();
        _inverse_fade_in->clear ();
 
-       const int num_steps = min((framecnt_t)256, max ((framecnt_t) 16, len / 512));
+       const int num_steps = 32;
 
        switch (shape) {
        case FadeLinear:
@@ -1032,11 +1060,9 @@ AudioRegion::set_fade_in (FadeShape shape, framecnt_t len)
                _fade_in->fast_simple_add (0.5 * len, 0.6);
                //now generate a fade-out curve by successively applying a gain drop
                const double breakpoint = 0.7;  //linear for first 70%
-               for (int i = max(2, (int)ceil(num_steps / 9.0)); i < num_steps; i++) {
-                       const double offset = 1.0 - breakpoint;
-                       float coeff = 1.0 - breakpoint;
-                       coeff *= powf(0.5, i * 9.0 / (double)num_steps); // -6dB per step for 7 steps
-                       _fade_in->fast_simple_add (len * (breakpoint + (offset * (double)i / (double)num_steps)), coeff);
+               for (int i = 2; i < 9; ++i) {
+                       const float coeff = (1.f - breakpoint) * powf (0.5, i);
+                       _fade_in->fast_simple_add (len * (breakpoint + ((1.0 - breakpoint) * (double)i / 9.0)), coeff);
                }
                _fade_in->fast_simple_add (len, VERY_SMALL_SIGNAL);
                reverse_curve (c3, _fade_in.val());
@@ -1045,6 +1071,9 @@ AudioRegion::set_fade_in (FadeShape shape, framecnt_t len)
                break;
        }
 
+       _fade_in->set_interpolation(Evoral::ControlList::Curved);
+       _inverse_fade_in->set_interpolation(Evoral::ControlList::Curved);
+
        _default_fade_in = false;
        _fade_in->thaw ();
        send_change (PropertyChange (Properties::fade_in));
@@ -1071,7 +1100,7 @@ AudioRegion::set_fade_out (FadeShape shape, framecnt_t len)
        _fade_out->clear ();
        _inverse_fade_out->clear ();
 
-       const int num_steps = min((framecnt_t)256, max ((framecnt_t) 16, len / 512));
+       const int num_steps = 32;
 
        switch (shape) {
        case FadeLinear:
@@ -1108,20 +1137,20 @@ AudioRegion::set_fade_out (FadeShape shape, framecnt_t len)
                //start with a nearly linear cuve
                _fade_out->fast_simple_add (0, 1);
                _fade_out->fast_simple_add (0.5 * len, 0.6);
-
                //now generate a fade-out curve by successively applying a gain drop
                const double breakpoint = 0.7;  //linear for first 70%
-               for (int i = max(2, (int)ceil(num_steps / 9.0)); i < num_steps; i++) {
-                       const double offset = 1.0 - breakpoint;
-                       float coeff = 1.0 - breakpoint;
-                       coeff *= powf(0.5, i * 9.0 / (double)num_steps); // -6dB per step for 7 steps
-                       _fade_out->fast_simple_add (len * (breakpoint + (offset * (double)i / (double)num_steps)), coeff);
+               for (int i = 2; i < 9; ++i) {
+                       const float coeff = (1.f - breakpoint) * powf (0.5, i);
+                       _fade_out->fast_simple_add (len * (breakpoint + ((1.0 - breakpoint) * (double)i / 9.0)), coeff);
                }
                _fade_out->fast_simple_add (len, VERY_SMALL_SIGNAL);
                reverse_curve (_inverse_fade_out.val(), _fade_out.val());
                break;
        }
 
+       _fade_out->set_interpolation(Evoral::ControlList::Curved);
+       _inverse_fade_out->set_interpolation(Evoral::ControlList::Curved);
+
        _default_fade_out = false;
        _fade_out->thaw ();
        send_change (PropertyChange (Properties::fade_out));