fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / strip_silence.cc
index ee6bfb7a5e46bea740a21802696a2a44caec7811..3141f422a80e28ccec22e2b25e571c0b3af788ed 100644 (file)
 #include "ardour/strip_silence.h"
 #include "ardour/audioregion.h"
 #include "ardour/region_factory.h"
-#include "ardour/session.h"
-#include "ardour/dB.h"
 #include "ardour/progress.h"
 
 using namespace ARDOUR;
 
 /** Construct a StripSilence filter.
  *  @param s Session.
- *  @param threshold Threshold below which audio is considered silence, in dBFS.
- *  @param minimum_length Minimum length of silence period to recognise, in samples.
+ *  @param sm Silences to remove.
  *  @param fade_length Length of fade in/out to apply to trimmed regions, in samples.
  */
 
@@ -49,7 +46,7 @@ StripSilence::run (boost::shared_ptr<Region> r, Progress* progress)
        results.clear ();
 
        /* we only operate on AudioRegions, for now, though this could be adapted to MIDI
-          as well I guess 
+          as well I guess
         */
        boost::shared_ptr<AudioRegion> region = boost::dynamic_pointer_cast<AudioRegion> (r);
         InterThreadInfo itt;
@@ -102,35 +99,41 @@ StripSilence::run (boost::shared_ptr<Region> r, Progress* progress)
        --last_silence;
 
        frameoffset_t const end_of_region = r->start() + r->length();
-       
-       if (last_silence->second != end_of_region - 1) {
+
+       if (last_silence->second < end_of_region - 1) {
                audible.push_back (std::make_pair (last_silence->second, end_of_region - 1));
        }
 
        int n = 0;
        int const N = audible.size ();
-       
-       for (AudioIntervalResult::const_iterator i = audible.begin(); i != audible.end(); ++i) {
+
+       for (AudioIntervalResult::const_iterator i = audible.begin(); i != audible.end(); ++i, ++n) {
 
                PBD::PropertyList plist;
                boost::shared_ptr<AudioRegion> copy;
 
                plist.add (Properties::length, i->second - i->first);
                plist.add (Properties::position, r->position() + (i->first - r->start()));
-               
+
                copy = boost::dynamic_pointer_cast<AudioRegion> (
                        RegionFactory::create (region, (i->first - r->start()), plist)
                        );
-               
+
                copy->set_name (RegionFactory::new_region_name (region->name ()));
 
-               framecnt_t const f = std::min (_fade_length, (i->second - i->first));
-               
-               copy->set_fade_in_active (true);
-               copy->set_fade_in (FadeLinear, f);
-               copy->set_fade_out (FadeLinear, f);
+               framecnt_t const f = std::min (_fade_length, (i->second - i->first) / 2);
+
+               if (f > 0) {
+                       copy->set_fade_in_active (true);
+                       copy->set_fade_out_active (true);
+                       copy->set_fade_in (FadeLinear, f);
+                       copy->set_fade_out (FadeLinear, f);
+               } else {
+                       copy->set_fade_in_active (false);
+                       copy->set_fade_out_active (false);
+               }
                results.push_back (copy);
-       
+
                if (progress && (n <= N)) {
                        progress->set_progress (float (n) / N);
                }