fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / strip_silence.cc
index cd7ab879e7e6bd4fee9af90d242f28f992b141ad..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;
@@ -78,66 +75,68 @@ StripSilence::run (boost::shared_ptr<Region> r, Progress* progress)
                return 0;
        }
 
-        AudioIntervalResult::const_iterator s = silence.begin ();
-        PBD::PropertyList plist;
-        framepos_t start;
-        framepos_t end;
-        bool in_silence;
-        boost::shared_ptr<AudioRegion> copy;
-
-        start = r->start();
-
-        if (s->first == start) {
-                /* segment starting at zero is silent */
-                end = s->second;
-                in_silence = true;
-        } else {
-                /* segment starting at zero is audible, and begins at the start of the region in the source */
-                end = s->first;
-                in_silence = false;
-        }
+       /* Turn the silence list into an `audible' list */
+       AudioIntervalResult audible;
 
-       int n = 0;
-       int const N = silence.size ();
+       /* Add the possible audible section at the start of the region */
+       AudioIntervalResult::const_iterator first_silence = silence.begin ();
+       if (first_silence->first != region->start()) {
+               audible.push_back (std::make_pair (r->start(), first_silence->first));
+       }
 
-        while (start < r->start() + r->length()) {
+       /* Add audible sections in the middle of the region */
+       for (AudioIntervalResult::const_iterator i = silence.begin (); i != silence.end(); ++i) {
+               AudioIntervalResult::const_iterator j = i;
+               ++j;
 
-                framecnt_t interval_duration;
+               if (j != silence.end ()) {
+                       audible.push_back (std::make_pair (i->second, j->first));
+               }
+       }
 
-                interval_duration = end - start;
+       /* Add the possible audible section at the end of the region */
+       AudioIntervalResult::const_iterator last_silence = silence.end ();
+       --last_silence;
 
-                if (!in_silence && interval_duration > 0) {
+       frameoffset_t const end_of_region = r->start() + r->length();
 
-                        plist.clear ();
-                        plist.add (Properties::length, interval_duration);
-                        plist.add (Properties::position, r->position() + (start - r->start()));
+       if (last_silence->second < end_of_region - 1) {
+               audible.push_back (std::make_pair (last_silence->second, end_of_region - 1));
+       }
 
-                        copy = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create 
-                                                                         (region, (start - r->start()), plist));
+       int n = 0;
+       int const N = audible.size ();
+
+       for (AudioIntervalResult::const_iterator i = audible.begin(); i != audible.end(); ++i, ++n) {
 
-                        copy->set_name (RegionFactory::new_region_name (region->name ()));
+               PBD::PropertyList plist;
+               boost::shared_ptr<AudioRegion> copy;
 
-                        copy->set_fade_in_active (true);
-                        copy->set_fade_in (FadeLinear, _fade_length);
-                        results.push_back (copy);
-                }
+               plist.add (Properties::length, i->second - i->first);
+               plist.add (Properties::position, r->position() + (i->first - r->start()));
 
-                start = end;
-                in_silence = !in_silence;
-                ++s;
+               copy = boost::dynamic_pointer_cast<AudioRegion> (
+                       RegionFactory::create (region, (i->first - r->start()), plist)
+                       );
 
-                if (s == silence.end()) {
-                        end = r->start() + r->length();
-                } else {
-                        end = s->first;
-                }
+               copy->set_name (RegionFactory::new_region_name (region->name ()));
 
-               ++n;
+               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);
                }
-
         }
 
        return 0;