Fix up strip silence.
authorCarl Hetherington <carl@carlh.net>
Mon, 28 Feb 2011 18:57:25 +0000 (18:57 +0000)
committerCarl Hetherington <carl@carlh.net>
Mon, 28 Feb 2011 18:57:25 +0000 (18:57 +0000)
git-svn-id: svn://localhost/ardour2/branches/3.0@8991 d708f5d6-7413-0410-9779-e7cbd77b26cf

libs/ardour/ardour/types.h
libs/ardour/strip_silence.cc

index fd3f6d9557061a401e1deb08b1d0dd390a42cb06..06c600eaee8a8e5607b64d6852fbe4dedd56ef64 100644 (file)
@@ -76,8 +76,8 @@ namespace ARDOUR {
        static const framepos_t max_framepos = INT64_MAX;
        static const framecnt_t max_framecnt = INT64_MAX;
 
-       // a set of (time) intervals: first of pair is the offset within the region, second is the length of the interval
-       typedef std::list<std::pair<frameoffset_t,framecnt_t> > AudioIntervalResult;
+       // a set of (time) intervals: first of pair is the offset of the start within the region, second is the offset of the end
+       typedef std::list<std::pair<frameoffset_t, frameoffset_t> > AudioIntervalResult;
        // associate a set of intervals with regions (e.g. for silence detection)
        typedef std::map<boost::shared_ptr<ARDOUR::Region>,AudioIntervalResult> AudioIntervalMap;
 
index cd7ab879e7e6bd4fee9af90d242f28f992b141ad..d04612f6044ec1c38a51711fc40ce9d1858fb028 100644 (file)
@@ -78,66 +78,59 @@ 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;
-        }
-
-       int n = 0;
-       int const N = silence.size ();
-
-        while (start < r->start() + r->length()) {
-
-                framecnt_t interval_duration;
-
-                interval_duration = end - start;
-
-                if (!in_silence && interval_duration > 0) {
-
-                        plist.clear ();
-                        plist.add (Properties::length, interval_duration);
-                        plist.add (Properties::position, r->position() + (start - r->start()));
+       /* Turn the silence list into an `audible' list */
+       AudioIntervalResult audible;
 
-                        copy = boost::dynamic_pointer_cast<AudioRegion> (RegionFactory::create 
-                                                                         (region, (start - r->start()), plist));
-
-                        copy->set_name (RegionFactory::new_region_name (region->name ()));
+       /* 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));
+       }
 
-                        copy->set_fade_in_active (true);
-                        copy->set_fade_in (FadeLinear, _fade_length);
-                        results.push_back (copy);
-                }
+       /* 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;
 
-                start = end;
-                in_silence = !in_silence;
-                ++s;
+               if (j != silence.end ()) {
+                       audible.push_back (std::make_pair (i->second, j->first));
+               }
+       }
 
-                if (s == silence.end()) {
-                        end = r->start() + r->length();
-                } else {
-                        end = s->first;
-                }
+       /* Add the possible audible section at the end of the region */
+       AudioIntervalResult::const_iterator last_silence = silence.end ();
+       --last_silence;
 
-               ++n;
+       frameoffset_t const end_of_region = r->start() + r->length();
+       
+       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) {
+
+               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 ()));
+               
+               copy->set_fade_in_active (true);
+               copy->set_fade_in (FadeLinear, _fade_length);
+               results.push_back (copy);
+       
                if (progress && (n <= N)) {
                        progress->set_progress (float (n) / N);
                }
-
         }
 
        return 0;